summaryrefslogtreecommitdiff
path: root/src/bdrem/Source/Ldap.php
blob: aa45b0aede89acdb8c2efe3221b66e9865a78c5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
/**
 * Part of bdrem
 *
 * PHP version 5
 *
 * @category  Tools
 * @package   Bdrem
 * @author    Christian Weiske <cweiske@cweiske.de>
 * @copyright 2014 Christian Weiske
 * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
 * @link      http://cweiske.de/bdrem.htm
 */
namespace bdrem;

/**
 * Fetch data from an LDAP server.
 * Works fine with evolutionPerson schema.
 *
 * @category  Tools
 * @package   Bdrem
 * @author    Christian Weiske <cweiske@cweiske.de>
 * @copyright 2014 Christian Weiske
 * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
 * @link      http://cweiske.de/bdrem.htm
 */
class Source_Ldap
{
    /**
     * LDAP server configuration
     *
     * Keys:
     * - host   - LDAP server host name
     * - basedn - root DN that gets searched
     * - binddn - Username to authenticate with
     * - bindpw - Password for username
     *
     * @var array
     */
    protected $config;

    /**
     * Create new ldap source
     *
     * @param array $config Array of Net_LDAP2 configuration parameters.
     *                      Some of those you might want to use:
     *                      - host   - LDAP server host name
     *                      - basedn - root DN that gets searched
     *                      - binddn - Username to authenticate with
     *                      - bindpw - Password for username
     */
    public function __construct($config)
    {
        $this->config = $config;
    }

    /**
     * Return all events for the given date range
     *
     * @param string  $strDate       Date the events shall be found for,
     *                               YYYY-MM-DD
     * @param integer $nDaysPrevious Include number of days before $strDate
     * @param integer $nDaysNext     Include number of days after $strDate
     *
     * @return Event[] Array of matching event objects
     */
    public function getEvents($strDate, $nDaysPrevious, $nDaysNext)
    {
        //Net_LDAP2 is not E_STRICT compatible
        error_reporting(error_reporting() & ~E_STRICT);

        $ldap = \Net_LDAP2::connect($this->config);
        if (\PEAR::isError($ldap)) {
            throw new \Exception(
                'Could not connect to LDAP-server: ' . $ldap->getMessage()
            );
        }

        $dateAttributes = array(
            'birthDate'   => 'Birthday',
            'anniversary' => 'Anniversary',
        );

        $arDays   = $this->getDates($strDate, $nDaysPrevious, $nDaysNext);
        $arEvents = array();

        foreach ($dateAttributes as $dateAttribute => $attributeTitle) {
            $filters = array();
            foreach ($arDays as $day) {
                $filters[] = \Net_LDAP2_Filter::create($dateAttribute, 'ends', $day);
            }

            if (count($filters) < 2) {
                $filter = $filters[0];
            } else {
                $filter = \Net_LDAP2_Filter::combine('or', $filters);
            }
            $options = array(
                'scope'      => 'sub',
                'attributes' => array(
                    'displayName', 'givenName', 'sn', 'cn', $dateAttribute
                )
            );

            $search = $ldap->search(null, $filter, $options);
            if (!$search instanceof \Net_LDAP2_Search) {
                throw new \Exception(
                    'Error searching LDAP: ' . $search->getMessage()
                );
            } else if ($search->count() == 0) {
                continue;
            }

            while ($entry = $search->shiftEntry()) {
                $event = new Event(
                    $this->getNameFromEntry($entry),
                    $attributeTitle,
                    $entry->getValue($dateAttribute, 'single')
                );
                if ($event->isWithin($strDate, $nDaysPrevious, $nDaysNext)) {
                    $arEvents[] = $event;
                }
            }
        }

        return $arEvents;
    }

    /**
     * Extract the name from the given LDAP entry object.
     * Uses displayName or givenName + sn
     *
     * @param object $entry LDAP entry
     *
     * @return string Name or NULL
     */
    protected function getNameFromEntry(\Net_LDAP2_Entry $entry)
    {
        $arEntry = $entry->getValues();
        if (isset($arEntry['displayName'])) {
            return $arEntry['displayName'];
        } else if (isset($arEntry['sn']) && isset($arEntry['givenName'])) {
            return $arEntry['givenName'] . ' ' . $arEntry['sn'];
        } else if (isset($arEntry['cn'])) {
            return $arEntry['cn'];
        }
        return null;
    }

    /**
     * Create an array of dates that are included in the given range.
     *
     * @param string  $strDate       Date the events shall be found for,
     *                               YYYY-MM-DD
     * @param integer $nDaysPrevious Include number of days before $strDate
     * @param integer $nDaysNext     Include number of days after $strDate
     *
     * @return array Values like "-01-24" ("-$month-$day")
     */
    protected function getDates($strDate, $nDaysPrevious, $nDaysNext)
    {
        $ts = strtotime($strDate) - 86400 * $nDaysPrevious;
        $numDays = $nDaysPrevious + $nDaysNext;

        $arDays = array();
        do {
            $arDays[] = date('-m-d', $ts);
            $ts += 86400;
        } while (--$numDays >= 0);
        return $arDays;
    }
}
?>