opengeodb and ldap detaillers work!
[auerswald-callnotifier.git] / src / callnotifier / CallMonitor / Detailler / LDAP.php
1 <?php
2 namespace callnotifier;
3
4 class CallMonitor_Detailler_LDAP implements CallMonitor_Detailler
5 {
6     public function __construct($ldapConfig)
7     {
8         $this->ldap = \Net_LDAP2::connect($ldapConfig);
9         if (\PEAR::isError($this->ldap)) {
10             throw new \Exception(
11                 'Could not connect to LDAP-server: ' . $this->ldap->getMessage()
12             );
13         }
14     }
15
16     public function loadCallDetails(CallMonitor_Call $call)
17     {
18         if ($call->type == CallMonitor_Call::INCOMING) {
19             $call->fromName = $this->loadName($call->from);
20         } else {
21             $call->toName = $this->loadName($call->to);
22         }
23     }
24
25     protected function loadName($number)
26     {
27         $filter = \Net_LDAP2_Filter::combine(
28             'or',
29             array(
30                 \Net_LDAP2_Filter::create('companyPhone', 'equals', $number),
31                 \Net_LDAP2_Filter::create('homePhone', 'equals', $number),
32                 \Net_LDAP2_Filter::create('mobile', 'equals', $number),
33                 \Net_LDAP2_Filter::create('otherPhone', 'equals', $number),
34                 \Net_LDAP2_Filter::create('telephoneNumber', 'equals', $number),
35             )
36         );
37         $options = array(
38             'scope' => 'sub',
39             'attributes' => array('displayName', 'givenName', 'sn', 'cn')
40         );
41
42         $search = $this->ldap->search(null, $filter, $options);
43         if (\PEAR::isError($search)) {
44             throw new \Exception(
45                 'Error searching LDAP: ' . $search->getMessage()
46             );
47         }
48         if ($search->count() == 0) {
49             return null;
50         }
51
52         $arEntry = $search->shiftEntry()->getValues();
53         if (isset($arEntry['displayName'])) {
54             return $arEntry['displayName'];
55         } else if (isset($arEntry['sn']) && $arEntry['givenName']) {
56             return $arEntry['givenName'] . ' ' . $arEntry['sn'];
57         } else if (isset($arEntry['cn'])) {
58             return $arEntry['cn'];
59         }
60         return null;
61     }
62
63 }
64
65 ?>