PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/comm/action/class/ical.class.php

https://github.com/asterix14/dolibarr
PHP | 383 lines | 210 code | 47 blank | 126 comment | 20 complexity | d369c51391aefd799be02a818014536d MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2006 Roman Ozana <ozana@omdesign.cz>
  3. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  4. * Copyright (C) 2011 Laurent Destailleur <eldy@users.sourceforge.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/comm/action/class/ical.class.php
  21. * \ingroup commercial
  22. * \brief File of class to parse ical calendars
  23. */
  24. class ical
  25. {
  26. var $file_text; // Text in file
  27. var $cal; // Array to save iCalendar parse data
  28. var $event_count; // Number of Events
  29. var $todo_count; // Number of Todos
  30. var $last_key; //Help variable save last key (multiline string)
  31. /**
  32. * Constructor
  33. */
  34. public function ical()
  35. {
  36. }
  37. /**
  38. * Read text file, icalender text file
  39. *
  40. * @param string $file
  41. * @return string
  42. */
  43. function read_file($file)
  44. {
  45. $this->file = $file;
  46. $file_text = join ("", file ($file)); //load file
  47. $file_text = preg_replace("/[\r\n]{1,} ([:;])/","\\1",$file_text);
  48. return $file_text; // return all text
  49. }
  50. /**
  51. * Returns the number of calendar events
  52. *
  53. * @return int
  54. */
  55. function get_event_count()
  56. {
  57. return $this->event_count;
  58. }
  59. /**
  60. * Returns the number of ToDo
  61. *
  62. * @return int
  63. */
  64. function get_todo_count()
  65. {
  66. return $this->todo_count;
  67. }
  68. /**
  69. * Translate Calendar
  70. *
  71. * @param string $uri
  72. * @return array
  73. */
  74. function parse($uri)
  75. {
  76. $this->cal = array(); // new empty array
  77. $this->event_count = -1;
  78. // read FILE text
  79. $this->file_text = $this->read_file($uri);
  80. $this->file_text = preg_split("[\n]", $this->file_text);
  81. // is this text vcalendar standart text ? on line 1 is BEGIN:VCALENDAR
  82. if (!stristr($this->file_text[0],'BEGIN:VCALENDAR')) return 'error not VCALENDAR';
  83. $insidealarm=0;
  84. foreach ($this->file_text as $text)
  85. {
  86. $text = trim($text); // trim one line
  87. if (!empty($text))
  88. {
  89. // get Key and Value VCALENDAR:Begin -> Key = VCALENDAR, Value = begin
  90. list($key, $value) = $this->retun_key_value($text);
  91. switch ($text) // search special string
  92. {
  93. case "BEGIN:VTODO":
  94. $this->todo_count = $this->todo_count+1; // new todo begin
  95. $type = "VTODO";
  96. break;
  97. case "BEGIN:VEVENT":
  98. $this->event_count = $this->event_count+1; // new event begin
  99. $type = "VEVENT";
  100. break;
  101. case "BEGIN:VFREEBUSY":
  102. $this->freebusy_count = $this->freebusy_count+1; // new event begin
  103. $type = "VFREEBUSY";
  104. break;
  105. case "BEGIN:VCALENDAR": // all other special string
  106. case "BEGIN:DAYLIGHT":
  107. case "BEGIN:VTIMEZONE":
  108. case "BEGIN:STANDARD":
  109. $type = $value; // save tu array under value key
  110. break;
  111. case "END:VTODO": // end special text - goto VCALENDAR key
  112. case "END:VEVENT":
  113. case "END:VFREEBUSY":
  114. case "END:VCALENDAR":
  115. case "END:DAYLIGHT":
  116. case "END:VTIMEZONE":
  117. case "END:STANDARD":
  118. $type = "VCALENDAR";
  119. break;
  120. // Manage VALARM that are inside a VEVENT to avoid fields of VALARM to overwrites fields of VEVENT
  121. case "BEGIN:VALARM":
  122. $insidealarm=1;
  123. break;
  124. case "END:VALARM":
  125. $insidealarm=0;
  126. break;
  127. default: // no special string
  128. if (! $insidealarm) $this->add_to_array($type, $key, $value); // add to array
  129. break;
  130. }
  131. }
  132. }
  133. return $this->cal;
  134. }
  135. /**
  136. * Add to $this->ical array one value and key. Type is VTODO, VEVENT, VFREEBUSY, VCALENDAR ... .
  137. *
  138. * @param string $type
  139. * @param string $key
  140. * @param string $value
  141. */
  142. function add_to_array($type, $key, $value)
  143. {
  144. //print 'type='.$type.' key='.$key.' value='.$value.'<br>'."\n";
  145. if ($key == false)
  146. {
  147. $key = $this->last_key;
  148. switch ($type)
  149. {
  150. case 'VEVENT': $value = $this->cal[$type][$this->event_count][$key].$value;break;
  151. case 'VFREEBUSY': $value = $this->cal[$type][$this->freebusy_count][$key].$value;break;
  152. case 'VTODO': $value = $this->cal[$type][$this->todo_count][$key].$value;break;
  153. }
  154. }
  155. if (($key == "DTSTAMP") or ($key == "LAST-MODIFIED") or ($key == "CREATED")) $value = $this->ical_date_to_unix($value);
  156. if ($key == "RRULE" ) $value = $this->ical_rrule($value);
  157. if (stristr($key,"DTSTART") or stristr($key,"DTEND") or stristr($key,"DTSTART;VALUE=DATE") or stristr($key,"DTEND;VALUE=DATE"))
  158. {
  159. if (stristr($key,"DTSTART;VALUE=DATE") or stristr($key,"DTEND;VALUE=DATE"))
  160. {
  161. list($key,$value) = array($key,$value);
  162. }
  163. else
  164. {
  165. list($key,$value) = $this->ical_dt_date($key,$value);
  166. }
  167. }
  168. switch ($type)
  169. {
  170. case "VTODO":
  171. $this->cal[$type][$this->todo_count][$key] = $value;
  172. break;
  173. case "VEVENT":
  174. $this->cal[$type][$this->event_count][$key] = $value;
  175. break;
  176. case "VFREEBUSY":
  177. $this->cal[$type][$this->freebusy_count][$key] = $value;
  178. break;
  179. default:
  180. $this->cal[$type][$key] = $value;
  181. break;
  182. }
  183. $this->last_key = $key;
  184. }
  185. /**
  186. * Parse text "XXXX:value text some with : " and return array($key = "XXXX", $value="value");
  187. *
  188. * @param string $text
  189. * @return array
  190. */
  191. function retun_key_value($text)
  192. {
  193. preg_match("/([^:]+)[:]([\w\W]+)/", $text, $matches);
  194. if (empty($matches))
  195. {
  196. return array(false,$text);
  197. }
  198. else
  199. {
  200. $matches = array_splice($matches, 1, 2);
  201. return $matches;
  202. }
  203. }
  204. /**
  205. * Parse RRULE return array
  206. *
  207. * @param string $value
  208. * @return array
  209. */
  210. function ical_rrule($value)
  211. {
  212. $rrule = explode(';',$value);
  213. foreach ($rrule as $line)
  214. {
  215. $rcontent = explode('=', $line);
  216. $result[$rcontent[0]] = $rcontent[1];
  217. }
  218. return $result;
  219. }
  220. /**
  221. * Return Unix time from ical date time fomrat (YYYYMMDD[T]HHMMSS[Z] or YYYYMMDD[T]HHMMSS)
  222. *
  223. * @param unknown_type $ical_date
  224. * @return timestamp
  225. */
  226. function ical_date_to_unix($ical_date)
  227. {
  228. $ical_date = str_replace('T', '', $ical_date);
  229. $ical_date = str_replace('Z', '', $ical_date);
  230. $ntime=0;
  231. // TIME LIMITED EVENT
  232. 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))
  233. $ntime=dol_mktime($date[4], $date[5], $date[6], $date[2],$date[3], $date[1], true);
  234. //if (empty($date[4])) print 'Error bad date: '.$ical_date.' - date1='.$date[1];
  235. //print dol_print_date($ntime,'dayhour');exit;
  236. return $ntime; // ntime is a GTM time
  237. }
  238. /**
  239. * Return unix date from iCal date format
  240. *
  241. * @param string $key
  242. * @param string $value
  243. * @return array
  244. */
  245. function ical_dt_date($key, $value)
  246. {
  247. $value = $this->ical_date_to_unix($value);
  248. // Analyse TZID
  249. $temp = explode(";",$key);
  250. if (empty($temp[1])) // not TZID
  251. {
  252. $value = str_replace('T', '', $value);
  253. return array($key,$value);
  254. }
  255. // adding $value and $tzid
  256. $key = $temp[0];
  257. $temp = explode("=", $temp[1]);
  258. $return_value[$temp[0]] = $temp[1];
  259. $return_value['unixtime'] = $value;
  260. return array($key,$return_value);
  261. }
  262. /**
  263. * Return sorted eventlist as array or false if calenar is empty
  264. *
  265. * @return array
  266. */
  267. function get_sort_event_list()
  268. {
  269. $temp = $this->get_event_list();
  270. if (!empty($temp))
  271. {
  272. usort($temp, array(&$this, "ical_dtstart_compare"));
  273. return $temp;
  274. } else
  275. {
  276. return false;
  277. }
  278. }
  279. /**
  280. * Compare two unix timestamp
  281. *
  282. * @param array $a
  283. * @param array $b
  284. * @return integer
  285. */
  286. function ical_dtstart_compare($a, $b)
  287. {
  288. return strnatcasecmp($a['DTSTART']['unixtime'], $b['DTSTART']['unixtime']);
  289. }
  290. /**
  291. * Return eventlist array (not sort eventlist array)
  292. *
  293. * @return array
  294. */
  295. function get_event_list()
  296. {
  297. return $this->cal['VEVENT'];
  298. }
  299. /**
  300. * Return eventlist array (not sort eventlist array)
  301. *
  302. * @return array
  303. */
  304. function get_freebusy_list()
  305. {
  306. return $this->cal['VFREEBUSY'];
  307. }
  308. /**
  309. * Return todo array (not sort todo array)
  310. *
  311. * @return array
  312. */
  313. function get_todo_list()
  314. {
  315. return $this->cal['VTODO'];
  316. }
  317. /**
  318. * Return base calendar data
  319. *
  320. * @return array
  321. */
  322. function get_calender_data()
  323. {
  324. return $this->cal['VCALENDAR'];
  325. }
  326. /**
  327. * Return array with all data
  328. *
  329. * @return array
  330. */
  331. function get_all_data()
  332. {
  333. return $this->cal;
  334. }
  335. }
  336. ?>