1<?php
2
3//print_r($master_array);
4
5class Page {
6	var $page;
7	function draw_subscribe($template_p) {
8		global $phpiCal_config, $getdate, $cal, $subscribe_path, $download_filename;
9		if ($cal != $phpiCal_config->ALL_CALENDARS_COMBINED && $subscribe_path != '' && $download_filename != '') {
10			$this->page 	= str_replace('{SUBSCRIBE_PATH}', $subscribe_path, $this->page);
11			$this->page 	= str_replace('{DOWNLOAD_FILENAME}', $download_filename, $this->page);
12		} else {
13			$this->page = preg_replace('!<\!-- switch display_download on -->.*<\!-- switch display_download off -->!Uis', '', $this->page);
14		}
15	}
16
17	function draw_admin() {
18		global $phpiCal_config, $getdate, $cal, $master_array, $is_loged_in;
19		$delete_table = '';
20		if ($is_loged_in == TRUE) {
21			// Print Calendar Checkboxes
22			$COLUMNS_TO_PRINT = 3;
23			$column = 1;
24			$filelist = availableCalendars('', '', '', true);
25			foreach ($filelist as $file) {
26				if ($column > $COLUMNS_TO_PRINT) {
27					$delete_table 	.= '</tr>';
28					$column 	= 1;
29				}
30				if ($column == 1) {
31					$delete_table .= '<tr>';
32				}
33
34				$cal_filename_tmp = getCalendarName($file);
35				$cal_tmp = urlencode($file);
36				$cal_displayname_tmp = str_replace('32', ' ', $cal_filename_tmp);
37
38				$delete_table .= '<td align="left"><input name="delete_calendar[]" value="'.$cal_tmp.'" type="checkbox" />'.$cal_displayname_tmp.'</td>';
39
40				$column++;
41			}
42			// Print remaining empty columns if necessary
43			$number_of_columns = count($filelist);
44			while (gettype($number_of_columns/$COLUMNS_TO_PRINT) != 'integer') {
45				$delete_table .= '<td>&nbsp;</td>';
46				$number_of_columns++;
47			}
48
49			$this->page 	= str_replace('{DELETE_TABLE}', $delete_table, $this->page);
50		} else {
51			$this->page 	= preg_replace('!<\!-- switch logged_in on -->.*<\!-- switch logged_in off -->!Uis', '', $this->page);
52			$this->page 	= preg_replace('!<\!-- switch logged_in2 on -->.*<\!-- switch logged_in2 off -->!Uis', '', $this->page);
53		}
54
55	}
56
57	function draw_print($template_p) {
58		global $phpiCal_config, $getdate, $cal, $master_array, $printview, $dateFormat_day, $timeFormat, $week_start, $week_end, $lang;
59		preg_match('!<\!-- loop events on -->(.*)<\!-- loop events off -->!Uis', $this->page, $match1);
60		preg_match('!<\!-- switch some_events on -->(.*)<\!-- loop events on -->!Uis', $this->page, $match3);
61		$loop_event		= trim($match1[1]);
62		$loop_day 		= trim($match3[1]);
63		$parse_month 	= date ('Ym', strtotime($getdate));
64		$parse_year 	= date ('Y', strtotime($getdate));
65
66		$seen_events = array();
67		$final = '';
68		$events_week = 0;
69		foreach($master_array as $key => $val) {
70			preg_match ('/([0-9]{6})([0-9]{2})/', $key, $regs);
71			if (((@$regs[1] == $parse_month) && ($printview == 'month')) || (($key == $getdate) && ($printview == 'day')) || ((($key >= $week_start) && ($key <= $week_end)) && ($printview == 'week')) || ((substr(@$regs[1],0,4) == $parse_year) && ($printview == 'year'))) {
72				$events_week++;
73				$dayofmonth = strtotime ($key);
74				$dayofmonth = localizeDate ($dateFormat_day, $dayofmonth);
75				$events_tmp = $loop_event;
76				$day_tmp	= $loop_day;
77				$day_events = 0;
78				// Pull out each day
79				$some_events = '';
80				foreach ($val as $cal_time => $new_val) {
81					foreach ($new_val as $new_key2 => $new_val2) {
82						if (isset($seen_events[$new_key2]) && isset($new_val2['spans_day']) && $new_val2['spans_day'] == 1){
83							$new_val2['event_text'] .= ' second instance of '.$new_key2;
84							continue;
85						}
86						$seen_events[$new_key2] = 1;
87						$day_events++;
88						if (isset($new_val2['event_text'])) {
89							$event_text 	= sanitizeForWeb(stripslashes(urldecode($new_val2['event_text'])));
90							$location 		= sanitizeForWeb(stripslashes(urldecode($new_val2['location'])));
91							$description 	= sanitizeForWeb(stripslashes(urldecode($new_val2['description'])));
92							if (!empty($description)) {
93								$description = ereg_replace('(blocked)?([[:alpha:]]+://([^<>&[:space:]]|&amp;)+[[:alnum:]/])', '<a target="_new" href="\2">\2</a>', $description);
94								$description = ereg_replace('(blocked)?(mailto:)?([[:alnum:]_.%+-]+@[[:alnum:].-]+\.[[:alpha:]]{2,4})', '<a href="mailto:\3">\3</a>', $description);
95							}
96
97							$event_start 	= $new_val2['event_start'];
98							$event_end 		= $new_val2['event_end'];
99							if (isset($new_val2['display_end'])) $event_end = $new_val2['display_end'];
100							if ($cal_time == -1) {
101								$event_start = $lang['l_all_day'];
102								$event_start2 = '';
103								$event_end = '';
104							} else {
105								$event_start 	= date ($timeFormat, strtotime ($event_start));
106								$event_end 		= date ($timeFormat, strtotime ($event_end));
107								$event_start 	= $event_start .' - '.$event_end;
108								if (date('Ymd', $new_val2['start_unixtime']) != date('Ymd', $new_val2['end_unixtime'])) $event_start .= ' '.localizeDate($dateFormat_day, $new_val2['end_unixtime']);
109							}
110						}
111
112						if ($description == '') {
113							$events_tmp = preg_replace('!<\!-- switch description_events on -->.*<\!-- switch description_events off -->!Uis', '', $events_tmp);
114						}
115
116						if ($location == '') {
117							$events_tmp = preg_replace('!<\!-- switch location_events on -->.*<\!-- switch location_events off -->!Uis', '', $events_tmp);
118						}
119
120						$search		= array('{EVENT_START}', '{EVENT_TEXT}', '{DESCRIPTION}', '{LOCATION}');
121						$replace	= array($event_start, $event_text, $description, $location);
122						$events_tmp = str_replace($search, $replace, $events_tmp);
123						$some_events .= $events_tmp;
124						$events_tmp	= $loop_event;
125					}
126				}
127				if ($day_events == 0) continue;
128				$day_tmp  = str_replace('{DAYOFMONTH}', $dayofmonth, $day_tmp);
129				$final   .= $day_tmp.$some_events;
130				unset ($day_tmp);
131				$some_events = '';
132			}
133		}
134
135		if ($events_week < 1) {
136			$this->page = preg_replace('!<\!-- switch some_events on -->.*<\!-- switch some_events off -->!Uis', '', $this->page);
137		} else {
138			$this->page = preg_replace('!<\!-- switch some_events on -->.*<\!-- switch some_events off -->!Uis', $final, $this->page);
139			$this->page = preg_replace('!<\!-- switch no_events on -->.*<\!-- switch no_events off -->!Uis', '', $this->page);
140		}
141	}
142
143	function draw_search($template_p) {
144		global $phpiCal_config, $getdate, $cal, $the_arr, $printview, $dateFormat_day, $timeFormat, $week_start, $week_end, $lang;
145
146		preg_match('!<\!-- switch results on -->(.*)<\!-- switch results off -->!Uis', $this->page, $match1);
147		preg_match('!<\!-- switch recur on -->(.*)<\!-- switch recur off -->!Uis', $this->page, $match2);
148		preg_match('!<\!-- switch exceptions on -->(.*)<\!-- switch exceptions off -->!Uis', $this->page, $match3);
149		$loop_event		= trim($match1[1]);
150		$loop_recur 	= trim($match2[1]);
151		$loop_except 	= trim($match3[1]);
152		$parse_month 	= date ('Ym', strtotime($getdate));
153
154		$events_found = 0;
155		$some_events = '';
156		$final = '';
157		if (isset($the_arr)){
158			// Pull out each event
159			foreach($the_arr as $key => $val) {
160				$events_found++;
161				$dayofmonth = strtotime($val['date']);
162				$dayofmonth = localizeDate ('%A, %B %e %Y', $dayofmonth);
163				$events_tmp = $loop_event;
164				$recur_tmp	= $loop_recur;
165				if ($val['event_text']) {
166					$event_text 	= sanitizeForWeb(stripslashes(urldecode($val['event_text'])));
167					$description 	= sanitizeForWeb(stripslashes(urldecode($val['description'])));
168					$location 		= sanitizeForWeb(stripslashes(urldecode($val['location'])));
169					$event_start 	= $val['event_start'];
170					$event_end 		= $val['event_end'];
171					if (isset($val['display_end'])) $event_end = $val['display_end'];
172					if (!$val['event_start']) {
173						$event_start = $lang['l_all_day'];
174						$event_start2 = '';
175						$event_end = '';
176					} else {
177							$event_start    = date ($timeFormat, strtotime ($event_start));
178							$event_end      = date ($timeFormat, strtotime ($event_end));
179							$event_start    = $event_start .' - '.$event_end;
180					}
181				}
182
183				if ($description == '') {
184					$events_tmp = preg_replace('!<\!-- switch description_events on -->.*<\!-- switch description_events off -->!Uis', '', $events_tmp);
185				}
186					if (!isset($val['exceptions'])) {
187					$events_tmp = preg_replace('!<\!-- switch exceptions on -->.*<\!-- switch exceptions off -->!Uis', '', $events_tmp);
188				}else{
189					$some_exceptions = '';
190					foreach ($val['exceptions'] as $except_val){
191						$except_tmp	= $loop_except;
192
193						$except_date = strtotime($except_val['date']);
194						$except_date = localizeDate ('%A, %B %e %Y', $except_date);
195						$except_tmp = str_replace('{DAYOFMONTH}', $except_date, $except_tmp);
196
197						$except_event_start    	= date ($timeFormat, strtotime ($except_val['event_start']));
198						$except_event_end    	= date ($timeFormat, strtotime ($except_val['event_end']));
199						$except_event_start    	= $except_event_start .' - '.$except_event_end;
200
201						$except_tmp = str_replace('{EVENT_START}', $except_event_start, $except_tmp);
202
203						$except_event_text 	= sanitizeForWeb(stripslashes(urldecode($except_val['event_text'])));
204						$except_tmp = str_replace('{EVENT_TEXT}', $except_event_text, $except_tmp);
205
206						#is there a recur in the exception?
207						if (!$except_val['recur']) {
208							$except_tmp = preg_replace('!<\!-- switch except_recur on -->.*<\!-- switch except_recur off -->!Uis', '', $except_tmp);
209						}else{
210							$except_tmp = str_replace('{EXCEPT_RECUR}', $except_val['recur'], $except_tmp);
211						}
212						#is there a description in the exception?
213						if (!$except_val['description']) {
214							$except_tmp = preg_replace('!<\!-- switch except_description on -->.*<\!-- switch except_description off -->!Uis', '', $except_tmp);
215						}else{
216							$except_description = stripslashes(urldecode($except_val['description']));
217							$except_tmp = str_replace('{EXCEPT_DESCRIPTION}', $except_description, $except_tmp);
218						}
219						$some_exceptions .= $except_tmp;
220
221					}
222					$events_tmp = preg_replace('!<\!-- switch exceptions on -->.*<\!-- switch exceptions off -->!Uis', $some_exceptions,$events_tmp );
223
224
225				}
226
227				if (!$val['recur']) {
228					$events_tmp = preg_replace('!<\!-- switch recur on -->.*<\!-- switch recur off -->!Uis', '', $events_tmp);
229					$events_tmp = str_replace('{L_STARTING_ON}', '', $events_tmp);
230				}else{
231					$events_tmp = str_replace('{RECUR}', $val['recur'], $events_tmp);
232				}
233
234				$search		= array('{EVENT_START}', '{EVENT_TEXT}', '{DESCRIPTION}','{LOCATION}');
235				$replace	= array($event_start, $event_text, $description, $location);
236				$events_tmp = str_replace($search, $replace, $events_tmp);
237				$some_events .= $events_tmp;
238				$events_tmp	= $loop_event;
239
240
241				$some_events  = str_replace('{KEY}', $val['date'], $some_events);
242				$some_events  = str_replace('{DAYOFMONTH}', $dayofmonth, $some_events);
243				$final   .= $some_events;
244				$some_events = '';
245
246			}
247		}
248		if ($events_found < 1) {
249			$this->page = preg_replace('!<\!-- switch results on -->.*<\!-- switch results off -->!Uis', '', $this->page);
250		} else {
251			$this->page = preg_replace('!<\!-- switch results on -->.*<\!-- switch results off -->!Uis', $final, $this->page);
252			$this->page = preg_replace('!<\!-- switch no_results on -->.*<\!-- switch no_results off -->!Uis', '', $this->page);
253			#echo "<hr>this->page: $this->page<br><hr>";
254
255		}
256	}#end draw_search
257
258	function draw_week($template_p) {
259		global $phpiCal_config, $start_week_time, $getdate, $cal, $master_array, $dateFormat_week_list, $current_view, $day_array, $timeFormat;
260
261		// Figure out colspans and initialize weekarray
262		$thisdate  	= $start_week_time;
263		$swt	   	= $start_week_time;
264		for ($i=0;$i < $phpiCal_config->week_length;$i++) {
265			$thisday = date('Ymd', $thisdate);
266			$nbrGridCols[$thisday] = 1;
267			if (isset($master_array[$thisday])) {
268				foreach($master_array[($thisday)] as $ovlKey => $ovlValue) {
269					# ovlKey is a time slot; $ovlValue is an array with key=uid
270					if ($ovlKey != '-1') {
271						foreach($ovlValue as $ovl2Value) {
272							$nbrGridCols[$thisday] = kgv($nbrGridCols[$thisday], ($ovl2Value['event_overlap'] + 1));
273						}
274					}
275				}
276			}
277			$weekarray[$i] 		= $thisday;
278			$event_length[$thisday] = array ();
279			$thisdate = ($thisdate + (25 * 60 * 60));
280		}
281		#echo "<pre>";print_r($nbrGridCols);
282		// Replaces the allday events
283		preg_match('!<\!-- loop allday row on -->(.*)<\!-- loop alldaysofweek on -->!Uis', $this->page, $match1);
284		$loop_row_begin = trim($match1[1]); # <tr>etc
285		preg_match('!<\!-- loop allday on -->(.*)<\!-- loop allday off -->!Uis', $this->page, $match1);
286		preg_match('!<\!-- loop alldaysofweek on -->(.*)<\!-- loop allday on -->!Uis', $this->page, $match2);
287		preg_match('!<\!-- loop allday off -->(.*)<\!-- loop alldaysofweek off -->!Uis', $this->page, $match3);
288		$loop_ad 	= trim($match1[1]); # one day cell
289		$loop_begin = trim($match2[1]); # <td>
290		$loop_end 	= trim($match3[1]); # </td>
291		preg_match('!<\!-- loop alldaysofweek off -->(.*)<\!-- loop allday row off -->!Uis', $this->page, $match3);
292		$loop_row_end 	= trim($match3[1]); # </tr>
293		$allday_uids = array();
294		$allday_uid_dates = array();
295		$weekreplace = '';
296		foreach ($weekarray as $i=>$get_date){
297			if (isset($master_array[$get_date]['-1']) && is_array($master_array[$get_date]['-1']) && !empty($master_array[$get_date]['-1'])){
298				foreach ($master_array[$get_date]['-1'] as $uid => $allday){
299					if (!array_key_exists($uid, $allday_uids)) $allday_uids[$uid] = $get_date;
300					$allday_uid_dates[$uid][] = $get_date;
301				}
302			}
303		}
304		# new allday routine is better for multiday events
305		while(!empty($allday_uids)){
306			$row = $loop_row_begin;
307			$day = 0;
308			$replace = '';
309			while ($day < $phpiCal_config->week_length){
310				$colspan  = 0;
311				$replace  .= $loop_begin; # <td>
312
313				if ($weekarray[$day] == $getdate) $replace = str_replace('{TODAY}', 'rowToday', $replace);
314				$replace 	= str_replace('{TODAY}', 'rowOff', $replace);
315
316				if(array_search($weekarray[$day], $allday_uids)){
317					$uid = array_search($weekarray[$day], $allday_uids);
318					unset($allday_uids[$uid]);
319					$allday = $master_array[$weekarray[$day]]['-1'][$uid];
320					foreach ($allday_uid_dates[$uid] as $date){
321						$colspan += $nbrGridCols[$weekarray[$day]];
322						$day++;
323					}
324					$event_calno  	= $allday['calnumber'];
325					$event_calno	= (($event_calno - 1) % $phpiCal_config->unique_colors) + 1;
326 					$event 			= openevent($get_date, -1, $uid, $allday, $phpiCal_config->allday_week_lines, (8*$colspan), 'psf');
327					$loop_tmp 		= str_replace('{ALLDAY}', $event, $loop_ad);
328					$loop_tmp 		= str_replace('{CALNO}', $event_calno, $loop_tmp);
329					$replace		= str_replace('{NOBORDER}', ' style="border-top: none;"', $replace);
330					$replace		.= $loop_tmp;
331					$replace		.= $loop_end;
332				}else{
333					$replace		= str_replace('{NOBORDER}', '', $replace);
334					$colspan		= $nbrGridCols[$weekarray[$day]];
335					$replace		.= $loop_end;
336					$day++;
337				}
338				unset ($ev);
339				$replace 	= str_replace('{COLSPAN}', 'colspan="'.$colspan.'"', $replace);
340			}
341			$row .= $replace.' '.$loop_row_end."\n";
342			$weekreplace .= $row."\n";
343		}
344		$this->page = preg_replace('!<\!-- loop allday row on -->.*<\!-- loop allday row off -->!Uis', $weekreplace, $this->page);
345
346		// Replaces the daysofweek
347		preg_match('!<\!-- loop daysofweek on -->(.*)<\!-- loop daysofweek off -->!Uis', $this->page, $match1);
348		$loop_dof = trim($match1[1]);
349		$start_wt		 	= strtotime(dateOfWeek($getdate, $phpiCal_config->week_start_day));
350		$weekday_loop = '';
351		for ($i=0; $i<$phpiCal_config->week_length; $i++) {
352			$daylink = date('Ymd', $start_wt);
353			$weekday = localizeDate($dateFormat_week_list, strtotime($daylink));
354			if ($daylink == $getdate) {
355				$row1 = 'rowToday';
356				$row2 = 'rowOn';
357				$row3 = 'rowToday';
358			}else{
359				$row1 = 'rowOff';
360				$row2 = 'rowOn';
361				$row3 = 'rowOff';
362			}
363			$start_wt 		= strtotime('+1 day', $start_wt);
364			$colspan		= 'colspan="'.$nbrGridCols[$daylink].'"';
365			$search			= array('{DAY}', '{DAYLINK}', '{ROW1}', '{ROW2}', '{ROW3}', '{COLSPAN}');
366			$replace		= array($weekday, $daylink, $row1, $row2, $row3, $colspan);
367			$loop_tmp 		= str_replace($search, $replace, $loop_dof);
368			$weekday_loop  .= $loop_tmp;
369		}
370		$this->page = preg_replace('!<\!-- loop daysofweek on -->.*<\!-- loop daysofweek off -->!Uis', $weekday_loop, $this->page);
371
372		// Build the body
373		preg_match('!<\!-- loop row on -->(.*)<\!-- loop row off -->!Uis', $this->page, $match2);
374		preg_match('!<\!-- loop event on -->(.*)<\!-- loop event off -->!Uis', $this->page, $match3);
375		$loop_hours = trim($match2[1]);
376		$loop_event = trim($match3[1]);
377
378		$event_length = array ();
379		$border = 0;
380		preg_match ('/([0-9]{4})([0-9]{2})([0-9]{2})/', $getdate, $day_array2);
381		$this_day = $day_array2[3];
382		$this_month = $day_array2[2];
383		$this_year = $day_array2[1];
384		$thisdate = $swt;
385
386		$weekdisplay = '';
387
388		#day_array is an array of time blocks of length $phpiCal_config->gridLength
389		foreach ($day_array as $key) {
390			$cal_time = $key;
391			preg_match('/([0-9]{2})([0-9]{2})/', $key, $regs_tmp);
392			$key = mktime($regs_tmp[1],$regs_tmp[2],0,$this_month,$this_day,$this_year);
393			$key = date ($timeFormat, $key);
394
395			if (ereg('([0-9]{1,2}):00', $key)) {
396				$even_row = '2';
397				# column of times colspan = 4 to cover navigation links at top
398				$weekdisplay .= '<tr>';
399				$weekdisplay .= '<td colspan="4" rowspan="' . (60 / $phpiCal_config->gridLength) . '" align="center" valign="top" width="60" class="timeborder2">'.$key.'</td>';
400				$weekdisplay .= '<td bgcolor="#a1a5a9" width="1" height="' . $phpiCal_config->gridLength . '"></td>';
401			} elseif ($cal_time == $phpiCal_config->day_start) {
402				$size_tmp = 60 - (int)substr($cal_time,2,2);
403				$weekdisplay .= '<tr>';
404				$weekdisplay .= '<td colspan="4" rowspan="' . ($size_tmp / $phpiCal_config->gridLength) . '" align="center" valign="top" width="60" class="timeborder2">'.$key.' </td>';
405				$weekdisplay .= '<td bgcolor="#a1a5a9" width="1" height="' . $phpiCal_config->gridLength . '"></td>';
406			} else {
407				$even_row = '';
408				if (ereg('([0-9]{1,2}):30', $key)) $even_row = '3';
409				# empty row for each gridLength, to the right of times and left of first weekday
410				$weekdisplay .= '<tr>';
411				$weekdisplay .= '<td bgcolor="#a1a5a9" width="1" height="' . $phpiCal_config->gridLength . '"></td>';
412			}
413
414			/* 	add columns in the $cal_time grid slot for each day
415				each cell will have $this_time_arr of events 	*/
416			foreach ($weekarray as $thisday) {
417				$this_time_arr = array();
418				if ($phpiCal_config->day_start == $cal_time && isset($master_array[$thisday]) && is_array($master_array[$thisday])) {
419					# want to pile up all the events before day_start that end in the displayed times
420					foreach($master_array[$thisday] as $time_key => $time_arr) {
421						if ((int)$time_key <= (int)$cal_time && is_array($time_arr) && $time_key != '-1') {
422							foreach($time_arr as $uid => $event_tmp) {
423								if ((int)$event_tmp['display_end'] > (int)$cal_time) $this_time_arr[$uid] = $event_tmp;
424							}
425						}
426					}
427				} else {
428					# events that start in internal cal_times the grid
429					if (isset($master_array[$thisday][$cal_time]) && sizeof($master_array[$thisday][$cal_time]) > 0) {
430						$this_time_arr = $master_array[$thisday][$cal_time];
431					}
432				}
433
434				// go through $this_time_array and fill the event_length array
435				foreach ($this_time_arr as $eventKey => $loopevent) {
436					$drawEvent = drawEventTimes ($cal_time, $loopevent['display_end']);
437					$j = 0;
438					while (isset($event_length[$thisday][$j])) {
439						if ($event_length[$thisday][$j]['state'] == 'ended') {
440							$event_length[$thisday][$j] = array ('length' => ($drawEvent['draw_length'] / $phpiCal_config->gridLength), 'key' => $eventKey, 'overlap' => $loopevent['event_overlap'],'state' => 'begin');
441							break;
442						}
443						$j++;
444					}
445					if ($j == sizeof(@$event_length[$thisday])) {
446						$event_length[$thisday][] = array ('length' => ($drawEvent['draw_length'] / $phpiCal_config->gridLength), 'key' => $eventKey, 'overlap' => $loopevent['event_overlap'],'state' => 'begin');
447					}
448				}
449				$class = ' class="weekborder'.$even_row.'"';
450				if (empty($event_length[$thisday])) {
451					# no events
452					$tclass = $class;
453					if ($thisday == $getdate) $tclass = ' class="weekborder'.$even_row.' weektoday"';
454					$drawWidth = 1;
455					$weekdisplay .= '<td colspan="' . $nbrGridCols[$thisday] . '"' . $tclass . '>&nbsp;</td>'."\n";
456				} else {
457					# have events
458					$emptyWidth = $nbrGridCols[$thisday];
459					// Used to "join" ended events, so the ended case below results in one colspan'd td instead of multiple tds.
460					$ended_counter = 0;
461					foreach($event_length[$thisday] as $i=>$el) {
462						$drawWidth = $nbrGridCols[$thisday] / ($el['overlap'] + 1);
463						$emptyWidth = $emptyWidth - $drawWidth;
464						switch ($el['state']) {
465							case 'begin':
466								if ($ended_counter) {
467									$tclass = $class;
468									if ($thisday == $getdate) $tclass = ' class="weekborder'.$even_row.' weektoday"';
469									$weekdisplay .= '<td colspan="' . $ended_counter . '"' . $tclass . '>&nbsp;</td>';
470									$ended_counter = 0;
471								}
472								$event_length[$thisday][$i]['state'] = 'started';
473 								$uid = $event_length[$thisday][$i]['key'];
474 								$event_start 	= $this_time_arr[$uid]['start_unixtime'];
475								$event_start 	= date ($timeFormat, $event_start);
476 								$event_calno  	= $this_time_arr[$uid]['calnumber'];
477 								$event_status	= strtolower($this_time_arr[$uid]['status']);
478 								$event_recur = $this_time_arr[$uid]['recur'];
479								$event_calno = (($event_calno - 1) % $phpiCal_config->unique_colors) + 1;
480								$confirmed = '';
481						  		if (is_array($event_recur)) $confirmed .= '<img src="images/recurring.gif" width="9" height="9" alt="" border="0" hspace="0" vspace="0" />&nbsp;';
482								if ($event_status != '') {
483						  			$confirmed .= '<img src="images/'.$event_status.'.gif" width="9" height="9" alt="" border="0" hspace="0" vspace="0" />&nbsp;';
484						  		}
485								$event_date_start = date('Ymd', $this_time_arr[$uid]['start_unixtime']);
486								$event_date_end   = date('Ymd', $this_time_arr[$uid]['end_unixtime'] - 1);
487								if ($event_date_start != $event_date_end) {
488									if ($thisday == $event_date_start) {
489										$confirmed .= '<img src="images/longevent_start.gif" width="9" height="9" alt="" border="0" hspace="0" vspace="0" />&nbsp;';
490									}
491									else if ($thisday == $event_date_end) {
492										$confirmed .= '<img src="images/longevent_end.gif" width="9" height="9" alt="" border="0" hspace="0" vspace="0" />&nbsp;';
493									}
494									else {
495										$confirmed .= '<img src="images/longevent_mid.gif" width="9" height="9" alt="" border="0" hspace="0" vspace="0" />&nbsp;';
496									}
497								}
498								$event_temp   = $loop_event;
499								$event 		  = openevent($thisday, $cal_time, $uid, $this_time_arr[$uid], $phpiCal_config->week_events_lines, 25, 'ps');
500								$weekdisplay .= '<td rowspan="' . $event_length[$thisday][$i]['length'] . '" colspan="' . $drawWidth . '" align="left" valign="top" class="eventbg2_'.$event_calno.'">'."\n";
501
502								// Start drawing the event
503								$event_temp   = str_replace('{EVENT}', $event, $event_temp);
504								$event_temp   = str_replace('{EVENT_START}', $event_start, $event_temp);
505								$event_temp   = str_replace('{CONFIRMED}', $confirmed, $event_temp);
506								$event_temp   = str_replace('{EVENT_CALNO}', $event_calno, $event_temp);
507								$weekdisplay .= $event_temp;
508								$weekdisplay .= '</td>';
509								// End event drawing
510
511								break;
512							case 'started':
513								if ($ended_counter) {
514									$tclass = $class;
515									if ($thisday == $getdate) $tclass = ' class="weekborder'.$even_row.' weektoday"';
516									$weekdisplay .= '<td colspan="' . $ended_counter . '"' . $tclass . '>&nbsp;</td>';
517									$ended_counter = 0;
518								}
519								break;
520							case 'ended':
521								$ended_counter += $drawWidth;
522								break;
523						}
524						$event_length[$thisday][$i]['length']--;
525						if ($event_length[$thisday][$i]['length'] == 0) {
526							$event_length[$thisday][$i]['state'] = 'ended';
527						}
528					}
529
530					// Clean up
531					$emptyWidth += $ended_counter;
532					//fill empty space on the right
533					if ($emptyWidth > 0) {
534						$tclass = $class;
535						if ($thisday == $getdate) $tclass = ' class="weekborder'.$even_row.' weektoday"';
536						$weekdisplay .= '<td colspan="' . $emptyWidth . '"'.$tclass.'>&nbsp;</td>'."\n";
537					}
538					while (isset($event_length[$thisday][(sizeof($event_length[$thisday]) - 1)]['state']) && $event_length[$thisday][(sizeof($event_length[$thisday]) - 1)]['state'] == 'ended') {
539						array_pop($event_length[$thisday]);
540					}
541				}
542			}
543			$weekdisplay .= "</tr>\n";
544		}
545
546		$this->page = preg_replace('!<\!-- loop row on -->.*<\!-- loop event off -->!Uis', $weekdisplay, $this->page);
547	}
548
549	function draw_day($template_p) {
550		global $getdate, $cal, $master_array, $dateFormat_week_list, $current_view, $day_array, $timeFormat, $phpiCal_config, $daysofweek_lang;
551		// Replaces the allday events
552		$replace = '';
553		$weekday_loop = '';
554		$daydisplay = '';
555		if (is_array(@$master_array[$getdate]['-1'])) {
556			preg_match('!<\!-- loop allday on -->(.*)<\!-- loop allday off -->!Uis', $this->page, $match1);
557			$loop_ad = trim($match1[1]);
558			foreach ($master_array[$getdate]['-1'] as $uid => $allday) {
559				$event_calno  	= $allday['calnumber'];
560				$event_calno	= (($event_calno - 1) % $phpiCal_config->unique_colors) + 1;
561 				$event 			= openevent($getdate, -1, $uid, $allday);
562				$loop_tmp 		= str_replace('{ALLDAY}', $event, $loop_ad);
563				$loop_tmp 		= str_replace('{CALNO}', $event_calno, $loop_tmp);
564				$replace		.= $loop_tmp;
565			}
566		}
567		$this->page = preg_replace('!<\!-- loop allday on -->.*<\!-- loop allday off -->!Uis', $replace, $this->page);
568
569		// Replaces the daysofweek
570		preg_match('!<\!-- loop daysofweek on -->(.*)<\!-- loop daysofweek off -->!Uis', $this->page, $match1);
571		$loop_dof = trim($match1[1]);
572		$start_wt		 	= strtotime(dateOfWeek($getdate, $phpiCal_config->week_start_day));
573		$start_day 			= strtotime(dateOfWeek($getdate, $phpiCal_config->week_start_day));
574		for ($i=0; $i< $phpiCal_config->week_length; $i++) {
575			$day_num 		= date('w', $start_day);
576			$daylink		= date('Ymd', $start_wt);
577			if ($current_view == 'day') {
578				$weekday 		= $daysofweek_lang[$day_num];
579			} else {
580				$weekday = localizeDate($dateFormat_week_list, strtotime($daylink));
581			}
582			if ($daylink == $getdate) {
583				$row1 = 'rowToday';
584				$row2 = 'rowOn';
585				$row3 = 'rowToday';
586			} else {
587				$row1 = 'rowOff';
588				$row2 = 'rowOn';
589				$row3 = 'rowOff';
590			}
591			$start_day 		= strtotime('+1 day', $start_day);
592			$start_wt 		= strtotime('+1 day', $start_wt);
593			$search			= array('{DAY}', '{DAYLINK}', '{ROW1}', '{ROW2}', '{ROW3}');
594			$replace		= array($weekday, $daylink, $row1, $row2, $row3);
595			$loop_tmp 		= str_replace($search, $replace, $loop_dof);
596			$weekday_loop  .= $loop_tmp;
597		}
598		$this->page = preg_replace('!<\!-- loop daysofweek on -->.*<\!-- loop daysofweek off -->!Uis', $weekday_loop, $this->page);
599
600		// Build the body
601		$dayborder = 0;
602
603		$nbrGridCols = 1;
604		if (isset($master_array[($getdate)])) {
605			foreach($master_array[($getdate)] as $ovlKey => $ovlValue) {
606				if ($ovlKey != '-1') {
607					foreach($ovlValue as $ovl2Value) {
608						$nbrGridCols = kgv($nbrGridCols, ($ovl2Value['event_overlap'] + 1));
609					}
610				}
611			}
612		}
613		preg_match('!<\!-- loop row on -->(.*)<\!-- loop row off -->!Uis', $this->page, $match2);
614		preg_match('!<\!-- loop event on -->(.*)<\!-- loop event off -->!Uis', $this->page, $match3);
615		$loop_hours = trim($match2[1]);
616		$loop_event = trim($match3[1]);
617
618		$event_length = array ();
619		$border = 0;
620		preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})/', $getdate, $day_array2);
621		$this_day = $day_array2[3];
622		$this_month = $day_array2[2];
623		$this_year = $day_array2[1];
624		foreach ($day_array as $key) {
625			preg_match('/([0-9]{2})([0-9]{2})/', $key, $regs_tmp);
626			$cal_time = $key;
627			$key = mktime($regs_tmp[1],$regs_tmp[2],0,$this_month,$this_day,$this_year);
628			$key = date ($timeFormat, $key);
629			unset($this_time_arr);
630
631			// add events that overlap $phpiCal_config->day_start instead of cutting them out completely
632			if (($phpiCal_config->day_start == $cal_time) && (isset($master_array[$getdate]))) {
633				foreach($master_array[$getdate] as $time_key => $time_arr) {
634					if ((int)$time_key <= (int)$cal_time) {
635						if (is_array($time_arr) && $time_key != '-1') {
636							foreach($time_arr as $uid => $event_tmp) {
637								if ((int)$event_tmp['event_end'] > (int)$cal_time) {
638									$this_time_arr[$uid] = $event_tmp;
639								}
640							}
641						}
642					} else {
643						break;
644					}
645				}
646			} else {
647				// add events that overlap the start time
648				if (isset($master_array[$getdate][$cal_time]) && sizeof($master_array[$getdate][$cal_time]) > 0) {
649					$this_time_arr = $master_array[$getdate][$cal_time];
650				}
651			}
652
653			// check for eventstart
654			if (isset($this_time_arr) && sizeof($this_time_arr) > 0) {
655				foreach ($this_time_arr as $eventKey => $loopevent) {
656					$drawEvent = drawEventTimes ($cal_time, $loopevent['display_end']);
657					$j = 0;
658					while (isset($event_length[$j])) {
659						if ($event_length[$j]['state'] == 'ended') {
660							$event_length[$j] = array ('length' => ($drawEvent['draw_length'] / $phpiCal_config->gridLength), 'key' => $eventKey, 'overlap' => $loopevent['event_overlap'],'state' => 'begin');
661							break;
662						}
663						$j++;
664					}
665					if ($j == sizeof($event_length)) {
666						array_push ($event_length, array ('length' => ($drawEvent['draw_length'] / $phpiCal_config->gridLength), 'key' => $eventKey, 'overlap' => $loopevent['event_overlap'],'state' => 'begin'));
667					}
668				}
669			}
670			if (preg_match('/([0-9]{1,2}):00/', $key)) {
671				$daydisplay .= '<tr>'."\n";
672				$daydisplay .= '<td rowspan="' . (60 / $phpiCal_config->gridLength) . '" align="center" valign="top" width="60" class="timeborder">'.$key.'</td>'."\n";
673				$daydisplay .= '<td bgcolor="#a1a5a9" width="1" height="' . $phpiCal_config->gridLength . '"></td>'."\n";
674			} elseif($cal_time == $phpiCal_config->day_start) {
675				$size_tmp = 60 - (int)substr($cal_time,2,2);
676				$daydisplay .= '<tr>'."\n";
677				$daydisplay .= '<td rowspan="' . ($size_tmp / $phpiCal_config->gridLength) . '" align="center" valign="top" width="60" class="timeborder">'.$key.'</td>'."\n";
678				$daydisplay .= '<td bgcolor="#a1a5a9" width="1" height="' . $phpiCal_config->gridLength . '"></td>'."\n";
679			} else {
680				$daydisplay .= '<tr>'."\n";
681				$daydisplay .= '<td bgcolor="#a1a5a9" width="1" height="' . $phpiCal_config->gridLength . '"></td>'."\n";
682			}
683
684			switch (++$dayborder) {
685				case 1:
686					$class = ' class="dayborder"';
687					break;
688
689				case 2:
690					$class = ' class="dayborder2"';
691					break;
692
693				case 3:
694					$class = ' class="dayborder3"';
695					break;
696
697				case 4:
698					$class = ' class="dayborder2"';
699					$dayborder = 0;
700					break;
701			}
702
703			if (sizeof($event_length) == 0) {
704				$daydisplay .= '<td colspan="' . $nbrGridCols . '"' . $class . '>&nbsp;</td>' . "\n";
705
706			} else {
707				$emptyWidth = $nbrGridCols;
708				// Used to "join" ended events, so the ended case below results in one colspan'd td instead of multiple tds.
709				$ended_counter = 0;
710				for ($i=0;$i<sizeof($event_length);$i++) {
711					$drawWidth = $nbrGridCols / ($event_length[$i]['overlap'] + 1);
712					$emptyWidth = $emptyWidth - $drawWidth;
713					switch ($event_length[$i]['state']) {
714						case 'begin':
715						  if ($ended_counter) {
716							$daydisplay .= '<td colspan="' . $ended_counter . '"' . $class . '>&nbsp;</td>';
717							$ended_counter = 0;
718						  }
719						  $event_length[$i]['state'] = 'started';
720 						  $uid = $event_length[$i]['key'];
721 						  $event_calno  = $this_time_arr[$uid]['calnumber'];
722 						  $event_recur  = $this_time_arr[$uid]['recur'];
723 						  $event_status = strtolower($this_time_arr[$uid]['status']);
724						  $event_calno  = (($event_calno - 1) % $phpiCal_config->unique_colors) + 1;
725						  $confirmed = '';
726						  if (is_array($event_recur)) $confirmed .= '<img src="images/recurring.gif" width="9" height="9" alt="" border="0" hspace="0" vspace="0" />&nbsp;';
727						  if ($event_status != '') $confirmed .= '<img src="images/'.$event_status.'.gif" width="9" height="9" alt="" border="0" hspace="0" vspace="0" />&nbsp;';
728						  $event_date_start = date('Ymd', $this_time_arr[$uid]['start_unixtime']);
729						  $event_date_end   = date('Ymd', $this_time_arr[$uid]['end_unixtime']);
730						  if ($event_date_start != $event_date_end) {
731							  if ($getdate == $event_date_start) {
732								  $confirmed .= '<img src="images/longevent_start.gif" width="9" height="9" alt="" border="0" hspace="0" vspace="0" />&nbsp;';
733							  }
734							  else if ($getdate == $event_date_end) {
735								  $confirmed .= '<img src="images/longevent_end.gif" width="9" height="9" alt="" border="0" hspace="0" vspace="0" />&nbsp;';
736							  }
737							  else {
738								  $confirmed .= '<img src="images/longevent_mid.gif" width="9" height="9" alt="" border="0" hspace="0" vspace="0" />&nbsp;';
739							  }
740						  }
741						  $daydisplay .= '<td rowspan="' . $event_length[$i]['length'] . '" colspan="' . $drawWidth . '" align="left" valign="top" class="eventbg2_' . $event_calno . '">' . "\n";
742
743						  // Start drawing the event
744						  $event_temp  = $loop_event;
745						  $event 	   = openevent($getdate, $cal_time, $uid, $this_time_arr[$uid], 0, 0, 'ps');
746						  $event_temp  = str_replace('{EVENT}', $event, $event_temp);
747						  $event_temp  = str_replace('{EVENT_START}', date($timeFormat, $this_time_arr[$uid]['start_unixtime']), $event_temp);
748						  $event_temp  = str_replace('{EVENT_END}', date($timeFormat, $this_time_arr[$uid]['end_unixtime']), $event_temp);
749						  $event_temp  = str_replace('{CONFIRMED}', $confirmed, $event_temp);
750						  $event_temp  = str_replace('{EVENT_CALNO}', $event_calno, $event_temp);
751						  $daydisplay .= $event_temp;
752						  $daydisplay .= '</td>';
753						  // End event drawing
754
755						  break;
756						case 'started':
757							if ($ended_counter) {
758								$daydisplay .= '<td colspan="' . $ended_counter . '"' . $class . '>&nbsp;</td>';
759								$ended_counter = 0;
760							}
761							break;
762						case 'ended':
763							$daydisplay .= '<td colspan="' . $drawWidth . '"' . $class . '>&nbsp;</td>' . "\n";
764							break;
765					}
766					$event_length[$i]['length']--;
767					if ($event_length[$i]['length'] == 0) {
768						$event_length[$i]['state'] = 'ended';
769					}
770				}
771
772				// Clean up.
773				$emptyWidth += $ended_counter;
774				//fill empty space on the right
775				if ($emptyWidth > 0) {
776					$daydisplay .= '<td colspan="' . $emptyWidth . '"' . $class . '>&nbsp;</td>' . "\n";
777				}
778				while (isset($event_length[(sizeof($event_length) - 1)]) && $event_length[(sizeof($event_length) - 1)]['state'] == 'ended') {
779					array_pop($event_length);
780				}
781
782			}
783			$daydisplay .= '</tr>'."\n";
784		}
785
786		$this->page = preg_replace('!<\!-- loop row on -->.*<\!-- loop event off -->!Uis', $daydisplay, $this->page);
787
788
789	}
790
791	function tomorrows_events() {
792		global $phpiCal_config, $getdate, $master_array, $next_day, $timeFormat, $tomorrows_events_lines;
793
794		preg_match('!<\!-- switch t_allday on -->(.*)<\!-- switch t_allday off -->!Uis', $this->page, $match1);
795		preg_match('!<\!-- switch t_event on -->(.*)<\!-- switch t_event off -->!Uis', $this->page, $match2);
796		$loop_t_ad 	= trim($match1[1]);
797		$loop_t_e 	= trim($match2[1]);
798		$replace_ad	= '';
799		$replace_e	= '';
800		$return_adtmp	= '';
801		$return_etmp	= '';
802
803		if (isset($master_array[$next_day]) && is_array($master_array[$next_day]) && sizeof($master_array[$next_day]) > 0) {
804			foreach ($master_array[$next_day] as $cal_time => $event_times) {
805				foreach ($event_times as $uid => $val) {
806					$event_text = sanitizeForWeb(stripslashes(urldecode($val['event_text'])));
807					if ($event_text != '') {
808						if (!isset($val['event_start'])) {
809							$return_adtmp = openevent($next_day, $cal_time, $uid, $val, $phpiCal_config->tomorrows_events_lines, 21, 'psf');
810							$replace_ad  .= str_replace('{T_ALLDAY}', $return_adtmp, $loop_t_ad);
811						} else {
812							$return_etmp  = openevent($next_day, $cal_time, $uid, $val, $phpiCal_config->tomorrows_events_lines, 21, 'ps3');
813							$replace_e   .= str_replace('{T_EVENT}', $return_etmp, $loop_t_e);
814						}
815					}
816				}
817			}
818
819			$this->page = preg_replace('!<\!-- switch t_allday on -->.*<\!-- switch t_allday off -->!Uis', $replace_ad, $this->page);
820			$this->page = preg_replace('!<\!-- switch t_event on -->.*<\!-- switch t_event off -->!Uis', $replace_e, $this->page);
821
822		} else {
823
824			$this->page = preg_replace('!<\!-- switch tomorrows_events on -->.*<\!-- switch tomorrows_events off -->!Uis', '', $this->page);
825
826		}
827	}
828
829	function get_vtodo() {
830		global $phpiCal_config, $getdate, $master_array, $next_day, $timeFormat, $tomorrows_events_lines;
831
832		preg_match('!<\!-- switch show_completed on -->(.*)<\!-- switch show_completed off -->!Uis', $this->page, $match1);
833		preg_match('!<\!-- switch show_important on -->(.*)<\!-- switch show_important off -->!Uis', $this->page, $match2);
834		preg_match('!<\!-- switch show_normal on -->(.*)<\!-- switch show_normal off -->!Uis', $this->page, $match3);
835		$completed 	= trim($match1[1]);
836		$important 	= trim($match2[1]);
837		$normal 	= trim($match3[1]);
838		$nugget2	= '';
839		$todo_popup_data_index = 0;
840		if (is_array(@$master_array['-2'])) {
841			foreach ($master_array['-2'] as $vtodo_times) {
842				foreach ($vtodo_times as $val) {
843					if (isset($val['vtodo_text']) && !empty($val['vtodo_text'])) {
844						$vtodo_text = sanitizeForWeb(stripslashes(urldecode($val['vtodo_text'])));
845						if (isset($val['description']) && !empty($val['description'])) {
846							$description 	= sanitizeForWeb(stripslashes(urldecode($val['description'])));
847						} else {
848							$description = '';
849						}
850						$completed_date = $val['completed_date'];
851						$event_calna 	= $val['calname'];
852						$status 		= $val['status'];
853						$priority 		= $val['priority'];
854						$start_date 	= $val['start_date'];
855						$due_date 		= $val['due_date'];
856						$vtodo_array 	= array(
857							'cal'			=> $event_calna,
858							'completed_date'=> $completed_date,
859							'description'	=> $description,
860							'due_date'		=> $due_date,
861							'priority'		=> $priority,
862							'start_date'	=> $start_date,
863							'status'		=> $status,
864							'vtodo_text' 	=> $vtodo_text);
865						$vtodo_array 	= base64_encode(urlencode(serialize($vtodo_array)));
866						$todo_text		= str_replace('"', '\\"', $vtodo_text);
867						$todo = <<<HEREDOC
868<script language="Javascript" type="text/javascript">
869<!--
870var todoData = new TodoData("$vtodo_array", "$todo_text");
871document.todo_popup_data[$todo_popup_data_index] = todoData;
872// -->
873</script>
874
875HEREDOC;
876
877						$todo .= '<a class="psf" title="' . @$title . '" href="#" onclick="openTodoInfo(' . $todo_popup_data_index . '); return false;">';
878						$todo_popup_data_index++;
879						$vtodo_array = $todo;
880
881						$vtodo_text 	= word_wrap(str_replace('<br />', ' ', $vtodo_text), 21, $phpiCal_config->tomorrows_events_lines);
882						$data 			= array ('{VTODO_TEXT}', '{VTODO_ARRAY}');
883						$rep			= array ($vtodo_text, $vtodo_array);
884
885						// Reset this TODO's category.
886
887
888						$temp = '';
889						if ($status == 'COMPLETED' || ($val['completed_date'] != '' && $val['completed_time'] != '')) {
890							if ($phpiCal_config->show_completed == 'yes') {
891								$temp = $completed;
892							}
893						} elseif (isset($val['priority']) && ($val['priority'] != 0) && ($val['priority'] <= 5)) {
894							$temp = $important;
895						} else {
896							$temp = $normal;
897						}
898
899						// Do not include TODOs which do not have the
900						// category set.
901						if ($temp != '') {
902							$nugget1 = str_replace($data, $rep, $temp);
903							$nugget2 .= $nugget1;
904						}
905					}
906				}
907			}
908		}
909
910		// If there are no TODO items, completely hide the TODO list.
911		if (($nugget2 == '') || ($phpiCal_config->show_todos != 'yes')) {
912			$this->page = preg_replace('!<\!-- switch vtodo on -->.*<\!-- switch vtodo off -->!Uis', '', $this->page);
913		}
914
915		// Otherwise display the list of TODOs.
916		else {
917			$this->page = preg_replace('!<\!-- switch show_completed on -->.*<\!-- switch show_normal off -->!Uis', $nugget2, $this->page);
918		}
919	}
920
921	function draw_month($template_p, $offset = '+0', $type) {
922		global $phpiCal_config, $getdate, $master_array, $this_year, $this_month, $dateFormat_month, $cal, $minical_view, $month_event_lines, $daysofweekreallyshort_lang, $daysofweekshort_lang, $daysofweek_lang, $timeFormat_small, $timeFormat;
923
924		$unique_colors = $phpiCal_config->unique_colors;
925		preg_match('!<\!-- loop weekday on -->(.*)<\!-- loop weekday off -->!Uis', $template_p, $match1);
926		preg_match('!<\!-- loop monthdays on -->(.*)<\!-- loop monthdays off -->!Uis', $template_p, $match2);
927		preg_match('!<\!-- switch notthismonth on -->(.*)<\!-- switch notthismonth off -->!Uis', $template_p, $match3);
928		preg_match('!<\!-- switch istoday on -->(.*)<\!-- switch istoday off -->!Uis', $template_p, $match4);
929		preg_match('!<\!-- switch ismonth on -->(.*)<\!-- switch ismonth off -->!Uis', $template_p, $match5);
930		preg_match('!<\!-- loop monthweeks on -->(.*)<\!-- loop monthdays on -->!Uis', $template_p, $match6);
931		preg_match('!<\!-- loop monthdays off -->(.*)<\!-- loop monthweeks off -->!Uis', $template_p, $match7);
932
933		$loop_wd 			= trim($match1[1]);
934		$loop_md 			= trim($match2[1]);
935		$t_month[0]			= trim($match3[1]);
936		$t_month[1]			= trim($match4[1]);
937		$t_month[2] 		= trim($match5[1]);
938		$startweek 			= trim($match6[1]);
939		$endweek 			= trim($match7[1]);
940		if ($type != 'medium') {
941			$fake_getdate_time 	= strtotime($this_year.'-'.$this_month.'-15');
942			$fake_getdate_time	= strtotime($offset.' month', $fake_getdate_time);
943		} else {
944			$fake_getdate_time 	= strtotime($this_year.'-'.$offset.'-15');
945		}
946
947		$minical_month 		= date('m', $fake_getdate_time);
948		$minical_year 		= date('Y', $fake_getdate_time);
949		$first_of_month 	= $minical_year.$minical_month.'01';
950		$first_of_year 		= $minical_year.'0101';
951
952		// Add links in to the month/year views.
953		$dateFormat_month_local = str_replace('%B', '<a class="ps3" href="month.php?cal='.$cal.'&amp;getdate='.$first_of_month.'">%B</a>', $dateFormat_month);
954		$dateFormat_month_local = str_replace('%Y', '<a class="ps3" href="year.php?cal='.$cal.'&amp;getdate='.$first_of_year.'">%Y</a>', $dateFormat_month_local);
955
956		//$start_day 			= strtotime($phpiCal_config->week_start_day);
957		$start_day			= strtotime(dateOfWeek($getdate, $phpiCal_config->week_start_day));
958		$month_title 		= localizeDate ($dateFormat_month_local, $fake_getdate_time);
959		$month_date 		= date ('Ymd', $fake_getdate_time);
960
961		if ($type == 'small') {
962			$langtype = $daysofweekreallyshort_lang;
963		} elseif ($type == 'medium') {
964			$langtype = $daysofweekshort_lang;
965		} elseif ($type == 'large') {
966			$langtype = $daysofweek_lang;
967		}
968
969		$weekday_loop = '';
970		$middle = '';
971		for ($i=0; $i< $phpiCal_config->week_length; $i++) {
972			$day_num 		= date('w', $start_day);
973			$weekday 		= $langtype[$day_num];
974			$start_day 		= strtotime('+1 day', $start_day);
975			$loop_tmp 		= str_replace('{LOOP_WEEKDAY}', $weekday, $loop_wd);
976			$weekday_loop  .= $loop_tmp;
977		}
978
979		$start_day 			= strtotime(dateOfWeek($first_of_month, $phpiCal_config->week_start_day));
980		$i 					= 0;
981		$whole_month 		= TRUE;
982
983		do {
984			if ($i == 0) $middle .= $startweek; $i++;
985			#$temp_middle			= $loop_md;
986			$switch					= array('ALLDAY' => '', 'CAL' => $cal, 'MINICAL_VIEW' => $minical_view);
987			$check_month 			= date ('m', $start_day);
988			$daylink 				= date ('Ymd', $start_day);
989			$switch['DAY']	 		= date ('j', $start_day);
990			$switch['DAYLINK'] 		= date ('Ymd', $start_day);
991			if ($check_month != $minical_month) {
992				$temp = $t_month[0];
993			} elseif ($daylink == $getdate) {
994				$temp = $t_month[1];
995			} else {
996				$temp = $t_month[2];
997			}
998			$switch['ALLDAY'] = $switch['EVENT'] = '';
999			if (isset($master_array[$daylink]) && $i <= $phpiCal_config->week_length) {
1000				if ($type != 'small') {
1001					foreach ($master_array[$daylink] as $cal_time => $event_times) {
1002						foreach ($event_times as $uid => $val) {
1003							if (!isset($val['calnumber'])) continue;
1004
1005							if (($phpiCal_config->month_locations == 'yes') && isset($val['location']) && !empty($val['location'])) {
1006								$location = '<br /><span style="font-size: 95%">' . sanitizeForWeb(stripslashes(urldecode($val['location']))) . '</span>';
1007							}
1008							else $location = '';
1009
1010							$event_calno 	= $val['calnumber'];
1011							$event_calno	= (($event_calno - 1) % $unique_colors) + 1;
1012							if ($cal_time == -1) {
1013								if ($type == 'large') {
1014									$switch['ALLDAY'] .= '<div class="V10"><img src="templates/'.$phpiCal_config->template.'/images/monthdot_'.$event_calno.'.gif" alt="" width="9" height="9" border="0" />';
1015 									$switch['ALLDAY'] .= openevent($daylink, $cal_time, $uid, $val, $phpiCal_config->month_event_lines, 15, 'psf');
1016 									$switch['ALLDAY'] .= $location.'</div>';
1017								} else {
1018									$switch['ALLDAY'] = '<img src="templates/'.$phpiCal_config->template.'/images/allday_dot.gif" alt=" " width="11" height="10" border="0" />';
1019								}
1020							} else {
1021								$start2		 = date($timeFormat_small, $val['start_unixtime']);
1022								if ($type == 'large') {
1023									$switch['EVENT'] .= '<div class="V9"><img src="templates/'.$phpiCal_config->template.'/images/monthdot_'.$event_calno.'.gif" alt="" width="9" height="9" border="0" />';
1024 									$switch['EVENT'] .= openevent($daylink, $cal_time, $uid, $val, $phpiCal_config->month_event_lines, 10, 'ps3', '<span style="font-weight: bold;">'.$start2.'</span> ');
1025 									$switch['EVENT'] .= $location.'</div>';
1026								} else {
1027									$switch['EVENT'] = '<img src="templates/'.$phpiCal_config->template.'/images/event_dot.gif" alt=" " width="11" height="10" border="0" />';
1028								}
1029							}
1030						}
1031					}
1032				}
1033			}
1034
1035			$switch['EVENT'] = (isset($switch['EVENT'])) ? $switch['EVENT'] : '';
1036			$switch['ALLDAY'] = (isset($switch['ALLDAY'])) ? $switch['ALLDAY'] : '';
1037
1038			#echo "<pre>";print_r($switch);echo "</pre>";
1039
1040			foreach ($switch as $tag => $data) {
1041				$temp = str_replace('{'.$tag.'}', $data, $temp);
1042			}
1043			$middle .= $temp;
1044
1045			$start_day = strtotime('+1 day', $start_day);
1046			if ($i == $phpiCal_config->week_length) {
1047				if ($phpiCal_config->week_length != 7) {
1048					$start_day = strtotime('+'.(7-$phpiCal_config->week_length).' day', $start_day);
1049				}
1050				$i = 0;
1051				$middle .= $endweek;
1052				$checkagain = date ('m', $start_day);
1053				if ($checkagain != $minical_month) $whole_month = FALSE;
1054			}
1055		} while ($whole_month == TRUE);
1056
1057		$return = str_replace('<!-- loop weekday on -->'.$match1[1].'<!-- loop weekday off -->', $weekday_loop, $template_p);
1058		$return = preg_replace('!<\!-- loop monthweeks on -->.*<\!-- loop monthweeks off -->!Uis', $middle, $return);
1059		$return = str_replace('{MONTH_TITLE}', $month_title, $return);
1060		$return = str_replace('{CAL}', $cal, $return);
1061		$return = str_replace('{MONTH_DATE}', $month_date, $return);
1062
1063		return $return;
1064	}
1065
1066	function nomonthbottom() {
1067		$this->page = preg_replace('!<\!-- switch showbottom on -->.*<\!-- switch showbottom off -->!Uis','', $this->page);
1068	}
1069
1070	function nosearch() {
1071		$this->page = preg_replace('!<\!-- switch show_search on -->.*<\!-- switch show_search off -->!Uis','', $this->page);
1072	}
1073
1074	function monthbottom() {
1075		global $phpiCal_config, $getdate, $master_array, $this_year, $this_month, $cal, $timeFormat, $dateFormat_week_list, $lang;
1076		preg_match('!<\!-- loop showbottomevents_odd on -->(.*)<\!-- loop showbottomevents_odd off -->!Uis', $this->page, $match1);
1077		preg_match('!<\!-- loop showbottomevents_even on -->(.*)<\!-- loop showbottomevents_even off -->!Uis', $this->page, $match2);
1078
1079		$loop[0] 	= trim($match1[1]);
1080		$loop[1] 	= trim($match2[1]);
1081
1082		$m_start = $this_year.$this_month.'01';
1083		$u_start = strtotime($m_start);
1084		$i=0;
1085		$seen_events = array();
1086		$middle = '';
1087		do {
1088			if (isset($master_array[$m_start])) {
1089				foreach ($master_array[$m_start] as $cal_time => $event_times) {
1090				#	$switch['CAL'] 			= $cal;
1091				#	$switch['START_DATE'] 	= localizeDate ($dateFormat_week_list, $u_start);
1092					$start_date 	= localizeDate ($dateFormat_week_list, $u_start);
1093					foreach ($event_times as $uid => $val) {
1094						if (isset($seen_events[$uid]) && @$val['spans_day'] == 1) continue;
1095						$seen_events[$uid] = 1;
1096						$switch['CAL'] 			= $cal;
1097						$switch['START_DATE'] 	= $start_date;
1098						$switch['CALNAME'] 	= $val['calname'];
1099						if (!isset($val['event_start'])) {
1100							$switch['START_TIME'] 	= $lang['l_all_day'];
1101							$switch['EVENT_TEXT'] 	= openevent($m_start, $cal_time, $uid, $val, $phpiCal_config->month_event_lines, 15, 'psf');
1102							$switch['DESCRIPTION'] 	= sanitizeForWeb(stripslashes(urldecode($val['description'])));
1103						} else {
1104							$event_start = $val['start_unixtime'];
1105							$event_end 	 = (isset($val['display_end'])) ? $val['display_end'] : $val['event_end'];
1106							$event_start = date($timeFormat, $val['start_unixtime']);
1107							$event_end   = date($timeFormat, $val['end_unixtime']);
1108							$switch['START_TIME'] 	= $event_start . ' - ' . $event_end;
1109							$switch['EVENT_TEXT'] 	= openevent($m_start, $cal_time, $uid, $val, 0, 15, 'psf');
1110							$switch['DESCRIPTION'] 	= sanitizeForWeb(stripslashes(urldecode($val['description'])));
1111						}
1112
1113						if ($switch['EVENT_TEXT'] != '') {
1114							$switch['DAYLINK'] = $m_start;
1115							$temp = $loop[$i];
1116							foreach ($switch as $tag => $data) {
1117								$temp = str_replace('{'.$tag.'}', $data, $temp);
1118							}
1119							$middle .= $temp;
1120							$i = ($i == 1) ? 0 : 1;
1121						}
1122						unset ($switch);
1123					}
1124				}
1125			}
1126			$u_start 	 = strtotime('+1 day', $u_start);
1127			$m_start 	 = date('Ymd', $u_start);
1128			$check_month = date('m', $u_start);
1129		#	unset ($switch);
1130		} while ($this_month == $check_month);
1131
1132		$this->page = preg_replace('!<\!-- loop showbottomevents_odd on -->.*<\!-- loop showbottomevents_even off -->!Uis', $middle, $this->page);
1133
1134	}
1135
1136	function Page($file = 'std.tpl') {
1137		global $phpiCal_config;
1138		if (!file_exists($file)){
1139			#look for it in default if not found
1140			$file = str_replace('templates/'.$phpiCal_config->template,'templates/default',$file);
1141			if (!file_exists($file)) die('Template file '.$file.' not found.');
1142		}
1143		$this->page = join('', file($file));
1144		return;
1145	}
1146
1147	function parse($file) {
1148		global $phpiCal_config; $lang;
1149		if (basename(dirname($file)) == $phpiCal_config->template || $file =='./functions/event.js'){
1150			if (!is_file($file)){
1151				#look for it in default if not found
1152				$file = str_replace('templates/'.$phpiCal_config->template,'templates/default',$file);
1153			}
1154			if (!is_file($file)){
1155				exit(error($lang['l_error_path'], $file));
1156			}
1157			ob_start();
1158			include($file);
1159			$buffer = ob_get_contents();
1160			ob_end_clean();
1161			return $buffer;
1162		}
1163	}
1164
1165	function replace_tags($tags = array()) {
1166		if (sizeof($tags) > 0)
1167			foreach ($tags as $tag => $data) {
1168
1169				// This removes any unfilled tags
1170				if (!$data) {
1171					$this->page = preg_replace('!<\!-- switch ' . $tag . ' on -->.*<\!-- switch ' . $tag . ' off -->!Uis', '', $this->page);
1172				}
1173
1174				// This replaces any tags
1175				$this->page = str_replace('{' . strtoupper($tag) . '}', $data, $this->page);
1176			}
1177
1178		else
1179			die('No tags designated for replacement.');
1180		}
1181
1182	function replace_files($tags = array()) {
1183		if (sizeof($tags) > 0)
1184			foreach ($tags as $tag => $data) {
1185
1186				// This opens up another template and parses it as well.
1187				$data = $this->parse($data);
1188
1189				// This removes any unfilled tags
1190				if (!$data) {
1191					$this->page = preg_replace('!<\!-- switch ' . $tag . ' on -->.*<\!-- switch ' . $tag . ' off -->!Uis', '', $this->page);
1192				}
1193
1194				// This replaces any tags
1195				$this->page = str_replace('{' . strtoupper($tag) . '}', $data, $this->page);
1196			}
1197
1198		else
1199			die('No tags designated for replacement.');
1200		}
1201
1202	function output() {
1203		global $phpiCal_config, $php_started, $lang, $template_started, $cpath;
1204
1205		// Looks for {MONTH} before sending page out
1206		preg_match_all ('!\{MONTH_([A-Z]*)\|?([+|-])([0-9]{1,2})\}!Uis', $this->page, $match);
1207		if (sizeof($match) > 0) {
1208			$i=0;
1209			foreach ($match[1] as $key => $val) {
1210				if ($match[1][$i] == 'SMALL') {
1211					$template_file 	= $this->parse(BASE.'templates/'.$phpiCal_config->template.'/month_small.tpl');
1212					$type 			= 'small';
1213					$offset 		= $match[2][$i].$match[3][$i];
1214				} elseif ($match[1][$i] == 'MEDIUM') {
1215					$template_file 	= $this->parse(BASE.'templates/'.$phpiCal_config->template.'/month_medium.tpl');
1216					$type 			= 'medium';
1217					$offset 		= $match[3][$i];
1218				} else {
1219					$template_file 	= $this->parse(BASE.'templates/'.$phpiCal_config->template.'/month_large.tpl');
1220					$type 			= 'large';
1221					$offset 		= $match[2][$i].$match[3][$i];
1222				}
1223				$data = $this->draw_month($template_file, $offset, $type);
1224				$this->page = str_replace($match[0][$i], $data, $this->page);
1225				$i++;
1226			}
1227		}
1228
1229		$php_ended = @getmicrotime();
1230		$generated1 = number_format(($php_ended-$php_started),3);
1231		$generated2 = number_format(($php_ended-$template_started),3);
1232		$this->page = str_replace('{GENERATED1}', $generated1, $this->page);
1233		$this->page = str_replace('{GENERATED2}', $generated2, $this->page);
1234		if ($phpiCal_config->enable_rss != 'yes') {
1235			$this->page = preg_replace('!<\!-- switch rss_powered on -->.*<\!-- switch rss_powered off -->!Uis', '', $this->page);
1236		} else {
1237			$this->page = str_replace('{BASE}', BASE, $this->page);
1238		}
1239		if ($cpath){
1240			$this->page = str_replace('&amp;getdate', '&amp;cpath='.$cpath.'&amp;getdate', $this->page);
1241		}
1242		print($this->page);
1243	}
1244}
1245?>
1246