*/ class LSerror { private static $_errorCodes = array( '0' => array('msg' => "%{msg}") ); /** * Define an error * * @author Benjamin Renard * * @param[in] $code string The error code * @param[in] $msg LSformat The LSformat of the error message * @param[in] $escape boolean Set to false if you don't want the message * to be escaped on display (Default: true) * * @retval void */ public static function defineError($code=-1, $msg='', $escape=True) { self :: $_errorCodes[$code] = array( 'msg' => $msg, 'escape' => boolval($escape), ); } /** * Add an error * * @author Benjamin Renard * * @param[in] $code string The error code * @param[in] $msg mixed If error code is not specified (or not defined), it could content the * the error message. If error code is provided (and defined), this parameter * will be used to format registred error message (as LSformat). In this case, * it could be any of data support by getFData function as $data parameter. * * @param[in] $escape boolean Set to false if you don't want the message * to be escaped on display (Default: true) * * @retval void */ public static function addErrorCode($code=null, $msg=null, $escape=true) { $_SESSION['LSerror'][] = self :: formatError($code, $msg, $escape); if (class_exists('LSlog')) LSlog :: error(self :: formatError($code, $msg, $escape, 'addslashes')); } /** * Show errors * * @author Benjamin Renard * * @param[in] $return boolean True if you want to retrieved errors message. If false, * (default), LSerrors template variable will be assigned * with errors message. * * @retval string|null */ public static function display($return=False) { $errors = self :: getErrors(); if ($errors && $return) return $errors; LStemplate :: assign('LSerrors', base64_encode(json_encode($errors))); return; } /** * Print errors and stop LdapSaisie * * @author Benjamin Renard * * @param[in] $code Error code (see addErrorCode()) * @param[in] $msg Error msg (see addErrorCode()) * @param[in] $escape boolean (see addErrorCode()) * * @retval void */ public static function stop($code=-1, $msg='', $escape=true) { if(!empty($_SESSION['LSerror'])) { print "

"._('Errors')."

\n"; print self :: display(true); } print "

"._('Stop')."

\n"; exit (self :: formatError($code, $msg, $escape)); } /** * Format current errors message * * @author Benjamin Renard * * @retvat string Le texte des erreurs */ public static function getErrors() { $return = ( self :: errorsDefined()? $_SESSION['LSerror']: array() ); self :: resetError(); return $return; } /** * Format error message * * @author Benjamin Renard * * @retvat string Le texte des erreurs */ private static function formatError($code=null, $message=null, $escape=True, $escape_method=null) { if ($code && array_key_exists($code, self :: $_errorCodes)) { $message = getFData(__(self :: $_errorCodes[$code]['msg']), $message); if (!self :: $_errorCodes[$code]['escape'] === false) $escape = false; } else if (!$message) { if ($code) $message = $code; else $message = _("Unknown error"); } if ($escape !== false) { if (is_null($escape_method) || !is_callable($escape_method)) $escape_method = 'htmlentities'; $code = call_user_func($escape_method, $code); $message = call_user_func($escape_method, $message); } return ($code?"(Code $code) ":"").$message; } /** * Check if some errors are defined * * @author Benjamin Renard * * @retvat boolean */ public static function errorsDefined() { return ( isset($_SESSION['LSerror']) && is_array($_SESSION['LSerror']) && !empty($_SESSION['LSerror']) ); } /** * Clear current errors * * @author Benjamin Renard * * @retvat void */ private static function resetError() { if (isset($_SESSION['LSerror'])) unset ($_SESSION['LSerror']); } /** * Check if is Net_LDAP2 error and display possible error message * * @param[in] $data mixed Data * * @retval boolean True if it's an error or False **/ public static function isLdapError($data) { if (Net_LDAP2::isError($data)) { LSerror :: addErrorCode(0, $data -> getMessage()); return true; } return false; } } /* * Error Codes */ LSerror :: defineError(-1, ___("Unknown error : %{error}"));