From 73928a9eef05a05a8a9e31ccd6d749593a7e30d5 Mon Sep 17 00:00:00 2001 From: s22-tech Date: Sat, 25 May 2024 12:18:45 -0700 Subject: [PATCH] Code cleanup --- admin.php | 2 +- async_header.php | 20 ++++---- bookmarks/add_bookmark.php | 3 +- bookmarks/async_bookmarks.php | 41 +++++++-------- bookmarks/delete_bookmark.inc.php | 36 +++++++++----- bookmarks/delete_bookmark.php | 39 +++++++++------ bookmarks/edit_bookmark.php | 40 ++++++++------- bookmarks/move_bookmark.php | 2 +- bookmarks/new_bookmark.php | 2 +- config/config.php | 14 +++--- export.php | 2 +- favicon.php | 83 ++++++++++++++++++++----------- footer.php | 13 ----- header.min.php | 4 +- header.php | 4 +- import.php | 2 +- lib/lib.php | 5 +- register.php | 7 +-- shared.php | 36 +++++++------- sidebar.php | 3 +- 20 files changed, 198 insertions(+), 160 deletions(-) diff --git a/admin.php b/admin.php index d5c8faa..3c98e50 100644 --- a/admin.php +++ b/admin.php @@ -1,6 +1,6 @@ install OpenBookmark.'); } else { - require_once (ABSOLUTE_PATH . 'config/config.php'); + require_once(DOC_ROOT . '/config/config.php'); } - require_once (ABSOLUTE_PATH . 'lib/mysql.php'); + require_once(DOC_ROOT . '/lib/mysql.php'); $mysql = new mysql; - require_once (ABSOLUTE_PATH . 'lib/auth.php'); + require_once(DOC_ROOT . '/lib/auth.php'); $auth = new Auth; - require_once (ABSOLUTE_PATH . 'lib/lib.php'); - require_once (ABSOLUTE_PATH . 'lib/login.php'); + require_once(DOC_ROOT . '/lib/lib.php'); + require_once(DOC_ROOT . '/lib/login.php'); - //if (is_file (ABSOLUTE_PATH . 'install.php')) { + //if (is_file (DOC_ROOT . '/install.php')) { // message ('Remove "install.php" before using OpenBookmark.'); //} if ($display_login_form) { $auth->display_login_form(); - require_once (ABSOLUTE_PATH . 'footer.php'); + require_once(DOC_ROOT . '/footer.php'); } diff --git a/bookmarks/add_bookmark.php b/bookmarks/add_bookmark.php index 661403c..bff89c5 100644 --- a/bookmarks/add_bookmark.php +++ b/bookmarks/add_bookmark.php @@ -1,2 +1,3 @@ escape($username), - $mysql->escape($folderid) + $mysql->escape($username), + $mysql->escape($folderid) ); if ($mysql->query($query)) { @@ -23,20 +23,21 @@ while ($row = mysqli_fetch_assoc($mysql->result)) { array_push($bookmarks, $row); } - list_bookmarks($bookmarks, - true, - false, - $settings['show_bookmark_icon'], - true, - $settings['show_bookmark_description'], - $settings['show_column_date'], - $settings['show_column_edit'], - $settings['show_column_move'], - $settings['show_column_delete'], - $settings['show_public'], - true, - false, - 'index.php' + list_bookmarks( + bookmarks: $bookmarks, + show_checkbox: true, + show_folder: false, + show_icon: $settings['show_bookmark_icon'], + show_link: true, + show_desc: $settings['show_bookmark_description'], + show_date: $settings['show_column_date'], + show_edit: $settings['show_column_edit'], + show_move: $settings['show_column_move'], + show_delete: $settings['show_column_delete'], + show_share: $settings['show_public'], + show_header: true, + user: false, + scriptname: 'index.php' ); } else { diff --git a/bookmarks/delete_bookmark.inc.php b/bookmarks/delete_bookmark.inc.php index 1a8d101..1d574f3 100644 --- a/bookmarks/delete_bookmark.inc.php +++ b/bookmarks/delete_bookmark.inc.php @@ -1,20 +1,19 @@ escape($favicon), + $mysql->escape($username) + ); + if ($mysql->query($count_query)) { + if (mysql_result($mysql->result, 0) >= 1) { + // Skip deletion if more than one bookmark uses the same favicon. + continue; } } + + if (is_file(DOC_ROOT .'/icons/'. $favicon)) { + unlink(DOC_ROOT .'/icons/'. $favicon); + } else { - debug_logger(name:'ERROR -- No favicon was found for deletion.', variable:$favicon, file:__FILE__, function:__FUNCTION__); + debug_logger(name: 'ERROR -- No favicon was found for deletion.', variable: $favicon, file: __FILE__, function: __FUNCTION__); } } } @@ -50,7 +62,7 @@ echo ""; } else { - echo 'Demo users cannot delete folders.

' . PHP_EOL; + echo 'Demo users cannot delete bookmarks.

' . PHP_EOL; echo ''; } diff --git a/bookmarks/delete_bookmark.php b/bookmarks/delete_bookmark.php index 9959a1e..371d3de 100644 --- a/bookmarks/delete_bookmark.php +++ b/bookmarks/delete_bookmark.php @@ -4,7 +4,7 @@ ini_set('error_prepend_string', '
');
 	ini_set('error_append_string', '
'); - require_once(realpath(dirname(__FILE__, 2)) . '/header.php'); + require_once(dirname(__DIR__, 1) . '/header.php'); logged_in_only(); echo <<result)) { array_push($bookmarks, $row); } - list_bookmarks($bookmarks, - false, - false, - $settings['show_bookmark_icon'], - false, - false, - false, - false, - false, - false, - false, - false + list_bookmarks( + bookmarks: $bookmarks, + show_checkbox: false, + show_folder: false, + show_icon: $settings['show_bookmark_icon'], + show_link: false, + show_desc: false, + show_date: false, + show_edit: false, + show_move: false, + show_delete: false, + show_share: false, + show_header: false ); $bm_string = ''; @@ -85,9 +86,15 @@ ?> - -
- Delete Me + +
+
+ +
+ +
+ Delete Me +


diff --git a/bookmarks/edit_bookmark.php b/bookmarks/edit_bookmark.php index 4a50572..3eb1726 100644 --- a/bookmarks/edit_bookmark.php +++ b/bookmarks/edit_bookmark.php @@ -1,5 +1,5 @@ escape($qbmlist), @@ -36,18 +36,19 @@ while ($row = mysqli_fetch_assoc($mysql->result)) { array_push($bookmarks, $row); } - list_bookmarks($bookmarks, - false, - false, - $settings['show_bookmark_icon'], - false, - false, - false, - false, - false, - false, - true, - false + list_bookmarks( + bookmarks: $bookmarks, + show_checkbox: false, + show_folder: false, + show_icon: $settings['show_bookmark_icon'], + show_link: false, + show_desc: false, + show_date: false, + show_edit: false, + show_move: false, + show_delete: false, + show_share: true, + show_header: false ); ?> @@ -96,7 +97,8 @@ message('No Bookmark to edit.'); } elseif ($post_title == '' || $post_url == '' || $refresh_icon) { - $query = sprintf(" + // Edit a single bookmark. + $select_query = sprintf(" SELECT `title`, `url`, `description`, `childof`, `id`, `favicon`, `public` FROM `obm_bookmarks` WHERE `id` = '%d' @@ -106,12 +108,13 @@ $mysql->escape($username) ); $icon = $new_fav = ''; - if ($mysql->query($query)) { + if ($mysql->query($select_query)) { if (mysqli_num_rows($mysql->result) != 1) { message('No Bookmark to edit'); } else { $row = mysqli_fetch_object($mysql->result); + $saved_favicon = $row->favicon ?? ''; require_once(DOC_ROOT . '/folders/folder.php'); $tree = new Folder(); @@ -150,8 +153,9 @@ if (!$mysql->query($update_query)) { message($mysql->error); } - if (!empty($row->favicon)) { /* && is_file($row->favicon) */ - @unlink(DOC_ROOT .'/icons/'. $row->favicon); + if (!empty($saved_favicon) && $saved_favicon !== $new_fav) { /* && is_file($row->favicon) */ + // Only delete the existing icon if a new one's been saved. + @unlink(DOC_ROOT .'/icons/'. $saved_favicon); } $icon = ''; } diff --git a/bookmarks/move_bookmark.php b/bookmarks/move_bookmark.php index 6073bfa..7109838 100644 --- a/bookmarks/move_bookmark.php +++ b/bookmarks/move_bookmark.php @@ -1,5 +1,5 @@ url = $url; if ($settings['show_bookmark_icon']) { -debug_logger(name:'--------', variable:'-------------------------', file:'-----', function:'-----'); -debug_logger(name:'URL', variable:$url, newline:false, file:__FILE__, function:__FUNCTION__, time:true); +debug_logger(name: '--------', variable: '-------------------------', file: '--------', function: '--------'); +debug_logger(name: 'URL', variable: $url, newline: false, file: __FILE__, function: __FUNCTION__, time: true); if ($this->parsed_url = $this->return_parse_url($url)) { if ($this->favicon_url = $this->get_favicon_url()) { @@ -44,7 +44,7 @@ function __construct($url) { [$fav_ext, $ident] = $this->identify_fav($tmp_file); debug_logger(name:'tmp_file', variable:$tmp_file, newline:false, file:__FILE__, function:__FUNCTION__); debug_logger(name:'fav_ext', variable:$fav_ext, newline:false, file:__FILE__, function:__FUNCTION__); -debug_logger(name:'ident', variable:$ident, file:__FILE__, function:__FUNCTION__); +debug_logger(name:'ident-status', variable:$ident, file:__FILE__, function:__FUNCTION__); if ($cfg['convert_favicons']) { $this->favicon = $this->convert_favicon($tmp_file, $fav_ext, $ident); @@ -76,45 +76,45 @@ function __construct($url) { ### function convert_favicon($tmp_file, $fav_ext, $ident) : string { global $cfg; - $tmp_ext = pathinfo($tmp_file, PATHINFO_EXTENSION); - $tmp_file = str_replace('.'.$tmp_ext, '.'.$fav_ext, $tmp_file); // Replace extension with correct one. + $tmp_ext = '.'.pathinfo($tmp_file, PATHINFO_EXTENSION); + $new_ext = '.png'; + + if (file_exists($tmp_file)) { +debug_logger(name:'file_exists - YES', variable:$tmp_file, newline:false, file:__FILE__, function:__FUNCTION__); + } debug_logger(name:'ident-status', variable:$ident, newline:false, file:__FILE__, function:__FUNCTION__); debug_logger(name:'tmp_file', variable:$tmp_file, newline:false, file:__FILE__, function:__FUNCTION__); debug_logger(name:'fav_ext', variable:$fav_ext, file:__FILE__, function:__FUNCTION__); $new_name = $this->rename_favicon($this->url); + $new_name = str_replace($tmp_ext, '', $new_name); debug_logger(name:'new_name', variable:$new_name, newline:false, file:__FILE__, function:__FUNCTION__); $save_path_name = DOC_ROOT . '/icons/'. $new_name; debug_logger(name:'save_path_name', variable:$save_path_name, newline:false, file:__FILE__, function:__FUNCTION__); -// if (strtolower($fav_ext) === 'svg') { -// $convert = "{$cfg['convert']} -background none -size {$cfg['icon_size']} $tmp_file $save_path_name"; -// } -// else { -// $convert = "{$cfg['convert']} $tmp_file -resize {$cfg['icon_size']}\> -unsharp 0x1 $save_path_name"; -// } - if ($ident && $fav_ext !== 'svg') { - $convert = "{$cfg['convert']} $tmp_file -resize {$cfg['icon_size']}\> -unsharp 0x1 $save_path_name"; + $convert = "{$cfg['convert']} $tmp_file -resize {$cfg['icon_size']}\> -unsharp 0x1 {$save_path_name}{$new_ext}"; debug_logger(name:'convert-cmd', variable:$convert, newline:false, file:__FILE__, function:__FUNCTION__); - // Convert image to .png, and resize to $cfg['icon_size'] if original is larger. + // Convert image to .png, and resize to $cfg['icon_size'] if original is different. ## https://legacy.imagemagick.org/Usage/resize/ system("$convert", $status); -debug_logger(name:'SUCCESS--conversion status', variable:$status, file:__FILE__, function:__FUNCTION__); + debug_logger(name:'SUCCESS--conversion status', variable:$status, file:__FILE__, function:__FUNCTION__); if (file_exists($tmp_file)) { + debug_logger(name:'tmp_file_exists - YES', variable:'', newline:false, file:__FILE__, function:__FUNCTION__); unlink($tmp_file); } } else { + $rename = rename($tmp_file, $save_path_name . $tmp_ext); // Move & rename the file. debug_logger(name:'MOVED--no conversion', variable:$this->icon_name, newline:false, file:__FILE__, function:__FUNCTION__); - $rename = rename($tmp_file, $save_path_name); // Move the file. -debug_logger(name:'tmp_file', variable:$tmp_file, newline:false, file:__FILE__, function:__FUNCTION__); -debug_logger(name:'rename-move', variable:$rename, file:__FILE__, function:__FUNCTION__); +debug_logger(name:'tmp_file', variable:$tmp_file, newline:false, file:__FILE__, function:__FUNCTION__); +debug_logger(name:'save_path_name', variable:$save_path_name . $new_ext, newline:false, file:__FILE__, function:__FUNCTION__); +debug_logger(name:'rename-move', variable:$rename, file:__FILE__, function:__FUNCTION__); } - return basename($save_path_name); + return basename($save_path_name) . $new_ext; } @@ -124,20 +124,35 @@ function convert_favicon($tmp_file, $fav_ext, $ident) : string { /////////////////////////////// function download_favicon_image() { global $cfg; + $save_path_name = DOC_ROOT . '/tmp/'. $this->temp_icon_name; +debug_logger(name: 'temp_icon_name', variable: $save_path_name, newline:false, file: __FILE__, function: __FUNCTION__); + + $open_file_in_binary = fopen($save_path_name, 'wb'); $options = [ CURLOPT_URL => $this->favicon_url, CURLOPT_USERAGENT => $cfg['user_agent'], CURLOPT_RETURNTRANSFER => 1, CURLOPT_CONNECTTIMEOUT => 2, CURLOPT_TIMEOUT => 10, + CURLOPT_FILE => $open_file_in_binary, + CURLOPT_HEADER => 0, + /* Lets you use this script when there is redirect on the server. */ + CURLOPT_FOLLOWLOCATION => true, + /* Auto detect encoding for the response | identity deflation and gzip */ + CURLOPT_ENCODING => '', ]; $ch = curl_init(); curl_setopt_array($ch, $options); $response = curl_exec($ch); curl_close($ch); + // Close the file pointer. + fclose($open_file_in_binary); + if (admin_only()) { - $bytes = file_put_contents(DOC_ROOT . '/tmp/'. $this->temp_icon_name, $response); +// $bytes = file_put_contents($save_path_name, $response); +debug_logger(name: 'response', variable: $response, newline:false, file: __FILE__, function: __FUNCTION__); +// debug_logger(name: 'bytes saved', variable: $bytes, file: __FILE__, function: __FUNCTION__); } return true; @@ -156,7 +171,7 @@ function get_favicon_url() { $dom = new HtmlWeb(); $html = $dom->load($host_url); -debug_logger(name:'html', variable:$html, newline:false, file:__FILE__, function:__FUNCTION__); +debug_logger(name:'html', variable:$html, file:__FILE__, function:__FUNCTION__); if (empty($html)) { echo '• favicon.php — $html is blank.
'; @@ -243,9 +258,11 @@ function rename_favicon($domain) { $host_name = $parsed['host']; $host_name = str_replace('.', '-', $host_name); $parts = explode('-', $host_name); +debug_logger(name:'•••parts', variable:print_r($parts, true), file:__FILE__, function:__FUNCTION__); $last = array_pop($parts); - $parts = [implode('-', $parts), $last]; // Get domain w/o the extension. + $parts = [implode('-', $parts), $last]; // Get domain w/o the domain extension. $host_name = $parts[0]; +debug_logger(name:'•••host_name', variable:$host_name, file:__FILE__, function:__FUNCTION__); $host_name = str_replace('www-', '', $host_name); $ext = pathinfo($this->favicon_url, PATHINFO_EXTENSION); $ext = strlen($ext) < 3 ? 'png' : $ext; @@ -265,26 +282,28 @@ function identify_fav($tmp_file) { $file_ext = strtolower($idents[1]); $file_ext = ($file_ext === 'jpeg') ? 'jpg' : $file_ext; - if (count($idents) > 1 && !str_starts_with($idents[0], 'identify')) { - $file_to_convert = $idents[0]; + if (count($idents) > 1 && str_starts_with($idents[0], 'identify')) { + $file_to_convert = $tmp_file; } else { - $file_to_convert = $tmp_file; + $file_to_convert = $idents[0]; } -// if ($file_ext !== $tmp_ext) { + if ($file_ext !== $tmp_ext) { $info = pathinfo($file_to_convert); + debug_logger(name:'identify info', variable:$info, newline:false, file:__FILE__, function:__FUNCTION__); + } return [$file_ext, true]; -// } } else { +debug_logger(name:'identify return', variable:'', newline:false, file:__FILE__, function:__FUNCTION__); return ['png', false]; } } function get_current_url($url) { - // If an old URL gets redirected to a new one. + // If a saved URL changes and gets redirected to a new one... $ch = curl_init($url); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); @@ -303,4 +322,8 @@ function get_current_url($url) { prints out: error_log('• '. basename($file) .':'.$function.'()->$'. $name .': '. $variable . PHP_EOL, 3, $cfg['error_log']); e.g.: -• favicon.php:__construct()->$__construct1: https://www.invizbox.com/products/invizbox-2-pro/#select-plan \ No newline at end of file +• favicon.php:__construct()->$__construct1: https://www.invizbox.com/products/invizbox-2-pro/#select-plan + +NOTES: +• If the site's favicon is an svg image, DO NOT convert it. It looks terrible as a png. +• Don't convert icons to jpg. JPEG doesn't offer transparency, so there might be black banding if the image isn't square. diff --git a/footer.php b/footer.php index f4597e3..b2a8250 100644 --- a/footer.php +++ b/footer.php @@ -1,17 +1,4 @@ -'; - echo '  sub_dir: '. $cfg['sub_dir'] . '
'; - } - else { - echo '• OBM is installed in the root directory:' . '
'; - } - echo '  '. $_SERVER['DOCUMENT_ROOT'] . ''; - } -?> - diff --git a/header.min.php b/header.min.php index e0d7ba9..f6a0b50 100644 --- a/header.min.php +++ b/header.min.php @@ -6,11 +6,11 @@ // require_once(DOC_ROOT . '/lib/webstart.php'); - if (!is_file(realpath(dirname(__FILE__, 1)) . '/config/config.php')) { + if (!is_file(__DIR__ . '/config/config.php')) { die ('You need to install OpenBookmark II.'); } else { - require_once(realpath(dirname(__FILE__, 1)) . '/config/config.php'); + require_once(__DIR__ . '/config/config.php'); } require_once(DOC_ROOT . '/lib/mysql.php'); diff --git a/header.php b/header.php index 6ef7b3e..2c0007b 100644 --- a/header.php +++ b/header.php @@ -11,11 +11,11 @@ } */ // require_once(DOC_ROOT .'/lib/webstart.php'); - if (!is_file(realpath(dirname(__FILE__, 1)) . '/config/config.php')) { + if (!is_file(__DIR__ . '/config/config.php')) { die ('You need to install OpenBookmark II.'); } else { - require_once(realpath(dirname(__FILE__, 1)) . '/config/config.php'); + require_once(__DIR__ . '/config/config.php'); } require_once(DOC_ROOT .'/lib/mysql.php'); $mysql = new mysql; diff --git a/import.php b/import.php index 1b383be..c5b7185 100644 --- a/import.php +++ b/import.php @@ -1,5 +1,5 @@ diff --git a/lib/lib.php b/lib/lib.php index d442dee..b5fb8bb 100644 --- a/lib/lib.php +++ b/lib/lib.php @@ -459,7 +459,7 @@ function set_post_bool_var($varname, $default = true) { } -function set_get_num_list($varname) { +function set_get_num_list($varname) : array { if (empty($_GET[$varname]) || $_GET[$varname] == '') { $return = []; } @@ -574,7 +574,8 @@ function get_current_url($url) { function debug_logger($name, $variable, $file, $function, $newline=true, $time='') { global $cfg; - if ($cfg(debug)) { + [$function] = explode('(', $function); + if ($cfg['debug']) { if (is_array($variable)) { $variable = print_r($variable, true); } diff --git a/register.php b/register.php index d1999d9..f35e068 100644 --- a/register.php +++ b/register.php @@ -1,5 +1,6 @@ Passwords do not match.
'. PHP_EOL; $password = false; diff --git a/shared.php b/shared.php index 89991ca..c6ebd31 100644 --- a/shared.php +++ b/shared.php @@ -1,5 +1,6 @@ Tools @@ -93,19 +94,20 @@ while ($row = mysqli_fetch_assoc($mysql->result)) { array_push($bookmarks, $row); } - list_bookmarks($bookmarks, - false, - false, - $settings['show_bookmark_icon'], - true, - $settings['show_bookmark_description'], - $settings['show_column_date'], - false, - false, - false, - false, - true, - $user + list_bookmarks( + bookmarks: $bookmarks, + show_checkbox: false, + show_folder: false, + show_icon: $settings['show_bookmark_icon'], + show_link: true, + show_desc: $settings['show_bookmark_description'], + show_date: $settings['show_column_date'], + show_edit: false, + show_move: false, + show_delete: false, + show_share: false, + show_header: true, + user: $user ); } else { diff --git a/sidebar.php b/sidebar.php index 86d098b..c02d9c5 100644 --- a/sidebar.php +++ b/sidebar.php @@ -1,6 +1,7 @@