From 423a95d2b3a1af994d92f3e26b128cc41561afd4 Mon Sep 17 00:00:00 2001 From: Craig Knudsen Date: Thu, 5 Oct 2023 09:32:51 -0400 Subject: [PATCH] Fix issue #426: Category settings page gives a 500 error - Don't fclose if fopen failed - Reformat code --- category.php | 175 ++++++++++++++++++++++++++------------------------- 1 file changed, 90 insertions(+), 85 deletions(-) diff --git a/category.php b/category.php index ec09a86ba..bfa2b662e 100644 --- a/category.php +++ b/category.php @@ -3,7 +3,7 @@ // Load user and global cats. load_user_categories(); -if ( $CATEGORIES_ENABLED == 'N' ) { +if ($CATEGORIES_ENABLED == 'N') { send_to_preferred_view(); exit; } @@ -11,178 +11,183 @@ // Verify that permissions allow writing to the "wc-icons" directory. $canWrite = false; $permError = false; -if ( $ENABLE_ICON_UPLOADS == 'Y' || $is_admin ) { +if ($ENABLE_ICON_UPLOADS == 'Y' || $is_admin) { $testFile = "wc-icons/testWrite.txt"; - $testFd = @fopen ( $testFile, "w+b", false ); - @fclose ( $testFd ); - $canWrite = file_exists ( $testFile ); - if ( ! $canWrite ) { + $testFd = @fopen($testFile, "w+b", false); + if ($testFd !== false) { + fclose($testFd); + } + $canWrite = file_exists($testFile); + if (!$canWrite) { $permError = true; } else { - @unlink ( $testFile ); + @unlink($testFile); } } $catIcon = $catname = $error = $idStr = ''; -$catIconStr = translate ( 'Category Icon' ); -$globalStr = translate ( 'Global' ); +$catIconStr = translate('Category Icon'); +$globalStr = translate('Global'); $icon_path = 'wc-icons/'; // If editing, make sure they are editing their own (or they are an admin user). -if ( ! empty ( $id ) ) { - if ( empty ( $categories[$id] ) ) +if (!empty($id)) { + if (empty($categories[$id])) $error = - str_replace ( 'XXX', $id, translate ( 'Invalid entry id XXX.' ) ); + str_replace('XXX', $id, translate('Invalid entry id XXX.')); $catcolor = $categories[$id]['cat_color']; $catname = $categories[$id]['cat_name']; $catowner = $categories[$id]['cat_owner']; $catIcon = $icon_path . 'cat-' . $id . '.gif'; // Try PNG if GIF not found - if ( ! file_exists ( $catIcon ) ) + if (!file_exists($catIcon)) $catIcon = $icon_path . 'cat-' . $id . '.png'; $idStr = ''; } else $catcolor = '#000000'; -$showIconStyle = ( ! empty ( $catIcon ) && file_exists ( $catIcon ) - ? '' : 'display: none;' ); +$showIconStyle = (!empty($catIcon) && file_exists($catIcon) + ? '' : 'display: none;'); -print_header ( ['js/visible.php'] ); +print_header(['js/visible.php']); echo ' -

' . translate ( 'Categories' ) . '

- ' . display_admin_link( false ); +

' . translate('Categories') . '

+ ' . display_admin_link(false); // Display permission error if found above. -if ( $permError && $is_admin ) { - print_error_box ( - translate('The permissions for the icons directory are set to read-only') ); +if ($permError && $is_admin) { + print_error_box( + translate('The permissions for the icons directory are set to read-only') + ); } -$add = getGetValue ( 'add' ); -if ( empty ( $add ) ) +$add = getGetValue('add'); +if (empty($add)) $add = 0; // Adding/Editing category. -if ( ( ( $add == '1' ) || ( ! empty ( $id ) ) ) && empty ( $error ) ) { +if ((($add == '1') || (!empty($id))) && empty($error)) { echo '
' . csrf_form_key() . $idStr . '
- +
' . - ($is_admin && empty ($id) ? ' + . htmlspecialchars($catname) . '">' . + ($is_admin && empty($id) ? '
" - . print_radio ( 'isglobal', '', '', (empty ($catowner) && ! empty ($id)) ? 'Y' : 'N', '') . - '
' : '' ) . + . $globalStr . ":" + . print_radio('isglobal', '', '', (empty($catowner) && !empty($id)) ? 'Y' : 'N', '') . + '' : '') . '
- ' - . print_color_input_html ('catcolor', translate ('Color'), $catcolor) . + ' + . print_color_input_html('catcolor', translate('Color'), $catcolor) . '
'; - // Category icon - echo ' + // Category icon + echo '
- + ' . $catIconStr . '
- +
+ . (is_dir($icon_path) && + (($ENABLE_ICON_UPLOADS == 'Y' || $is_admin) && $canWrite) + ? translate('Add Icon to Category') . ': + . 'maxlength="50" value=""> (' - . translate ('GIF or PNG 6kb max') . ') + . translate('GIF or PNG 6kb max') . ')
    -
' : '' ) // end test of ENABLE_ICON_UPLOADS - . '
+ . translate('Search for existing icons...') + . '"> +
' : '') // end test of ENABLE_ICON_UPLOADS + . '
' - . 'Cancel ' - . ( ! empty ( $id ) ? ' + . ($add == '1' ? translate('Add') : translate('Save')) . '">' + . 'Cancel ' + . (!empty($id) ? ' ' : '' ) . ' + . translate('Delete') . '" onclick="return confirm( ' + . translate('Are you sure you want to delete this entry?', true) + . '\' )">' : '') . '
'; } else -if ( empty ( $error ) ) { +if (empty($error)) { // Displaying Categories. $global_found = false; //echo "
"; print_r($categories); echo "
"; - if ( ! empty ( $categories ) ) { + if (!empty($categories)) { echo ' '; } - echo ( $global_found ? '* ' . $globalStr : '' ) . ' -
' . translate ( 'Make New Category' ) - . '

'; + echo ($global_found ? '* ' . $globalStr : '') . ' +
' . translate('Make New Category') + . '

'; } ?>