]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/OStatus/extlib/Crypt/RSA/ErrorHandler.php
generate keypairs for users, and put them in the XRD for discovery
[quix0rs-gnu-social.git] / plugins / OStatus / extlib / Crypt / RSA / ErrorHandler.php
1 <?php
2 /**
3  * Crypt_RSA allows to do following operations:
4  *     - key pair generation
5  *     - encryption and decryption
6  *     - signing and sign validation
7  *
8  * PHP versions 4 and 5
9  *
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.
15  *
16  * @category  Encryption
17  * @package   Crypt_RSA
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
23  */
24
25 /**
26  * uses PEAR's error handling
27  */
28 require_once 'PEAR.php';
29
30 /**
31  * cannot load required extension for math wrapper
32  */
33 define('CRYPT_RSA_ERROR_NO_EXT', 1);
34
35 /**
36  * cannot load any math wrappers.
37  * Possible reasons:
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
41  */
42 define('CRYPT_RSA_ERROR_NO_WRAPPERS', 2);
43
44 /**
45  * cannot find file, containing requested math wrapper
46  */
47 define('CRYPT_RSA_ERROR_NO_FILE', 3);
48
49 /**
50  * cannot find math wrapper class in the math wrapper file
51  */
52 define('CRYPT_RSA_ERROR_NO_CLASS', 4);
53
54 /**
55  * invalid key type passed to function (it must be 'public' or 'private')
56  */
57 define('CRYPT_RSA_ERROR_WRONG_KEY_TYPE', 5);
58
59 /**
60  * key modulus must be greater than key exponent
61  */
62 define('CRYPT_RSA_ERROR_EXP_GE_MOD', 6);
63
64 /**
65  * missing $key_len parameter in Crypt_RSA_KeyPair::generate($key_len) function
66  */
67 define('CRYPT_RSA_ERROR_MISSING_KEY_LEN', 7);
68
69 /**
70  * wrong key object passed to function (it must be an object of Crypt_RSA_Key class)
71  */
72 define('CRYPT_RSA_ERROR_WRONG_KEY', 8);
73
74 /**
75  * wrong name of hash function passed to Crypt_RSA::setParams() function
76  */
77 define('CRYPT_RSA_ERROR_WRONG_HASH_FUNC', 9);
78
79 /**
80  * key, used for signing, must be private
81  */
82 define('CRYPT_RSA_ERROR_NEED_PRV_KEY', 10);
83
84 /**
85  * key, used for sign validating, must be public
86  */
87 define('CRYPT_RSA_ERROR_NEED_PUB_KEY', 11);
88
89 /**
90  * parameters must be passed to function as associative array
91  */
92 define('CRYPT_RSA_ERROR_WRONG_PARAMS', 12);
93
94 /**
95  * error tail of decrypted text. Maybe, wrong decryption key?
96  */
97 define('CRYPT_RSA_ERROR_WRONG_TAIL', 13);
98
99 /**
100  * Crypt_RSA_ErrorHandler class.
101  *
102  * This class is used as base for Crypt_RSA, Crypt_RSA_Key
103  * and Crypt_RSA_KeyPair classes.
104  *
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
112  *
113  * @category  Encryption
114  * @package   Crypt_RSA
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
120  * @access    public
121  */
122 class Crypt_RSA_ErrorHandler
123 {
124     /**
125      * array of error objects, pushed by $this->pushError()
126      *
127      * @var array
128      * @access private
129      */
130     var $_errors = array();
131
132     /**
133      * name of error handler - function, which calls on $this->pushError() call
134      *
135      * @var string
136      * @access private
137      */
138     var $_error_handler = '';
139
140     /**
141      * Returns true if list of errors is not empty, else returns false
142      *
143      * @param mixed $err Check if the object is an error
144      *
145      * @return bool    true, if list of errors is not empty or $err is PEAR_Error object, else false
146      * @access public
147      */
148     function isError($err = null)
149     {
150         return is_null($err) ? (sizeof($this->_errors) > 0) : PEAR::isError($err);
151     }
152
153     /**
154      * Returns list of all errors, pushed to error list by $this->pushError()
155      *
156      * @return array    list of errors (usually it contains objects of PEAR_Error class)
157      * @access public
158      */
159     function getErrorList()
160     {
161         return $this->_errors;
162     }
163
164     /**
165      * Returns last error from errors list or false, if list is empty
166      *
167      * @return mixed
168      *         last error from errors list (usually it is PEAR_Error object)
169      *         or false, if list is empty.
170      *
171      * @access public
172      */
173     function getLastError()
174     {
175         $len = sizeof($this->_errors);
176         return $len ? $this->_errors[$len - 1] : false;
177     }
178
179     /**
180      * pushes error object $error to the error list
181      *
182      * @param string $errstr error string
183      * @param int    $errno  error number
184      *
185      * @return bool          true on success, false on error
186      * @access public
187      */
188     function pushError($errstr, $errno = 0)
189     {
190         $this->_errors[] = PEAR::raiseError($errstr, $errno);
191
192         if ($this->_error_handler != '') {
193             // call user defined error handler
194             $func = $this->_error_handler;
195             $func($this);
196         }
197         return true;
198     }
199
200     /**
201      * sets error handler to function with name $func_name.
202      * Function $func_name must accept one parameter - current
203      * object, which triggered error.
204      *
205      * @param string $func_name name of error handler function
206      *
207      * @return bool             true on success, false on error
208      * @access public
209      */
210     function setErrorHandler($func_name = '')
211     {
212         if ($func_name == '') {
213             $this->_error_handler = '';
214         }
215         if (!function_exists($func_name)) {
216             return false;
217         }
218         $this->_error_handler = $func_name;
219         return true;
220     }
221
222     /**
223      * returns name of current error handler, or null if there is no error handler
224      *
225      * @return mixed  error handler name as string or null, if there is no error handler
226      * @access public
227      */
228     function getErrorHandler()
229     {
230         return $this->_error_handler;
231     }
232 }
233
234 ?>