show error, not exception
[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             if (count($filters) < 2) {
57                 $filter = $filters[0];
58             } else {
59                 $filter = \Net_LDAP2_Filter::combine('or', $filters);
60             }
61             $options = array(
62                 'scope'      => 'sub',
63                 'attributes' => array(
64                     'displayName', 'givenName', 'sn', 'cn', $dateAttribute
65                 )
66             );
67
68             $search = $ldap->search(null, $filter, $options);
69             if (!$search instanceof \Net_LDAP2_Search) {
70                 throw new \Exception(
71                     'Error searching LDAP: ' . $search->getMessage()
72                 );
73             } else if ($search->count() == 0) {
74                 continue;
75             }
76
77             while ($entry = $search->shiftEntry()) {
78                 $event = new Event(
79                     $this->getNameFromEntry($entry),
80                     $attributeTitle,
81                     $entry->getValue($dateAttribute, 'single')
82                 );
83                 if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
84                     $arEvents[] = $event;
85                 }
86             }
87         }
88
89         return $arEvents;
90     }
91
92     protected function getNameFromEntry(\Net_LDAP2_Entry $entry)
93     {
94         $arEntry = $entry->getValues();
95         if (isset($arEntry['displayName'])) {
96             return $arEntry['displayName'];
97         } else if (isset($arEntry['sn']) && isset($arEntry['givenName'])) {
98             return $arEntry['givenName'] . ' ' . $arEntry['sn'];
99         } else if (isset($arEntry['cn'])) {
100             return $arEntry['cn'];
101         }
102         return null;
103     }
104
105     /**
106      * @return array Values like "-01-24" ("-$month-$day")
107      */
108     protected function getDates($strDate, $nDaysPrevious, $nDaysNext)
109     {
110         $ts = strtotime($strDate) - 86400 * $nDaysPrevious;
111         $numDays = $nDaysPrevious + $nDaysNext;
112
113         $arDays = array();
114         do {
115             $arDays[] = date('-m-d', $ts);
116             $ts += 86400;
117         } while (--$numDays >= 0);
118         return $arDays;
119     }
120
121 }
122 ?>