PageRenderTime 61ms CodeModel.GetById 34ms RepoModel.GetById 1ms app.codeStats 0ms

/tine20/Tinebase/Ldap.php

https://github.com/testruby/Tine-2.0-Open-Source-Groupware-and-CRM
PHP | 188 lines | 106 code | 17 blank | 65 comment | 15 complexity | 37656b346db134e41b2d446d007aec3c MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0
  4. *
  5. * @package Tinebase
  6. * @subpackage Ldap
  7. * @license http://www.gnu.org/licenses/agpl.html AGPL3
  8. * @copyright Copyright (c) 2008 Metaways Infosystems GmbH (http://www.metaways.de)
  9. * @author Lars Kneschke <l.kneschke@metaways.de>
  10. */
  11. /**
  12. * LDAP base class for tine 2.0
  13. * @package Tinebase
  14. * @subpackage Ldap
  15. */
  16. class Tinebase_Ldap extends Zend_Ldap
  17. {
  18. /**
  19. * Extend constructor
  20. *
  21. * @param array $_options
  22. * @return @see Zend_Ldap
  23. */
  24. public function __construct(array $_options)
  25. {
  26. // strip non Zend_Ldap options
  27. $options = array_intersect_key($_options, array(
  28. 'host' => null,
  29. 'port' => null,
  30. 'useSsl' => null,
  31. 'username' => null,
  32. 'password' => null,
  33. 'bindRequiresDn' => null,
  34. 'baseDn' => null,
  35. 'accountCanonicalForm' => null,
  36. 'accountDomainName' => null,
  37. 'accountDomainNameShort' => null,
  38. 'accountFilterFormat' => null,
  39. 'allowEmptyPassword' => null,
  40. 'useStartTls' => null,
  41. 'optReferrals' => null,
  42. 'tryUsernameSplit' => null
  43. ));
  44. $returnValue = parent::__construct($options);
  45. return $returnValue;
  46. }
  47. /**
  48. * Delete an LDAP entry
  49. *
  50. * @param string|Zend_Ldap_Dn $dn
  51. * @param array $data
  52. * @return Zend_Ldap *Provides a fluid interface*
  53. * @throws Zend_Ldap_Exception
  54. */
  55. public function deleteProperty($dn, array $data)
  56. {
  57. if ($dn instanceof Zend_Ldap_Dn) {
  58. $dn = $dn->toString();
  59. }
  60. $isDeleted = @ldap_mod_del($this->getResource(), $dn, $data);
  61. if($isDeleted === false) {
  62. /**
  63. * @see Zend_Ldap_Exception
  64. */
  65. require_once 'Zend/Ldap/Exception.php';
  66. throw new Zend_Ldap_Exception($this, 'deleting: ' . $dn);
  67. }
  68. return $this;
  69. }
  70. /**
  71. * read binary attribute from one entry from the ldap directory
  72. *
  73. * @todo still needed???
  74. *
  75. * @param string $_dn the dn to read
  76. * @param string $_filter search filter
  77. * @param array $_attribute which field to return
  78. * @return blob binary data of given field
  79. * @throws Exception with ldap error
  80. */
  81. public function fetchBinaryAttribute($_dn, $_filter, $_attribute)
  82. {
  83. $searchResult = @ldap_search($this->getResource(), $_dn, $_filter, $_attributes, $this->_attrsOnly, $this->_sizeLimit, $this->_timeLimit);
  84. if($searchResult === FALSE) {
  85. throw new Exception(ldap_error($this->getResource()));
  86. }
  87. $searchCount = ldap_count_entries($this->getResource(), $searchResult);
  88. if($searchCount === 0) {
  89. throw new Exception('Nothing found for filter: ' . $_filter);
  90. } elseif ($searchCount > 1) {
  91. throw new Exception('More than one entry found for filter: ' . $_filter);
  92. }
  93. $entry = ldap_first_entry($this->getResource(), $searchResult);
  94. return ldap_get_values_len($this->getResource(), $entry, $attribute);
  95. }
  96. /**
  97. * Add new information to the LDAP repository
  98. *
  99. * @param string|Zend_Ldap_Dn $dn
  100. * @param array $entry
  101. * @return Zend_Ldap *Provides a fluid interface*
  102. * @throws Zend_Ldap_Exception
  103. */
  104. public function addProperty($dn, array $entry)
  105. {
  106. if (!($dn instanceof Zend_Ldap_Dn)) {
  107. $dn = Zend_Ldap_Dn::factory($dn, null);
  108. }
  109. self::prepareLdapEntryArray($entry);
  110. foreach ($entry as $key => $value) {
  111. if (is_array($value) && count($value) === 0) {
  112. unset($entry[$key]);
  113. }
  114. }
  115. $rdnParts = $dn->getRdn(Zend_Ldap_Dn::ATTR_CASEFOLD_LOWER);
  116. $adAttributes = array('distinguishedname', 'instancetype', 'name', 'objectcategory',
  117. 'objectguid', 'usnchanged', 'usncreated', 'whenchanged', 'whencreated');
  118. $stripAttributes = array_merge(array_keys($rdnParts), $adAttributes);
  119. foreach ($stripAttributes as $attr) {
  120. if (array_key_exists($attr, $entry)) {
  121. unset($entry[$attr]);
  122. }
  123. }
  124. if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' $dn: ' . $dn->toString());
  125. if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' $data: ' . print_r($entry, true));
  126. $isAdded = @ldap_mod_add($this->getResource(), $dn->toString(), $entry);
  127. if($isAdded === false) {
  128. /**
  129. * @see Zend_Ldap_Exception
  130. */
  131. require_once 'Zend/Ldap/Exception.php';
  132. throw new Zend_Ldap_Exception($this, 'adding: ' . $dn->toString());
  133. }
  134. return $this;
  135. }
  136. /**
  137. * Update LDAP registry
  138. *
  139. * @param string|Zend_Ldap_Dn $dn
  140. * @param array $entry
  141. * @return Zend_Ldap *Provides a fluid interface*
  142. * @throws Zend_Ldap_Exception
  143. */
  144. public function updateProperty($dn, array $entry)
  145. {
  146. if (!($dn instanceof Zend_Ldap_Dn)) {
  147. $dn = Zend_Ldap_Dn::factory($dn, null);
  148. }
  149. self::prepareLdapEntryArray($entry);
  150. $rdnParts = $dn->getRdn(Zend_Ldap_Dn::ATTR_CASEFOLD_LOWER);
  151. $adAttributes = array('distinguishedname', 'instancetype', 'name', 'objectcategory',
  152. 'objectguid', 'usnchanged', 'usncreated', 'whenchanged', 'whencreated');
  153. $stripAttributes = array_merge(array_keys($rdnParts), $adAttributes);
  154. foreach ($stripAttributes as $attr) {
  155. if (array_key_exists($attr, $entry)) {
  156. unset($entry[$attr]);
  157. }
  158. }
  159. if (count($entry) > 0) {
  160. $isModified = @ldap_mod_replace($this->getResource(), $dn->toString(), $entry);
  161. if($isModified === false) {
  162. /**
  163. * @see Zend_Ldap_Exception
  164. */
  165. require_once 'Zend/Ldap/Exception.php';
  166. throw new Zend_Ldap_Exception($this, 'updating: ' . $dn->toString());
  167. }
  168. }
  169. return $this;
  170. }
  171. }