describe what the ldap detailler does
[auerswald-callnotifier.git] / src / callnotifier / CallMonitor / Detailler / LDAP.php
1 <?php
2 namespace callnotifier;
3
4 /**
5  * Fetch caller names from a LDAP address book.
6  *
7  * The following attributes are searched:
8  * - companyPhone
9  * - homePhone
10  * - mobile
11  * - otherPhone
12  * - telephoneNumber
13  *
14  * For the first result, the displayName is used if defined.
15  * If it does not exist, the givenName + sn are used.
16  *
17  * Set "toName" or "fromName", depending on call type.
18  *
19  * Uses the Net_LDAP2 PEAR package
20  */
21 class CallMonitor_Detailler_LDAP implements CallMonitor_Detailler
22 {
23     /**
24      * Create new ldap name resolver
25      *
26      * @param array $ldapConfig Array of Net_LDAP2 configuration parameters.
27      *                          Some of those you might want to use:
28      *                          - host   - LDAP server host name
29      *                          - basedn - root DN that gets searched
30      *                          - binddn - Username to authenticate with
31      *                          - bindpw - Password for username
32      */
33     public function __construct($ldapConfig)
34     {
35         $this->ldap = \Net_LDAP2::connect($ldapConfig);
36         if (\PEAR::isError($this->ldap)) {
37             throw new \Exception(
38                 'Could not connect to LDAP-server: ' . $this->ldap->getMessage()
39             );
40         }
41     }
42
43     public function loadCallDetails(CallMonitor_Call $call)
44     {
45         if ($call->type == CallMonitor_Call::INCOMING) {
46             $call->fromName = $this->loadName($call->from);
47         } else {
48             $call->toName = $this->loadName($call->to);
49         }
50     }
51
52     protected function loadName($number)
53     {
54         $filter = \Net_LDAP2_Filter::combine(
55             'or',
56             array(
57                 \Net_LDAP2_Filter::create('companyPhone', 'equals', $number),
58                 \Net_LDAP2_Filter::create('homePhone', 'equals', $number),
59                 \Net_LDAP2_Filter::create('mobile', 'equals', $number),
60                 \Net_LDAP2_Filter::create('otherPhone', 'equals', $number),
61                 \Net_LDAP2_Filter::create('telephoneNumber', 'equals', $number),
62             )
63         );
64         $options = array(
65             'scope' => 'sub',
66             'attributes' => array('displayName', 'givenName', 'sn', 'cn')
67         );
68
69         $search = $this->ldap->search(null, $filter, $options);
70         if (\PEAR::isError($search)) {
71             throw new \Exception(
72                 'Error searching LDAP: ' . $search->getMessage()
73             );
74         }
75         if ($search->count() == 0) {
76             return null;
77         }
78
79         $arEntry = $search->shiftEntry()->getValues();
80         if (isset($arEntry['displayName'])) {
81             return $arEntry['displayName'];
82         } else if (isset($arEntry['sn']) && $arEntry['givenName']) {
83             return $arEntry['givenName'] . ' ' . $arEntry['sn'];
84         } else if (isset($arEntry['cn'])) {
85             return $arEntry['cn'];
86         }
87         return null;
88     }
89
90 }
91
92 ?>