9 * @author Christian Weiske <cweiske@cweiske.de>
10 * @copyright 2014 Christian Weiske
11 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12 * @link http://cweiske.de/bdrem.htm
17 * Fetch data from an LDAP server.
18 * Works fine with evolutionPerson schema.
22 * @author Christian Weiske <cweiske@cweiske.de>
23 * @copyright 2014 Christian Weiske
24 * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3
25 * @link http://cweiske.de/bdrem.htm
30 * LDAP server configuration
33 * - host - LDAP server host name
34 * - basedn - root DN that gets searched
35 * - binddn - Username to authenticate with
36 * - bindpw - Password for username
43 * Create new ldap source
45 * @param array $config Array of Net_LDAP2 configuration parameters.
46 * Some of those you might want to use:
47 * - host - LDAP server host name
48 * - basedn - root DN that gets searched
49 * - binddn - Username to authenticate with
50 * - bindpw - Password for username
52 public function __construct($config)
54 $this->config = $config;
58 * Return all events for the given date range
60 * @param string $strDate Date the events shall be found for,
62 * @param integer $nDaysPrevious Include number of days before $strDate
63 * @param integer $nDaysNext Include number of days after $strDate
65 * @return Event[] Array of matching event objects
67 public function getEvents($strDate, $nDaysPrevious, $nDaysNext)
69 //Net_LDAP2 is not E_STRICT compatible
70 error_reporting(error_reporting() & ~E_STRICT);
72 $ldap = \Net_LDAP2::connect($this->config);
73 if (\PEAR::isError($ldap)) {
75 'Could not connect to LDAP-server: ' . $ldap->getMessage()
79 $dateAttributes = array(
80 'birthDate' => 'Birthday',
81 'anniversary' => 'Anniversary',
84 $arDays = $this->getDates($strDate, $nDaysPrevious, $nDaysNext);
87 foreach ($dateAttributes as $dateAttribute => $attributeTitle) {
89 foreach ($arDays as $day) {
90 $filters[] = \Net_LDAP2_Filter::create($dateAttribute, 'ends', $day);
93 if (count($filters) < 2) {
94 $filter = $filters[0];
96 $filter = \Net_LDAP2_Filter::combine('or', $filters);
100 'attributes' => array(
101 'displayName', 'givenName', 'sn', 'cn', $dateAttribute
105 $search = $ldap->search(null, $filter, $options);
106 if (!$search instanceof \Net_LDAP2_Search) {
107 throw new \Exception(
108 'Error searching LDAP: ' . $search->getMessage()
110 } else if ($search->count() == 0) {
114 while ($entry = $search->shiftEntry()) {
116 $this->getNameFromEntry($entry),
118 $entry->getValue($dateAttribute, 'single')
120 if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
121 $arEvents[] = $event;
130 * Extract the name from the given LDAP entry object.
131 * Uses displayName or givenName + sn
133 * @param object $entry LDAP entry
135 * @return string Name or NULL
137 protected function getNameFromEntry(\Net_LDAP2_Entry $entry)
139 $arEntry = $entry->getValues();
140 if (isset($arEntry['displayName'])) {
141 return $arEntry['displayName'];
142 } else if (isset($arEntry['sn']) && isset($arEntry['givenName'])) {
143 return $arEntry['givenName'] . ' ' . $arEntry['sn'];
144 } else if (isset($arEntry['cn'])) {
145 return $arEntry['cn'];
151 * Create an array of dates that are included in the given range.
153 * @param string $strDate Date the events shall be found for,
155 * @param integer $nDaysPrevious Include number of days before $strDate
156 * @param integer $nDaysNext Include number of days after $strDate
158 * @return array Values like "-01-24" ("-$month-$day")
160 protected function getDates($strDate, $nDaysPrevious, $nDaysNext)
162 $ts = strtotime($strDate) - 86400 * $nDaysPrevious;
163 $numDays = $nDaysPrevious + $nDaysNext;
167 $arDays[] = date('-m-d', $ts);
169 } while (--$numDays >= 0);