source: trunk/feed.php @ 2296

Last change on this file since 2296 was 2296, checked in by rvelices, 16 years ago
  • an optimization of a Mysql query for tags (oddly but it's faster)
  • now the dates in the RSS feed are always correct (and in GMT)
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 6.6 KB
RevLine 
[801]1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
[2296]5// | Copyright (C) 2003-2008 PhpWebGallery Team - http://phpwebgallery.net |
[801]6// +-----------------------------------------------------------------------+
[1858]7// | file          : $Id: feed.php 2296 2008-04-04 19:47:07Z rvelices $
[801]8// | last update   : $Date: 2008-04-04 19:47:07 +0000 (Fri, 04 Apr 2008) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2296 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27define('PHPWG_ROOT_PATH','./');
28include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
[1018]29include_once(PHPWG_ROOT_PATH.'include/functions_notification.inc.php');
[801]30
31// +-----------------------------------------------------------------------+
32// |                               functions                               |
33// +-----------------------------------------------------------------------+
34
35/**
36 * creates a Unix timestamp (number of seconds since 1970-01-01 00:00:00
37 * GMT) from a MySQL datetime format (2005-07-14 23:01:37)
38 *
39 * @param string mysql datetime format
40 * @return int timestamp
41 */
42function mysqldt_to_ts($mysqldt)
43{
44  $date = explode_mysqldt($mysqldt);
45  return mktime($date['hour'], $date['minute'], $date['second'],
46                $date['month'], $date['day'], $date['year']);
47}
48
49/**
50 * creates an ISO 8601 format date (2003-01-20T18:05:41+04:00) from Unix
51 * timestamp (number of seconds since 1970-01-01 00:00:00 GMT)
52 *
53 * function copied from Dotclear project http://dotclear.net
54 *
55 * @param int timestamp
56 * @return string ISO 8601 date format
57 */
58function ts_to_iso8601($ts)
59{
60  $tz = date('O',$ts);
61  $tz = substr($tz, 0, -2).':'.substr($tz, -2);
62  return date('Y-m-d\\TH:i:s',$ts).$tz;
63}
64
65// +-----------------------------------------------------------------------+
66// |                            initialization                             |
67// +-----------------------------------------------------------------------+
68
[1750]69$feed_id= isset($_GET['feed']) ? $_GET['feed'] : '';
70$image_only=isset($_GET['image_only']);
[801]71
72// echo '<pre>'.generate_key(50).'</pre>';
[1750]73if ( !empty($feed_id) )
[801]74{
75  $query = '
[1750]76SELECT user_id,
77       last_check
78  FROM '.USER_FEED_TABLE.'
79  WHERE id = \''.$feed_id.'\'
[801]80;';
[1750]81  $feed_row = mysql_fetch_assoc(pwg_query($query));
82  if ( empty($feed_row) )
83  {
84    page_not_found('Unknown/missing feed identifier');
85  }
86  if ($feed_row['user_id']!=$user['id'])
87  { // new user
88    $user = build_user( $feed_row['user_id'], true );
89  }
[801]90}
[1750]91else
[801]92{
[1750]93  $image_only = true;
[2029]94  if (!is_a_guest())
[1750]95  {// auto session was created - so switch to guest
96    $user = build_user( $conf['guest_id'], true );
97  }
[801]98}
99
[1850]100// Check the status now after the user has been loaded
101check_status(ACCESS_GUEST);
102
[801]103list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
104
105include_once(PHPWG_ROOT_PATH.'include/feedcreator.class.php');
106
[1750]107set_make_full_url();
[1549]108
[801]109$rss = new UniversalFeedCreator();
[2132]110$rss->encoding=get_pwg_charset();
[1549]111$rss->title = $conf['gallery_title'];
[833]112$rss->title.= ' (as '.$user['username'].')';
113
114$rss->link = $conf['gallery_url'];
115
[801]116// +-----------------------------------------------------------------------+
117// |                            Feed creation                              |
118// +-----------------------------------------------------------------------+
119
[2152]120$news = array();
[1750]121if (!$image_only)
[801]122{
[1750]123  $news = news($feed_row['last_check'], $dbnow, true, true);
[1549]124
[1636]125  if (count($news) > 0)
[801]126  {
[1636]127    $item = new FeedItem();
128    $item->title = sprintf(l10n('New on %s'),
129        format_date($dbnow, 'mysql_datetime') );
130    $item->link = $conf['gallery_url'];
[1549]131
[1636]132    // content creation
133    $item->description = '<ul>';
134    foreach ($news as $line)
135    {
136      $item->description.= '<li>'.$line.'</li>';
137    }
138    $item->description.= '</ul>';
139    $item->descriptionHtmlSyndicated = true;
[1549]140
[2296]141    $item->date = mysqldt_to_ts($dbnow);
[1636]142    $item->author = 'PhpWebGallery notifier';
143    $item->guid= sprintf('%s', $dbnow);;
[801]144
[1636]145    $rss->addItem($item);
146
147    $query = '
148UPDATE '.USER_FEED_TABLE.'
149  SET last_check = \''.$dbnow.'\'
[1750]150  WHERE id = \''.$feed_id.'\'
[1636]151;';
152    pwg_query($query);
153  }
154}
[2152]155
156if ( !empty($feed_id) and empty($news) )
157{// update the last check from time to time to avoid deletion by maintenance tasks
158  if ( !isset($feed_row['last_check'])
159    or time()-mysqldt_to_ts($feed_row['last_check']) > 30*24*3600 )
160  {
[1750]161    $query = '
[2152]162UPDATE '.USER_FEED_TABLE.'
163  SET last_check = DATE_ADD(\''.$dbnow.'\', INTERVAL -15 DAY )
164  WHERE id = \''.$feed_id.'\'
165;';
[1750]166    pwg_query($query);
167  }
[1549]168}
[801]169
[1871]170$dates = get_recent_post_dates_array($conf['recent_post_dates']['RSS']);
[1549]171
[1784]172foreach($dates as $date_detail)
[1549]173{ // for each recent post date we create a feed item
[1784]174  $item = new FeedItem();
[1549]175  $date = $date_detail['date_available'];
[1784]176  $item->title = get_title_recent_post_date($date_detail);
[1549]177  $item->link = make_index_url(
178        array(
179          'chronology_field' => 'posted',
180          'chronology_style'=> 'monthly',
181          'chronology_view' => 'calendar',
182          'chronology_date' => explode('-', substr($date,0,10) )
183        )
184      );
185
186  $item->description .=
187    '<a href="'.make_index_url().'">'.$conf['gallery_title'].'</a><br/> ';
188
[1784]189  $item->description .= get_html_description_recent_post_date($date_detail);
[1549]190
191  $item->descriptionHtmlSyndicated = true;
192
[2296]193  $item->date = mysqldt_to_ts($date);
[1549]194  $item->author = 'PhpWebGallery notifier';
195  $item->guid= sprintf('%s', 'pics-'.$date);;
196
197  $rss->addItem($item);
198}
199
[2296]200$fileName= $conf['local_data_dir'].'/tmp';
201@mkdir($fileName); // just in case
202$fileName.='/feed.xml';
[801]203// send XML feed
[2296]204echo $rss->saveFeed('RSS2.0', $fileName, true);
[801]205?>
Note: See TracBrowser for help on using the repository browser.