PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Calendar/rss/rss.php

https://github.com/tlezotte/HCR
PHP | 256 lines | 171 code | 32 blank | 53 comment | 30 complexity | 5e622ed6188aaaba306a7449f76475c2 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /********************************************************************************
  3. * Modified from phpicalendar 2.0a distribution by Jim Hu
  4. * philosophical changes
  5. * - instead of having separate generators, use a date range for all views (done)
  6. * - change the rss generation method to conform to standards(not done)
  7. * PHP note: #@ is error control operator to suppress execution halt on error
  8. * - used below to deal with undef?
  9. *
  10. * using rssview, RSS feeds can be specified to return events for a given day, week, month, or year
  11. * feeds can be specified for a number of days to or from a given date
  12. * feeds can be specified for a range of dates
  13. *
  14. *********************************************************************************/
  15. /* Modified from 2.21 by dyfrin 2006/03/08 19:09:28
  16. Changes:
  17. -RSS changed to 2.0, encoding removed, languages converted to ISO standard for feeds
  18. -RSS title changed to be set by config.inc.php. Make sure that is added to it.
  19. Lines modified: 135-165, 208-223
  20. Additional mods by J. Hu
  21. */
  22. define('BASE', '../');
  23. require(BASE.'functions/init.inc.php');
  24. if ($enable_rss != 'yes') {
  25. die ("RSS feeds are not enabled on this site.");
  26. }
  27. include_once(BASE.'functions/date_functions.php');
  28. //set the range of days to return based on the view chosen
  29. $rssview = $_GET['rssview'];
  30. if (isset($_GET['getdate']) && $_GET['getdate'] !=''){
  31. $getdate = $_GET['getdate'];
  32. }else{
  33. $getdate = date("Ymd");
  34. }
  35. # for all views, $fromdate is the first day to be shown and $todate should be the last day.
  36. switch ($rssview){
  37. case 'day':
  38. $fromdate = $getdate;
  39. $todate = $getdate;
  40. $theview = $lang['l_day'];
  41. break;
  42. case 'week':
  43. $fromdate = dateOfWeek($getdate, 'Sunday');
  44. $todate = $fromdate + 6;
  45. $theview = $lang['l_week']." of ".date('n/d/Y',strtotime($fromdate));
  46. break;
  47. case 'month':
  48. $parse_month = date ("Ym", strtotime($getdate));
  49. $fromdate = ($parse_month *100) + 1;
  50. $todate = ($parse_month *100) + date("t",strtotime($getdate));
  51. $theview = date('M Y',strtotime($fromdate));
  52. break;
  53. case 'year':
  54. if(isset($_GET['year'])){
  55. $theyear = $_GET['year'];
  56. }else{
  57. $theyear = substr($getdate,0,4);
  58. }
  59. $fromdate = ($theyear*10000)+101;
  60. $todate = date("Ymd", strtotime($theyear*10000+1231));
  61. $theview = $theyear;
  62. break;
  63. case 'daysfrom':
  64. $fromdate = $getdate;
  65. $todate = date("Ymd", strtotime($getdate) + $_GET['days']*60*60*24);
  66. $theview = $_GET['days']." days from ".date('n/d/Y',strtotime($fromdate));
  67. break;
  68. case 'daysto':
  69. $todate = $getdate;
  70. $fromdate = date("Ymd", strtotime($getdate) - $_GET['days']*60*60*24);
  71. $theview = $_GET['days']." days before ".date('n/d/Y',strtotime($todate));
  72. break;
  73. case 'range':
  74. if(isset($_GET['from'])){
  75. $fromdate = $_GET['from'];
  76. }else{
  77. $fromdate = $getdate;
  78. }
  79. $todate = $_GET['to'];
  80. $theview = date('n/d/Y',strtotime($fromdate)).'-'.date('n/d/Y',strtotime($todate));
  81. break;
  82. default:
  83. #default to week
  84. $fromdate = dateOfWeek($getdate, 'Sunday');
  85. $todate = $fromdate + 6;
  86. $theview = "";
  87. }
  88. #need to give ical_parser the most distant date to correctly set up master_array.
  89. $getdate = $todate;
  90. #echo "from:$fromdate to:$todate";
  91. #Note that ical_parser supplies cal_displayname.
  92. include(BASE.'functions/ical_parser.php');
  93. $events_count = 0;
  94. // calculate a value for Last Modified and ETag
  95. $cal = implode(",",$cals);
  96. //get filemtime from master array
  97. $filemod = 0; #default to start of unix era, overwrite with most recent mtime from master array
  98. foreach ($master_array['-4'] as $calinfo){
  99. if ($calinfo['mtime'] > $filemod) $filemod = $calinfo['mtime'];
  100. }
  101. $filemodtime = date("r", $filemod);
  102. //send relevant headers
  103. header ("Last-Modified: $filemodtime");
  104. header ("ETag:\"$filemodtime\"");
  105. // checks the user agents headers to see if they kept track of our
  106. // stuff, if so be nice and send back a 304 and exit.
  107. if ( ($_SERVER['HTTP_IF_MODIFIED_SINCE'] == $filemodtime) || ($_SERVER['HTTP_IF_NONE_MATCH'] == $filemodtime)){
  108. header ("HTTP/1.1 304 Not Modified");
  109. exit;
  110. }
  111. /* Change languages to ISO 639-1 to validate RSS without changing long version in config.inc.php */
  112. $user_language = array ("english", "polish", "german", "french", "dutch", "italian", "japanese", "norwegian", "spanish", "swedish", "portuguese", "catalan", "traditional_chinese", "esperanto", "korean");
  113. $iso_language = array ("en", "pl", "de", "fr", "nl", "da", "it", "ja", "no", "es", "sv", "pt", "ca", "zh-tw", "eo", "ko");
  114. $rss_language = str_replace($user_language, $iso_language, $language);
  115. /* End language modification */
  116. $rss = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"."\n";
  117. /* Use 1.0 and strip encoding, use rss_language */
  118. $rss .= '<rdf:RDF
  119. xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
  120. xmlns:ev="http://purl.org/rss/1.0/modules/event/"
  121. xmlns:dc="http://purl.org/dc/elements/1.1/"
  122. xmlns="http://purl.org/rss/1.0/">'."\n";
  123. $rss .= '<channel rdf:about="'.$default_path.'/rss/rss.php/';
  124. if (isset($cpath) && $cpath !='') $rss_link.="?cpath=$cpath";
  125. $rss .='">'."\n";
  126. $rss .= '<title>'.$cal_displayname;
  127. if ($theview !=""){$rss .= ' - '.$theview;}
  128. $rss .= "</title>\n";
  129. $rss .= '<link>'.htmlspecialchars("$default_path").'</link>'."\n";
  130. if (isset($cpath) && $cpath !='') $rss_link.="?cpath=$cpath";
  131. $rss .= "<link>$rss_link</link>\n";
  132. $rss .= '<description>'.$cal_displayname.' '.$lang['l_calendar'].' - '.$theview.'</description>'."\n";
  133. $rss .= '<language>'.$language.'</language>'."\n";
  134. $rss .= '<copyright>Copyright '.date('Y').', '.htmlspecialchars ("$default_path").'</copyright>'."\n";
  135. //generate the items
  136. $numdays = round((strtotime($todate) - strtotime($fromdate))/(60*60*24))+1;
  137. $thisdate = $fromdate; # start at beginning of date range,
  138. # note that usage of $thisdate is different from distribution
  139. # I use it as a date, dist uses it as a time
  140. $i = 1; #day counter
  141. $uid_arr = array();
  142. do {
  143. $thisdate=date('Ymd', strtotime($thisdate));
  144. #echo "Date: $thisdate<br>\n";
  145. $dayofweek = localizeDate ("%a %b %e %Y", strtotime($thisdate));
  146. if (isset($master_array[($thisdate)]) && sizeof($master_array[($thisdate)]) > 0) {
  147. foreach ($master_array[("$thisdate")] as $event_times) {
  148. foreach ($event_times as $uid=>$val) {
  149. #handle multiday all day events
  150. if(!$val["event_start"]){
  151. if (isset($uid_arr[$uid])){
  152. $uid_arr[$uid] .= "+$dayofweek" ;
  153. continue;
  154. }else{
  155. $uid_arr[$uid] = "$dayofweek" ;
  156. }
  157. $event_start = $lang['l_all_day'];
  158. }else{
  159. $event_start = @$val["event_start"];
  160. $event_start = date ($timeFormat, @strtotime ("$event_start"));
  161. }
  162. $event_text = stripslashes(urldecode($val["event_text"]));
  163. $event_text = strip_tags($event_text, '<b><i><u>');
  164. $event_text = str_replace('&','&amp;',$event_text);
  165. $event_text = str_replace('&amp;amp;','&amp;',$event_text);
  166. # $event_text = urlencode($event_text);
  167. #uncomment for shorter event text with ...
  168. # $event_text = word_wrap($event_text, 21, $tomorrows_events_lines);
  169. $description = stripslashes(urldecode($val["description"]));
  170. $description = strip_tags($description, '<b><i><u>');
  171. $description = str_replace('&','&amp;',$description);
  172. $description = str_replace('&amp;amp;','&amp;',$description);
  173. $rss_title = urldecode ("$dayofweek: $event_text");
  174. $rss_link = "$default_path/day.php?getdate=$thisdate&amp;cal=$cal";
  175. if (isset($cpath) && $cpath !='') $rss_link.="&amp;cpath=$cpath";
  176. $rss_description = htmlspecialchars ("$dayofweek $event_start: $description");
  177. $rss .= '<item>'."\n";
  178. $rss .= '<uid>'.$uid.'</uid>'."\n";
  179. $rss .= '<event_start>'.$event_start.'</event_start>'."\n";
  180. $rss .= '<title>'.$rss_title.'</title>'."\n";
  181. /* custom stuff for Jim Hu's RSS feeds. Deprecated
  182. $rss .= '<seminardate>'.$dayofweek.'</seminardate>'."\n";
  183. $rss .= '<seminarspeaker>'.$event_text.'</seminarspeaker>'."\n";
  184. $rss .= '<seminartitle>'.$description.'</seminartitle>'."\n";
  185. $rss .= '<tagged>'.$val["description"].'</tagged>'."\n";
  186. $rss .= '<seminarhost>'.$val['attendee'].'</seminarhost>'."\n";
  187. $rss .= '<organizer>'.$val['organizer'].'</organizer>'."\n";
  188. $rss .= '<status>'.$val['status'].'</status>'."\n";
  189. */
  190. $rss .= '<link>'.$rss_link.'</link>'."\n";
  191. $rss .= '<description>'.$rss_description.'</description>'."\n";
  192. if (isset($val['location']) && $val['location'] !=''){
  193. $location = str_replace('&','&amp;',$val['location']);
  194. $location = str_replace('&amp;amp;','&amp;',$location);
  195. $rss .= '<location>'.$location."</location>\n";
  196. }
  197. $rss .= '</item>'."\n";
  198. $events_count++;
  199. }
  200. }
  201. }
  202. if (($events_count < 1) && ($i == $numdays)) {
  203. $rss .= '<item>'."\n";
  204. $rss .= '<title>No events found</title>'."\n";
  205. $rss .= '<link>'.htmlspecialchars ("$default_path").'</link>'."\n";
  206. $rss .= '</item>'."\n";
  207. }
  208. $thisdate++;
  209. $i++;
  210. } while ($i <= $numdays);
  211. $rss .= '</channel>'."\n";
  212. $rss .= '</rss>'."\n";
  213. foreach ($uid_arr as $uid=>$date_range){
  214. #echo "date_range:$date_range<br>";
  215. if(strpos($date_range,"+")>0){
  216. #echo "+ in date_range<br>";
  217. $temp = explode("+",$date_range);
  218. $date_range = $temp[0].'-'.array_pop($temp);
  219. }
  220. $rss = str_replace("<uid>$uid</uid>\n<event_start>".$lang['l_all_day']."</event_start>","<uid>$uid</uid>\n<event_start>$date_range</event_start>", $rss);
  221. }
  222. header ("Content-Type: text/xml");
  223. echo "$rss";
  224. ?>