summaryrefslogtreecommitdiffstats
path: root/modules-available/locationinfo/inc/coursebackend/exchange.todo
blob: 538f738204406f43d50a4c6d4092dce7122d4f17 (plain) (blame)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<?php

require_once __DIR__ . '/../../vendor/autoload.php';

use jamesiarmes\PhpEws\Client;
use jamesiarmes\PhpEws\Enumeration\DefaultShapeNamesType;
use jamesiarmes\PhpEws\Enumeration\DistinguishedFolderIdNameType;
use jamesiarmes\PhpEws\Enumeration\ItemQueryTraversalType;
use jamesiarmes\PhpEws\Enumeration\ResponseClassType;
use jamesiarmes\PhpEws\Request\FindItemType;
use jamesiarmes\PhpEws\Request\ResolveNamesType;
use jamesiarmes\PhpEws\Type\CalendarViewType;
use jamesiarmes\PhpEws\Type\DistinguishedFolderIdType;
use jamesiarmes\PhpEws\Type\EmailAddressType;
use jamesiarmes\PhpEws\Type\ItemResponseShapeType;

class CourseBackend_Exchange extends CourseBackend {

    private $username = '';
    private $password = '';
    private $baseUrl;
    private $client_version;
    private $timezone = 'W. Europe Standard Time';  // TODO: make this configurable some time
    private $verifyHostname = true;
    private $verifyCert = true;

    /**
     * @return string return display name of backend
     */
    public function getDisplayName() {
        return "Microsoft Exchange";
    }

    /**
     * @returns \BackendProperty[] list of properties that need to be set
     */
    public function getCredentialDefinitions() {
        $options = [Client::VERSION_2007, Client::VERSION_2007_SP1, Client::VERSION_2009, Client::VERSION_2010,
            Client::VERSION_2010_SP1, Client::VERSION_2010_SP2, Client::VERSION_2013, Client::VERSION_2013_SP1, Client::VERSION_2016];
        return [
            new BackendProperty('baseUrl', 'string'),
            new BackendProperty('username', 'string'),
            new BackendProperty('password', 'password'),
            new BackendProperty('client_version', $options),
            new BackendProperty('verifyCert', 'bool', true),
            new BackendProperty('verifyHostname', 'bool', true)
        ];
    }

    /**
     * @return boolean true if the connection works, false otherwise
     */
    public function checkConnection() {
        $client = $this->getClient();
        $request = new ResolveNamesType();
        $request->UnresolvedEntry = $this->username;
        $request->ReturnFullContactData = false;

        try {
            $response = $client->ResolveNames($request);
        } catch (Exception $e) {
            error_log("There was an error");
            error_log($e->getMessage());
            return false;
        }

        if ($response->ResponseMessages->ResolveNamesResponseMessage[0]->ResponseCode == "NoError") {
            $mailadress = $response->ResponseMessages->ResolveNamesResponseMessage[0]->ResolutionSet->Resolution[0]->Mailbox->EmailAddress;
            return !empty($mailadress);
        }
        return false;
    }

    /**
     * uses json to setCredentials, the json must follow the form given in
     * getCredentials
     *
     * @param array $data assoc array with data required by backend
     * @returns bool if the credentials were in the correct format
     */
    public function setCredentialsInternal($data) {
        foreach (['username', 'password'] as $field) {
            if (empty($data[$field])) {
                $this->error = 'setCredentials: Missing field ' . $field;
                return false;
            }
        }

        if (empty($data['baseUrl'])) {
            $this->error = "No url is given";
            return false;
        }

        $this->username = $data['username'];
        $this->password = $data['password'];

        $this->baseUrl = $data['baseUrl'];
        $this->client_version = $data['client_version'];

        $this->verifyHostname = $data['verifyHostname'];
        $this->verifyCert = $data['verifyCert'];

        return true;
    }

    /**
     * @return int desired caching time of results, in seconds. 0 = no caching
     */
    public function getCacheTime() {
        return 0;
    }

    /**
     * @return int age after which timetables are no longer refreshed should be
     * greater then CacheTime
     */
    public function getRefreshTime() {
        return 0;
    }

    /**
     * Internal version of fetch, to be overridden by subclasses.
     *
     * @param $roomIds array with local ID as key and serverId as value
     * @return array a recursive array that uses the roomID as key
     * and has the schedule array as value. A shedule array contains an array in this format:
     * ["start"=>'JJJJ-MM-DD HH:MM:SS',"end"=>'JJJJ-MM-DD HH:MM:SS',"title"=>string]
     */
    protected function fetchSchedulesInternal($requestedRoomIds) {
        $startDate = new DateTime('today 0:00');
        $endDate = new DateTime('+7 days 0:00');
        $client = $this->getClient();

        $schedules = [];
        foreach ($requestedRoomIds as $roomId) {
            $items = $this->findEventsForRoom($client, $startDate, $endDate, $roomId);

            // Iterate over the events that were found, printing some data for each.
            foreach ($items as $item) {
                $start = new DateTime($item->Start);
                $end = new DateTime($item->End);

                $schedules[$roomId][] = array(
                    'title' => $item->Subject,
                    'start' => $start->format('Y-m-d') . "T" . $start->format('G:i:s'),
                    'end' => $end->format('Y-m-d') . "T" . $end->format('G:i:s')
                );
            }
        }
        return $schedules;
    }

    public function findEventsForRoom($client, $start_date, $end_date, $email_room) {
        $request = new FindItemType();
        $request->Traversal = ItemQueryTraversalType::SHALLOW;
        $request->ItemShape = new ItemResponseShapeType();
        $request->ItemShape->BaseShape = DefaultShapeNamesType::ALL_PROPERTIES;

        $request->CalendarView = new CalendarViewType();
        $request->CalendarView->StartDate = $start_date->format('c');
        $request->CalendarView->EndDate = $end_date->format('c');
        $folder_id = new DistinguishedFolderIdType();
        $folder_id->Id = DistinguishedFolderIdNameType::CALENDAR;
        $folder_id->Mailbox = new EmailAddressType();
        $folder_id->Mailbox->EmailAddress = $email_room;
        $request->ParentFolderIds->DistinguishedFolderId[] = $folder_id;
        $response = $client->FindItem($request);
        $response_messages = $response->ResponseMessages->FindItemResponseMessage;

        $items = [];
        foreach ($response_messages as $response_message) {
            // Make sure the request succeeded.
            if ($response_message->ResponseClass != ResponseClassType::SUCCESS) {
                $code = $response_message->ResponseCode;
                $message = $response_message->MessageText;
                error_log("Failed to search for events with \"$code: $message\"\n");
                continue;
            }
            $items = $response_message->RootFolder->Items->CalendarItem;
        }
        return $items;
    }

    public function getClient() {
        $client = new Client($this->baseUrl, $this->username, $this->password, $this->client_version);
        $client->setTimezone($this->timezone);
        $client->setCurlOptions(array(
            CURLOPT_SSL_VERIFYPEER => $this->verifyHostname,
            CURLOPT_SSL_VERIFYHOST => $this->verifyCert
        ));

        return $client;
    }

    function var_error_log($object = null) {
        ob_start(); // start buffer capture
        var_dump($object); // dump the values
        $contents = ob_get_contents(); // put the buffer into a variable
        ob_end_clean(); // end capture
        error_log($contents); // log contents of the result of var_dump( $object )
    }
}
?>