load call details on finished calls, too
[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             if (!isset($call->fromName) || $call->fromName === null) {
47                 $call->fromName = $this->loadName($call->from);
48             }
49         } else {
50             if (!isset($call->toName) || $call->toName === null) {
51                 $call->toName = $this->loadName($call->to);
52             }
53         }
54     }
55
56     protected function loadName($number)
57     {
58         $filter = \Net_LDAP2_Filter::combine(
59             'or',
60             array(
61                 \Net_LDAP2_Filter::create('companyPhone', 'equals', $number),
62                 \Net_LDAP2_Filter::create('homePhone', 'equals', $number),
63                 \Net_LDAP2_Filter::create('mobile', 'equals', $number),
64                 \Net_LDAP2_Filter::create('otherPhone', 'equals', $number),
65                 \Net_LDAP2_Filter::create('telephoneNumber', 'equals', $number),
66             )
67         );
68         $options = array(
69             'scope' => 'sub',
70             'attributes' => array('displayName', 'givenName', 'sn', 'cn')
71         );
72
73         $search = $this->ldap->search(null, $filter, $options);
74         if (\PEAR::isError($search)) {
75             throw new \Exception(
76                 'Error searching LDAP: ' . $search->getMessage()
77             );
78         }
79         if ($search->count() == 0) {
80             return null;
81         }
82
83         $arEntry = $search->shiftEntry()->getValues();
84         if (isset($arEntry['displayName'])) {
85             return $arEntry['displayName'];
86         } else if (isset($arEntry['sn']) && $arEntry['givenName']) {
87             return $arEntry['givenName'] . ' ' . $arEntry['sn'];
88         } else if (isset($arEntry['cn'])) {
89             return $arEntry['cn'];
90         }
91         return null;
92     }
93
94 }
95
96 ?>