| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | if (!defined('DEBUG_MODE')) { die(); } |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | class Hm_LDAP_Contacts extends Hm_Auth_LDAP { |
| 15: | |
| 16: | public function __construct($config) { |
| 17: | if (is_array($config)) { |
| 18: | $this->config = $config; |
| 19: | } |
| 20: | if (is_array($config) && array_key_exists('name', $config)) { |
| 21: | $this->source = $config['name']; |
| 22: | } |
| 23: | } |
| 24: | |
| 25: | public function rename($dn, $new_dn, $parent) { |
| 26: | return @ldap_rename($this->fh, $dn, $new_dn, $parent, true); |
| 27: | } |
| 28: | |
| 29: | public function modify($entry, $dn) { |
| 30: | return @ldap_modify($this->fh, $dn, $entry); |
| 31: | } |
| 32: | |
| 33: | public function add($entry, $dn) { |
| 34: | return @ldap_add($this->fh, $dn, $entry); |
| 35: | } |
| 36: | |
| 37: | public function delete($dn) { |
| 38: | return @ldap_delete($this->fh, $dn); |
| 39: | } |
| 40: | |
| 41: | protected function auth() { |
| 42: | if (array_key_exists('auth', $this->config) && $this->config['auth'] && |
| 43: | array_key_exists('user', $this->config) && $this->config['user'] && |
| 44: | array_key_exists('pass', $this->config) && $this->config['pass']) { |
| 45: | $uid_attr = $this->config['ldap_uid_attr']; |
| 46: | $user_dn = sprintf('%s=%s,%s', $uid_attr, $this->config['user'], $this->config['base_dn']); |
| 47: | return @ldap_bind($this->fh, $user_dn, $this->config['pass']); |
| 48: | } |
| 49: | else { |
| 50: | return @ldap_bind($this->fh); |
| 51: | } |
| 52: | return false; |
| 53: | } |
| 54: | |
| 55: | private function parse($data) { |
| 56: | $result = array(); |
| 57: | $flds = array( |
| 58: | 'mail' => 'email_address', |
| 59: | 'cn' => 'display_name', |
| 60: | 'telephonenumber' => 'phone_number' |
| 61: | ); |
| 62: | foreach ($data as $contact) { |
| 63: | $res = array(); |
| 64: | if (!is_array($contact)) { |
| 65: | continue; |
| 66: | } |
| 67: | $all = array(); |
| 68: | foreach ($contact as $name => $fld) { |
| 69: | if (in_array($name, array_keys($flds), true)) { |
| 70: | $res[$flds[$name]] = $fld[0]; |
| 71: | } |
| 72: | elseif (!is_int($name) && $name != 'count' && $name != 'dn') { |
| 73: | $all[$name] = $fld[0]; |
| 74: | } |
| 75: | elseif ($name == 'dn') { |
| 76: | $all[$name] = $fld; |
| 77: | } |
| 78: | } |
| 79: | if (array_key_exists('email_address', $res) && $res['email_address'] && |
| 80: | array_key_exists('display_name', $res) && $res['display_name']) { |
| 81: | $res['source'] = $this->source; |
| 82: | $res['type'] = 'ldap'; |
| 83: | $res['all_fields'] = $all; |
| 84: | $result[] = $res; |
| 85: | } |
| 86: | } |
| 87: | return $result; |
| 88: | } |
| 89: | |
| 90: | public function fetch() { |
| 91: | $base_dn = 'dc=example,dc=com'; |
| 92: | $search_term='objectclass=inetOrgPerson'; |
| 93: | if (array_key_exists('search_term', $this->config)) { |
| 94: | $search_term = $this->config['search_term']; |
| 95: | } |
| 96: | if (array_key_exists('base_dn', $this->config)) { |
| 97: | $base_dn = $this->config['base_dn']; |
| 98: | $res = @ldap_search($this->fh, $base_dn, $search_term, array(), 0, 0); |
| 99: | if ($res) { |
| 100: | $contacts = ldap_get_entries($this->fh, $res); |
| 101: | return $this->parse($contacts); |
| 102: | } |
| 103: | } |
| 104: | return array(); |
| 105: | } |
| 106: | } |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | class Hm_LDAP_Contact extends Hm_Contact { |
| 112: | |
| 113: | public function getDN() { |
| 114: | $all_fields = $this->value('all_fields'); |
| 115: | if ($all_fields && isset($all_fields['dn'])) { |
| 116: | return $all_fields['dn']; |
| 117: | } |
| 118: | return null; |
| 119: | } |
| 120: | |
| 121: | public static function findByDN($contact_store, $target_dn, $contact_source) { |
| 122: | $all_contacts = $contact_store->dump(); |
| 123: | |
| 124: | foreach ($all_contacts as $contact_id => $contact_obj) { |
| 125: | if ($contact_obj->value('source') == $contact_source && |
| 126: | $contact_obj->value('type') == 'ldap') { |
| 127: | |
| 128: | $all_fields = $contact_obj->value('all_fields'); |
| 129: | |
| 130: | if (isset($all_fields['dn']) && $all_fields['dn'] === $target_dn) { |
| 131: | return $contact_obj; |
| 132: | } |
| 133: | } |
| 134: | } |
| 135: | return null; |
| 136: | } |
| 137: | |
| 138: | public static function isLdapContact($contact) { |
| 139: | return $contact instanceof self || $contact->value('type') === 'ldap'; |
| 140: | } |
| 141: | |
| 142: | public static function decodeDN($encoded_dn) { |
| 143: | return urldecode($encoded_dn); |
| 144: | } |
| 145: | |
| 146: | public static function generateDeleteAttributes($contact, $html_safe) { |
| 147: | if (!self::isLdapContact($contact)) { |
| 148: | return ''; |
| 149: | } |
| 150: | |
| 151: | $all_fields = $contact->value('all_fields'); |
| 152: | if ($all_fields && isset($all_fields['dn'])) { |
| 153: | return ' data-ldap-dn="'.$html_safe($all_fields['dn']).'"'; |
| 154: | } |
| 155: | |
| 156: | return ''; |
| 157: | } |
| 158: | |
| 159: | public static function addDNToUrl($contact, $base_url) { |
| 160: | if (!self::isLdapContact($contact)) { |
| 161: | return $base_url; |
| 162: | } |
| 163: | |
| 164: | $all_fields = $contact->value('all_fields'); |
| 165: | if ($all_fields && isset($all_fields['dn'])) { |
| 166: | return $base_url . '&dn='.urlencode($all_fields['dn']); |
| 167: | } |
| 168: | |
| 169: | return $base_url; |
| 170: | } |
| 171: | |
| 172: | public static function fromContact($contact) { |
| 173: | if ($contact->value('type') !== 'ldap') { |
| 174: | return null; |
| 175: | } |
| 176: | |
| 177: | if ($contact instanceof self) { |
| 178: | return $contact; |
| 179: | } |
| 180: | |
| 181: | $contact_data = $contact->export(); |
| 182: | $ldap_contact = new self($contact_data); |
| 183: | |
| 184: | return $ldap_contact; |
| 185: | } |
| 186: | } |
| 187: | |