WebCalendar PHP Cross Reference Groupware Applications

Source: /rss.php - 365 lines - 12439 bytes - Summary - Text - Print

   1  <?php
   2  /* $Id: rss.php,v 1.46.2.10 2011/04/27 00:27:35 rjones6061 Exp $
   3   *
   4   * Description:
   5   * This script is intended to be used outside of normal WebCalendar use,
   6   * as an RSS 2.0 feed to a RSS client.
   7   *
   8   * You must have "Enable RSS feed" set to "Yes"
   9   * in both System Settings and in the specific user's Preferences.
  10   *
  11   * Simply use the URL of this file as the feed address in the client.
  12   * For public user access:
  13   * http://xxxxx/aaa/rss.php
  14   * For any other user (where "joe" is the user login):
  15   * http:/xxxxxx/aaa/rss.php?user=joe
  16   *
  17   * By default (if you do not edit this file), events
  18   * will be loaded for either:
  19   *   - the next 30 days
  20   *   - the next 10 events
  21   *
  22   * Input parameters:
  23   * You can override settings by changing the URL parameters:
  24   *   - days: number of days ahead to look for events
  25   *   - cat_id: specify a category id to filter on
  26   *   - repeats: output all events including all repeat instances
  27   *       repeats=0 do not output repeating events (default)
  28   *       repeats=1 outputs repeating events
  29   *       repeats=2 outputs repeating events but suppresses display of
  30   *                 2nd & subsequent occurences of daily events
  31   *   - user: login name of calendar to display (instead of public user).
  32   *       You must have the following System Settings configured for this:
  33   *         Allow viewing other user's calendars: Yes
  34   *         Public access can view others: Yes
  35   *   - showdate: put the date and time (if specified in the title
  36   *       of the item) in the title
  37   *
  38   * Security:
  39   * $RSS_ENABLED must be set true
  40   * $USER_RSS_ENABLED must be set true unless this is for the public user
  41   * $USER_REMOTE_ACCESS can be set as follows in pref.php
  42   *      0 = Public entries only
  43   *      1 = Public & Confidential entries only
  44   *      2 = All entries are included in the feed *USE WITH CARE
  45   *
  46   * We do not include unapproved events in the RSS feed.
  47   *
  48   *
  49   * TODO
  50   * Add other RSS 2.0 options such as media.
  51   * Add <managingEditor>: dan@spam_me.com (Dan Deletekey)
  52   */
  53  
  54  $debug = false;
  55  
  56  include_once  'includes/translate.php';
  57  require_once 'includes/classes/WebCalendar.class';
  58  require_once 'includes/classes/Event.class';
  59  require_once 'includes/classes/RptEvent.class';
  60  
  61  $WebCalendar = new WebCalendar ( __FILE__ );
  62  
  63  include  'includes/formvars.php';
  64  include  'includes/functions.php';
  65  include  'includes/config.php';
  66  include  'includes/dbi4php.php';
  67  
  68  $WebCalendar->initializeFirstPhase ();
  69  
  70  include 'includes/' . $user_inc;
  71  
  72  include_once  'includes/validate.php';
  73  include  'includes/site_extras.php';
  74  
  75  include_once  'includes/xcal.php';
  76  
  77  $WebCalendar->initializeSecondPhase ();
  78  
  79  load_global_settings ();
  80  
  81  $WebCalendar->setLanguage ();
  82  
  83  if ( empty ( $RSS_ENABLED ) || $RSS_ENABLED != 'Y' ) {
  84    header ( 'Content-Type: text/plain' );
  85    echo print_not_auth (28);
  86    exit;
  87  }
  88  /* Configurable settings for this file. You may change the settings below to
  89   * change the default settings. These settings will likely move into the System
  90   * Settings in the web admin interface in a future release.
  91   */
  92  
  93  // Show the date in the title and how to format it.
  94  $date_in_title = false; //Can override with "rss.php?showdate=1|true".
  95  $showdate = getValue ( 'showdate' );
  96  if ( ! empty ( $showdate ) )
  97    $date_in_title = ( $showdate == 'true' || $showdate == 1 ? true : false );
  98  
  99  $date_format = 'M jS'; //Aug 10th, 8/10
 100  $time_format = 'g:ia'; //4:30pm, 16:30
 101  $time_separator = ', '; //Aug 10th @ 4:30pm, Aug 10th, 4:30pm
 102  
 103  // Default time window of events to load.
 104  // Can override with "rss.php?days=60".
 105  $numDays = 30;
 106  
 107  // Max number of events to display.
 108  // Can override with "rss.php?max=20".
 109  $maxEvents = 10;
 110  
 111  // Login of calendar user to use.
 112  // '__public__' is the login name for the public user.
 113  $username = '__public__';
 114  
 115  // Allow the URL to override the user setting such as "rss.php?user=craig".
 116  $allow_user_override = true;
 117  
 118  // Load layers.
 119  $load_layers = false;
 120  
 121  // Load just a specified category (by its id).
 122  // Leave blank to not filter on category (unless specified in URL).
 123  // Can override in URL with "rss.php?cat_id=4".
 124  $cat_id = '';
 125  
 126  // Load all repeating events.
 127  // Can override with "rss.php?repeats=1".
 128  $allow_repeats = false;
 129  
 130  // Load show only first occurence within the given time span of daily repeating events.
 131  // Can override with "rss.php?repeats=2".
 132  $show_daily_events_only_once = false;
 133  
 134  // End configurable settings...
 135  
 136  // Set for use elsewhere as a global.
 137  $login = $username;
 138  
 139  if ( $allow_user_override ) {
 140    $u = getValue ( 'user', '[A-Za-z0-9_\.=@,\-]+', true );
 141    if ( ! empty ( $u ) ) {
 142      if ( $u == 'public' )
 143        $u = '__public__';
 144      // We also set $login since some functions assume that it is set..
 145      $login = $username = $u;
 146    }
 147  }
 148  
 149  load_user_preferences ();
 150  
 151  // public entries only
 152  $allow_access = array ( 'P' );
 153  
 154  // .
 155  // Determine what remote access has been set up by user.
 156  // This will only be used if $username is not __public__.
 157  if ( isset ( $USER_REMOTE_ACCESS ) && $username != '__public__' ) {
 158    if ( $USER_REMOTE_ACCESS > 0 ) // plus confidential
 159      $allow_access[] = 'C';
 160  
 161    if ( $USER_REMOTE_ACCESS == 2 ) // plus private
 162      $allow_access[] = 'R';
 163  }
 164  user_load_variables ( $login, 'rss_' );
 165  $creator = ( $username == '__public__' ) ? 'Public' : $rss_fullname;
 166  
 167  if ( $username != '__public__' &&
 168    ( empty ( $USER_RSS_ENABLED ) || $USER_RSS_ENABLED != 'Y' ) ) {
 169    header ( 'Content-Type: text/plain' );
 170    echo print_not_auth (29);
 171    exit;
 172  }
 173  
 174  $cat_id = '';
 175  if ( $CATEGORIES_ENABLED == 'Y' ) {
 176    $x = getValue ( 'cat_id', '-?[0-9]+', true );
 177    if ( ! empty ( $x ) ) {
 178      load_user_categories ();
 179      $cat_id = $x;
 180      $category = $categories[$cat_id]['cat_name'];
 181    }
 182  }
 183  
 184  if ( $load_layers )
 185    load_user_layers ( $username );
 186  
 187  // Calculate date range.
 188  $date = getValue ( 'date', '-?[0-9]+', true );
 189  if ( empty ( $date ) || strlen ( $date ) != 8 )
 190    // If no date specified, start with today.
 191    $date = date ( 'Ymd' );
 192  
 193  $thisyear = substr ( $date, 0, 4 );
 194  $thismonth = substr ( $date, 4, 2 );
 195  $thisday = substr ( $date, 6, 2 );
 196  
 197  $startTime = mktime ( 0, 0, 0, $thismonth, $thisday, $thisyear );
 198  
 199  $x = getValue ( 'days', '-?[0-9]+', true );
 200  if ( ! empty ( $x ) )
 201    $numDays = $x;
 202  
 203  // Don't let a malicious user specify more than 365 days.
 204  if ( $numDays > 365 )
 205    $numDays = 365;
 206  
 207  $x = getValue ( 'max', '-?[0-9]+', true );
 208  if ( ! empty ( $x ) )
 209    $maxEvents = $x;
 210  
 211  // Don't let a malicious user specify more than 100 events.
 212  if ( $maxEvents > 100 )
 213    $maxEvents = 100;
 214  
 215  $x = getValue ( 'repeats', '-?[0-9]+', true );
 216  if ( ! empty ( $x ) ) {
 217    $allow_repeats = $x;
 218    if ( $x == 2 )
 219      $show_daily_events_only_once = true;
 220  }
 221  
 222  $endTime = mktime ( 0, 0, 0, $thismonth, $thisday + $numDays -1, $thisyear );
 223  $endDate = date ( 'Ymd', $endTime );
 224  
 225  /* Pre-Load the repeated events for quicker access */
 226  if ( $allow_repeats == true )
 227    $repeated_events = read_repeated_events ( $username, $startTime, $endTime, $cat_id );
 228  
 229  /* Pre-load the non-repeating events for quicker access */
 230  $events = read_events ( $username, $startTime, $endTime, $cat_id );
 231  
 232  $charset = ( empty ( $LANGUAGE ) ? 'iso-8859-1' : translate ( 'charset' ) );
 233  // This should work ok with RSS, may need to hardcode fallback value.
 234  $lang = languageToAbbrev ( $LANGUAGE == 'Browser-defined' || $LANGUAGE == 'none'
 235    ? $lang : $LANGUAGE );
 236  if ( $lang == 'en' )
 237    $lang = 'en-us'; //the RSS 2.0 default.
 238  
 239  $appStr = generate_application_name ();
 240  
 241  ob_start ();
 242  // header ( 'Content-type: application/rss+xml');
 243  header ( 'Content-type: text/xml' );
 244  echo '<?xml version="1.0" encoding="' . $charset . '"?>
 245  <rss version="2.0" xml:lang="' . $lang . '">
 246    <channel>
 247      <title><![CDATA[' . $appStr . ']]></title>
 248      <link>' . $SERVER_URL . '</link>
 249      <description><![CDATA[' . $appStr . ']]></description>
 250      <language>' . $lang . '</language>
 251      <generator>:"http://www.k5n.us/webcalendar.php?v=' . $PROGRAM_VERSION
 252   . '"</generator>
 253      <image>
 254        <title><![CDATA[' . $appStr . ']]></title>
 255        <link>' . $SERVER_URL . '</link>
 256        <url>http://www.k5n.us/k5n_small.gif</url>
 257      </image>';
 258  
 259  $endtimeYmd = date ( 'Ymd', $endTime );
 260  $numEvents = 0;
 261  $reventIds = array ();
 262  for ( $i = $startTime; date ( 'Ymd', $i ) <= $endtimeYmd && $numEvents < $maxEvents;
 263    $i += 86400 ) {
 264    $d = date ( 'Ymd', $i );
 265    $eventIds = array ();
 266    $pubDate = gmdate ( 'D, d M Y', $i );
 267  
 268    $entries = get_entries ( $d, false );
 269    $rentries = get_repeating_entries ( $username, $d );
 270    $entrycnt = count ( $entries );
 271    $rentrycnt = count ( $rentries );
 272    if ( $debug )
 273      echo '
 274  
 275  countentries==' . $entrycnt . ' ' . $rentrycnt . '
 276  
 277  ';
 278  
 279    if ( $entrycnt > 0 || $rentrycnt > 0 ) {
 280      for ( $j = 0; $j < $entrycnt && $numEvents < $maxEvents; $j++ ) {
 281        // Prevent non-Public events from feeding
 282        if ( in_array ( $entries[$j]->getAccess (), $allow_access ) ) {
 283          $eventIds[] = $entries[$j]->getID ();
 284          $unixtime = $entries[$j]->getDateTimeTS ();
 285          $dateinfo = ( $date_in_title
 286            ? date ( $date_format, $unixtime )
 287             . ( $entries[$j]->isTimed () ? $time_separator 
 288             . date ( $time_format, $unixtime ) : '' ) . ' ' : '' );
 289  
 290          echo '
 291      <item>
 292        <title><![CDATA[' . $dateinfo . $entries[$j]->getName () . ']]></title>
 293        <link>' . $SERVER_URL . 'view_entry.php?id=' . $entries[$j]->getID ()
 294           . '&amp;friendly=1&amp;rssuser=' . $login . '&amp;date=' . $d . '</link>
 295        <description><![CDATA[' . $entries[$j]->getDescription () . ']]></description>'
 296           . ( empty ( $category ) ? '' : '
 297        <category><![CDATA[' . $category . ']]></category>' )
 298          // . '<creator><![CDATA[' . $creator . ']]></creator>'
 299          /* RSS 2.0 date format Wed, 02 Oct 2002 13:00:00 GMT */. '
 300        <pubDate>' . gmdate ( 'D, d M Y H:i:s', $unixtime ) . ' GMT</pubDate>
 301        <guid>' . $SERVER_URL . 'view_entry.php?id=' . $entries[$j]->getID ()
 302           . '&amp;friendly=1&amp;rssuser=' . $login . '&amp;date=' . $d . '</guid>
 303      </item>';
 304          $numEvents++;
 305        }
 306      }
 307      for ( $j = 0; $j < $rentrycnt && $numEvents < $maxEvents; $j++ ) {
 308        // To allow repeated daily entries to be suppressed. Step below is
 309        // necessary because 1st occurence of repeating events shows up in
 310        // $entries AND $rentries & we suppress display of it in $rentries.
 311        if ( in_array ( $rentries[$j]->getID (),
 312              $eventIds ) && $rentries[$j]->getrepeatType () == 'daily' )
 313          $reventIds[] = $rentries[$j]->getID ();
 314  
 315        // Prevent non-Public events from feeding.
 316        // Prevent a repeating event from displaying if the original event has
 317        // already been displayed; prevent 2nd & later recurrence of daily events
 318        // from displaying if that option has been selected.
 319        if ( ! in_array ( $rentries[$j]->getID (), $eventIds ) &&
 320            ( ! $show_daily_events_only_once || ! in_array ( $rentries[$j]->getID (),
 321                $reventIds ) ) &&
 322            ( in_array ( $rentries[$j]->getAccess (), $allow_access ) ) ) {
 323  
 324          // Show repeating events only once.
 325          if ( $rentries[$j]->getrepeatType () == 'daily' )
 326            $reventIds[] = $rentries[$j]->getID ();
 327  
 328          echo '
 329      <item>';
 330          $unixtime = $rentries[$j]->getDateTimeTS ();
 331          // Constructing the TS for the current repeating event
 332          $unixtime = strtotime( date ( 'D, d M Y ', $i ) . date ( 'H:i:s', $unixtime ) ); 
 333          $dateinfo = ( $date_in_title
 334            ? date ( $date_format, $unixtime )
 335             . ( $rentries[$j]->isTimed ()
 336              ? $time_separator . date ( $time_format, $unixtime ) : '' ) . ' '
 337            : '' );
 338  
 339          echo '
 340        <title><![CDATA[' . $dateinfo . $rentries[$j]->getName () . ']]></title>
 341        <link>' . $SERVER_URL . "view_entry.php?id=" . $rentries[$j]->getID ()
 342           . '&amp;friendly=1&amp;rssuser=' . $login . '&amp;date=' . $d . '</link>
 343        <description><![CDATA[' . $rentries[$j]->getDescription () . ']]></description>'
 344           . ( empty ( $category ) ? '' : '
 345        <category><![CDATA[' . $category . ']]></category>' )
 346          // . '<creator><![CDATA[' . $creator . ']]></creator>'
 347          . '
 348        <pubDate>' . gmdate ( 'D, d M Y H:i:s', $unixtime ) . ' GMT</pubDate>
 349        <guid>' . $SERVER_URL . 'view_entry.php?id=' . $rentries[$j]->getID ()
 350           . '&amp;friendly=1&amp;rssuser=' . $login . '&amp;date=' . $d . '</guid>
 351      </item>';
 352          $numEvents++;
 353        }
 354      }
 355    }
 356  }
 357  echo '
 358    </channel>
 359  </rss>';
 360  ob_end_flush ();
 361  // Clear login...just in case.
 362  $login = '';
 363  exit;
 364  
 365  ?>

title

Description

title

Description

title

Description

title

title

Body