dolibarr  7.0.0-beta
ical.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2006 Roman Ozana <ozana@omdesign.cz>
3  * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
4  * Copyright (C) 2013-2014 Laurent Destailleur <eldy@users.sourceforge.net>
5  * Copyright (C) 2012 Regis Houssin <regis.houssin@capnetworks.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
26 require_once DOL_DOCUMENT_ROOT.'/core/lib/xcal.lib.php';
27 
28 
32 class ICal
33 {
34  var $file_text; // Text in file
35  var $cal; // Array to save iCalendar parse data
36  var $event_count; // Number of Events
37  var $todo_count; // Number of Todos
38  var $freebusy_count; // Number of Freebusy
39  var $last_key; //Help variable save last key (multiline string)
40 
41 
45  public function __construct()
46  {
47 
48  }
49 
56  function read_file($file)
57  {
58  $this->file = $file;
59  $file_text='';
60 
61  $tmparray=file($file);
62  if (is_array($tmparray))
63  {
64  $file_text = join("", $tmparray); //load file
65  $file_text = preg_replace("/[\r\n]{1,} /","",$file_text);
66  }
67  return $file_text; // return all text
68  }
69 
75  function get_event_count()
76  {
77  return $this->event_count;
78  }
79 
85  function get_todo_count()
86  {
87  return $this->todo_count;
88  }
89 
96  function parse($uri)
97  {
98  $this->cal = array(); // new empty array
99 
100  $this->event_count = -1;
101 
102  // read FILE text
103  $this->file_text = $this->read_file($uri);
104 
105  $this->file_text = preg_split("[\n]", $this->file_text);
106 
107  // is this text vcalendar standart text ? on line 1 is BEGIN:VCALENDAR
108  if (!stristr($this->file_text[0],'BEGIN:VCALENDAR')) return 'error not VCALENDAR';
109 
110  $insidealarm=0;
111  $tmpkey='';$tmpvalue=''; $type='';
112  foreach ($this->file_text as $text)
113  {
114  $text = trim($text); // trim one line
115  if (!empty($text))
116  {
117  // get Key and Value VCALENDAR:Begin -> Key = VCALENDAR, Value = begin
118  list($key, $value) = $this->retun_key_value($text);
119  //var_dump($text.' -> '.$key.' - '.$value);
120 
121  switch ($text) // search special string
122  {
123  case "BEGIN:VTODO":
124  $this->todo_count = $this->todo_count+1; // new to do begin
125  $type = "VTODO";
126  break;
127 
128  case "BEGIN:VEVENT":
129  $this->event_count = $this->event_count+1; // new event begin
130  $type = "VEVENT";
131  break;
132 
133  case "BEGIN:VFREEBUSY":
134  $this->freebusy_count = $this->freebusy_count+1; // new event begin
135  $type = "VFREEBUSY";
136  break;
137 
138  case "BEGIN:VCALENDAR": // all other special string
139  case "BEGIN:DAYLIGHT":
140  case "BEGIN:VTIMEZONE":
141  case "BEGIN:STANDARD":
142  $type = $value; // save array under value key
143  break;
144 
145  case "END:VTODO": // end special text - goto VCALENDAR key
146  case "END:VEVENT":
147  case "END:VFREEBUSY":
148 
149  case "END:VCALENDAR":
150  case "END:DAYLIGHT":
151  case "END:VTIMEZONE":
152  case "END:STANDARD":
153  $type = "VCALENDAR";
154  break;
155 
156  // Manage VALARM that are inside a VEVENT to avoid fields of VALARM to overwrites fields of VEVENT
157  case "BEGIN:VALARM":
158  $insidealarm=1;
159  break;
160  case "END:VALARM":
161  $insidealarm=0;
162  break;
163 
164  default: // no special string (SUMMARY, DESCRIPTION, ...)
165  if ($tmpvalue)
166  {
167  $tmpvalue .= $text;
168  if (! preg_match('/=$/',$text)) // No more lines
169  {
170  $key=$tmpkey;
171  $value=quotedPrintDecode(preg_replace('/^ENCODING=QUOTED-PRINTABLE:/i','',$tmpvalue));
172  $tmpkey='';
173  $tmpvalue='';
174  }
175  }
176  elseif (preg_match('/^ENCODING=QUOTED-PRINTABLE:/i',$value))
177  {
178  if (preg_match('/=$/',$value))
179  {
180  $tmpkey=$key;
181  $tmpvalue=$tmpvalue.preg_replace('/=$/',"",$value); // We must wait to have next line to have complete message
182  }
183  else
184  {
185  $value=quotedPrintDecode(preg_replace('/^ENCODING=QUOTED-PRINTABLE:/i','',$tmpvalue.$value));
186  }
187  } //$value=quotedPrintDecode($tmpvalue.$value);
188  if (! $insidealarm && ! $tmpkey) $this->add_to_array($type, $key, $value); // add to array
189  break;
190  }
191  }
192  }
193 
194  //var_dump($this->cal);
195  return $this->cal;
196  }
197 
206  function add_to_array($type, $key, $value)
207  {
208 
209  //print 'type='.$type.' key='.$key.' value='.$value.'<br>'."\n";
210 
211  if (empty($key))
212  {
213  $key = $this->last_key;
214  switch ($type)
215  {
216  case 'VEVENT': $value = $this->cal[$type][$this->event_count][$key].$value;break;
217  case 'VFREEBUSY': $value = $this->cal[$type][$this->freebusy_count][$key].$value;break;
218  case 'VTODO': $value = $this->cal[$type][$this->todo_count][$key].$value;break;
219  }
220  }
221 
222  if (($key == "DTSTAMP") || ($key == "LAST-MODIFIED") || ($key == "CREATED")) $value = $this->ical_date_to_unix($value);
223  //if ($key == "RRULE" ) $value = $this->ical_rrule($value);
224 
225  if (stristr($key,"DTSTART") || stristr($key,"DTEND") || stristr($key,"DTSTART;VALUE=DATE") || stristr($key,"DTEND;VALUE=DATE"))
226  {
227  if (stristr($key,"DTSTART;VALUE=DATE") || stristr($key,"DTEND;VALUE=DATE"))
228  {
229  list($key,$value) = array($key,$value);
230  }
231  else
232  {
233  list($key,$value) = $this->ical_dt_date($key,$value);
234  }
235  }
236 
237  switch ($type)
238  {
239  case "VTODO":
240  $this->cal[$type][$this->todo_count][$key] = $value;
241  break;
242 
243  case "VEVENT":
244  $this->cal[$type][$this->event_count][$key] = $value;
245  break;
246 
247  case "VFREEBUSY":
248  $this->cal[$type][$this->freebusy_count][$key] = $value;
249  break;
250 
251  default:
252  $this->cal[$type][$key] = $value;
253  break;
254  }
255  $this->last_key = $key;
256  }
257 
264  function retun_key_value($text)
265  {
266  /*
267  preg_match("/([^:]+)[:]([\w\W]+)/", $text, $matches);
268 
269  if (empty($matches))
270  {
271  return array(false,$text);
272  }
273  else
274  {
275  $matches = array_splice($matches, 1, 2);
276  return $matches;
277  }*/
278  return explode(':',$text,2);
279  }
280 
287  function ical_rrule($value)
288  {
289  $result=array();
290  $rrule = explode(';',$value);
291  foreach ($rrule as $line)
292  {
293  $rcontent = explode('=', $line);
294  $result[$rcontent[0]] = $rcontent[1];
295  }
296  return $result;
297  }
304  function ical_date_to_unix($ical_date)
305  {
306  $ical_date = str_replace('T', '', $ical_date);
307  $ical_date = str_replace('Z', '', $ical_date);
308 
309  $ntime=0;
310  // TIME LIMITED EVENT
311  if (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{0,2})([0-9]{0,2})([0-9]{0,2})/', $ical_date, $date))
312  $ntime=dol_mktime($date[4], $date[5], $date[6], $date[2],$date[3], $date[1], true);
313 
314  //if (empty($date[4])) print 'Error bad date: '.$ical_date.' - date1='.$date[1];
315  //print dol_print_date($ntime,'dayhour');exit;
316  return $ntime; // ntime is a GTM time
317  }
318 
326  function ical_dt_date($key, $value)
327  {
328  $return_value=array();
329  $value = $this->ical_date_to_unix($value);
330 
331  // Analyse TZID
332  $temp = explode(";",$key);
333 
334  if (empty($temp[1])) // not TZID
335  {
336  $value = str_replace('T', '', $value);
337  return array($key,$value);
338  }
339 
340  $key = $temp[0];
341  $temp = explode("=", $temp[1]);
342  $return_value[$temp[0]] = $temp[1];
343  $return_value['unixtime'] = $value;
344 
345  return array($key,$return_value);
346  }
347 
354  {
355  $temp = $this->get_event_list();
356  if (!empty($temp))
357  {
358  usort($temp, array(&$this, "ical_dtstart_compare"));
359  return $temp;
360  }
361  else
362  {
363  return false;
364  }
365  }
366 
374  function ical_dtstart_compare($a, $b)
375  {
376  return strnatcasecmp($a['DTSTART']['unixtime'], $b['DTSTART']['unixtime']);
377  }
378 
384  function get_event_list()
385  {
386  return (! empty($this->cal['VEVENT'])?$this->cal['VEVENT']:'');
387  }
388 
394  function get_freebusy_list()
395  {
396  return $this->cal['VFREEBUSY'];
397  }
398 
404  function get_todo_list()
405  {
406  return $this->cal['VTODO'];
407  }
408 
414  function get_calender_data()
415  {
416  return $this->cal['VCALENDAR'];
417  }
418 
424  function get_all_data()
425  {
426  return $this->cal;
427  }
428 }
ical_rrule($value)
Parse RRULE return array.
Definition: ical.class.php:287
ical_date_to_unix($ical_date)
Return Unix time from ical date time fomrat (YYYYMMDD[T]HHMMSS[Z] or YYYYMMDD[T]HHMMSS) ...
Definition: ical.class.php:304
get_calender_data()
Return base calendar data.
Definition: ical.class.php:414
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm=false, $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
ical_dt_date($key, $value)
Return unix date from iCal date format.
Definition: ical.class.php:326
add_to_array($type, $key, $value)
Add to $this->ical array one value and key.
Definition: ical.class.php:206
ical_dtstart_compare($a, $b)
Compare two unix timestamp.
Definition: ical.class.php:374
__construct()
Constructor.
Definition: ical.class.php:45
retun_key_value($text)
Parse text "XXXX:value text some with : " and return array($key = "XXXX", $value="value");.
Definition: ical.class.php:264
get_all_data()
Return array with all data.
Definition: ical.class.php:424
Class to read/parse ICal calendars.
Definition: ical.class.php:32
get_todo_list()
Return to do array (not sort to do array)
Definition: ical.class.php:404
quotedPrintDecode($str)
Decode vcal format.
Definition: xcal.lib.php:517
read_file($file)
Read text file, icalender text file.
Definition: ical.class.php:56
get_freebusy_list()
Return eventlist array (not sort eventlist array)
Definition: ical.class.php:394
get_todo_count()
Returns the number of to do.
Definition: ical.class.php:85
parse($uri)
Translate Calendar.
Definition: ical.class.php:96
get_event_count()
Returns the number of calendar events.
Definition: ical.class.php:75
get_sort_event_list()
Return sorted eventlist as array or false if calenar is empty.
Definition: ical.class.php:353
get_event_list()
Return eventlist array (not sort eventlist array)
Definition: ical.class.php:384