* @package api * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License * @version $Id$ */ use EGroupware\Api; /** * Translate message only if translation object is already loaded * * This function is usefull for exception handlers or early stages of the initialisation of the egw object, * as calling lang would try to load the translations, evtl. cause more errors, eg. because there's no db-connection. * * @param string $key message in englich with %1, %2, ... placeholders * @param string $vars =null multiple values to replace the placeholders * @return string translated message with placeholders replaced */ function try_lang($key,$vars=null) { static $varnames = array('%1','%2','%3','%4'); if(!is_array($vars)) { $vars = func_get_args(); array_shift($vars); // remove $key } return class_exists('EGroupware\Api\Translations',false) ? Api\Translation::translate($key,$vars) : str_replace($varnames,$vars,$key); } /** * Clasify exception for a headline and log it to error_log, if not running as cli * * @param Exception|Error $e * @param string &$headline */ function _egw_log_exception($e,&$headline=null) { $trace = explode("\n", $e->getTraceAsString()); if ($e instanceof Api\Exception\NoPermission) { $headline = try_lang('Permission denied!'); } elseif ($e instanceof Api\Db\Exception) { $headline = try_lang('Database error'); } elseif ($e instanceof Api\Exception\WrongUserinput) { $headline = ''; // message contains the whole message, it's usually no real error but some input validation } elseif ($e instanceof egw_exception_warning) { $headline = 'PHP Warning'; array_shift($trace); } else { $headline = try_lang('An error happened'); } // log exception to error log, if not running as cli, // which outputs the error_log to stderr and therefore output it twice to the user if(isset($_SERVER['HTTP_HOST']) || $GLOBALS['egw_info']['flags']['no_exception_handler'] !== 'cli') { error_log($headline.($e instanceof egw_exception_warning ? ': ' : ' ('.get_class($e).'): ').$e->getMessage()); foreach($trace as $line) { error_log($line); } error_log('# Instance='.$GLOBALS['egw_info']['user']['domain'].', User='.$GLOBALS['egw_info']['user']['account_lid']. ', Request='.$_SERVER['REQUEST_METHOD'].' '.($_SERVER['HTTPS']?'https://':'http://').$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']. ', User-agent='.$_SERVER['HTTP_USER_AGENT']); } } /** * Fail a little bit more gracefully then an uncought exception * * Does NOT return * * @param Exception|Error $e */ function egw_exception_handler($e) { // handle redirects without logging if ($e instanceof Api\Exception\Redirect) { Api\Egw::redirect($e->url, $e->app); } // logging all exceptions to the error_log (if not cli) and get headline $headline = null; _egw_log_exception($e,$headline); // exception handler for cli (command line interface) clients, no html, no logging if(!isset($_SERVER['HTTP_HOST']) || $GLOBALS['egw_info']['flags']['no_exception_handler'] == 'cli') { echo ($headline ? $headline.': ' : '').$e->getMessage()."\n"; if ($GLOBALS['egw_info']['server']['exception_show_trace']) { echo $e->getTraceAsString()."\n"; } exit($e->getCode() ? $e->getCode() : 9999); // allways give a non-zero exit code } // regular GUI exception if (!isset($GLOBALS['egw_info']['flags']['no_exception_handler'])) { header('HTTP/1.1 500 '.$headline); $message = '
'.Api\Html::htmlspecialchars($e->getMessage())."\n\n"; // only show trace (incl. function arguments) if explicitly enabled, eg. on a development system if ($GLOBALS['egw_info']['server']['exception_show_trace']) { $message .= Api\Html::htmlspecialchars($e->getTraceAsString()); } $message .= "\n"; if (is_a($e, 'EGroupware\Api\Db\Exception\Setup')) { $setup_dir = str_replace(array('home/index.php','index.php'),'setup/',$_SERVER['PHP_SELF']); $message .= 'Run setup to install or configure EGroupware.'; } elseif (is_object($GLOBALS['egw']) && isset($GLOBALS['egw']->session) && method_exists($GLOBALS['egw'],'link')) { $message .= '
'.try_lang('Click here to resume your eGroupWare Session.').'
'; } if (is_object($GLOBALS['egw']) && isset($GLOBALS['egw']->framework)) { $GLOBALS['egw']->framework->render($message,$headline); } else { echo "\n\n