PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/add-ons/ical/functions/parse/parse_tzs.php

https://github.com/jcplat/console-seolan
PHP | 94 lines | 87 code | 4 blank | 3 comment | 22 complexity | 572d5146af10d56b5681882bb33d9d2c MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. $ifile = @fopen($filename, "r");
  3. if ($ifile == FALSE) exit(error($lang['l_error_cantopen'], $filename));
  4. $nextline = fgets($ifile);
  5. if (trim($nextline) != 'BEGIN:VCALENDAR') exit(error($lang['l_error_invalidcal'], $filename));
  6. // read file in line by line
  7. // XXX end line is skipped because of the 1-line readahead
  8. $is_daylight = false;
  9. $is_std = false;
  10. while (!feof($ifile)) {
  11. $line = $nextline;
  12. $nextline = fgets($ifile, 1024);
  13. $nextline = ereg_replace("[\r\n]", "", $nextline);
  14. #handle continuation lines that start with either a space or a tab (MS Outlook)
  15. while (isset($nextline{0}) && ($nextline{0} == " " || $nextline{0} == "\t")) {
  16. $line = $line . substr($nextline, 1);
  17. $nextline = fgets($ifile, 1024);
  18. $nextline = ereg_replace("[\r\n]", "", $nextline);
  19. }
  20. $line = trim($line);
  21. switch ($line) {
  22. case 'BEGIN:VTIMEZONE':
  23. unset($tz_name, $offset_from, $offset_to, $tz_id);
  24. break;
  25. case 'BEGIN:STANDARD':
  26. unset ($offset_s);
  27. $is_std = true;
  28. $is_daylight = false;
  29. break;
  30. case 'END:STANDARD':
  31. $offset_s = $offset_to;
  32. $is_std = false;
  33. break;
  34. case 'BEGIN:DAYLIGHT':
  35. unset ($offset_d);
  36. $is_daylight = true;
  37. $is_std = false;
  38. break;
  39. case 'END:DAYLIGHT':
  40. $offset_d = $offset_to;
  41. $is_daylight = false;
  42. break;
  43. case 'END:VTIMEZONE':
  44. if (!isset($offset_d) && isset($offset_s)) $offset_d = $offset_s;
  45. $tz_array[$tz_id] = array(
  46. 0 => @$offset_s,
  47. 1 => @$offset_d,
  48. 'dt_start' => @$begin_daylight,
  49. 'st_start' => @$begin_std,
  50. 'st_name' => @$st_name,
  51. 'dt_name' => @$dt_name
  52. ); #echo "<pre>$tz_id"; print_r($tz_array[$tz_id]);echo"</pre>";
  53. break;
  54. default:
  55. unset ($field, $data, $prop_pos, $property);
  56. if (ereg ("([^:]+):(.*)", $line, $line)){
  57. $field = $line[1];
  58. $data = $line[2];
  59. $property = strtoupper($field);
  60. $prop_pos = strpos($property,';');
  61. if ($prop_pos !== false) $property = substr($property,0,$prop_pos);
  62. switch ($property) {
  63. case 'TZID':
  64. $tz_id = $data;
  65. break;
  66. case 'TZOFFSETFROM':
  67. $offset_from = $data;
  68. break;
  69. case 'TZOFFSETTO':
  70. $offset_to = $data;
  71. break;
  72. case 'DTSTART':
  73. if($is_std || $is_daylight){
  74. $datetime = extractDateTime($data, $property, $field);
  75. $start_unixtime = $datetime[0];
  76. $start_date = $datetime[1];
  77. $year = substr($start_date,0,4);
  78. if($is_std) $begin_std[$year] = $data;
  79. if($is_daylight) $begin_daylight[$year] = $data;
  80. }
  81. break;
  82. case 'TZNAME':
  83. if($is_std) $st_name = $data;
  84. if($is_daylight) $dt_name = $data;
  85. break;
  86. }
  87. }
  88. }
  89. }
  90. ?>