diff --git a/includes/cli.php b/includes/cli.php index 95eb6e1..2968639 100644 --- a/includes/cli.php +++ b/includes/cli.php @@ -5,12 +5,12 @@ function add_cli_command($command, $handler, $short_desc, $usage_args=false, $lo $override=false) { global $cli_commands; if (array_key_exists($command, $cli_commands) && !$override) { - logging('ERROR', sprintf(_("The CLI command '%s' already exists.", $command))); + logging('ERROR', _("The CLI command '%s' already exists.", $command)); return False; } if (!is_callable($handler)) { - logging('ERROR', sprintf(_("The CLI command '%s' handler is not callable !"), $command)); + logging('ERROR', _("The CLI command '%s' handler is not callable !"), $command); return False; } @@ -41,6 +41,14 @@ $cli_command = null; function usage($error=false) { global $cli_commands, $cli_command, $argv; + // If more than 1 arguments passed, format error message using sprintf + if (func_num_args() > 1) { + $error = call_user_func_array( + 'sprintf', + array_merge(array($error), array_slice(func_get_args(), 1)) + ); + } + if ($error) echo "$error\n\n"; printf(_("Usage: %s [-h] [-qd] command\n"), basename($argv[0])); @@ -115,10 +123,10 @@ function handle_cli_args() { $command_args[] = $argv[$i]; else usage( - sprintf( - _("Invalid parameter \"%s\".\nNote: Command's parameter/argument must be place ". - "after the command."), $argv[$i] - ) + _( + "Invalid parameter \"%s\".\nNote: Command's parameter/argument must be place ". + "after the command." + ), $argv[$i] ); } } @@ -128,11 +136,8 @@ function handle_cli_args() { usage(); logging( - 'DEBUG', sprintf( - "Run %s command %s with argument(s) '%s'.", - basename($argv[0]), $cli_command, - implode("', '", $command_args) - ) + 'DEBUG', "Run %s command %s with argument(s) '%s'.", + basename($argv[0]), $cli_command, implode("', '", $command_args) ); try { @@ -141,7 +146,7 @@ function handle_cli_args() { exit($result?0:1); } catch(Exception $e) { - log_exception(sprintf(_("An exception occured running command %s"), $cli_command)); + log_exception($e, _("An exception occured running command %s"), $cli_command); exit(1); } } @@ -259,7 +264,7 @@ function cli_show($command_args) { $item = get_item($item_id); if (!$item) - logging('FATAL', sprintf(_("Item #%s not found."), $item_id)); + logging('FATAL', _("Item #%s not found."), $item_id); print_item_info($item); return True; @@ -283,7 +288,7 @@ function cli_delete($command_args) { $item_id = $command_args[0]; $item = get_item($item_id); if (!$item) - logging('FATAL', sprintf(_("Item #%s not found."), $item_id)); + logging('FATAL', _("Item #%s not found."), $item_id); print_item_info($item); @@ -298,7 +303,7 @@ function cli_delete($command_args) { echo "\n"; if (!delete_item($item['id'])) - logging('FATAL', sprintf(_("An error occured deleting item #%d."), $item_id)); + logging('FATAL', _("An error occured deleting item #%d."), $item_id); return True; } @@ -376,31 +381,27 @@ function cli_cron($command_args) { foreach($items['items'] as $item) { if ($item['date'] < $limit) { if ($just_try) { - logging('DEBUG', sprintf( - 'Just-try mode: do not really delete item #%s (%s, creation date: %s)', + logging('DEBUG', 'Just-try mode: do not really delete item #%s (%s, creation date: %s)', $item['id'], $item['name'], format_time($item['date']) - )); + ); } else if (delete_item($item['id'])) { - logging('INFO', sprintf( - 'Item #%s (%s) deleted (creation date: %s)', - - )); + logging('INFO', 'Item #%s (%s) deleted (creation date: %s)', + $item['id'], $item['name'], format_time($item['date']) + ); remove_item_attachments($item['id']); } else { - logging('ERROR', sprintf( - 'Fail to delete item "%s" (%s, creation date: %s)', + logging('ERROR', 'Fail to delete item "%s" (%s, creation date: %s)', $item['id'], $item['name'], format_time($item['date']) - )); + ); $error = true; } } else { - logging('DEBUG', sprintf( - 'Item "%s" (%s) still valid (creation date: %s)', + logging('DEBUG', 'Item "%s" (%s) still valid (creation date: %s)', $item['id'], $item['name'], format_time($item['date']) - )); + ); } } exit($error?1:0); diff --git a/includes/db.php b/includes/db.php index 2c3ff8d..5fa3d6a 100644 --- a/includes/db.php +++ b/includes/db.php @@ -353,7 +353,10 @@ function search_items($params) { logging('ERROR', 'search_items() : search in DB return false'); } catch (Exception $e) { - log_exception($e, "An exception occured searching items with params ".preg_replace("/\n[ \t]*/"," ",print_r($params,1))." infos from database : "); + log_exception( + $e, "An exception occured searching items with params %s infos from database : ", + preg_replace("/\n[ \t]*/", " ", print_r($params, true)) + ); } return false; } diff --git a/includes/logging.php b/includes/logging.php index 731e928..4de09ff 100644 --- a/includes/logging.php +++ b/includes/logging.php @@ -37,6 +37,14 @@ function logging($level, $message) { $_log_file_fd = fopen($log_file, 'a'); } + // If more than 2 arguments passed, format message using sprintf + if (func_num_args() > 2) { + $message = call_user_func_array( + 'sprintf', + array_merge(array($message), array_slice(func_get_args(), 2)) + ); + } + if (php_sapi_name() == "cli") { $msg = implode(' - ', array( date('Y/m/d H:i:s'), @@ -110,18 +118,27 @@ function get_debug_backtrace_context($ignore_last=0) { return implode("\n", $msg); } -function log_exception($exception, $prefix='') { +function log_exception($exception, $prefix=null) { + // If more than 2 arguments passed, format prefix message using sprintf + if ($prefix && func_num_args() > 2) { + $prefix = call_user_func_array( + 'sprintf', + array_merge(array($prefix), array_slice(func_get_args(), 2)) + ); + } logging( - "ERROR", - ($prefix?"$prefix :\n":"An exception occured :\n"). - get_debug_backtrace_context(1). "\n" . - "## ".$exception->getFile().":".$exception->getLine(). " : ". $exception->getMessage()); + "ERROR", "%s:\n%s\n## %s:%d : %s", + ($prefix?$prefix:"An exception occured"), + get_debug_backtrace_context(1), + $exception->getFile(), $exception->getLine(), + $exception->getMessage()); } set_exception_handler('log_exception'); // Handle PHP error logging function log_php_eror($errno, $errstr, $errfile, $errline) { - logging("ERROR", "A PHP error occured : [$errno] $errstr\nFile : $errfile (line : $errline)"); + logging("ERROR", "A PHP error occured : [%d] %s\nFile : %s (line : %d)", + $errno, $errstr, $errfile, $errline); return False; } if ($log_level == 'DEBUG') diff --git a/includes/smarty.php b/includes/smarty.php index 4190ad5..dafd3fc 100644 --- a/includes/smarty.php +++ b/includes/smarty.php @@ -85,13 +85,27 @@ $smarty->assign('session_key', $_SESSION['session_key']); if (!isset($_SESSION['errors'])) $_SESSION['errors']=array(); function add_error($error) { - $_SESSION['errors'][]=$error; + // If more than one arguments passed, format error message using sprintf + if (func_num_args() > 2) { + $error = call_user_func_array( + 'sprintf', + array_merge(array($error), array_slice(func_get_args(), 1)) + ); + } + $_SESSION['errors'][] = $error; } if (!isset($_SESSION['messages'])) $_SESSION['messages']=array(); function add_message($message) { - $_SESSION['messages'][]=$message; + // If more than one arguments passed, format message using sprintf + if (func_num_args() > 2) { + $message = call_user_func_array( + 'sprintf', + array_merge(array($message), array_slice(func_get_args(), 1)) + ); + } + $_SESSION['messages'][] = $message; } // Handle CSS & JS files included @@ -133,6 +147,13 @@ function display_template($template, $pagetitle=false) { if (!$template) logging("FATAL", _("No template specified.")); global $smarty; + // If more than 2 arguments passed, format pagetitle using sprintf + if ($pagetitle & func_num_args() > 2) { + $pagetitle = call_user_func_array( + 'sprintf', + array_merge(array($pagetitle), array_slice(func_get_args(), 2)) + ); + } try { _defineCommonTemplateVariables($template, $pagetitle); $smarty->display($template); diff --git a/includes/translation-cli.php b/includes/translation-cli.php index 364197b..fadf791 100644 --- a/includes/translation-cli.php +++ b/includes/translation-cli.php @@ -162,24 +162,14 @@ function cli_update_messages($command_args) { $compendium_args = array(); foreach ($command_args as $path) { if (!file_exists($path)) - logging( - 'FATAL', sprintf( - _("Compendium file %s not found."), - $path - ) - ); + logging('FATAL', _("Compendium file %s not found."), $path); $compendium_args[] = '-C'; $compendium_args[] = $path; } $pot_file = "$root_lang_dir/messages.pot"; if (!is_file($pot_file)) - logging( - 'FATAL', sprintf( - _("POT file not found (%s). Please run extract_messages first."), - $pot_file - ) - ); + logging('FATAL', _("POT file not found (%s). Please run extract_messages first."), $pot_file); if ($dh = opendir($root_lang_dir)) { $error = False; @@ -191,16 +181,14 @@ function cli_update_messages($command_args) { ) continue; - logging('DEBUG', sprintf(_("Lang directory '%s' found"), $file)); + logging('DEBUG', _("Lang directory '%s' found"), $file); // Check LC_MESSAGES directory exists $lang = $file; $lang_dir = $root_lang_dir . '/' . $file . '/LC_MESSAGES' ; if (!is_dir($lang_dir)) { - logging('DEBUG', sprintf( - _("LC_MESSAGES directory not found in lang '%s' directory, ignore it."), - $lang) - ); + logging('DEBUG', _("LC_MESSAGES directory not found in lang '%s' directory, ignore it."), + $lang); continue; } @@ -214,10 +202,8 @@ function cli_update_messages($command_args) { if (is_array($result) && $result[0] == 0) { $created = true; } else { - logging('ERROR', sprintf( - _("Fail to init messages in %s PO file using msginit (%s)."), - $lang, $po_file) - ); + logging('ERROR', _("Fail to init messages in %s PO file using msginit (%s)."), + $lang, $po_file); $error = True; } } @@ -234,25 +220,20 @@ function cli_update_messages($command_args) { ) ); if (!is_array($result) || $result[0] != 0) { - logging('ERROR', sprintf( - _("Fail to update messages in %s PO file using msgmerge (%s)."), - $lang, $po_file) - ); + logging('ERROR', _("Fail to update messages in %s PO file using msgmerge (%s)."), + $lang, $po_file); $error = True; } } elseif (!$created) { - logging('DEBUG', sprintf( - _("PO file not found in lang '%s' directory, ignore it."), - $lang) - ); + logging('DEBUG', _("PO file not found in lang '%s' directory, ignore it."), $lang); } } closedir($dh); return !$error; } - logging('FATAL', sprintf(_("Fail to open root lang directory (%s)."), $root_dir_path)); + logging('FATAL', _("Fail to open root lang directory (%s)."), $root_dir_path); } add_cli_command( 'update_messages', @@ -286,70 +267,53 @@ function cli_compile_messages($command_args) { if (dirname($real_lang_dir) != '.' || !is_dir($root_lang_dir . '/' . $real_lang_dir)) continue; $lang = $file; - logging('DEBUG', sprintf(_("Lang alias symlink found: %s -> %s"), $lang, $real_lang_dir)); + logging('DEBUG', _("Lang alias symlink found: %s -> %s"), $lang, $real_lang_dir); // Create JSON catalog symlink (if not exists) $json_link = "$root_dir_path/public_html/translations/$lang.json"; $link_target = "$real_lang_dir.json"; if (!file_exists($json_link)) { if (symlink($link_target, $json_link)) { - logging( - 'INFO', - sprintf( - _("JSON catalog symlink for %s -> %s created (%s)"), - $lang, $real_lang_dir, $json_link) - ); + logging('INFO', _("JSON catalog symlink for %s -> %s created (%s)"), + $lang, $real_lang_dir, $json_link); } else { - logging( - 'ERROR', - sprintf( - _("Fail to create JSON catalog symlink for %s -> %s (%s)"), - $lang, $real_lang_dir, $json_link) - ); + logging('ERROR', _("Fail to create JSON catalog symlink for %s -> %s (%s)"), + $lang, $real_lang_dir, $json_link); $error = True; } } elseif (readlink($json_link) == $link_target) { - logging( - 'DEBUG', - sprintf( - _("JSON catalog symlink for %s -> %s already exist (%s)"), - $lang, $real_lang_dir, $json_link) - ); + logging('DEBUG', _("JSON catalog symlink for %s -> %s already exist (%s)"), + $lang, $real_lang_dir, $json_link); } else { logging( 'WARNING', - sprintf( - _("JSON catalog file for %s already exist, but it's not a symlink to %s (%s)"), - $lang, $real_lang_dir, $json_link) + _("JSON catalog file for %s already exist, but it's not a symlink to %s (%s)"), + $lang, $real_lang_dir, $json_link ); $error = True; } continue; } - logging('DEBUG', sprintf(_("Lang directory '%s' found"), $file)); + logging('DEBUG', _("Lang directory '%s' found"), $file); // Check LC_MESSAGES directory exists $lang = $file; $lang_dir = $root_lang_dir . '/' . $file . '/LC_MESSAGES' ; if (!is_dir($lang_dir)) { - logging('DEBUG', sprintf( - _("LC_MESSAGES directory not found in lang '%s' directory, ignore it."), - $lang) - ); + logging('DEBUG', _("LC_MESSAGES directory not found in lang '%s' directory, ignore it."), + $lang); continue; } // Test .PO file is present $po_file = $lang_dir . '/' . TEXT_DOMAIN . '.po'; if (!is_file($po_file)) { - logging('DEBUG', sprintf( - _("PO file not found in lang '%s' directory, ignore it."), - $lang) - ); + logging('DEBUG', _("PO file not found in lang '%s' directory, ignore it."), + $lang); continue; } @@ -360,9 +324,10 @@ function cli_compile_messages($command_args) { array("msgfmt", "-o", $mo_file, $po_file) ); if (!is_array($result) || $result[0] != 0) { - logging('ERROR', sprintf( + logging( + 'ERROR', _("Fail to compile messages from %s PO file as MO file using msgfmt (%s)."), - $lang, $po_file) + $lang, $po_file ); $error = True; } @@ -371,31 +336,24 @@ function cli_compile_messages($command_args) { $json_catalog = po2json($lang, $po_file); $json_file = "$root_dir_path/public_html/translations/$lang.json"; if(!$fd = fopen($json_file, 'w')) { - logging('ERROR', sprintf( - _("Fail to open %s JSON catalog file in write mode (%s)."), - $lang, $json_file) - ); + logging('ERROR', _("Fail to open %s JSON catalog file in write mode (%s)."), + $lang, $json_file); $error = True; } elseif (fwrite($fd, $json_catalog) === false) { - logging('ERROR', sprintf( - _("Fail to write %s JSON catalog in file (%s)."), - $lang, $json_file) - ); + logging('ERROR', _("Fail to write %s JSON catalog in file (%s)."), + $lang, $json_file); $error = True; } else { - logging('INFO', sprintf( - _("%s JSON catalog writed (%s)."), - $lang, $json_file) - ); + logging('INFO', _("%s JSON catalog writed (%s)."), $lang, $json_file); } } closedir($dh); return !$error; } - logging('FATAL', sprintf(_("Fail to open root lang directory (%s)."), $root_dir_path)); + logging('FATAL', _("Fail to open root lang directory (%s)."), $root_dir_path); } add_cli_command( 'compile_messages', diff --git a/includes/url-public.php b/includes/url-public.php index 41921e7..f7849a6 100644 --- a/includes/url-public.php +++ b/includes/url-public.php @@ -125,8 +125,8 @@ function handle_show($request) { )); display_template( - "show.tpl", - sprintf(_("Element %s"), (is_array($item)?$item['name']:"#".$request -> id)) + "show.tpl", _("Element %s"), + (is_array($item)?$item['name']:"#".$request -> id) ); } add_url_handler('|^item/(?P[0-9]+)$|', 'handle_show'); @@ -139,7 +139,7 @@ function handle_create($request) { if (isset($_POST['submit']) && empty($field_errors)) { $item = add_item($info); if (is_array($item)) { - add_message(sprintf(_("The element '% s' has been created."), $item['name'])); + add_message(_("The element '% s' has been created."), $item['name']); redirect('item/'.$item['id']); } else { @@ -180,11 +180,11 @@ function handle_modify($request) { } logging('DEBUG', 'Changes : '.vardump($changes)); if (empty($changes)) { - add_message(sprintf(_("You have not made any changes to element '% s'."), $item['name'])); + add_message(_("You have not made any changes to element '% s'."), $item['name']); redirect('item/'.$item['id']); } else if (update_item($item['id'], $changes) === true) { - add_message(sprintf(_("The element '% s' has been updated successfully."), $item['name'])); + add_message(_("The element '% s' has been updated successfully."), $item['name']); redirect('item/'.$item['id']); } else { @@ -207,7 +207,10 @@ function handle_modify($request) { error_404(); } - display_template("form.tpl", sprintf(_("Element %s: Modification"), (is_array($item)?$item['name']:"#".$request -> id))); + display_template( + "form.tpl", _("Element %s: Modification"), + (is_array($item)?$item['name']:"#".$request -> id) + ); } add_url_handler('|^item/(?P[0-9]+)/modify$|', 'handle_modify'); @@ -216,7 +219,7 @@ function handle_archive($request) { $item = get_item_from_url($request -> id); if(!is_array($item)) { - add_error(sprintf(_("Item #% s not found."), $request -> id)); + add_error(_("Item #% s not found."), $request -> id); redirect('item'); } elseif ($item['status'] == 'archived') { @@ -226,9 +229,7 @@ function handle_archive($request) { add_error(_('You cannot archive this item.')); } else if (archive_item($item['id']) === true) { - add_message(sprintf( - _("The element '% s' has been archived successfully."), - $item['name'])); + add_message(_("The element '% s' has been archived successfully."), $item['name']); } else { add_error(_('An error occurred while archiving this item.')); @@ -242,15 +243,13 @@ function handle_delete($request) { $item = get_item_from_url($request -> id); if(!is_array($item)) { - add_error(sprintf(_("Item #% s not found."), $request -> id)); + add_error(_("Item #% s not found."), $request -> id); } else if (!can_delete($item)) { add_error(_('You cannot delete this item.')); } else if (delete_item($item['id']) === true) { - add_message(sprintf( - _("The element '% s' has been deleted successfully."), - $item['name'])); + add_message(_("The element '% s' has been deleted successfully."), $item['name']); } else { add_error(_('An error occurred while deleting this item.'));