5 * Fetch data from an LDAP server.
6 * Works fine with evolutionPerson schema.
13 * Create new ldap source
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
22 public function __construct($config)
24 $this->config = $config;
28 * @param string $strDate Date the events shall be found for, YYYY-MM-DD
30 public function getEvents($strDate, $nDaysPrevious, $nDaysNext)
32 //Net_LDAP2 is not E_STRICT compatible
33 error_reporting(error_reporting() & ~E_STRICT);
35 $ldap = \Net_LDAP2::connect($this->config);
36 if (\PEAR::isError($ldap)) {
38 'Could not connect to LDAP-server: ' . $ldap->getMessage()
42 $dateAttributes = array(
43 'birthDate' => 'Birthday',
44 'anniversary' => 'Anniversary',
47 $arDays = $this->getDates($strDate, $nDaysPrevious, $nDaysNext);
50 foreach ($dateAttributes as $dateAttribute => $attributeTitle) {
52 foreach ($arDays as $day) {
53 $filters[] = \Net_LDAP2_Filter::create($dateAttribute, 'ends', $day);
56 if (count($filters) < 2) {
57 $filter = $filters[0];
59 $filter = \Net_LDAP2_Filter::combine('or', $filters);
63 'attributes' => array(
64 'displayName', 'givenName', 'sn', 'cn', $dateAttribute
68 $search = $ldap->search(null, $filter, $options);
69 if (!$search instanceof \Net_LDAP2_Search) {
71 'Error searching LDAP: ' . $search->getMessage()
73 } else if ($search->count() == 0) {
77 while ($entry = $search->shiftEntry()) {
79 $this->getNameFromEntry($entry),
81 $entry->getValue($dateAttribute, 'single')
83 if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
92 protected function getNameFromEntry(\Net_LDAP2_Entry $entry)
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'];
106 * @return array Values like "-01-24" ("-$month-$day")
108 protected function getDates($strDate, $nDaysPrevious, $nDaysNext)
110 $ts = strtotime($strDate) - 86400 * $nDaysPrevious;
111 $numDays = $nDaysPrevious + $nDaysNext;
115 $arDays[] = date('-m-d', $ts);
117 } while (--$numDays >= 0);