File: class.freebusypublish.php

package info (click to toggle)
d-push 2.0-1.1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,928 kB
  • sloc: php: 29,162; sh: 104; makefile: 17
file content (395 lines) | stat: -rw-r--r-- 18,796 bytes parent folder | download
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/*
 * Copyright 2005 - 2012  Zarafa B.V.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation with the following additional
 * term according to sec. 7:
 *
 * According to sec. 7 of the GNU Affero General Public License, version
 * 3, the terms of the AGPL are supplemented with the following terms:
 *
 * "Zarafa" is a registered trademark of Zarafa B.V. The licensing of
 * the Program under the AGPL does not imply a trademark license.
 * Therefore any rights, title and interest in our trademarks remain
 * entirely with us.
 *
 * However, if you propagate an unmodified version of the Program you are
 * allowed to use the term "Zarafa" to indicate that you distribute the
 * Program. Furthermore you may use our trademarks where it is necessary
 * to indicate the intended purpose of a product or service provided you
 * use it in accordance with honest practices in industrial or commercial
 * matters.  If you want to propagate modified versions of the Program
 * under the name "Zarafa" or "Zarafa Server", you may only do so if you
 * have a written permission by Zarafa B.V. (to acquire a permission
 * please contact Zarafa at trademark@zarafa.com).
 *
 * The interactive user interface of the software displays an attribution
 * notice containing the term "Zarafa" and/or the logo of Zarafa.
 * Interactive user interfaces of unmodified and modified versions must
 * display Appropriate Legal Notices according to sec. 5 of the GNU
 * Affero General Public License, version 3, when you propagate
 * unmodified or modified versions of the Program. In accordance with
 * sec. 7 b) of the GNU Affero General Public License, version 3, these
 * Appropriate Legal Notices must retain the logo of Zarafa or display
 * the words "Initial Development by Zarafa" if the display of the logo
 * is not reasonably feasible for technical reasons. The use of the logo
 * of Zarafa in Legal Notices is allowed for unmodified and modified
 * versions of the software.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


include_once('backend/zarafa/mapi/class.recurrence.php');

class FreeBusyPublish {

    var $session;
    var $calendar;
    var $entryid;
    var $starttime;
    var $length;
    var $store;
    var $proptags;

    /**
     * Constuctor
     *
     * @param mapi_session $session MAPI Session
     * @param mapi_folder $calendar Calendar to publish
     * @param string $entryid AddressBook Entry ID for the user we're publishing for
     */


    function FreeBusyPublish($session, $store, $calendar, $entryid)
    {
        $properties["entryid"] = PR_ENTRYID;
        $properties["parent_entryid"] = PR_PARENT_ENTRYID;
        $properties["message_class"] = PR_MESSAGE_CLASS;
        $properties["icon_index"] = PR_ICON_INDEX;
        $properties["subject"] = PR_SUBJECT;
        $properties["display_to"] = PR_DISPLAY_TO;
        $properties["importance"] = PR_IMPORTANCE;
        $properties["sensitivity"] = PR_SENSITIVITY;
        $properties["startdate"] = "PT_SYSTIME:PSETID_Appointment:0x820d";
        $properties["duedate"] = "PT_SYSTIME:PSETID_Appointment:0x820e";
        $properties["recurring"] = "PT_BOOLEAN:PSETID_Appointment:0x8223";
        $properties["recurring_data"] = "PT_BINARY:PSETID_Appointment:0x8216";
        $properties["busystatus"] = "PT_LONG:PSETID_Appointment:0x8205";
        $properties["label"] = "PT_LONG:PSETID_Appointment:0x8214";
        $properties["alldayevent"] = "PT_BOOLEAN:PSETID_Appointment:0x8215";
        $properties["private"] = "PT_BOOLEAN:PSETID_Common:0x8506";
        $properties["meeting"] = "PT_LONG:PSETID_Appointment:0x8217";
        $properties["startdate_recurring"] = "PT_SYSTIME:PSETID_Appointment:0x8235";
        $properties["enddate_recurring"] = "PT_SYSTIME:PSETID_Appointment:0x8236";
        $properties["location"] = "PT_STRING8:PSETID_Appointment:0x8208";
        $properties["duration"] = "PT_LONG:PSETID_Appointment:0x8213";
        $properties["responsestatus"] = "PT_LONG:PSETID_Appointment:0x8218";
        $properties["reminder"] = "PT_BOOLEAN:PSETID_Common:0x8503";
        $properties["reminder_minutes"] = "PT_LONG:PSETID_Common:0x8501";
        $properties["contacts"] = "PT_MV_STRING8:PSETID_Common:0x853a";
        $properties["contacts_string"] = "PT_STRING8:PSETID_Common:0x8586";
        $properties["categories"] = "PT_MV_STRING8:PS_PUBLIC_STRINGS:Keywords";
        $properties["reminder_time"] = "PT_SYSTIME:PSETID_Common:0x8502";
        $properties["commonstart"] = "PT_SYSTIME:PSETID_Common:0x8516";
        $properties["commonend"] = "PT_SYSTIME:PSETID_Common:0x8517";
        $properties["basedate"] = "PT_SYSTIME:PSETID_Appointment:0x8228";
        $properties["timezone_data"] = "PT_BINARY:PSETID_Appointment:0x8233";
        $this->proptags = getPropIdsFromStrings($store, $properties);

        $this->session = $session;
        $this->calendar = $calendar;
        $this->entryid = $entryid;
        $this->store = $store;
    }

    /**
     * Publishes the infomation
     * @paam timestamp $starttime Time from which to publish data  (usually now)
     * @paam integer $length Amount of seconds from $starttime we should publish
     */
    function publishFB($starttime, $length) {
        $start = $starttime;
        $end = $starttime + $length;

        // Get all the items in the calendar that we need

        $calendaritems = Array();

        $restrict = Array(RES_OR,
                             Array(
                                   // OR
                                   // (item[start] >= start && item[start] <= end)
                                   Array(RES_AND,
                                         Array(
                                               Array(RES_PROPERTY,
                                                     Array(RELOP => RELOP_GE,
                                                           ULPROPTAG => $this->proptags["startdate"],
                                                           VALUE => $start
                                                           )
                                                     ),
                                               Array(RES_PROPERTY,
                                                     Array(RELOP => RELOP_LE,
                                                           ULPROPTAG => $this->proptags["startdate"],
                                                           VALUE => $end
                                                           )
                                                     )
                                               )
                                         ),
                                   // OR
                                   // (item[end]   >= start && item[end]   <= end)
                                   Array(RES_AND,
                                         Array(
                                               Array(RES_PROPERTY,
                                                     Array(RELOP => RELOP_GE,
                                                           ULPROPTAG => $this->proptags["duedate"],
                                                           VALUE => $start
                                                           )
                                                     ),
                                               Array(RES_PROPERTY,
                                                     Array(RELOP => RELOP_LE,
                                                           ULPROPTAG => $this->proptags["duedate"],
                                                           VALUE => $end
                                                           )
                                                     )
                                               )
                                         ),
                                   // OR
                                   // (item[start] <  start && item[end]   >  end)
                                   Array(RES_AND,
                                         Array(
                                               Array(RES_PROPERTY,
                                                     Array(RELOP => RELOP_LT,
                                                           ULPROPTAG => $this->proptags["startdate"],
                                                           VALUE => $start
                                                           )
                                                     ),
                                               Array(RES_PROPERTY,
                                                     Array(RELOP => RELOP_GT,
                                                           ULPROPTAG => $this->proptags["duedate"],
                                                           VALUE => $end
                                                           )
                                                     )
                                               )
                                         ),
                                   // OR
                                   Array(RES_OR,
                                         Array(
                                               // OR
                                               // (EXIST(ecurrence_enddate_property) && item[isRecurring] == true && item[end] >= start)
                                               Array(RES_AND,
                                                     Array(
                                                           Array(RES_EXIST,
                                                                 Array(ULPROPTAG => $this->proptags["enddate_recurring"],
                                                                       )
                                                                 ),
                                                           Array(RES_PROPERTY,
                                                                 Array(RELOP => RELOP_EQ,
                                                                       ULPROPTAG => $this->proptags["recurring"],
                                                                       VALUE => true
                                                                       )
                                                                 ),
                                                           Array(RES_PROPERTY,
                                                                 Array(RELOP => RELOP_GE,
                                                                       ULPROPTAG => $this->proptags["enddate_recurring"],
                                                                       VALUE => $start
                                                                       )
                                                                 )
                                                           )
                                                     ),
                                               // OR
                                               // (!EXIST(ecurrence_enddate_property) && item[isRecurring] == true && item[start] <= end)
                                               Array(RES_AND,
                                                     Array(
                                                           Array(RES_NOT,
                                                                 Array(
                                                                       Array(RES_EXIST,
                                                                             Array(ULPROPTAG => $this->proptags["enddate_recurring"]
                                                                                   )
                                                                             )
                                                                       )
                                                                 ),
                                                           Array(RES_PROPERTY,
                                                                 Array(RELOP => RELOP_LE,
                                                                       ULPROPTAG => $this->proptags["startdate"],
                                                                       VALUE => $end
                                                                       )
                                                                 ),
                                                           Array(RES_PROPERTY,
                                                                 Array(RELOP => RELOP_EQ,
                                                                       ULPROPTAG => $this->proptags["recurring"],
                                                                       VALUE => true
                                                                       )
                                                                 )
                                                           )
                                                     )
                                               )
                                         ) // EXISTS OR
                                   )
                             );        // global OR

        $contents = mapi_folder_getcontentstable($this->calendar);
        mapi_table_restrict($contents, $restrict);

        while(1) {
            $rows = mapi_table_queryrows($contents, array_values($this->proptags), 0, 50);

            if(!is_array($rows))
                break;

            if(empty($rows))
                break;

            foreach ($rows as $row) {
                $occurrences = Array();
                if(isset($row[$this->proptags['recurring']]) && $row[$this->proptags['recurring']]) {
                    $recur = new Recurrence($this->store, $row);

                    $occurrences = $recur->getItems($starttime, $starttime + $length);
                } else {
                    $occurrences[] = $row;
                }

                $calendaritems = array_merge($calendaritems, $occurrences);
            }
        }

        // $calendaritems now contains all the calendar items in the specified time
        // frame. We now need to merge these into a flat array of begin/end/status
        // objects. This also filters out all the 'free' items (status 0)

        $freebusy = $this->mergeItemsFB($calendaritems);

        // $freebusy now contains the start, end and status of all items, merged.

        // Get the FB interface
        try {
            $fbsupport = mapi_freebusysupport_open($this->session, $this->store);
        } catch (MAPIException $e) {
            if($e->getCode() == MAPI_E_NOT_FOUND) {
                $e->setHandled();
                if(function_exists("dump")) {
                    dump("Error in opening freebusysupport object.");
                }
            }
        }

        // Open updater for this user
        if(isset($fbsupport)) {
            $updaters = mapi_freebusysupport_loadupdate($fbsupport, Array($this->entryid));

            $updater = $updaters[0];

            // Send the data
            mapi_freebusyupdate_reset($updater);
            mapi_freebusyupdate_publish($updater, $freebusy);
            mapi_freebusyupdate_savechanges($updater, $start-24*60*60, $end);

            // We're finished
            mapi_freebusysupport_close($fbsupport);
        }
    }

    /**
    * Sorts by timestamp, if equal, then end before start
    */
    function cmp($a, $b)
    {
        if ($a["time"] == $b["time"]) {
            if($a["type"] < $b["type"])
                return 1;
            if($a["type"] > $b["type"])
                return -1;
            return 0;
        }
        return ($a["time"] > $b["time"] ? 1 : -1);
    }

    /**
    * Function mergeItems
    * @author Steve Hardy
    */
    function mergeItemsFB($items)
    {
        $merged = Array();
        $timestamps = Array();
        $csubj = Array();
        $cbusy = Array();
        $level = 0;
        $laststart = null;

        foreach($items as $item)
        {
            $ts["type"] = 0;
            $ts["time"] = $item[$this->proptags["startdate"]];
            $ts["subject"] = $item[PR_SUBJECT];
            $ts["status"] = $item[$this->proptags["busystatus"]];
            $timestamps[] = $ts;

            $ts["type"] = 1;
            $ts["time"] = $item[$this->proptags["duedate"]];
            $ts["subject"] = $item[PR_SUBJECT];
            $ts["status"] = $item[$this->proptags["busystatus"]];
            $timestamps[] = $ts;
        }

        usort($timestamps, Array($this, "cmp"));
        $laststart = 0; // seb added

        foreach($timestamps as $ts)
        {
            switch ($ts["type"])
            {
                case 0: // Start
                    if ($level != 0 && $laststart != $ts["time"])
                    {
                        $newitem["start"] = $laststart;
                        $newitem["end"] = $ts["time"];
                        $newitem["subject"] = join(",", $csubj);
                        $newitem["status"] = !empty($cbusy) ? max($cbusy) : 0;
                        if($newitem["status"] > 0)
                            $merged[] = $newitem;
                    }

                    $level++;

                    $csubj[] = $ts["subject"];
                    $cbusy[] = $ts["status"];

                    $laststart = $ts["time"];
                    break;
                case 1: // End
                    if ($laststart != $ts["time"])
                    {
                        $newitem["start"] = $laststart;
                        $newitem["end"] = $ts["time"];
                        $newitem["subject"] = join(",", $csubj);
                        $newitem["status"] = !empty($cbusy) ? max($cbusy) : 0;
                        if($newitem["status"] > 0)
                            $merged[] = $newitem;
                    }

                    $level--;

                    array_splice($csubj, array_search($ts["subject"], $csubj, 1), 1);
                    array_splice($cbusy, array_search($ts["status"], $cbusy, 1), 1);

                    $laststart = $ts["time"];
                    break;
            }
        }

        return $merged;
    }

}
?>