source: branches/1.7/feed.php @ 27569

Last change on this file since 27569 was 2151, checked in by rvelices, 16 years ago
  • update last_check from time to time even if no news, so that we don't delete used feeds
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: feed.php 2151 2007-10-23 00:08:48Z rvelices $
8// | last update   : $Date: 2007-10-23 00:08:48 +0000 (Tue, 23 Oct 2007) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2151 $
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');
29include_once(PHPWG_ROOT_PATH.'include/functions_notification.inc.php');
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
69$feed_id= isset($_GET['feed']) ? $_GET['feed'] : '';
70$image_only=isset($_GET['image_only']);
71
72// echo '<pre>'.generate_key(50).'</pre>';
73if ( !empty($feed_id) )
74{
75  $query = '
76SELECT user_id,
77       last_check
78  FROM '.USER_FEED_TABLE.'
79  WHERE id = \''.$feed_id.'\'
80;';
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  }
90}
91else
92{
93  $image_only = true;
94  if (!$user['is_the_guest'])
95  {// auto session was created - so switch to guest
96    $user = build_user( $conf['guest_id'], true );
97  }
98}
99
100// Check the status now after the user has been loaded
101check_status(ACCESS_GUEST);
102
103list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
104
105include_once(PHPWG_ROOT_PATH.'include/feedcreator.class.php');
106
107set_make_full_url();
108
109$rss = new UniversalFeedCreator();
110
111$rss->title = $conf['gallery_title'];
112$rss->title.= ' (as '.$user['username'].')';
113
114$rss->link = $conf['gallery_url'];
115
116// +-----------------------------------------------------------------------+
117// |                            Feed creation                              |
118// +-----------------------------------------------------------------------+
119
120$news = array();
121if (!$image_only)
122{
123  $news = news($feed_row['last_check'], $dbnow, true, true);
124
125  if (count($news) > 0)
126  {
127    $item = new FeedItem();
128    $item->title = sprintf(l10n('New on %s'),
129        format_date($dbnow, 'mysql_datetime') );
130    $item->link = $conf['gallery_url'];
131
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;
140
141    $item->date = ts_to_iso8601(mysqldt_to_ts($dbnow));
142    $item->author = 'PhpWebGallery notifier';
143    $item->guid= sprintf('%s', $dbnow);;
144
145    $rss->addItem($item);
146
147    $query = '
148UPDATE '.USER_FEED_TABLE.'
149  SET last_check = \''.$dbnow.'\'
150  WHERE id = \''.$feed_id.'\'
151;';
152    pwg_query($query);
153  }
154}
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  {
161    $query = '
162UPDATE '.USER_FEED_TABLE.'
163  SET last_check = DATE_ADD(\''.$dbnow.'\', INTERVAL -15 DAY )
164  WHERE id = \''.$feed_id.'\'
165;';
166    pwg_query($query);
167  }
168}
169
170$dates = get_recent_post_dates_array($conf['recent_post_dates']['RSS']);
171
172foreach($dates as $date_detail)
173{ // for each recent post date we create a feed item
174  $item = new FeedItem();
175  $date = $date_detail['date_available'];
176  $item->title = get_title_recent_post_date($date_detail);
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
189  $item->description .= get_html_description_recent_post_date($date_detail);
190
191  $item->descriptionHtmlSyndicated = true;
192
193  $item->date = ts_to_iso8601(mysqldt_to_ts($date));
194  $item->author = 'PhpWebGallery notifier';
195  $item->guid= sprintf('%s', 'pics-'.$date);;
196
197  $rss->addItem($item);
198}
199
200// send XML feed
201echo $rss->saveFeed('RSS2.0', '', true);
202?>
Note: See TracBrowser for help on using the repository browser.