Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
data_source_ical.inc
1 <?php
18 require_once SQ_INCLUDE_PATH.'/asset.inc';
19 require_once SQ_CORE_PACKAGE_PATH.'/data_source/data_source/data_source.inc';
20 require_once SQ_LIB_PATH.'/html_form/html_form.inc';
21 require_once SQ_FUDGE_PATH.'/general/datetime.inc';
22 require_once(SQ_INCLUDE_PATH.'/general_occasional.inc');
23 
36 {
37 
38 
45  function __construct($assetid=0)
46  {
47  parent::__construct($assetid);
48 
49  }//end constructor
50 
51 
65  function _getAllowedLinks()
66  {
67  // any link is allowed
68  $allowed_link['asset']['card'] = 'M';
69  $allowed_link['asset']['exclusive'] = FALSE;
70 
71  $links[SQ_LINK_TYPE_1] = $allowed_link;
72  $links[SQ_LINK_TYPE_2] = $allowed_link;
73  $links[SQ_LINK_TYPE_3] = $allowed_link;
74  $links[SQ_LINK_NOTICE] = $allowed_link;
75 
76  return $links;
77 
78  }//end _getAllowedLinks()
79 
80 
87  function &getResultSet()
88  {
89  $GLOBALS['SQ_SYSTEM']->pm->startTimer($this);
90 
91  $hash = md5($this->name);
92 
93  if (!isset($this->_tmp[$hash])) {
94  $result = parent::getResultSet($hash);
95  if (empty($result)) {
96  $result = $this->getItems();
97  if (!empty($result)) {
98  $this->_tmp[$hash] = $result;
99  parent::setResultSet($result, $hash);
100  }//end if
101  } else {
102  $this->_tmp[$hash] = $result;
103  }
104  } else {
105  $result = $this->_tmp[$hash];
106  }
107 
108  $GLOBALS['SQ_SYSTEM']->pm->stopTimer($this);
109 
110  return $result;
111 
112  }//end getResultSet()
113 
114 
123  function getItems()
124  {
125  $result = Array();
126  $file = $this->attr('path');
127  if ((strpos($file, 'http://') === 0) || (strpos($file, 'ftp://') === 0)) {
128  $file = str_replace(' ', '%20', $file);
129  }//end if
130  $ical_data = @file_get_contents($file);
131  $results = Array();
132  $startCalendar = FALSE;
133  $startEvent = FALSE;
134  $index = 0;
135 
136  if ($ical_data) {
137  // "Unfold" the lines in the ical feed
138  $ical_data = str_replace("\r\n ", '', str_replace("\r\n\t", '', $ical_data));
139  $ical_lines = explode("\r\n", $ical_data);
140  foreach($ical_lines as $line) {
141  $line = trim($line);
142  $matches = Array();
143  $pattern = '/([^;:]*)([^:]*):(.*)/is';
144  $status = preg_match($pattern, $line, $matches);
145  $command = array_get_index($matches, 1, '');
146  $parameter = array_get_index($matches, 3, '');
147 
148  if (!empty($command)) {
149  switch ($command) {
150  case 'BEGIN':
151  if ($startCalendar) {
152  // We are in the calendar, so allow us to begin events etc.
153  if ($parameter == 'VEVENT') {
154  $startEvent = TRUE;
155  $results[$index] = Array();
156  }//end if
157  } else {
158  // Not in calendar
159  if ($parameter == 'VCALENDAR') {
160  $startCalendar = TRUE;
161  }//end if
162  }//end if
163  break;
164  case 'END':
165  if ($parameter == 'VEVENT') {
166  $startEvent = FALSE;
167  $index++;
168  } else if ($parameter == 'VCALENDAR') {
169  $startCalendar = TRUE;
170  }//end if
171  break;
172  default:
173  if ($startEvent) {
174  switch ($command) {
175  case 'DTEND':
176  case 'DTSTAMP':
177  case 'DTSTART':
178  case 'CREATED':
179  case 'LAST-MODIFIED':
180  $results[$index][strtolower($command.'-raw')] = $parameter;
181  $date_str = $parameter;
182  if (strlen($parameter) == 8) {
183  $date_str .= 'T------';
184  }//end if
185  if (substr($date_str, 15, 1) === 'Z') {
186  $timestamp = gmmktime( substr($date_str, 9, 2),
187  substr($date_str, 11, 2),
188  substr($date_str, 13, 2),
189  substr($date_str, 4, 2),
190  substr($date_str, 6, 2),
191  substr($date_str, 0, 4) );
192  $value = readable_datetime($timestamp);
193  } else {
194  $date_value = substr($date_str, 0, 4); // Year
195  $date_value .= '-'.substr($date_str, 4, 2); // Month
196  $date_value .= '-'.substr($date_str, 6, 2); // Day
197  $date_value .= 'T'.substr($date_str, 9, 2); // Hour
198  $date_value .= ':'.substr($date_str, 11, 2); // Minute
199  $date_value .= ':'.substr($date_str, 13, 2); // Second
200  $value = readable_datetime(iso8601_ts($date_value));
201  }//end if
202  break;
203  default:
204  $value = $parameter;
205  }//end switch
206  $results[$index][strtolower($command)] = $value;
207  if ($command == 'SUMMARY') {
208  $results[$index]['title'] = ellipsisize($value, 20);
209  }//end if
210  }//end if
211  }//end switch
212  }//end if
213  }//end foreach
214  }//end if
215 
216  return $results;
217 
218  }//end getItems()
219 
220 
221 }//end class
222 
223 ?>