PageRenderTime 123ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/phpical/functions/parse/end_vevent.php

https://gitlab.com/endomorphosis/yogastudio
PHP | 386 lines | 272 code | 25 blank | 89 comment | 62 complexity | 9c40dfe5458afb8f96acdf5e8ee8f774 MD5 | raw file
  1. <?php
  2. /* end_vevent.php
  3. What happens in this file:
  4. 1. Initialization: add information not present by default
  5. a. duration
  6. b. class
  7. c. uid
  8. d. adjust start_time and end_time
  9. 2. Build recur_data array
  10. 3. Add occurrences to master_array
  11. */
  12. if (!isset($start_date)) $start_date = "19700101";
  13. // CLASS support
  14. if (isset($class)) {
  15. if ($class == 'PRIVATE') {
  16. $summary ='**PRIVATE**';
  17. $description ='**PRIVATE**';
  18. } elseif ($class == 'CONFIDENTIAL') {
  19. $summary ='**CONFIDENTIAL**';
  20. $description ='**CONFIDENTIAL**';
  21. }
  22. }
  23. // make sure we have some value for $uid
  24. if (!isset($uid)) {
  25. $uid = $uid_counter;
  26. $uid_counter++;
  27. $uid_valid = false;
  28. }elseif(in_array($uid, $uid_list)) {
  29. #$uid .= $uid_counter;
  30. #$uid_counter++;
  31. }else{
  32. $uid_valid = true;
  33. }
  34. $uid_list[] = $uid;
  35. # Handle DURATION
  36. if (!isset($end_unixtime)) {
  37. if(!isset($the_duration)) $the_duration = 0;
  38. $end_unixtime = $start_unixtime + $the_duration;
  39. $end_time = date ('Hi', $end_unixtime);
  40. }
  41. # at this point $end_unixtime should be set
  42. # adjust event start and end times
  43. if (isset($start_time) && isset($end_time)) {
  44. // Mozilla style all-day events or just really long events
  45. if (($end_unixtime - $start_unixtime) > 24*60*60) {
  46. $allday_start = $start_date;
  47. $allday_end = ($start_date + 1);
  48. }
  49. }
  50. # look for events that span more than one day
  51. if (isset($start_unixtime,$end_unixtime) && date('Ymd',$start_unixtime) < date('Ymd',$end_unixtime)) {
  52. $spans_day = true;
  53. $bleed_check = (($start_unixtime - $end_unixtime) <= (60*60*24)) ? '-1' : '0';
  54. } else {
  55. $spans_day = false;
  56. $bleed_check = 0;
  57. }
  58. $length = $end_unixtime - $start_unixtime;
  59. if ($length < 0){
  60. $length = 0;
  61. $end_time = $start_time;
  62. }
  63. # get hour and minute adjusted to allowed grid times
  64. $drawKey = drawEventTimes($start_time, $end_time);
  65. preg_match ('/([0-9]{2})([0-9]{2})/', $drawKey['draw_start'], $time3);
  66. preg_match ('/([0-9]{2})([0-9]{2})/', $drawKey['draw_end'], $time4);
  67. $hour = $time3[1];
  68. $minute = $time3[2];
  69. $end_hour = $time4[1];
  70. $end_minute = $time4[2];
  71. // RECURRENCE-ID Support
  72. if (isset($recurrence_d)) {
  73. $recurrence_delete["$recurrence_d"]["$recurrence_t"] = $uid;
  74. }
  75. if (isset($allday_start) && $allday_start != '') {
  76. $hour = '-';
  77. $minute = '1';
  78. $rrule_array['START_DAY'] = $allday_start;
  79. # $rrule_array['END_DAY'] = $allday_end; # this doesn't seem to be used anywhere.
  80. # $rrule_array['END'] = 'end';
  81. $recur_start = $allday_start;
  82. $start_date = $allday_start;
  83. if (isset($allday_end)) {
  84. $diff_allday_days = dayCompare($allday_end, $allday_start);
  85. } else {
  86. $diff_allday_days = 1;
  87. }
  88. } else {
  89. $rrule_array['START_DATE'] = $start_date;
  90. $rrule_array['START_TIME'] = $start_time;
  91. $rrule_array['END_TIME'] = $end_time;
  92. # $rrule_array['END'] = 'end';
  93. }
  94. $freq_type = 'none';
  95. # Load $rrule_array
  96. foreach ($rrule_array as $key => $val) {
  97. switch($key) {
  98. case 'FREQ':
  99. switch ($val) {
  100. case 'YEARLY': $freq_type = 'year'; break;
  101. case 'MONTHLY': $freq_type = 'month'; break;
  102. case 'WEEKLY': $freq_type = 'week'; break;
  103. case 'DAILY': $freq_type = 'day'; break;
  104. case 'HOURLY': $freq_type = 'hour'; break;
  105. case 'MINUTELY': $freq_type = 'minute'; break;
  106. case 'SECONDLY': $freq_type = 'second'; break;
  107. }
  108. $recur_array[($start_date)][($hour.$minute)][$uid]['recur'][$key] = strtolower($val);
  109. break;
  110. case 'COUNT':
  111. $count = $val;
  112. $recur_array[($start_date)][($hour.$minute)][$uid]['recur'][$key] = $count;
  113. break;
  114. case 'UNTIL':
  115. # UNTIL must be in UTC
  116. $until = date("YmdHis",strtotime($val));
  117. ereg ('([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})', $until, $regs);
  118. $until_unixtime = mktime($regs[4],@$regs[5],@$regs[6],$regs[2],$regs[3],$regs[1]);
  119. $recur_array[($start_date)][($hour.$minute)][$uid]['recur'][$key] = localizeDate($dateFormat_week,$until);
  120. break;
  121. case 'INTERVAL':
  122. if ($val > 0){
  123. $interval = $val;
  124. $recur_array[($start_date)][($hour.$minute)][$uid]['recur'][$key] = $interval;
  125. }
  126. break;
  127. case 'BYSECOND':
  128. $bysecond = split (',', $val);
  129. $recur_array[($start_date)][($hour.$minute)][$uid]['recur'][$key] = $bysecond;
  130. break;
  131. case 'BYMINUTE':
  132. $byminute = split (',', $val);
  133. $recur_array[($start_date)][($hour.$minute)][$uid]['recur'][$key] = $byminute;
  134. break;
  135. case 'BYHOUR':
  136. $byhour = split (',', $val);
  137. $recur_array[($start_date)][($hour.$minute)][$uid]['recur'][$key] = $byhour;
  138. break;
  139. case 'BYDAY':
  140. $byday = split (',', $val);
  141. $recur_array[($start_date)][($hour.$minute)][$uid]['recur'][$key] = $byday;
  142. break;
  143. case 'BYMONTHDAY':
  144. $bymonthday = split (',', $val);
  145. $recur_array[($start_date)][($hour.$minute)][$uid]['recur'][$key] = $bymonthday;
  146. break;
  147. case 'BYYEARDAY':
  148. $byyearday = split (',', $val);
  149. $recur_array[($start_date)][($hour.$minute)][$uid]['recur'][$key] = $byyearday;
  150. break;
  151. case 'BYWEEKNO':
  152. $byweekno = split (',', $val);
  153. $recur_array[($start_date)][($hour.$minute)][$uid]['recur'][$key] = $byweekno;
  154. break;
  155. case 'BYMONTH':
  156. $bymonth = split (',', $val);
  157. $recur_array[($start_date)][($hour.$minute)][$uid]['recur'][$key] = $bymonth;
  158. break;
  159. case 'BYSETPOS':
  160. $bysetpos = split (',', $val);
  161. $recur_array[($start_date)][($hour.$minute)][$uid]['recur'][$key] = $bysetpos;
  162. break;
  163. case 'WKST':
  164. $wkst = $val;
  165. $recur_array[($start_date)][($hour.$minute)][$uid]['recur'][$key] = $wkst;
  166. break;
  167. }
  168. }
  169. # convert wkst to a 3 char day for strtotime to work
  170. $wkst3char = two2threeCharDays($wkst);
  171. if($current_view == 'search') $freq_type = 'none';
  172. # $recur is the recurrence info that goes into the master array for this VEVENT
  173. $recur ='';
  174. if (isset($recur_array[($start_date)][($hour.$minute)][$uid]['recur'])) $recur = $recur_array[($start_date)][($hour.$minute)][$uid]['recur'];
  175. /* ============================ Load $recur_data ============================
  176. $recur_data is an array of unix times for days of recurrences of an event. This code handles repeats at the day level or above. The next loop handles the times.
  177. RDATE is currently not supported
  178. A. Set up the time range to scan for events.
  179. If COUNT is not set (default is 1,000,000) then we don't have to start at start_date; we can start at the minimum interval for the view.
  180. variables ending in date are in phpical date format: YYYYMMDD
  181. variables ending with time are in phpical time format: HHMM
  182. variables ending in unixtime are in unixtime
  183. mArray_begin and mArray_end are set in initialization by date_range.php and may be overwritten by rss_common.php.
  184. $start_date_unixtime should be the default for starting the range. Need this for the intervals to work out (e.g. every other day, week, month etc)
  185. mArray_end should be the default for end_range unixtimes.
  186. Conditions where overwrite these:
  187. $until_unixtime < $mArray_end - stop iterating early
  188. !isset($rrule_array['FREQ']) - only iterate once, set the end_range_unixtime to the end_date_unixtime
  189. Note that start_range_unixtime and end_range_unixtime are not the same as start_date_unixtime and end_date_unixtime */
  190. $end_range_unixtime = $mArray_end+60*60*24;
  191. $start_date_unixtime = strtotime($start_date);
  192. $next_range_unixtime = $start_date_unixtime;
  193. # conditions where we don't need to iterate over the whole range
  194. # if repeating without limit, and steps are by 1, don't go back before the mArray beginning.
  195. if($count == 1000000 && $interval == 1 && $mArray_begin > $next_range_unixtime) $next_range_unixtime = $mArray_begin;
  196. # if the beginning of our range is less than the start of the item, we may as well set the range to start at start_time
  197. if ($next_range_unixtime < $start_date_unixtime) $next_range_unixtime = $start_date_unixtime;
  198. # stop at the until limit if set
  199. if(isset($until) && $end_range_unixtime > $until_unixtime) $end_range_unixtime = $until_unixtime;else $until_unixtime = $mArray_end;
  200. # more adjustments
  201. switch ($freq_type){
  202. case 'week':
  203. # need to get the first value of $next_range_unixtime onto the right day of the week
  204. $next_range_unixtime = strtotime("this ".date("D", $start_date_unixtime), $next_range_unixtime);
  205. break;
  206. case 'year':
  207. $end_range_unixtime += 366*24*60*60;
  208. break;
  209. }
  210. #nonrepeating events can stop at the first instance
  211. if(!isset($rrule_array['FREQ']) && isset($end_date)){
  212. $end_range_unixtime = strtotime($end_date);
  213. $count = 1;
  214. }
  215. /* The while loop below increments $next_range_time by $freq type. For the larger freq types, there is only
  216. one $next_range_time per repeat, but the BYXXX rules may write more than one event in that repeat cycle
  217. $next_date_time handles those instances within a $freq_type */
  218. #echo "<pre>$summary\n\tstart mArray time:".date("Ymd his",$mArray_begin)."\n\tnext_range_unixtime:".date("Ymd his",$next_range_unixtime)."\n\tend range time ".date("Ymd his",$end_range_unixtime)."\n";
  219. $recur_data = array();
  220. while ($next_range_unixtime <= $end_range_unixtime && $count > 0) {
  221. $year = date("Y", $next_range_unixtime);
  222. $month = date('m', $next_range_unixtime);
  223. $time = mktime(12,0,0,$month,date("d",$start_unixtime),$year);
  224. switch ($freq_type){
  225. case 'day':
  226. add_recur($next_range_unixtime);
  227. break;
  228. case 'week':
  229. add_recur(expand_byday($next_range_unixtime));
  230. break;
  231. case 'month':
  232. if(!empty($bymonthday)) $time = mktime(12,0,0,$month,1,$year);
  233. $times = expand_bymonthday(array($time));#echo "\n $month exp bymonthday";dump_times($times);
  234. foreach($times as $time){
  235. add_recur(expand_byday($time));
  236. }
  237. break;
  238. case 'year':
  239. $times = expand_bymonth($time); #echo "exp bymonth";dump_times($times);
  240. $times = expand_byweekno($times); #echo "exp byweekno";dump_times($times);
  241. $times = expand_byyearday($times); #echo "exp byyearday";dump_times($times);
  242. $times = expand_bymonthday($times); #echo "\nexp bymonthday";dump_times($times);
  243. foreach($times as $time){
  244. add_recur(expand_byday($time));
  245. }
  246. break;
  247. default:
  248. add_recur($start_unixtime);
  249. break 2;
  250. }
  251. $next_range_unixtime = strtotime('+'.$interval.' '.$freq_type, $next_range_unixtime); #echo "\nnext $interval $freq_type".date("Ymd",$next_range_unixtime)."\n";
  252. } #end while loop
  253. sort($recur_data);
  254. /* ============================ Use $recur_data array to write the master array ============================
  255. // This used to use 5 different blocks to write the array... can it be reduced further?
  256. For each recurrence date, an event may still cross day boundaries. So we need to loop from the start_date for that recurrence to the end of that recurrence.
  257. To generate $recur_data we mostly only paid attention to start times.
  258. Now, a single event must be split into multiple master array values for each day on which it occurs
  259. $hour and $minute are the values from the start_time, rounded to the nearest grid time.
  260. */
  261. $recur_data_hour = @substr($start_time,0,2);
  262. $recur_data_minute = @substr($start_time,2,2);
  263. if (isset($allday_start) && $allday_start != ''){
  264. $recur_data_hour = '00';
  265. $recur_data_minute = '00';
  266. }
  267. foreach($recur_data as $recur_data_unixtime) {
  268. $recur_data_year = date('Y', $recur_data_unixtime);
  269. $recur_data_month = date('m', $recur_data_unixtime);
  270. $recur_data_day = date('d', $recur_data_unixtime);
  271. $recur_data_date = $recur_data_year.$recur_data_month.$recur_data_day;
  272. # initialize the loop range to the recur date + grid time
  273. $next_range_unixtime = mktime($recur_data_hour,$recur_data_minute,0,$recur_data_month,$recur_data_day,$recur_data_year);
  274. $end_range_unixtime = $next_range_unixtime + $length;
  275. $end_date_tmp = date("Ymd",$end_range_unixtime);
  276. #echo "<pre>$summary ".date("Ymd H:i:s",$next_range_unixtime)." ".date("Ymd H:i:s",$end_range_unixtime)."\n";
  277. # default the time_key to -1 for allday events, overwrite as needed
  278. $time_key = -1;
  279. $start_unixtime_tmp = strtotime($recur_data_date.$start_time);
  280. $end_unixtime_tmp = strtotime($end_date_tmp.$end_time);
  281. while (date("Ymd", $next_range_unixtime) <= $end_date_tmp) {
  282. $this_date_tmp = date('Ymd',$next_range_unixtime);
  283. $next_range_unixtime = strtotime('+1 day',$next_range_unixtime);
  284. if (!isset($allday_start) || $allday_start == '') $time_key = $hour.$minute;
  285. $display_end_tmp = $end_hour.$end_minute;
  286. if($time_key > -1){
  287. # the day is not the first day of the recurrence
  288. if ($this_date_tmp > $recur_data_date) $time_key = '0000';
  289. # the day is not the last day of the recurrence
  290. if ($this_date_tmp < $end_date_tmp) $display_end_tmp = '2400';
  291. }
  292. if($this_date_tmp == $end_date_tmp && ($end_time == '0000')) continue;
  293. if(!isset($master_array[$this_date_tmp][$time_key][$uid]['sequence']) ||
  294. $sequence > $master_array[$this_date_tmp][$time_key][$uid]['sequence']){
  295. $master_array[$this_date_tmp][$time_key][$uid] = array (
  296. 'event_start' => $start_time, # hhmm
  297. 'event_end' => $end_time, # hhmm
  298. 'display_end' => $display_end_tmp, # hhmm display_start is $time_key
  299. 'start_unixtime' => $start_unixtime_tmp, # start unixtime for this recurrence
  300. 'end_unixtime' => $end_unixtime_tmp, # end unixtime for this recurrence
  301. 'event_text' => $summary, #
  302. 'event_length' => $length, # length in seconds
  303. 'event_overlap' => 0, # checkOverlap modifies this
  304. 'description' => $description,
  305. 'status' => $status,
  306. 'class' => $class,
  307. 'spans_day' => $spans_day,
  308. 'location' => $location,
  309. 'categories' => $vtodo_categories,
  310. 'organizer' => serialize($organizer),
  311. 'attendee' => serialize($attendee),
  312. 'calnumber' => $calnumber,
  313. 'calname' => $actual_calname,
  314. 'timezone' => $start_tz,
  315. 'other' => trim($other),
  316. 'geo' => $geo,
  317. 'url' => $url,
  318. 'sequence' => $sequence,
  319. 'recur' => $recur
  320. );
  321. }
  322. if($time_key > -1) checkOverlap($this_date_tmp, $time_key, $uid);
  323. }
  324. } # end foreach recur_data
  325. unset($recur_data);
  326. // This should remove any exdates that were missed.
  327. // Added for version 0.9.5 modified in 2.22 remove anything that doesn't have an event_start
  328. if (is_array($except_dates)) {
  329. foreach ($except_dates as $key => $value) {
  330. if (isset ($master_array[$value])){
  331. foreach ($master_array[$value] as $time => $value2){
  332. if (!isset($value2[$uid]['event_start'])){
  333. unset($master_array[$value][$time][$uid]);
  334. }
  335. }
  336. }
  337. }
  338. }
  339. // Clear event data now that it's been saved.
  340. unset($start_time, $start_time_tmp, $end_time, $end_time_tmp, $start_unixtime, $start_unixtime_tmp, $end_unixtime, $end_unixtime_tmp, $summary, $length, $description, $status, $class, $location, $organizer, $attendee);
  341. //If you want to see the values in the arrays, uncomment below.
  342. //print '<pre>';
  343. //print_r($master_array);
  344. //print_r($overlap_array);
  345. //print_r($day_array);
  346. //print_r($rrule_array);
  347. //print_r($byday_arr);
  348. //print_r($recurrence_delete);
  349. //print_r($cal_displaynames);
  350. //print_r($cal_filelist);
  351. //print_r($tz_array);
  352. //print '</pre>';
  353. ?>