aboutsummaryrefslogtreecommitdiffstats
path: root/ical_parser.php
blob: a6409053977f3ccbc5ed06ed3cbdf75b0e1b5e7b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
<?php
// note from Jared: the _time suffix has been applied to all variables 
// that are timestamps to distinguish between them and Ymd format
// I did not change other variables to use this convention yet

// I started commenting the line above where $master_array gets written to
// I did this because I kept scrolling through looking for them so I decided to mark them

include("./init.inc.php");
include("./functions/date_add.php");
include("./functions/date_functions.php");
include("./functions/draw_functions.php");
include("./functions/overlapping_events.php");

$fillTime = $day_start;
$day_array = array ();
while ($fillTime != "2400") {
	array_push ($day_array, $fillTime);
	ereg ("([0-9]{2})([0-9]{2})", $fillTime, $dTime);
	$fill_h = $dTime[1];
	$fill_min = $dTime[2];
	$fill_min = sprintf("%02d", $fill_min + $gridLength);
	if ($fill_min == 60) {
		$fill_h = sprintf("%02d", ($fill_h + 1));
		$fill_min = "00";
	}
	$fillTime = $fill_h . $fill_min;
}


// what date we want to get data for (for day calendar)
if (!$getdate) $getdate = date("Ymd");
ereg ("([0-9]{4})([0-9]{2})([0-9]{2})", $getdate, $day_array2);
$this_day = $day_array2[3];
$this_month = $day_array2[2];
$this_year = $day_array2[1];

// Start the session
//session_start();
//if (($aYear != $this_year) || ($use_sessions != "yes") || (!is_array($aArray))) {
//echo "not using sessions";


// open the iCal file, read it into a string
$fp = @fopen($filename, "r");
$contents = @fread ($fp, filesize ($filename));
@fclose ($fp);


// turn that string into an array
$contents = ereg_replace("\n ", "", $contents);
$contents = split ("\n", $contents);

// auxiliary array for determining overlaps of events
$overlap_array = array ();

// parse our new array
foreach($contents as $line) {
	if (strstr($line, "BEGIN:VEVENT")) {
		$start_time = "";
		$end_time = "";
		$end_day = "";
		$summary = "";
		$allday_start = "";
		$allday_end = "";
		$start = "";
		$end = "";
		$the_duration = "";
		$beginning = "";
		$rrule_array = "";
		$start_of_vevent = "";
		$end_of_vevent = "";
		$interval = "";
		$number = "";
		$except_dates = array();
		$except_times = array();
		$first_duration = TRUE;
		$bymonthday = "";
		$count = 1000000;
	} elseif (strstr($line, "END:VEVENT")) {
		
		// Clean out \n's and other slashes
		$summary = str_replace("\\n", "<br>", $summary);
		$summary = stripslashes($summary);
		$description = str_replace("\\n", "<br>", $description);
		$mArray_begin = mktime (0,0,0,1,1,$this_year);
		$mArray_end = mktime (0,0,0,1,10,($this_year + 1));
				
		if ($start_time != "") {
			ereg ("([0-9]{2})([0-9]{2})", $start_time, $time);
			ereg ("([0-9]{2})([0-9]{2})", $end_time, $time2);
			$length = ($time2[1]*60+$time2[2]) - ($time[1]*60+$time[2]);
			
			$drawKey = drawEventTimes($start_time, $end_time);
			ereg ("([0-9]{2})([0-9]{2})", $drawKey["draw_start"], $time3);
			$hour = $time3[1];
			$minute = $time3[2];
		}
		
		
		// Handling of the all day events	
		if (($allday_start != "") && ($rrule_array == "")) {
			$start = strtotime("$allday_start");
			$end = strtotime("$allday_end");
			if (($end > $mArray_begin) && ($end < $mArray_end)) {
				while ($start != $end) {
					$start_date = date("Ymd", $start);
					$master_array[($start_date)][("-1")][]= array ("event_text" => "$summary", "description" => $description);
					$start = strtotime("+1 day", $start);
				}
			}
		}
		
		
		// Handling of the recurring events, RRULE
		// This will be quite a bit of work, thats for sure.
		if (is_array($rrule_array)) {
			if ($allday_start != "") {
				$rrule_array["START_DAY"] = $allday_start;
				$rrule_array["END_DAY"] = $allday_end;
				$rrule_array["END"] = "end";
				$recur_start = $allday_start;
			} else {
				$rrule_array["START_DATE"] = $start_date;
				$rrule_array["START_TIME"] = $start_time;
				$rrule_array["END_TIME"] = $end_time;
				$rrule_array["END"] = "end";
			}
			//print_r($rrule_array);
			foreach ($rrule_array as $key => $val) {
				if ($key == "FREQ") {
					if ($val == "YEARLY") {
						$interval = "yyyy";
					} elseif ($val == "MONTHLY") {
						$interval = "m";
					} elseif ($val == "WEEKLY") {
						$interval = "ww";
					} elseif ($val == "DAILY") {
						$interval = "d";
					} elseif ($val == "HOURLY") {
						$interval = "h";
					} elseif ($val == "MINUTELY") {
						$interval = "n";
					} elseif ($val == "SECONDLY") {
						$interval = "s";
					}		
				} elseif ($key == "COUNT") 		{
					$count = $val;
				
				} elseif ($key == "UNTIL") 		{
					$until = ereg_replace("T", "", $val);
					ereg ("([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{0,2})([0-9]{0,2})", $until, $regs);
					$year = $regs[1];
					$month = $regs[2];
					$day = $regs[3];
					$until = strtotime("$year$month$day");
					
				} elseif ($key == "INTERVAL")	{
					$number = $val;
				
				} elseif ($key == "BYSECOND") 	{
					$bysecond = $val;
					$bysecond = split (",", $bysecond);
				
				} elseif ($key == "BYMINUTE") 	{
					$byminute = $val;
					$byminute = split (",", $byminute);
				
				} elseif ($key == "BYHOUR")		{
					$byhour = $val;
					$byhour = split (",", $byhour);
				
				} elseif ($key == "BYDAY") 		{
					$byday = $val;
					$byday = split (",", $byday);
				
				} elseif ($key == "BYMONTHDAY") {
					$bymonthday = $val;
					$bymonthday = split (",", $bymonthday);
					//print_r ($bymonthday);
				
				} elseif ($key == "BYYEARDAY") 	{
					$byyearday = $val;
					$byyearday = split (",", $byyearday);
				
				} elseif ($key == "BYWEEKNO") 	{
					$byweekno = $val;
					$byweekno = split (",", $byweekno);
				
				} elseif ($key == "BYMONTH") 	{
					$bymonth = $val;
					$bymonth = split (",", $bymonth);
				
				} elseif ($key == "BYSETPOS") 	{
					$bysetpos = $val;
				
				} elseif ($key == "WKST") 		{
					$wkst = $val;
				
				} elseif ($key == "END")		{
					
					if ($allday_start != "") {
						
						// Since we hit the end of the RRULE array, lets do something.
						// Below handles yearly, montly, weekly, daily all-day events.
						// $start_of_vevent is the date the recurring event starts.
						// $end_of_vevent is the date the recurring event stops.
						// $count and $count_to check for the COUNT feature
						// $until checks for the UNTIL feature, makes sure we don't recur past it.
						 
						$start_of_vevent = strtotime("$allday_start");
						$end_of_vevent = strtotime("$allday_end");
						$count_to = 0;
						if (!$until) $until = $mArray_end;
						if ($start_of_vevent < $mArray_end) {
							do {
								// This steps through each day of a multiple all-day event and adds to master array
								// Every all day event should pass through here at least once if its recurring.
								$start = $start_of_vevent;
								$end = $end_of_vevent;
								while ($start != $end) {
									$start_date = date("Ymd", $start);
									if (($end > $mArray_begin) && ($end < $mArray_end)) {
										$master_array[($start_date)][("-1")][]= array ("event_text" => "$summary", "description" => $description);
									}
									$start = strtotime("+1 day", $start);
								}
								$start_of_vevent = DateAdd ($interval,  $number, $start_of_vevent);
								$end_of_vevent = DateAdd ($interval,  $number, $end_of_vevent);
								$count_to++;
							} while (($start_of_vevent < $mArray_end) && ($count != $count_to) && ($start_of_vevent < $until)); 
						}
					
					// Let's take care of recurring events that are not all day events
					// DAILY and WEEKLY recurrences seem to work fine. Need feedback.
					// Known bug, doesn't look at UNTIL or COUNT yet.
					} else {
					
						// again, $parse_to_year is set to January 10 of the upcoming year
						$parse_to_year_time  = mktime(0,0,0,1,10,($this_year + 1));
						$start_date_time = strtotime($start_date);
						
						// depending on which view we're looking at, we do one month or one week
						// one day is more difficult, I think, so I wrapped that into the week. We'll have to
						// add another case for "year" once that's added.
						if ($current_view == "month") {
							$start_range_time = strtotime("$this_year-$this_month-01");
							$end_range_time = strtotime("+1 month +1 week", $start_range_time);
						} else {
							$start_range_time = strtotime(dateOfWeek($getdate, substr($week_start_day, 0, 2)));
							$end_range_time = strtotime("+1 week", $start_range_time);
						}
						
						// if $until isn't set yet, we set it to the end of our range we're looking at
						if (!$until) $until = $end_range_time;
						$end_date_time = $until;
						
						// If the $end_range_time is less than the $start_date_time, or $start_range_time is greater
						// than $end_date_time, we may as well forget the whole thing
						// It doesn't do us any good to spend time adding data we aren't even looking at
						// this will prevent the year view from taking way longer than it needs to
						if ($end_range_time >= $start_date_time && $start_range_time <= $end_date_time) {
						
							// if the beginning of our range is less than the start of the item, we may as well set it equal to it
							if ($start_range_time < $start_date_time) $start_range_time = $start_date_time;
							if ($end_range_time > $end_date_time) $end_range_time = $end_date_time;
				
							// initialze the time we will increment
							$next_range_time = $start_range_time;
							
							$count_to = 0;
							// start at the $start_range and go until we hit the end of our range.
							while (($next_range_time >= $start_range_time) && ($next_range_time <= $end_range_time) && ($count_to != $count)) {
								
								// handling WEEKLY events here
								if ($rrule_array["FREQ"] == "WEEKLY") {

									// use weekCompare to see if we even have this event this week
									$diff_weeks = weekCompare(date("Ymd",$next_range_time), $start_date);
									if ($diff_weeks < $count) {
										if ($week_diff % $number == 0) {
											$interval = $number;
											// loop through the days on which this event happens
											foreach($byday as $day) {
											
												// use my fancy little function to get the date of each day
												$next_date = dateOfWeek(date("Ymd", $next_range_time),$day);
												$next_date_time = strtotime($next_date);
												
												if (($next_date_time > $start_date_time) && ($next_date_time <= $end_date_time) && ($count_to != $count) && !in_array($next_date, $except_dates)) {
													// add it to the array if it passes inspection, it allows the first time to be
													// written by the master data writer (hence the > instead of >=) otherwise we can special case these
													// before, the first one would get entered twice and show up twice
													// $next_date can fall up to a week behind $next_range_time because of how dateOfWeek works
													// so we have to check this again. It uses $except_dates so it doesn't add to $master_array
													// on days that have been deleted by the user
// check for overlapping events
													$nbrOfOverlaps = checkOverlap();
// writes to $master array here
													$master_array[($next_date)][($hour.$minute)][] = array ("event_start" => $start_time, "event_text" => $summary, "event_end" => $end_time, "event_length" => $length, "event_overlap" => $nbrOfOverlaps, "description" => $description);
												}
											}
										} else {
											$interval = 1;
										}
										$next_range_time = strtotime("+$interval week", $next_range_time);
									} else {
										// end the loop because we aren't going to write this event anyway
										$count_to = $count;
									}
									
								
								// handling DAILY events here
								} elseif ($rrule_array["FREQ"] == "DAILY") {

									// use dayCompare to see if we even have this event this day
									$diff_days = dayCompare(date("Ymd",$next_range_time), $start_date);
									if ($diff_days < $count) {
										if ($diff_days % $number == 0) {
											$interval = $number;
											$next_date = date("Ymd", $next_range_time);
											$next_date_time = strtotime($next_date);
											
											if (($next_date_time > $start_date_time) && ($next_date_time <= $end_date_time) && ($count_to != $count) && !in_array($next_date, $except_dates)) {
												// same general concept as the WEEKLY recurrence
// check for overlapping events
												$nbrOfOverlaps = checkOverlap();
// writes to $master array here
												$master_array[($next_date)][($hour.$minute)][] = array ("event_start" => $start_time, "event_text" => $summary, "event_end" => $end_time, "event_length" => $length, "event_overlap" => $nbrOfOverlaps, "description" => $description);
											}
										} else {
											$interval = 1;
										}
										$next_range_time = strtotime("+$interval day", $next_range_time);
									} else {
										// end the loop because we aren't going to write this event anyway
										$count_to = $count;
									}
									
									
								// handling MONTHLY events here
								} elseif ($rrule_array["FREQ"] == "MONTHLY") {
									$next_range_time = strtotime(date("Y-m-01", $next_range_time));
									// use monthCompare to see if we even have this event this month
									
									$diff_months = monthCompare(date("Ymd",$next_range_time), $start_date);
									if ($diff_months < $count) {
										if ($diff_months % $number == 0) {
											$interval = $number;
											
											// month has two cases, either $bymonthday or $byday
											if (is_array($bymonthday)) {
											
												// loop through the days on which this event happens
												foreach($bymonthday as $day) {
													if ($day != "0") {
														$next_date_time = strtotime(date("Y-m-",$next_range_time).$day);
														$next_date = date("Ymd", $next_date_time);
														if (($next_date_time > $start_date_time) && ($next_date_time <= $end_date_time) && ($count_to != $count) && !in_array($next_date, $except_dates)) {
															// same general concept as the WEEKLY recurrence
// check for overlapping events	
															$nbrOfOverlaps = checkOverlap();
// writes to $master array here
															$master_array[($next_date)][($hour.$minute)][] = array ("event_start" => $start_time, "event_text" => $summary, "event_end" => $end_time, "event_length" => $length, "event_overlap" => $nbrOfOverlaps, "description" => $description);
														}
													}
												}
												
											// our other case
											} else {
												// loop through the days on which this event happens
												foreach($byday as $day) {
													ereg ("([0-9]{1})([A-Z]{2})", $day, $byday_arr);
													$nth = $byday_arr[1]-1;
													$on_day = two2threeCharDays($byday_arr[2]);
													$next_date_time = strtotime("$on_day +$nth week", $next_range_time);
													$next_date = date("Ymd", $next_date_time);
													if (($next_date_time > $start_date_time) && ($next_date_time <= $end_date_time) && ($count_to != $count) && !in_array($next_date, $except_dates)) {
														// same general concept as the WEEKLY recurrence
// check for overlapping events	
														$nbrOfOverlaps = checkOverlap();
// writes to $master array here
														$master_array[($next_date)][($hour.$minute)][] = array ("event_start" => $start_time, "event_text" => $summary, "event_end" => $end_time, "event_length" => $length, "event_overlap" => $nbrOfOverlaps, "description" => $description);
													}
												}
											}
										} else {
											$interval = 1;
										}
										$next_range_time = strtotime("+$interval month", $next_range_time);
									} else {
										// end the loop because we aren't going to write this event anyway
										$count_to = $count;
									}
									
									
								// anything else we need to end the loop
								} else {
									$next_range_time = $end_range_time + 100;
									$count_to = $count;
								}
							}
						}
					}
				}	
			}
		}
	
	// Let's write all the data to the master array
	if ($start_time != "") {
// check for overlapping events
		$nbrOfOverlaps = checkOverlap();

// writes to $master array here
		$master_array[($start_date)][($hour.$minute)][] = array ("event_start" => $start_time, "event_text" => $summary, "event_end" => $end_time, "event_length" => $length, "event_overlap" => $nbrOfOverlaps, "description" => $description);
	}
		

		
		
		
	} else {
		
		$field = "";
		$data = "";
		
		sscanf($line, "%[^:]:%[^\n]", &$field, &$data);
		
		if(strstr($field, "DTSTART;TZID")) {
			$data = ereg_replace("T", "", $data);
			ereg ("([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{0,2})([0-9]{0,2})", $data, $regs);
			$year = $regs[1];
			$month = $regs[2];
			$day = $regs[3];
			$hour = $regs[4];
			$minute = $regs[5];
			
			$start_date = $year . $month . $day;
			$start_time = $hour . $minute;

		} elseif (strstr($field, "DTEND;TZID")) {
			$data = ereg_replace("T", "", $data);
			ereg ("([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{0,2})([0-9]{0,2})", $data, $regs);
			$year = $regs[1];
			$month = $regs[2];
			$day = $regs[3];
			$hour = $regs[4];
			$minute = $regs[5];
		
			$end_day = $year . $month . $day;
			$end_time = $hour . $minute;
			
		} elseif (strstr($field, "EXDATE;TZID")) {
			$data = ereg_replace("T", "", $data);
			ereg ("([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{0,2})([0-9]{0,2})", $data, $regs);
			$year = $regs[1];
			$month = $regs[2];
			$day = $regs[3];
			$hour = $regs[4];
			$minute = $regs[5];
		
			$except_dates[] = $year . $month . $day;
			$except_times[] = $hour . $minute;
			
		} elseif (strstr($field, "SUMMARY")) {
			$summary = $data;
			
		} elseif (strstr($field, "DESCRIPTION")) {
			$description = $data;	
		
		} elseif (strstr($field, "X-WR-CALNAME")) {
			$calendar_name = $data;
		
		} elseif (strstr($field, "DTSTART;VALUE=DATE")) {
			$allday_start = $data;
			// echo "$allday_start";
		
		} elseif (strstr($field, "DTEND;VALUE=DATE")) {
			$allday_end = $data;
			
		} elseif (strstr($field, "DURATION")) {
			
			if (($first_duration = TRUE) && (!strstr($field, "=DURATION"))) {
				ereg ("^P([0-9]{1,2})?([W,D]{0,1})?(T)?([0-9]{1,2})?(H)?([0-9]{1,2})?(M)?([0-9]{1,2})?(S)?", $data, $duration);
				if ($duration[2] = "W") {
					$weeks = $duration[1];
				} else {
					$days = $duration[1];
				}
				$hours = $duration[4];
				$minutes = $duration[6];
				$seconds = $duration[8];
				$the_duration = ($weeks * 60 * 60 * 24 * 7) + ($days * 60 * 60 * 24) + ($hours * 60 * 60) + ($minutes * 60) + ($seconds);
				$beginning = (strtotime($start_time) + $the_duration);
				$end_time = date ("Hi", $beginning);
				$first_duration = FALSE;
			}	
			
		} elseif (strstr($field, "RRULE")) {
			// $data = "RRULE:FREQ=YEARLY;INTERVAL=2;BYMONTH=1;BYDAY=SU;BYHOUR=8,9;BYMINUTE=30";
			$data = ereg_replace ("RRULE:", "", $data);
			$rrule = split (";", $data);
			foreach ($rrule as $recur) {
				ereg ("(.*)=(.*)", $recur, $regs);
				$rrule_array[$regs[1]] = $regs[2];
			}	
		} elseif (strstr($field, "ATTENDEE")) {
			$attendee = $data;
			// echo "$attendee";
			
		}
	}
}
// Sort the array by absolute date.
if (is_array($master_array)) { 
	ksort($master_array);
	reset($master_array);
}

// Store information in the session
/*if ($use_sessions == "yes") {
	session_start();
	session_register( "aArray", "aYear", "aLanguage", "aCalendar" );
	$aArray = $master_array;
	$aYear = $this_year;
	$aLanguage = $language;
	$aCalendar = $cal;
}*/


// End the session
//}

//If you want to see the values in the arrays, uncomment below.
//print "<pre>";
//print_r($master_array);
//print_r($overlap_array);
//print_r($day_array);
//print_r($rrule);			
//print "</pre>";
	
					
?>

© 2014-2024 Faster IT GmbH | imprint | privacy policy