source: contrib/z-push/backend/BackendCalendarExpresso.php @ 3637

Revision 3637, 40.6 KB checked in by emersonfaria, 13 years ago (diff)

Ticket #1476 - Commit inicial dos arquivos do Projeto Z-Push customizados para o Expresso

  • Property svn:executable set to *
Line 
1<?php
2/***********************************************
3 * File      :   BackendCalendarExpresso.php
4 * Project   :   Z-Push
5 * Descr     :   This Calendar Backend is for Expresso Groupware.
6 *
7 * Created   :   06.12.2010 - emerson-faria.nobre@serpro.gov.br
8 *
9 * ï¿œ Zarafa Deutschland GmbH, www.zarafaserver.de
10 * This file is distributed under GPL v2.
11 * Consult LICENSE file for details
12 ************************************************/
13include_once('diffbackend.php');
14include_once('include/z_RTF.php');
15include_once "include/dbconnect.php";
16
17class BackendCalendarExpresso extends BackendDiff {
18        var $_user;
19        var $_devid;
20        var $_protocolversion;
21        var $db;
22
23        function __construct()
24        {
25                $this->db = dbconnect();
26
27                if (!$this->db) {
28                        echo "A DB connect error occured.\n";
29                        exit;
30                }
31        }
32
33        function Setup($user, $devid, $protocolversion) {
34                $this->_user = $user;
35                $this->_devid = $devid;
36                $this->_protocolversion = $protocolversion;
37                //TODO: Autenticar Usuario no LDAP
38
39                return true;
40        }
41
42        function SendMail($rfc822, $forward = false, $reply = false, $parent = false) {
43                return false;
44        }
45
46        function GetWasteBasket() {
47                return false;
48        }
49
50        function GetMessageList($folderid, $cutoffdate) {
51                $id_owner = $this->parseOwner($this->_user);
52                if ($id_owner == false) return false;
53
54                debugLog('CalendarExpresso::GetMessageList('.$id_owner.')');
55                if ($folderid != "calendar") return false;
56
57                $messages = array();
58                try {
59                        $result = pg_query($this->db,"BEGIN;");
60                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
61                        if ($cutoffdate == 0) $result = pg_query($this->db, "select c.cal_id, c.last_update, c.datetime, c.cal_type from phpgw_cal c join phpgw_cal_user u on (c.cal_id = u.cal_id) where u.cal_login ='" . $id_owner . "'and c.reference = 0;");
62                        else $result = pg_query($this->db, "select c.cal_id, c.last_update, c.datetime, c.cal_type from phpgw_cal c join phpgw_cal_user u on (c.cal_id = u.cal_id) where u.cal_login ='" . $id_owner . "'and c.reference = 0 and c.edatetime > " . $cutoffdate . ";");
63                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
64                        while ($row = pg_fetch_row($result)) {
65                                $result_recur = pg_query($this->db,"select recur_enddate from phpgw_cal_repeats where cal_id = " . $row[0] . ";");
66                                if ($result_recur == FALSE) throw new Exception(pg_last_error($this->db));
67                                if ($row[3] == 'M') {
68                                        if ($recur_enddate = pg_fetch_result($result_recur, 0, 0)) {
69                                                if ($recur_enddate < $row[2]) continue; // Nao sincroniza este evento, porque a data final da recorrencia eh menor que a data inicial do evento. Eh o BD do Expresso que esta inconsistente.
70                                        }
71                                }
72                                $LatestUpdate = $row[1];
73                                $message = array();
74                                $message["id"] = $row[0];
75                                $message["mod"] = substr($LatestUpdate, 0, strlen($LatestUpdate)-3);
76                                $message["flags"] = 1; // always 'read'
77                                $messages[] = $message;
78                        }
79                        $result = pg_query($this->db,"COMMIT;");
80                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
81                } catch (Exception $e) {
82                        pg_query($this->db,"ROLLBACK;");
83                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
84                }
85                return $messages;
86        }
87
88        function GetFolderList() {
89                debugLog('CalendarExpresso::GetFolderList()');
90                $calendars = array();
91                $folder = $this->StatFolder("calendar");
92                $calendars[] = $folder;
93                //return $calendars;
94                return $folder;
95        }
96
97        function GetFolder($id) {
98                debugLog('CalendarExpresso::GetFolder('.$id.')');
99                if($id == "calendar") {
100                        $folder = new SyncFolder();
101                        $folder->serverid = $id;
102                        $folder->parentid = "0";
103                        $folder->displayname = "Calendario";
104                        $folder->type = SYNC_FOLDER_TYPE_APPOINTMENT;
105                        return $folder;
106                } else return false;
107        }
108
109        function StatFolder($id) {
110                debugLog('CalendarExpresso::StatFolder('.$id.')');
111                $folder = $this->GetFolder($id);
112                $stat = array();
113                $stat["id"] = $id;
114                $stat["parent"] = $folder->parentid;
115                $stat["mod"] = $folder->displayname;
116                return $stat;
117        }
118
119        function GetAttachmentData($attname) {
120                return false;
121        }
122
123        function StatMessage($folderid, $id) {
124                debugLog('CalendarExpresso::StatMessage('.$folderid.', '.$id.')');
125                if($folderid != "calendar") return false;
126                try {
127                 $result = pg_query($this->db,"BEGIN;");
128                 if ($result == FALSE) throw new Exception(pg_last_error($this->db));
129                 $result_calendar = pg_query($this->db, "select last_update from phpgw_cal where cal_id = " . $id . ";");
130                 if ($result_calendar == FALSE) throw new Exception(pg_last_error($this->db));
131                 while ($row_calendar = pg_fetch_row($result_calendar)) {
132                        if(isset($row_calendar[0])) {
133                                $message = array();
134                                $message["mod"] = substr($row_calendar[0], 0, strlen($row_calendar[0])-3);
135                                $message["id"] = $id;
136                                $message["flags"] = 1;
137                                return $message;
138                        }
139                 }
140                 $result = pg_query($this->db,"COMMIT;");
141                 if ($result == FALSE) throw new Exception(pg_last_error($this->db));
142                } catch (Exception $e) {
143                        pg_query($this->db,"ROLLBACK;");
144                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
145                }
146                return false;
147        }
148
149        function GetMessageDAO($id) {
150                debugLog('CalendarExpresso::GetMessageDAO('.$id.', ..)');
151                if(trim($id == "")) return false;
152                $message = new SyncAppointment();
153                try {
154                        $has_parent_event = false;
155                        $result = pg_query($this->db,"BEGIN;");
156                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
157                        //Evento
158                        $result = pg_query($this->db,"select config_value from phpgw_config WHERE config_name = 'hostname' AND config_app = 'phpgwapi';");
159                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
160                        $uid = "-@" . pg_fetch_result($result, 0, 0) . "-" . $id;
161                        $result = pg_query($this->db, "select cal_id, title, location, mdatetime, datetime, edatetime, description, reference, is_public, category from phpgw_cal where cal_id = " . $id . ";");
162                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
163                        if ($row = pg_fetch_row($result)) {
164                                if ($row[7] != 0) $has_parent_event = true;
165                                else $has_parent_event = false;
166                                if (!$has_parent_event) {
167                                        $message->fileas = $id;
168                                        $message->uid = utf8_encode($uid);
169                                        if(isset($row[3])) {
170                                                $tz = date_default_timezone_get();
171                                                date_default_timezone_set('UTC');
172                                                $message->dtstamp = $row[3]; // phpgw_cal.mdatetime
173                                                date_default_timezone_set($tz);
174                                        }
175                                        if(isset($row[4])) {
176                                                $tz = date_default_timezone_get();
177                                                date_default_timezone_set('UTC');
178                                                $message->starttime =  $row[4]; // phpgw_cal.datetime
179                                                date_default_timezone_set($tz);
180                                        }
181                                        if(isset($row[5])) {
182                                                $tz = date_default_timezone_get();
183                                                date_default_timezone_set('UTC');
184                                                $message->endtime = $row[5]; // phpgw_cal.edatetime
185                                                date_default_timezone_set($tz);
186                                        }
187                                }
188                                if ($has_parent_event) {
189                                        $message->exceptionstarttime = $row[4];
190                                        $message->deleted = 0;
191                                }
192                                if(isset($row[1])) {
193                                        $message->subject = utf8_encode($row[1]); // phpgw_cal.title
194                                }
195                                if(isset($row[2])) {
196                                        $message->location = utf8_encode($row[2]); // phpgw_cal.location
197                                }
198                                if(isset($row[6]) and $row[6] != "") {
199                                        $message->body = utf8_encode(str_replace('\\n', chr(13) . chr(10), $this->escape($row[6]))); // phpgw_cal.description
200                                        $message->bodysize = strlen($message->body);
201                                        $message->bodytruncated = 0;
202                                }
203                                if (isset($row[8]) and $row[8] == 1) $message->sensitivity = 0; // 0 - Normal
204                                else $message->sensitivity = 2; // 2 - Privado
205                                if (isset($row[9]) and $row[9] != "") {
206                                        $result = pg_query($this->db,"select cat_name from phpgw_categories WHERE cat_id in(" . $row[9] . ");");
207                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
208                                        while ($cat_name = pg_fetch_row($result)) {
209                                                if (!isset($message->categories)) $message->categories = array();
210                                                array_push($message->categories, utf8_encode($cat_name[0]));
211                                        }
212                                }
213                                $message->alldayevent = 0; // (0 - Não(default), 1- Sim)
214                                $message->busystatus = 2; // 2 - Ocupado
215                                //TODO: Tratar $message - timezone
216                                //$tz = $this->_getGMTTZ();
217                                $tz = array("bias" => 180, "stdbias" => 0, "dstbias" => -60, "dstendyear" => 0, "dstendmonth" => 2, "dstendday" => 0, "dstendweek" => 2, "dstendhour" => 2, "dstendminute" => 0, "dstendsecond" => 0, "dstendmillis" => 0,
218                                                      "dststartyear" => 0, "dststartmonth" =>10, "dststartday" =>0, "dststartweek" => 3, "dststarthour" => 2, "dststartminute" => 0, "dststartsecond" => 0, "dststartmillis" => 0);
219                                $message->timezone = base64_encode($this->_getSyncBlobFromTZ($tz));
220                                //TODO: Se necessario, tratar $message - meetingstatus
221                                //TODO: Se necessario, tratar AppointmentReplyTime e ResponseType
222                        }
223
224                        //Alarme do Evento - O alarme nao sera sincronizado por enquanto.
225                        /* Esse exemplo sincroniza o alarme mais distante do evento com o dispositivo movel
226                        * $result = pg_query("select data from phpgw_async where id like 'cal:" . $id . ":%' and data like '%enabled%' order by next desc;");
227                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
228                        if ($data = pg_fetch_result($result, 0, 0)) {
229                        $array_data = unserialize($data);
230                        $offset = $array_data["offset"];
231                        if (isset($array_data["offset"])) {
232                        if ($offset >= 60) $message->reminder = intval($offset / 60);
233                        else $message->reminder = 0;
234                        }
235                        }*/
236                        if (!$has_parent_event) {
237                                //Recorrencia do evento
238                                //TODO: Implementar recorrencia mensal por dia e anual por dia, recur - type 3 e 6 respectivamente. O Expresso ainda nao suporta isso. :(
239                                $result = pg_query($this->db,"select recur_type, recur_use_end, recur_enddate, recur_interval, recur_data, recur_exception from phpgw_cal_repeats where cal_id = " . $id . "and recur_type in (1,2,3,5);");
240                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
241                                if ($row = pg_fetch_row($result)){
242                                        $recur = new SyncRecurrence();
243                                        // Converte os tipos do Expresso para os tipos do Protocolo ActiveSync
244                                        switch ($row[0]) {
245                                                case 1: //repeticao diaria
246                                                        $recur->type = 0;
247                                                        break;
248                                                case 2: //repeticao semanal
249                                                        $recur->type = 1;
250                                                        break;
251                                                case 3: //repeticao mensal por data
252                                                        $recur->type = 2;
253                                                        break;
254                                                case 5: //repeticao anual por data
255                                                        $recur->type = 5;
256                                                        break;
257                                        }
258                                        if (isset($row[2])) {
259                                                $tz = date_default_timezone_get();
260                                                date_default_timezone_set('UTC');
261                                                $recur->until = $row[2];
262                                                date_default_timezone_set($tz);
263                                        }
264                                        if (isset($row[3]) and $row[3] != 0) $recur->interval = $row[3];
265                                        else $recur->interval = 1;
266                                        //TODO: Se o Expresso estiver utilizando o campo phpgw_cal_repeats.recur_use_end, implementar.
267                                        if ($recur->type == 1 or $recur->type == 3 or $recur->type == 6) {
268                                                if (isset($row[4])) $recur->dayofweek = $row[4];
269                                        }
270                                        if ($recur->type == 2 or $recur->type == 5) {
271                                                $tz = date_default_timezone_get();
272                                                date_default_timezone_set('UTC');
273                                                $array_startdatetime    = getdate($message->starttime);
274                                                $recur->dayofmonth = $array_startdatetime['mday'];
275                                                date_default_timezone_set($tz);
276
277                                        }
278                                        if ($recur->type == 5 or $recur->type == 6) {
279                                                $tz = date_default_timezone_get();
280                                                date_default_timezone_set('UTC');
281                                                $array_startdatetime    = getdate($message->starttime);
282                                                $recur->monthofyear = $array_startdatetime['mon'];
283                                                date_default_timezone_set($tz);
284
285                                        }
286                                        $message->recurrence = $recur;
287                                }
288
289                                //Eventos Unicos da Recorrencia Editados
290                                $result = pg_query($this->db, "select cal_id from phpgw_cal where reference = " . $id . ";");
291                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
292                                while ($row_recur_exception = pg_fetch_row($result)) {
293                                        $recur_exception = $this->GetMessageDAO($row_recur_exception[0]);
294                                        if ($recur_exception instanceof SyncAppointment) {
295                                                if (!isset($message->exceptions)) $message->exceptions = array();
296                                                array_push($message->exceptions, $recur_exception);
297                                        }
298                                }
299
300                                //Eventos Unicos da Recorrencia Deletados
301                                if (isset($row[5]) and $row[5] != "") {
302                                        $array_timestamps_recur_deleted = explode(",",$row[5]);
303                                        foreach ($array_timestamps_recur_deleted as $timestamp_recur_deleted) {
304                                                $recur_deleted = new SyncAppointment();
305                                                $recur_deleted->deleted = '1';
306                                                $recur_deleted->exceptionstarttime = (integer)$timestamp_recur_deleted;
307                                                if (!isset($message->exceptions)) $message->exceptions = array();
308                                                array_push($message->exceptions, $recur_deleted);
309                                        }
310                                }
311                                //TODO: Tratar Participantes do Evento
312                        }
313                        $result = pg_query($this->db,"COMMIT;");
314                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
315                } catch (Exception $e) {
316                        pg_query($this->db,"ROLLBACK;");
317                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
318                        return;
319                }
320                return $message;
321        }
322
323        function GetMessage($folderid, $id, $truncsize, $mimesupport = 0) {
324                debugLog('CalendarExpresso::GetMessage('.$folderid.', '.$id.', ..)');
325                if($folderid != "calendar")     return;
326                if(trim($id == "")) return;
327                $message = $this->GetMessageDAO($id);
328                return $message;
329
330                //$message->?????  //phpgw_cal.priority (1 - baixa, 2 - normal, 3 - alta) //Quando for criar um registro o valor será 2.
331                //$message->?????  //phpgw_cal.groups (Não vou atribuir nenhum valor)
332                //$message->?????  //phpgw_cal.cal_type (quando for criar um novo registro vou inserir R para Recursivo e E para não recursivo). Quando Alterar um registro entre de Recursivo para não Recursivo e vice-versa vou fazer o mesmo da inclusão. Quando alterar entre dois tipos não recursivos, não faço nada.
333                //$message->?????  //phpgw_cal.ex_participants
334                //$message->?????  //phpgw_cal_user.cal_status
335                //$message->?????  //phpgw_cal_user.cal_type
336                //$message->?????  //phpgw_cal_repeats.recur_interval
337                //$message->?????  //phpgw_cal_repeats.recur_data
338                //$message->?????  //phpgw_cal_repeats.recur_exception
339
340                //$message->timezone  //phpgw?????
341                //$message->organizername //phpgw?????
342                //$message->organizeremail //phpgw????
343                //$message->attendees // phpgw????  (SyncAttendee array)
344
345                //$syncAttendee = new SyncAttendee();
346                //$syncAttendee->email  //phpgw????
347                //$syncAttendee->name   //phpgw????
348        }
349
350        function DeleteMessage($folderid, $id) {
351                debugLog('CalendarExpresso::DeleteMessage('.$folderid.', '.$id.', ..)');
352                if (!isset($id)) return false;
353                $id_owner = $this->parseOwner($this->_user);
354                if ($id_owner == false) return false;
355                $result = pg_query($this->db, "select owner from phpgw_cal where cal_id = " . $id . ";");
356                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
357                if ($id_owner != pg_fetch_result($result, 0, 0)) return false;
358                try {
359                        $result = pg_query($this->db,"BEGIN;");
360                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
361                        $result = pg_delete($this->db, "phpgw_cal_user", array('cal_id' => $id));
362                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
363                        $result = pg_delete($this->db, "phpgw_cal", array('reference' => $id));
364                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
365                        $result = pg_delete($this->db, "phpgw_cal_repeats", array('cal_id' => $id));
366                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
367                        $result = pg_query("delete from phpgw_async where id like 'cal:" . $id . "%';");
368                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
369                        $result = pg_delete($this->db, "phpgw_cal_extra", array('cal_id' => $id));
370                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
371                        $result = pg_delete($this->db, "phpgw_cal", array('cal_id' => $id));
372                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
373                        $result = pg_query($this->db,"COMMIT;");
374                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
375                } catch (Exception $e) {
376                        pg_query($this->db,"ROLLBACK;");
377                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
378                        return false;
379                }
380                return true;
381        }
382
383        function SetReadFlag($folderid, $id, $flags) {
384                return false;
385        }
386
387        function ChangeMessageDAO($id, $message) {
388                debugLog('CalendarExpresso::ChangeMessageDAO('.$id.', ..)');
389                $id_owner = $this->parseOwner($this->_user);
390                if ($id_owner == false) return false;
391                try {
392                        $result = pg_query($this->db,"BEGIN;");
393                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
394                        $found_cal_id = false;
395                        $found_cal_user = false;
396                        $found_cal_repeats = false;
397                        if ($id != false) {
398                                $result = pg_query($this->db, "select cal_id, cal_type, owner from phpgw_cal where cal_id = " . $id . ";");
399                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
400                                // tenta localizar cal_id para fazer Update
401                                while ($row = pg_fetch_row($result)) {
402                                        if(isset($row[0])) {
403                                                if ($id_owner != $row[2]) return false; // Nao atualiza evento de outro proprietario
404                                                $cal_id = $row[0];
405                                                $cal_type_from_DB = $row[1];
406                                                $found_cal_id = true;
407                                        }
408                                }
409                                // Verifica se ja existe o registro do evento na tabela phpgw_cal_user
410                                $result = pg_query($this->db, "select cal_id, cal_login from phpgw_cal_user where cal_id = " . $cal_id . " and cal_login = '" . $id_owner . "';");
411                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
412                                if ($row = pg_fetch_row($result)) {
413                                        if(isset($row[0])) {
414                                                $found_cal_user = true;
415                                        }
416                                }
417                                // Verifica se ja existe o registro de recorrencia do evento na tabela phpgw_cal_repeats
418                                $result = pg_query($this->db, "select cal_id from phpgw_cal_repeats where cal_id = " . $cal_id . ";");
419                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
420                                if ($row = pg_fetch_row($result)) {
421                                        if(isset($row[0])) {
422                                                $found_cal_repeats = true;
423                                        }
424                                }
425                        }
426
427                        //Insere/Atualiza tabela phpgw_cal
428                        if(isset($message->uid)) {
429                                $arrayCal["uid"] = $this->truncateString(utf8_decode($message->uid),255);
430                        }
431                        if(isset($message->subject)) {
432                                $arrayCal["title"] = $this->truncateString(utf8_decode($message->subject),80);
433                        }
434                        if(isset($message->location)) {
435                                $arrayCal["location"] = $this->truncateString(utf8_decode($message->location),255);
436                        }
437                        if(isset($message->dtstamp)) {
438                                $arrayCal["mdatetime"] = $message->dtstamp;
439                        }
440                        if(isset($message->starttime)) {
441                                $arrayCal["datetime"] = $message->starttime;
442                        }
443                        if(isset($message->endtime)) {
444                                $arrayCal["edatetime"] = $message->endtime;
445                        }
446                        if(isset($message->sensitivity) and $message->sensitivity == 0) {
447                                $arrayCal["is_public"] = 1; // 1 - Normal
448                        } else $arrayCal["is_public"] = 0; // 0 - Privado
449
450                        if(isset($message->rtf)) {
451                                $rtf_to_ascii = new rtf();
452                                $rtf_to_ascii->output("ascii");
453                                $result_loadrtf = $rtf_to_ascii->loadrtf(base64_decode($message->rtf));
454                                if ($result_loadrtf == true) $rtf_to_ascii->parse();
455                                $arrayCal["description"] = $rtf_to_ascii->out;
456//                      } else {
457//                              $arrayCal["description"] = '';
458                        }
459                        $arrayCal["category"] = '';
460                        if (isset($message->categories)) {
461                                foreach ($message->categories as $category) {
462                                        $cat_id = $this->addCategory($category);
463                                        if ($cat_id != false) {
464                                                if ($arrayCal["category"] == '') {
465                                                        $arrayCal["category"] = $cat_id;
466                                                } else {
467                                                        $arrayCal["category"] .= ',' . $cat_id;
468                                                }
469                                        }
470                                }
471                        }
472                        $tz_CEL = $this->_getTZFromSyncBlob(base64_decode($message->timezone));
473                        //TODO: Atribuir valores para ex_participants
474                        $arrayCal["ex_participants"] = '';
475                        //TODO: Converter eventos que sao alldayevent para iniciar as 0:00hs e terminar as 23:59hs. Isso porque o Expresso nao suporta alldayevents
476                        //               if(isset($message->alldayevent)) {
477                        //                      $arrayCal["alldayevent"] = $this->truncateString(utf8_decode($message->alldayevent),30);
478                        //               }
479                        if (!$found_cal_id){
480                                $result = pg_query("select nextval('seq_phpgw_cal');");
481                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
482                                $cal_id = pg_fetch_result($result, 0, 0);
483                                $arrayCal["cal_id"] = $cal_id;
484                                $arrayCal["owner"] = $id_owner;
485                                if (isset($message->recurrence)) $arrayCal["cal_type"] = 'M';
486                                else $arrayCal["cal_type"] = 'E';
487                                $result = pg_insert($this->db, 'phpgw_cal', $arrayCal);
488                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
489                        } else {
490                                if (isset($cal_type_from_DB)) {
491                                        if (isset($message->recurrence)) $arrayCal["cal_type"] = 'M';
492                                        else if ($cal_type_from_DB == 'M') $arrayCal["cal_type"] = 'E';
493                                }
494                                $result = pg_update($this->db, 'phpgw_cal', $arrayCal, array('cal_id' => $cal_id));
495                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
496                        }
497
498                        // Insere/Atualiza tabela phpgw_cal_user
499                        $arrayCalUser["cal_type"] = 'u';
500                        if (!$found_cal_user){
501                                $arrayCalUser["cal_id"] = $cal_id;
502                                $arrayCalUser["cal_login"] = $id_owner;
503                                //TODO: Tratar os outros valores de cal_status
504                                $arrayCalUser["cal_status"] = 'A';
505                                $result = pg_insert($this->db, 'phpgw_cal_user', $arrayCalUser);
506                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
507                        } else {
508                                $result = pg_update($this->db, 'phpgw_cal_user', $arrayCalUser, array('cal_id' => $cal_id, 'cal_login' => $id_owner));
509                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
510                        }
511                        if (!$id) {
512                                $id = $cal_id;
513                        }
514
515                        //TODO: Implementar para os tipos 3 e 6. O Expresso ainda nao suporta esses tipos :-(
516                        if (isset($message->recurrence) and ($message->recurrence->type == 0 or $message->recurrence->type == 1 or $message->recurrence->type == 2 or $message->recurrence->type == 5)) {
517                                switch ($message->recurrence->type) {
518                                        case 0: //repeticao diaria
519                                                $arrayRecur["recur_type"] = 1;
520                                                $type_name = "day";
521                                                break;
522                                        case 1: //repeticao semanal
523                                                $arrayRecur["recur_type"] = 2;
524                                                $type_name = "week";
525                                                break;
526                                        case 2: //repeticao mensal por data
527                                                $arrayRecur["recur_type"] = 3;
528                                                $type_name = "month";
529                                                break;
530                                        case 5: //repeticao anual por data
531                                                $arrayRecur["recur_type"] = 5;
532                                                $type_name = "year";
533                                                break;
534                                }
535                                if (isset($message->recurrence->interval)) {
536                                        $arrayRecur["recur_interval"] = $message->recurrence->interval;
537                                }
538                                if ($message->recurrence->type == 1 or $message->recurrence->type == 3 or $message->recurrence->type == 6) {
539                                        if (isset($message->recurrence->dayofweek)) {
540                                                $arrayRecur["recur_data"] = $message->recurrence->dayofweek;
541                                                /*$day = $message->recurrence->dayofweek;
542                                                 $day_of_week_counter = 0;
543                                                 if (($day & 1) > 0) $day_of_week_counter++;
544                                                 if (($day & 2) > 0) $day_of_week_counter++;
545                                                 if (($day & 4) > 0) $day_of_week_counter++;
546                                                 if (($day & 8) > 0) $day_of_week_counter++;
547                                                 if (($day & 16) > 0) $day_of_week_counter++;
548                                                 if (($day & 32) > 0) $day_of_week_counter++;
549                                                 if (($day & 64) > 0) $day_of_week_counter++;*/
550                                        }
551                                }
552                                //TODO: Converter ocurrences para until.
553                                /*if (!isset($message->recurrence->until) and (isset($message->recurrence->occurrences))) {
554                                $tz = date_default_timezone_get();
555                                date_default_timezone_set('UTC');
556                                $message->recurrence->until = strtotime(date("Y-m-d H:i:s", $message->starttime) . " +" . $message->recurrence->occurrences * $message->recurrence->interval . " " . $type_name);
557                                unset($message->recurrence->occurrences);
558                                date_default_timezone_set($tz);
559                                }*/
560                                if (isset($message->recurrence->until)){
561                                        $defaultTZ = new DateTimeZone(date_default_timezone_get());
562                                        $gmtTZ = new DateTimeZone('GMT');
563                                        $gmtDate = new DateTime("now", $gmtTZ);
564                                        $GMT_EX = $defaultTZ->getOffset($gmtDate);
565                                        $GMT_CEL = -(($tz_CEL["bias"] + $tz_CEL["dstbias"]) * 60);
566
567                                        $tz = date_default_timezone_get();
568                                        date_default_timezone_set('UTC');
569                                        $day_CEL = date("d", $message->starttime +  $GMT_CEL);
570                                        $day_EX = date("d", $message->starttime +  $GMT_EX);
571
572                                        if ($day_EX > $day_CEL) $arrayRecur["recur_enddate"] = $message->recurrence->until + $GMT_CEL - $GMT_EX + 86400;
573                                        else if ($day_EX < $day_CEL) $arrayRecur["recur_enddate"] = $message->recurrence->until + $GMT_CEL - $GMT_EX - 86400;
574                                        else $arrayRecur["recur_enddate"] = $message->recurrence->until;
575                                        date_default_timezone_set($tz);
576                                        /*                                      //$arrayRecur["recur_enddate"] = $message->recurrence->until - $offsetTZ;
577                                         $hour = date("G", $message->starttime - (($tz["bias"] - $tz["dstbias"]) * 60));
578                                         $min = date("i", $message->starttime - (($tz["bias"] - $tz["dstbias"]) * 60));
579                                         $sec = date("s", $message->starttime - (($tz["bias"] - $tz["dstbias"]) * 60));
580                                               
581                                         $arrayRecur["recur_enddate"] = $message->recurrence->until + ($hour * 3600) + ($min * 60) + $sec + $offsetTZ + (($tz["bias"] - $tz["dstbias"]) * 60);*/
582                                } else {
583                                        $arrayRecur["recur_enddate"] = 1893283200; // Se nao tem data de termino seta para 30/12/2029
584                                }
585
586                                // Trata excecoes da recorrencia
587                                if (isset($message->exceptions)){
588                                        //if ($recur_exception_changed_starttimes != "") {
589                                        //Tem Excecoes de recorrencia do tipo Edicao
590                                        // Verifica se ja existe o registro do evento de excecao de recorrencia na tabela phpgw_cal
591                                        //foreach ($array_recur_exception_changed_starttime as $except_starttime) {
592                                        $recur_exception_changed_ids = "";
593                                        $recur_exception_deleted_starttimes = "";
594                                        foreach ($message->exceptions as $recur_exception) {
595                                                if ($recur_exception->deleted == 0){ //Tem Excecoes do Tipo Edicao
596                                                        $found_recur_exception_changed = true;
597                                                        if (isset($arrayExceptCal)) unset($arrayExceptCal);
598                                                        if (isset($arrayExceptCalUser)) unset($arrayExceptCalUser);
599                                                        if (isset($except_cal_id)) unset($except_cal_id);
600                                                        if (isset($except_cal_type_from_DB)) unset($except_cal_type_from_DB);
601                                                        $found_except_cal_id = false;
602                                                        $found_except_cal_user = false;
603                                                        $result = pg_query($this->db, "select cal_id, cal_type from phpgw_cal where reference = " . $id . " and datetime = " . $recur_exception->exceptionstarttime . ";");
604                                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
605                                                        // tenta localizar cal_id para fazer Update
606                                                        while ($row = pg_fetch_row($result)) {
607                                                                if(isset($row[0])) {
608                                                                        $except_cal_id = $row[0];
609                                                                        $except_cal_type_from_DB = $row[1];
610                                                                        $found_except_cal_id = true;
611                                                                }
612                                                        }
613                                                        // Verifica se ja existe o registro do evento de excecao de recorrencia na tabela phpgw_cal_user
614                                                        if (isset($except_cal_id)) {
615                                                                $result = pg_query($this->db, "select cal_id, cal_login from phpgw_cal_user where cal_id = " . $except_cal_id . " and cal_login = '" . $id_owner . "';");
616                                                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
617                                                                if ($row = pg_fetch_row($result)) {
618                                                                        if(isset($row[0])) {
619                                                                                $found_except_cal_user = true;
620                                                                        }
621                                                                }
622                                                        }
623                                                        //Insere/Atualiza tabela phpgw_cal
624                                                        $arrayExceptCal["reference"] = $cal_id;
625                                                        if(isset($message->uid)) {
626                                                                $arrayExceptCal["uid"] = $this->truncateString(utf8_decode($message->uid),255);
627                                                        }
628                                                        if(isset($recur_exception->subject)) {
629                                                                $arrayExceptCal["title"] = $this->truncateString(utf8_decode($recur_exception->subject),80);
630                                                        } elseif(isset($message->subject)) {
631                                                                $arrayExceptCal["title"] = $this->truncateString(utf8_decode($message->subject),80);
632                                                        }
633                                                        if(isset($recur_exception->location)) {
634                                                                $arrayExceptCal["location"] = $this->truncateString(utf8_decode($recur_exception->location),255);
635                                                        } else if(isset($message->location)) {
636                                                                $arrayExceptCal["location"] = $this->truncateString(utf8_decode($message->location),255);
637                                                        }
638                                                        if(isset($recur_exception->dtstamp)) {
639                                                                $arrayExceptCal["mdatetime"] = $recur_exception->dtstamp;
640                                                        } elseif(isset($message->dtstamp)) {
641                                                                $arrayExceptCal["mdatetime"] = $message->dtstamp;
642                                                        }
643                                                        if(isset($recur_exception->starttime)) {
644                                                                $arrayExceptCal["datetime"] = $recur_exception->starttime;
645                                                        } else if(isset($message->starttime)) {
646                                                                $arrayExceptCal["datetime"] = $message->starttime;
647                                                        }
648                                                        if(isset($recur_exception->endtime)) {
649                                                                $arrayExceptCal["edatetime"] = $recur_exception->endtime;
650                                                        }elseif(isset($message->endtime)) {
651                                                                $arrayExceptCal["edatetime"] = $message->endtime;
652                                                        }
653                                                        if(isset($recur_exception->sensitivity) and $recur_exception->sensitivity == 0) {
654                                                                $arrayExceptCal["is_public"] = 1; // 1 - Normal
655                                                        }elseif (isset($recur_exception->sensitivity) and $recur_exception->sensitivity == 1){
656                                                                $arrayExceptCal["is_public"] = 0; // 0 - Privado
657                                                        }elseif(isset($message->sensitivity) and $message->sensitivity == 0) {
658                                                                $arrayExceptCal["is_public"] = 1; // 1 - Normal
659                                                        }else $arrayExceptCal["is_public"] = 0; // 0 - Privado
660                                                        if(isset($recur_exception->rtf) or isset($message->rtf)) {
661                                                                $rtf_to_ascii = new rtf();
662                                                                $rtf_to_ascii->output("ascii");
663                                                                if(isset($recur_exception->rtf)) $result_loadrtf = $rtf_to_ascii->loadrtf(base64_decode($recur_exception->rtf));
664                                                                elseif(isset($message->rtf)) $result_loadrtf = $rtf_to_ascii->loadrtf(base64_decode($message->rtf));
665                                                                if ($result_loadrtf == true) $rtf_to_ascii->parse();
666                                                                $arrayExceptCal["description"] = $rtf_to_ascii->out;
667                                //                      } else {
668                                //                              $arrayExceptCal["description"] = '';
669                                                        }
670                                                        $arrayExceptCal["category"] = '';
671                                                        if (isset($recur_exception->categories)) {
672                                                                foreach ($recur_exception->categories as $category) {
673                                                                        $cat_id = $this->addCategory($category);
674                                                                        if ($cat_id != false) {
675                                                                                if ($arrayExceptCal["category"] == '') {
676                                                                                        $arrayExceptCal["category"] = $cat_id;
677                                                                                } else {
678                                                                                        $arrayExceptCal["category"] .= ',' . $cat_id;
679                                                                                }
680                                                                        }
681                                                                }
682                                                        }elseif (isset($message->categories)) {
683                                                                foreach ($message->categories as $category) {
684                                                                        $cat_id = $this->addCategory($category);
685                                                                        if ($cat_id != false) {
686                                                                                if ($arrayExceptCal["category"] == '') {
687                                                                                        $arrayExceptCal["category"] = $cat_id;
688                                                                                } else {
689                                                                                        $arrayExceptCal["category"] .= ',' . $cat_id;
690                                                                                }
691                                                                        }
692                                                                }
693                                                        }
694                                                        //TODO: Converter eventos que sao alldayevent para iniciar as 0:00hs e terminar as 23:59hs. Isso porque o Expresso nao suporta alldayevents
695                                                        //               if(isset($message->alldayevent)) {
696                                                        //                      $arrayCal["alldayevent"] = $this->truncateString(utf8_decode($message->alldayevent),30);
697                                                        //               }
698                                                        if (!$found_except_cal_id){
699                                                                $result = pg_query("select nextval('seq_phpgw_cal');");
700                                                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
701                                                                $except_cal_id = pg_fetch_result($result, 0, 0);
702                                                                $arrayExceptCal["cal_id"] = $except_cal_id;
703                                                                $arrayExceptCal["owner"] = $id_owner;
704                                                                $arrayExceptCal["cal_type"] = 'E';
705                                                                $result = pg_insert($this->db, 'phpgw_cal', $arrayExceptCal);
706                                                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
707                                                        } else {
708                                                                $result = pg_update($this->db, 'phpgw_cal', $arrayExceptCal, array('cal_id' => $except_cal_id));
709                                                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
710                                                        }
711
712                                                        // Insere/Atualiza Excecoes de Recorrencia na tabela phpgw_cal_user
713                                                        $arrayExceptCalUser["cal_type"] = 'u';
714                                                        if (!$found_except_cal_user){
715                                                                $arrayExceptCalUser["cal_id"] = $except_cal_id;
716                                                                $arrayExceptCalUser["cal_login"] = $id_owner;
717                                                                //TODO: Tratar os outros valores de cal_status
718                                                                $arrayExceptCalUser["cal_status"] = 'A';
719                                                                $result = pg_insert($this->db, 'phpgw_cal_user', $arrayExceptCalUser);
720                                                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
721                                                        } else {
722                                                                $result = pg_update($this->db, 'phpgw_cal_user', $arrayExceptCalUser, array('cal_id' => $except_cal_id, 'cal_login' => $id_owner));
723                                                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
724                                                        }
725                                                        if ($recur_exception_changed_ids == "") {
726                                                                $recur_exception_changed_ids = $except_cal_id;
727                                                        } else {
728                                                                $recur_exception_changed_ids .= "," . $except_cal_id;
729                                                        }
730                                                } else {
731                                                        if ($recur_exception_deleted_starttimes == "") {
732                                                                $recur_exception_deleted_starttimes = $recur_exception->exceptionstarttime;
733                                                        } else {
734                                                                $recur_exception_deleted_starttimes .= "," . $recur_exception->exceptionstarttime;
735                                                        }
736                                                }
737                                        }
738                                        if ($recur_exception_deleted_starttimes != "") {
739                                                //Tem Excecoes de recorrencia do tipo Exclusao
740                                                $arrayRecur["recur_exception"] = $recur_exception_deleted_starttimes;
741                                        } else {
742                                                $arrayRecur["recur_exception"] = "";
743                                        }
744                                        if ($recur_exception_changed_ids == "") {
745                                                // Se nao tem excecoes de recorrencia do tipo edicao, entao deleta as excecoes da recorrencia
746                                                $result = pg_query($this->db, "delete from phpgw_cal_user where cal_id in (select cal_id from phpgw_cal where reference = " . $cal_id . ");");
747                                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
748                                                $result = pg_query($this->db, "delete from phpgw_cal where reference = " . $cal_id . ";");
749                                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
750                                        } else {
751                                                //Deleta as excecoes da recorrencia, exceto as inseridas/atualizadas acima
752                                                $result = pg_query($this->db, "delete from phpgw_cal_user where cal_id in (select cal_id from phpgw_cal where reference = " . $cal_id . " and cal_id not in (" . $recur_exception_changed_ids . "));");
753                                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
754                                                $result = pg_query($this->db, "delete from phpgw_cal where reference = " . $cal_id . " and cal_id not in (" . $recur_exception_changed_ids . ");");
755                                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
756                                        }
757                                } else {
758                                        // Se nao tem excecoes da recorrencia entao
759                                        $arrayRecur["recur_exception"] = "";
760                                        $result = pg_query($this->db, "delete from phpgw_cal_user where cal_id in (select cal_id from phpgw_cal where reference = " . $cal_id . ");");
761                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
762                                        $result = pg_query($this->db, "delete from phpgw_cal where reference = " . $cal_id . ";");
763                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
764
765                                }
766                                if (!$found_cal_repeats){
767                                        $arrayRecur["cal_id"] = $cal_id;
768                                        $result = pg_insert($this->db, 'phpgw_cal_repeats', $arrayRecur);
769                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
770                                } else {
771                                        $result = pg_update($this->db, 'phpgw_cal_repeats', $arrayRecur, array('cal_id' => $cal_id));
772                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
773                                }
774                        } elseif ($found_cal_id) {
775                                // Se nao tem recorrencia entao
776                                $result = pg_query($this->db, "delete from phpgw_cal_user where cal_id in (select cal_id from phpgw_cal where reference = " . $cal_id . ");");
777                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
778                                $result = pg_query($this->db, "delete from phpgw_cal where reference = " . $cal_id . ";");
779                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
780                                $result = pg_query($this->db, "delete from phpgw_cal_repeats where cal_id = " . $cal_id . ";");
781                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
782                        }
783                        $result = pg_query($this->db,"COMMIT;");
784                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
785                        //TODO: Implementar Participantes
786                } catch (Exception $e) {
787                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
788                        pg_query($this->db,"ROLLBACK;");
789                        return false;
790                }
791                return $id;
792        }
793
794        function ChangeMessage($folderid, $id, $message) {
795                debugLog('CalendarExpresso::ChangeMessage('.$folderid.', '.$id.', ..)');
796                $id = $this->ChangeMessageDAO($id, $message);
797                if ($id != false) return $this->StatMessage($folderid, $id);
798                else return false;
799        }
800
801        function MoveMessage($folderid, $id, $newfolderid) {
802                return false;
803        }
804
805        function addCategory($category) {
806                $cat_id = false;
807                $id_owner = $this->parseOwner($this->_user);
808                if ($id_owner == false) return false;
809                try {
810                        $result = pg_query($this->db,"BEGIN;");
811                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
812                        $result = pg_query("SELECT cat_id from phpgw_categories WHERE cat_owner = -1 AND cat_appname = 'calendar' AND LOWER(to_ASCII(cat_name)) = '" . trim(strtolower($this->removeAccents(utf8_decode($category)))) . "';");
813                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
814                        if (!($cat_id = pg_fetch_result($result, 0, 0))){
815                                $result = pg_query("SELECT cat_id from phpgw_categories WHERE cat_owner = " . $id_owner . " AND cat_appname = 'calendar' AND LOWER(to_ASCII(cat_name)) = '" . trim(strtolower($this->removeAccents(utf8_decode($category)))) . "';");
816                                if ($result == FALSE) throw new Exception(pg_last_error($this->db));
817                                if (!($cat_id = pg_fetch_result($result, 0, 0))){
818                                        $result = pg_query("select nextval('seq_phpgw_categories');");
819                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
820                                        $cat_id = pg_fetch_result($result, 0, 0);
821                                        $arrayCat["cat_id"] = $cat_id;
822                                        $arrayCat["cat_owner"] = $id_owner;
823                                        $arrayCat["cat_access"] = 'public';
824                                        $arrayCat["cat_appname"] = 'calendar';
825                                        $arrayCat["cat_name"] = utf8_decode($category);
826                                        $arrayCat["cat_description"] = '';
827                                        $arrayCat["cat_data"] = 'N;';
828                                        $arrayCat["last_mod"] = time();
829                                        $result = pg_insert($this->db, 'phpgw_categories', $arrayCat);
830                                        if ($result == FALSE) throw new Exception(pg_last_error($this->db));
831                                }
832                        }
833                } catch (Exception $e) {
834                        pg_query($this->db,"ROLLBACK;");
835                        debugLog("exception -> " . $e->getMessage() . " - ARQUIVO: " . $e->getFile() . " - LINHA: " . $e->getLine());
836                        return false;
837                }
838                return $cat_id;
839        }
840
841        // -----------------------------------
842
843        function escape($data){
844                if (is_array($data)) {
845                        foreach ($data as $key => $val) {
846                                $data[$key] = $this->escape($val);
847                        }
848                        return $data;
849                }
850                $data = str_replace("\r\n", "\n", $data);
851                $data = str_replace("\r", "\n", $data);
852                $data = str_replace(array('\\', ';', ',', "\n"), array('\\\\', '\\;', '\\,', '\\n'), $data);
853                //return u2wi($data);
854                return $data;
855        }
856
857        function unescape($data){
858                $data = str_replace(array('\\\\', '\\;', '\\,', '\\n','\\N'),array('\\', ';', ',', "\n", "\n"),$data);
859                return $data;
860        }
861
862        function  removeAccents($data){
863                $data = strtr($data,"ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÜÚÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùüúþÿ","aaaaaaaceeeeiiii noooooxouuutbaaaaaaaceeeeiiii nooooo/ouuuty");
864                return $data;
865        }
866
867        function truncateString($string, $size)
868        {
869                if(strlen($string) <= $size) return $string;
870                else return substr($string, 0, $size - 1);
871        }
872
873        function parseOwner($id_owner)
874        {
875                $pos = strripos($id_owner, '\\');
876                if ($pos === false) {
877                        if (strlen($id_owner) <= 2) return false;
878                        else return substr($id_owner, 0, strlen($id_owner)-2);
879                } else {
880                        if (strlen($id_owner) <= $pos + 3) {
881                                return false;
882                        } else {
883                                return substr(substr($id_owner, $pos + 1), 0, strlen(substr($id_owner, $pos + 1))-2);
884                        }
885                }
886        }
887
888        function _getGMTTZ() {
889                //$tz = array("bias" => 0, "stdbias" => 0, "dstbias" => 0, "dstendyear" => 0, "dstendmonth" => 2, "dstendday" => 0, "dstendweek" => 2, "dstendhour" => 2, "dstendminute" => 0, "dstendsecond" => 0, "dstendmillis" => 0,
890                //                              "dststartyear" => 0, "dststartmonth" =>10, "dststartday" =>0, "dststartweek" => 3, "dststarthour" => 2, "dststartminute" => 0, "dststartsecond" => 0, "dststartmillis" => 0);
891                $tz = array("bias" => 120, "stdbias" => 0, "dstbias" => -60, "dstendyear" => 0, "dstendmonth" => 2, "dstendday" => 0, "dstendweek" => 2, "dstendhour" => 2, "dstendminute" => 0, "dstendsecond" => 0, "dstendmillis" => 0,
892                                                      "dststartyear" => 0, "dststartmonth" =>10, "dststartday" =>0, "dststartweek" => 3, "dststarthour" => 2, "dststartminute" => 0, "dststartsecond" => 0, "dststartmillis" => 0);
893                return $tz;
894        }
895
896        // Unpack timezone info from Sync
897        function _getTZFromSyncBlob($data) {
898                $tz = unpack(    "lbias/a64name/vdstendyear/vdstendmonth/vdstendday/vdstendweek/vdstendhour/vdstendminute/vdstendsecond/vdstendmillis/" .
899                        "lstdbias/a64name/vdststartyear/vdststartmonth/vdststartday/vdststartweek/vdststarthour/vdststartminute/vdststartsecond/vdststartmillis/" .
900                        "ldstbias", $data);
901
902                // Make the structure compatible with class.recurrence.php
903                $tz["timezone"] = $tz["bias"];
904                $tz["timezonedst"] = $tz["dstbias"];
905
906                return $tz;
907        }
908
909        // Pack timezone info for Sync
910        function _getSyncBlobFromTZ($tz) {
911                $packed = pack("la64vvvvvvvv" . "la64vvvvvvvv" . "l",
912                $tz["bias"], "", 0, $tz["dstendmonth"], $tz["dstendday"], $tz["dstendweek"], $tz["dstendhour"], $tz["dstendminute"], $tz["dstendsecond"], $tz["dstendmillis"],
913                $tz["stdbias"], "", 0, $tz["dststartmonth"], $tz["dststartday"], $tz["dststartweek"], $tz["dststarthour"], $tz["dststartminute"], $tz["dststartsecond"], $tz["dststartmillis"],
914                $tz["dstbias"]);
915
916                return $packed;
917        }
918};
919?>
Note: See TracBrowser for help on using the repository browser.