source: trunk/feed.php @ 26197

Last change on this file since 26197 was 25005, checked in by mistic100, 11 years ago

feature 2978: remove useless sprintf in the core

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