Exception.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Ldap
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Exception
  23. */
  24. require_once 'Zend/Exception.php';
  25. class Zend_Ldap_Exception extends Zend_Exception
  26. {
  27. const LDAP_SUCCESS = 0x00;
  28. const LDAP_OPERATIONS_ERROR = 0x01;
  29. const LDAP_PROTOCOL_ERROR = 0x02;
  30. const LDAP_TIMELIMIT_EXCEEDED = 0x03;
  31. const LDAP_SIZELIMIT_EXCEEDED = 0x04;
  32. const LDAP_COMPARE_FALSE = 0x05;
  33. const LDAP_COMPARE_TRUE = 0x06;
  34. const LDAP_AUTH_METHOD_NOT_SUPPORTED = 0x07;
  35. const LDAP_STRONG_AUTH_REQUIRED = 0x08;
  36. const LDAP_PARTIAL_RESULTS = 0x09;
  37. const LDAP_REFERRAL = 0x0a;
  38. const LDAP_ADMINLIMIT_EXCEEDED = 0x0b;
  39. const LDAP_UNAVAILABLE_CRITICAL_EXTENSION = 0x0c;
  40. const LDAP_CONFIDENTIALITY_REQUIRED = 0x0d;
  41. const LDAP_SASL_BIND_IN_PROGRESS = 0x0e;
  42. const LDAP_NO_SUCH_ATTRIBUTE = 0x10;
  43. const LDAP_UNDEFINED_TYPE = 0x11;
  44. const LDAP_INAPPROPRIATE_MATCHING = 0x12;
  45. const LDAP_CONSTRAINT_VIOLATION = 0x13;
  46. const LDAP_TYPE_OR_VALUE_EXISTS = 0x14;
  47. const LDAP_INVALID_SYNTAX = 0x15;
  48. const LDAP_NO_SUCH_OBJECT = 0x20;
  49. const LDAP_ALIAS_PROBLEM = 0x21;
  50. const LDAP_INVALID_DN_SYNTAX = 0x22;
  51. const LDAP_IS_LEAF = 0x23;
  52. const LDAP_ALIAS_DEREF_PROBLEM = 0x24;
  53. const LDAP_PROXY_AUTHZ_FAILURE = 0x2F;
  54. const LDAP_INAPPROPRIATE_AUTH = 0x30;
  55. const LDAP_INVALID_CREDENTIALS = 0x31;
  56. const LDAP_INSUFFICIENT_ACCESS = 0x32;
  57. const LDAP_BUSY = 0x33;
  58. const LDAP_UNAVAILABLE = 0x34;
  59. const LDAP_UNWILLING_TO_PERFORM = 0x35;
  60. const LDAP_LOOP_DETECT = 0x36;
  61. const LDAP_NAMING_VIOLATION = 0x40;
  62. const LDAP_OBJECT_CLASS_VIOLATION = 0x41;
  63. const LDAP_NOT_ALLOWED_ON_NONLEAF = 0x42;
  64. const LDAP_NOT_ALLOWED_ON_RDN = 0x43;
  65. const LDAP_ALREADY_EXISTS = 0x44;
  66. const LDAP_NO_OBJECT_CLASS_MODS = 0x45;
  67. const LDAP_RESULTS_TOO_LARGE = 0x46;
  68. const LDAP_AFFECTS_MULTIPLE_DSAS = 0x47;
  69. const LDAP_OTHER = 0x50;
  70. const LDAP_SERVER_DOWN = 0x51;
  71. const LDAP_LOCAL_ERROR = 0x52;
  72. const LDAP_ENCODING_ERROR = 0x53;
  73. const LDAP_DECODING_ERROR = 0x54;
  74. const LDAP_TIMEOUT = 0x55;
  75. const LDAP_AUTH_UNKNOWN = 0x56;
  76. const LDAP_FILTER_ERROR = 0x57;
  77. const LDAP_USER_CANCELLED = 0x58;
  78. const LDAP_PARAM_ERROR = 0x59;
  79. const LDAP_NO_MEMORY = 0x5a;
  80. const LDAP_CONNECT_ERROR = 0x5b;
  81. const LDAP_NOT_SUPPORTED = 0x5c;
  82. const LDAP_CONTROL_NOT_FOUND = 0x5d;
  83. const LDAP_NO_RESULTS_RETURNED = 0x5e;
  84. const LDAP_MORE_RESULTS_TO_RETURN = 0x5f;
  85. const LDAP_CLIENT_LOOP = 0x60;
  86. const LDAP_REFERRAL_LIMIT_EXCEEDED = 0x61;
  87. const LDAP_CUP_RESOURCES_EXHAUSTED = 0x71;
  88. const LDAP_CUP_SECURITY_VIOLATION = 0x72;
  89. const LDAP_CUP_INVALID_DATA = 0x73;
  90. const LDAP_CUP_UNSUPPORTED_SCHEME = 0x74;
  91. const LDAP_CUP_RELOAD_REQUIRED = 0x75;
  92. const LDAP_CANCELLED = 0x76;
  93. const LDAP_NO_SUCH_OPERATION = 0x77;
  94. const LDAP_TOO_LATE = 0x78;
  95. const LDAP_CANNOT_CANCEL = 0x79;
  96. const LDAP_ASSERTION_FAILED = 0x7A;
  97. const LDAP_SYNC_REFRESH_REQUIRED = 0x1000;
  98. const LDAP_X_SYNC_REFRESH_REQUIRED = 0x4100;
  99. const LDAP_X_NO_OPERATION = 0x410e;
  100. const LDAP_X_ASSERTION_FAILED = 0x410f;
  101. const LDAP_X_NO_REFERRALS_FOUND = 0x4110;
  102. const LDAP_X_CANNOT_CHAIN = 0x4111;
  103. /* internal error code constants */
  104. const LDAP_X_DOMAIN_MISMATCH = 0x7001;
  105. /**
  106. * @param mixed $ldap A Zend_Ldap object or raw LDAP context resource
  107. * @param string $str An informtive exception message
  108. * @param int $code An LDAP error code
  109. */
  110. public function __construct($ldap = null, $str = null, $code = 0)
  111. {
  112. $resource = null;
  113. if (is_resource($ldap)) {
  114. $resource = $ldap;
  115. } else if (is_object($ldap)) {
  116. $resource = $ldap->getResource();
  117. }
  118. $message = '';
  119. if ($code === 0)
  120. $code = Zend_Ldap_Exception::getLdapCode($resource);
  121. if ($code)
  122. $message .= '0x' . dechex($code);
  123. if (is_resource($resource)) {
  124. /* The various error retrieval functions can return
  125. * different things so we just try to collect what we
  126. * can and eliminate dupes.
  127. */
  128. $estr1 = @ldap_error($resource);
  129. if ($code !== 0 && $estr1 === 'Success')
  130. $estr1 = @ldap_err2str($code);
  131. if ($estr1 !== $str)
  132. $this->_append($message, $estr1);
  133. @ldap_get_option($resource, LDAP_OPT_ERROR_STRING, $estr2);
  134. if ($estr2 !== $str && $estr2 !== $estr1)
  135. $this->_append($message, $estr2);
  136. }
  137. $this->_append($message, $str);
  138. parent::__construct($message, $code);
  139. }
  140. private function _append(&$message, $estr)
  141. {
  142. if ($estr) {
  143. if ($message)
  144. $message .= ': ';
  145. $message .= $estr;
  146. }
  147. }
  148. /**
  149. * @param mixed $ldap A Zend_Ldap object or raw LDAP context resource
  150. * @return int The current error code for the resource
  151. */
  152. public static function getLdapCode($ldap)
  153. {
  154. $resource = null;
  155. if (is_resource($ldap)) {
  156. $resource = $ldap;
  157. } else if (is_object($ldap)) {
  158. $resource = $ldap->getResource();
  159. }
  160. if (is_resource($resource)) {
  161. $ret = @ldap_get_option($resource, LDAP_OPT_ERROR_NUMBER, $err);
  162. if ($ret === true) {
  163. if ($err <= -1 && $err >= -17) {
  164. /* For some reason draft-ietf-ldapext-ldap-c-api-xx.txt error
  165. * codes in OpenLDAP are negative values from -1 to -17.
  166. */
  167. $err = Zend_Ldap_Exception::LDAP_SERVER_DOWN + (-$err - 1);
  168. }
  169. return $err;
  170. }
  171. }
  172. return 0;
  173. }
  174. /**
  175. * @param string $message An informtive exception message
  176. *
  177. */
  178. public function setMessage($message)
  179. {
  180. $this->_message = $message;
  181. }
  182. /**
  183. * @return int The current error code for this exception
  184. */
  185. public function getErrorCode()
  186. {
  187. return $this->getCode();
  188. }
  189. }