3 * Crypt_RSA allows to do following operations:
4 * - key pair generation
5 * - encryption and decryption
6 * - signing and sign validation
10 * LICENSE: This source file is subject to version 3.0 of the PHP license
11 * that is available through the world-wide-web at the following URI:
12 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
13 * the PHP License and are unable to obtain it through the web, please
14 * send a note to license@php.net so we can mail you a copy immediately.
16 * @category Encryption
18 * @author Alexander Valyalkin <valyala@gmail.com>
19 * @copyright 2005 Alexander Valyalkin
20 * @license http://www.php.net/license/3_0.txt PHP License 3.0
21 * @version CVS: $Id: ErrorHandler.php,v 1.4 2009/01/05 08:30:29 clockwerx Exp $
22 * @link http://pear.php.net/package/Crypt_RSA
26 * uses PEAR's error handling
28 require_once 'PEAR.php';
31 * cannot load required extension for math wrapper
33 define('CRYPT_RSA_ERROR_NO_EXT', 1);
36 * cannot load any math wrappers.
38 * - there is no any wrappers (they must exist in Crypt/RSA/Math folder )
39 * - all available wrappers are incorrect (read docs/Crypt_RSA/docs/math_wrappers.txt )
40 * - cannot load any extension, required by available wrappers
42 define('CRYPT_RSA_ERROR_NO_WRAPPERS', 2);
45 * cannot find file, containing requested math wrapper
47 define('CRYPT_RSA_ERROR_NO_FILE', 3);
50 * cannot find math wrapper class in the math wrapper file
52 define('CRYPT_RSA_ERROR_NO_CLASS', 4);
55 * invalid key type passed to function (it must be 'public' or 'private')
57 define('CRYPT_RSA_ERROR_WRONG_KEY_TYPE', 5);
60 * key modulus must be greater than key exponent
62 define('CRYPT_RSA_ERROR_EXP_GE_MOD', 6);
65 * missing $key_len parameter in Crypt_RSA_KeyPair::generate($key_len) function
67 define('CRYPT_RSA_ERROR_MISSING_KEY_LEN', 7);
70 * wrong key object passed to function (it must be an object of Crypt_RSA_Key class)
72 define('CRYPT_RSA_ERROR_WRONG_KEY', 8);
75 * wrong name of hash function passed to Crypt_RSA::setParams() function
77 define('CRYPT_RSA_ERROR_WRONG_HASH_FUNC', 9);
80 * key, used for signing, must be private
82 define('CRYPT_RSA_ERROR_NEED_PRV_KEY', 10);
85 * key, used for sign validating, must be public
87 define('CRYPT_RSA_ERROR_NEED_PUB_KEY', 11);
90 * parameters must be passed to function as associative array
92 define('CRYPT_RSA_ERROR_WRONG_PARAMS', 12);
95 * error tail of decrypted text. Maybe, wrong decryption key?
97 define('CRYPT_RSA_ERROR_WRONG_TAIL', 13);
100 * Crypt_RSA_ErrorHandler class.
102 * This class is used as base for Crypt_RSA, Crypt_RSA_Key
103 * and Crypt_RSA_KeyPair classes.
105 * It provides following functions:
106 * - isError() - returns true, if list contains errors, else returns false
107 * - getErrorList() - returns error list
108 * - getLastError() - returns last error from error list or false, if list is empty
109 * - pushError($errstr) - pushes $errstr into the error list
110 * - setErrorHandler($new_error_handler) - sets error handler function
111 * - getErrorHandler() - returns name of error handler function
113 * @category Encryption
115 * @author Alexander Valyalkin <valyala@gmail.com>
116 * @copyright 2005 Alexander Valyalkin
117 * @license http://www.php.net/license/3_0.txt PHP License 3.0
118 * @version Release: @package_version@
119 * @link http://pear.php.net/package/Crypt_RSA
122 class Crypt_RSA_ErrorHandler
125 * array of error objects, pushed by $this->pushError()
130 var $_errors = array();
133 * name of error handler - function, which calls on $this->pushError() call
138 var $_error_handler = '';
141 * Returns true if list of errors is not empty, else returns false
143 * @param mixed $err Check if the object is an error
145 * @return bool true, if list of errors is not empty or $err is PEAR_Error object, else false
148 function isError($err = null)
150 return is_null($err) ? (sizeof($this->_errors) > 0) : PEAR::isError($err);
154 * Returns list of all errors, pushed to error list by $this->pushError()
156 * @return array list of errors (usually it contains objects of PEAR_Error class)
159 function getErrorList()
161 return $this->_errors;
165 * Returns last error from errors list or false, if list is empty
168 * last error from errors list (usually it is PEAR_Error object)
169 * or false, if list is empty.
173 function getLastError()
175 $len = sizeof($this->_errors);
176 return $len ? $this->_errors[$len - 1] : false;
180 * pushes error object $error to the error list
182 * @param string $errstr error string
183 * @param int $errno error number
185 * @return bool true on success, false on error
188 function pushError($errstr, $errno = 0)
190 $this->_errors[] = PEAR::raiseError($errstr, $errno);
192 if ($this->_error_handler != '') {
193 // call user defined error handler
194 $func = $this->_error_handler;
201 * sets error handler to function with name $func_name.
202 * Function $func_name must accept one parameter - current
203 * object, which triggered error.
205 * @param string $func_name name of error handler function
207 * @return bool true on success, false on error
210 function setErrorHandler($func_name = '')
212 if ($func_name == '') {
213 $this->_error_handler = '';
215 if (!function_exists($func_name)) {
218 $this->_error_handler = $func_name;
223 * returns name of current error handler, or null if there is no error handler
225 * @return mixed error handler name as string or null, if there is no error handler
228 function getErrorHandler()
230 return $this->_error_handler;