LDAP source driver
[bdrem.git] / src / bdrem / Source / Ldap.php
1 <?php
2 namespace bdrem;
3
4 /**
5  * Fetch data from an LDAP server.
6  * Works fine with evolutionPerson schema.
7  */
8 class Source_Ldap
9 {
10     protected $config;
11
12     /**
13      * Create new ldap source
14      *
15      * @param array $config Array of Net_LDAP2 configuration parameters.
16      *                      Some of those you might want to use:
17      *                      - host   - LDAP server host name
18      *                      - basedn - root DN that gets searched
19      *                      - binddn - Username to authenticate with
20      *                      - bindpw - Password for username
21      */
22     public function __construct($config)
23     {
24         $this->config = $config;
25     }
26
27     /**
28      * @param string $strDate Date the events shall be found for, YYYY-MM-DD
29      */
30     public function getEvents($strDate, $nDaysPrevious, $nDaysNext)
31     {
32         //Net_LDAP2 is not E_STRICT compatible
33         error_reporting(error_reporting() & ~E_STRICT);
34
35         $ldap = \Net_LDAP2::connect($this->config);
36         if (\PEAR::isError($ldap)) {
37             throw new \Exception(
38                 'Could not connect to LDAP-server: ' . $ldap->getMessage()
39             );
40         }
41
42         $dateAttributes = array(
43             'birthDate'   => 'Birthday',
44             'anniversary' => 'Anniversary',
45         );
46
47         $arDays   = $this->getDates($strDate, $nDaysPrevious, $nDaysNext);
48         $arEvents = array();
49
50         foreach ($dateAttributes as $dateAttribute => $attributeTitle) {
51             $filters = array();
52             foreach ($arDays as $day) {
53                 $filters[] = \Net_LDAP2_Filter::create($dateAttribute, 'ends', $day);
54             }
55
56             $filter  = \Net_LDAP2_Filter::combine('or', $filters);
57             $options = array(
58                 'scope'      => 'sub',
59                 'attributes' => array(
60                     'displayName', 'givenName', 'sn', 'cn', $dateAttribute
61                 )
62             );
63
64             $search = $ldap->search(null, $filter, $options);
65             if (!$search instanceof \Net_LDAP2_Search) {
66                 throw new \Exception(
67                     'Error searching LDAP: ' . $search->getMessage()
68                 );
69             } else if ($search->count() == 0) {
70                 continue;
71             }
72
73             while ($entry = $search->shiftEntry()) {
74                 $event = new Event(
75                     $this->getNameFromEntry($entry),
76                     $attributeTitle,
77                     $entry->getValue($dateAttribute, 'single')
78                 );
79                 if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
80                     $arEvents[] = $event;
81                 }
82             }
83         }
84
85         return $arEvents;
86     }
87
88     protected function getNameFromEntry(\Net_LDAP2_Entry $entry)
89     {
90         $arEntry = $entry->getValues();
91         if (isset($arEntry['displayName'])) {
92             return $arEntry['displayName'];
93         } else if (isset($arEntry['sn']) && isset($arEntry['givenName'])) {
94             return $arEntry['givenName'] . ' ' . $arEntry['sn'];
95         } else if (isset($arEntry['cn'])) {
96             return $arEntry['cn'];
97         }
98         return null;
99     }
100
101     /**
102      * @return array Values like "-01-24" ("-$month-$day")
103      */
104     protected function getDates($strDate, $nDaysPrevious, $nDaysNext)
105     {
106         $ts = strtotime($strDate) - 86400 * $nDaysPrevious;
107         $numDays = $nDaysPrevious + $nDaysNext;
108
109         $arDays = array();
110         do {
111             $arDays[] = date('-m-d', $ts);
112             $ts += 86400;
113         } while (--$numDays >= 0);
114         return $arDays;
115     }
116
117 }
118 ?>