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 $filter = \Net_LDAP2_Filter::combine('or', $filters);
59 'attributes' => array(
60 'displayName', 'givenName', 'sn', 'cn', $dateAttribute
64 $search = $ldap->search(null, $filter, $options);
65 if (!$search instanceof \Net_LDAP2_Search) {
67 'Error searching LDAP: ' . $search->getMessage()
69 } else if ($search->count() == 0) {
73 while ($entry = $search->shiftEntry()) {
75 $this->getNameFromEntry($entry),
77 $entry->getValue($dateAttribute, 'single')
79 if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
88 protected function getNameFromEntry(\Net_LDAP2_Entry $entry)
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'];
102 * @return array Values like "-01-24" ("-$month-$day")
104 protected function getDates($strDate, $nDaysPrevious, $nDaysNext)
106 $ts = strtotime($strDate) - 86400 * $nDaysPrevious;
107 $numDays = $nDaysPrevious + $nDaysNext;
111 $arDays[] = date('-m-d', $ts);
113 } while (--$numDays >= 0);