PageRenderTime 23ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/s3db3.5.10/pearlib/RSACrypt/Crypt/Crypt/RSA/ErrorHandler.php

https://code.google.com/p/s3db/
PHP | 231 lines | 58 code | 24 blank | 149 comment | 5 complexity | 47b891a20cc3dd80cec482b5d413b092 MD5 | raw file
  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, 2006 Alexander Valyalkin
  20. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  21. * @version 1.2.0b
  22. * @link http://pear.php.net/package/Crypt_RSA
  23. */
  24. /**
  25. * uses PEAR's error handling
  26. */
  27. require_once 'PEAR.php';
  28. /**
  29. * cannot load required extension for math wrapper
  30. */
  31. define('CRYPT_RSA_ERROR_NO_EXT', 1);
  32. /**
  33. * cannot load any math wrappers.
  34. * Possible reasons:
  35. * - there is no any wrappers (they must exist in Crypt/RSA/Math folder )
  36. * - all available wrappers are incorrect (read docs/Crypt_RSA/docs/math_wrappers.txt )
  37. * - cannot load any extension, required by available wrappers
  38. */
  39. define('CRYPT_RSA_ERROR_NO_WRAPPERS', 2);
  40. /**
  41. * cannot find file, containing requested math wrapper
  42. */
  43. define('CRYPT_RSA_ERROR_NO_FILE', 3);
  44. /**
  45. * cannot find math wrapper class in the math wrapper file
  46. */
  47. define('CRYPT_RSA_ERROR_NO_CLASS', 4);
  48. /**
  49. * invalid key type passed to function (it must be 'public' or 'private')
  50. */
  51. define('CRYPT_RSA_ERROR_WRONG_KEY_TYPE', 5);
  52. /**
  53. * key modulus must be greater than key exponent
  54. */
  55. define('CRYPT_RSA_ERROR_EXP_GE_MOD', 6);
  56. /**
  57. * missing $key_len parameter in Crypt_RSA_KeyPair::generate($key_len) function
  58. */
  59. define('CRYPT_RSA_ERROR_MISSING_KEY_LEN', 7);
  60. /**
  61. * wrong key object passed to function (it must be an object of Crypt_RSA_Key class)
  62. */
  63. define('CRYPT_RSA_ERROR_WRONG_KEY', 8);
  64. /**
  65. * wrong name of hash function passed to Crypt_RSA::setParams() function
  66. */
  67. define('CRYPT_RSA_ERROR_WRONG_HASH_FUNC', 9);
  68. /**
  69. * key, used for signing, must be private
  70. */
  71. define('CRYPT_RSA_ERROR_NEED_PRV_KEY', 10);
  72. /**
  73. * key, used for sign validating, must be public
  74. */
  75. define('CRYPT_RSA_ERROR_NEED_PUB_KEY', 11);
  76. /**
  77. * parameters must be passed to function as associative array
  78. */
  79. define('CRYPT_RSA_ERROR_WRONG_PARAMS', 12);
  80. /**
  81. * error tail of decrypted text. Maybe, wrong decryption key?
  82. */
  83. define('CRYPT_RSA_ERROR_WRONG_TAIL', 13);
  84. /**
  85. * Crypt_RSA_ErrorHandler class.
  86. *
  87. * This class is used as base for Crypt_RSA, Crypt_RSA_Key
  88. * and Crypt_RSA_KeyPair classes.
  89. *
  90. * It provides following functions:
  91. * - isError() - returns true, if list contains errors, else returns false
  92. * - getErrorList() - returns error list
  93. * - getLastError() - returns last error from error list or false, if list is empty
  94. * - pushError($errstr) - pushes $errstr into the error list
  95. * - setErrorHandler($new_error_handler) - sets error handler function
  96. * - getErrorHandler() - returns name of error handler function
  97. *
  98. * @category Encryption
  99. * @package Crypt_RSA
  100. * @author Alexander Valyalkin <valyala@gmail.com>
  101. * @copyright 2005 Alexander Valyalkin
  102. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  103. * @link http://pear.php.net/package/Crypt_RSA
  104. * @version @package_version@
  105. * @access public
  106. */
  107. class Crypt_RSA_ErrorHandler
  108. {
  109. /**
  110. * array of error objects, pushed by $this->pushError()
  111. *
  112. * @var array
  113. * @access private
  114. */
  115. var $_errors = array();
  116. /**
  117. * name of error handler - function, which calls on $this->pushError() call
  118. *
  119. * @var string
  120. * @access private
  121. */
  122. var $_error_handler = '';
  123. /**
  124. * Returns true if list of errors is not empty, else returns false
  125. *
  126. * @param object
  127. * @return bool true, if list of errors is not empty or $err is PEAR_Error object, else false
  128. * @access public
  129. */
  130. function isError($err = null)
  131. {
  132. return is_null($err) ? (sizeof($this->_errors) > 0) : PEAR::isError($err);
  133. }
  134. /**
  135. * Returns list of all errors, pushed to error list by $this->pushError()
  136. *
  137. * @return array list of errors (usually it contains objects of PEAR_Error class)
  138. * @access public
  139. */
  140. function getErrorList()
  141. {
  142. return $this->_errors;
  143. }
  144. /**
  145. * Returns last error from errors list or false, if list is empty
  146. *
  147. * @return mixed
  148. * last error from errors list (usually it is PEAR_Error object)
  149. * or false, if list is empty.
  150. *
  151. * @access public
  152. */
  153. function getLastError()
  154. {
  155. $len = sizeof($this->_errors);
  156. return $len ? $this->_errors[$len - 1] : false;
  157. }
  158. /**
  159. * pushes error object $error to the error list
  160. *
  161. * @param string $errstr error string
  162. * @param int $errno error number
  163. * @return bool true on success, false on error
  164. * @access public
  165. */
  166. function pushError($errstr, $errno = 0)
  167. {
  168. $this->_errors[] = PEAR::raiseError($errstr, $errno);
  169. if ($this->_error_handler != '') {
  170. // call user defined error handler
  171. $func = $this->_error_handler;
  172. $func($this);
  173. }
  174. return true;
  175. }
  176. /**
  177. * sets error handler to function with name $func_name.
  178. * Function $func_name must accept one parameter - current
  179. * object, which triggered error.
  180. *
  181. * @param string $func_name name of error handler function
  182. * @return bool true on success, false on error
  183. * @access public
  184. */
  185. function setErrorHandler($func_name = '')
  186. {
  187. if ($func_name == '') {
  188. $this->_error_handler = '';
  189. }
  190. if (!function_exists($func_name)) {
  191. return false;
  192. }
  193. $this->_error_handler = $func_name;
  194. return true;
  195. }
  196. /**
  197. * returns name of current error handler, or null if there is no error handler
  198. *
  199. * @return mixed error handler name as string or null, if there is no error handler
  200. * @access public
  201. */
  202. function getErrorHandler()
  203. {
  204. return $this->_error_handler;
  205. }
  206. }
  207. ?>