source: trunk/feed.php @ 824

Last change on this file since 824 was 808, checked in by plg, 19 years ago
  • new : external authentication in another users table. Previous users table is divided between users (common properties with any web application) and user_infos (phpwebgallery specific informations). External table and fields can be configured.
  • modification : profile.php is not reachable through administration anymore (not useful).
  • modification : in profile.php, current password is mandatory only if user tries to change his password. Username can't be changed.
  • deletion : of obsolete functions get_user_restrictions, update_user_restrictions, get_user_all_restrictions, is_user_allowed, update_user
  • modification : user_forbidden table becomes user_cache so that not only restriction informations can be stored in this table.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.0 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-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2005-08-08 20:52:19 +0000 (Mon, 08 Aug 2005) $
10// | last modifier : $Author: plg $
11// | revision      : $Revision: 808 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28define('PHPWG_ROOT_PATH','./');
29include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
30
31// +-----------------------------------------------------------------------+
32// |                               functions                               |
33// +-----------------------------------------------------------------------+
34
35/**
36 * new comments between two dates, according to authorized categories
37 *
38 * @param string start (mysql datetime format)
39 * @param string end (mysql datetime format)
40 * @param string forbidden categories (comma separated)
41 * @return array comment ids
42 */
43function new_comments($start, $end)
44{
45  global $user;
46 
47  $query = '
48SELECT DISTINCT c.id AS comment_id
49  FROM '.COMMENTS_TABLE.' AS c
50     , '.IMAGE_CATEGORY_TABLE.' AS ic
51  WHERE c.image_id = ic.image_id
52    AND c.validation_date > \''.$start.'\'
53    AND c.validation_date <= \''.$end.'\'
54    AND category_id NOT IN ('.$user['forbidden_categories'].')
55;';
56  return array_from_query($query, 'comment_id');
57}
58
59/**
60 * unvalidated at a precise date
61 *
62 * Comments that are registered and not validated yet on a precise date
63 *
64 * @param string date (mysql datetime format)
65 * @return array comment ids
66 */
67function unvalidated_comments($date)
68{
69  $query = '
70SELECT DISTINCT id
71  FROM '.COMMENTS_TABLE.'
72  WHERE date <= \''.$date.'\'
73    AND (validated = \'false\'
74         OR validation_date > \''.$date.'\')
75;';
76  return array_from_query($query, 'id');
77}
78
79/**
80 * new elements between two dates, according to authorized categories
81 *
82 * @param string start (mysql datetime format)
83 * @param string end (mysql datetime format)
84 * @param string forbidden categories (comma separated)
85 * @return array element ids
86 */
87function new_elements($start, $end)
88{
89  global $user;
90 
91  $query = '
92SELECT DISTINCT image_id
93  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON image_id = id
94  WHERE date_available > \''.$start.'\'
95    AND date_available <= \''.$end.'\'
96    AND category_id NOT IN ('.$user['forbidden_categories'].')
97;';
98  return array_from_query($query, 'image_id');
99}
100
101/**
102 * updated categories between two dates, according to authorized categories
103 *
104 * @param string start (mysql datetime format)
105 * @param string end (mysql datetime format)
106 * @param string forbidden categories (comma separated)
107 * @return array element ids
108 */
109function updated_categories($start, $end)
110{
111  global $user;
112 
113  $query = '
114SELECT DISTINCT category_id
115  FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON image_id = id
116  WHERE date_available > \''.$start.'\'
117    AND date_available <= \''.$end.'\'
118    AND category_id NOT IN ('.$user['forbidden_categories'].')
119;';
120  return array_from_query($query, 'category_id');
121}
122
123/**
124 * new registered users between two dates
125 *
126 * @param string start (mysql datetime format)
127 * @param string end (mysql datetime format)
128 * @return array user ids
129 */
130function new_users($start, $end)
131{
132  $query = '
133SELECT user_id
134  FROM '.USER_INFOS_TABLE.'
135  WHERE registration_date > \''.$start.'\'
136    AND registration_date <= \''.$end.'\'
137;';
138  return array_from_query($query, 'user_id');
139}
140
141/**
142 * What's new between two dates ?
143 *
144 * Informations : number of new comments, number of new elements, number of
145 * updated categories. Administrators are also informed about : number of
146 * unvalidated comments, number of new users (TODO : number of unvalidated
147 * elements)
148 *
149 * @param string start date (mysql datetime format)
150 * @param string end date (mysql datetime format)
151 */
152function news($start, $end)
153{
154  global $user;
155
156  $news = array();
157 
158  $nb_new_comments = count(new_comments($start, $end));
159  if ($nb_new_comments > 0)
160  {
161    array_push($news, sprintf(l10n('%d new comments'), $nb_new_comments));
162  }
163
164  $nb_new_elements = count(new_elements($start, $end));
165  if ($nb_new_elements > 0)
166  {
167    array_push($news, sprintf(l10n('%d new elements'), $nb_new_elements));
168  }
169
170  $nb_updated_categories = count(updated_categories($start, $end));
171  if ($nb_updated_categories > 0)
172  {
173    array_push($news, sprintf(l10n('%d categories updated'),
174                              $nb_updated_categories));
175  }
176 
177  if ('admin' == $user['status'])
178  {
179    $nb_unvalidated_comments = count(unvalidated_comments($end));
180    if ($nb_unvalidated_comments > 0)
181    {
182      array_push($news, sprintf(l10n('%d comments to validate'),
183                                $nb_unvalidated_comments));
184    }
185
186    $nb_new_users = count(new_users($start, $end));
187    if ($nb_new_users > 0)
188    {
189      array_push($news, sprintf(l10n('%d new users'), $nb_new_users));
190    }
191  }
192
193  return $news;
194}
195
196/**
197 * explodes a MySQL datetime format (2005-07-14 23:01:37) in fields "year",
198 * "month", "day", "hour", "minute", "second".
199 *
200 * @param string mysql datetime format
201 * @return array
202 */
203function explode_mysqldt($mysqldt)
204{
205  $date = array();
206  list($date['year'],
207       $date['month'],
208       $date['day'],
209       $date['hour'],
210       $date['minute'],
211       $date['second'])
212    = preg_split('/[-: ]/', $mysqldt);
213
214  return $date;
215}
216
217/**
218 * creates a MySQL datetime format (2005-07-14 23:01:37) from a Unix
219 * timestamp (number of seconds since 1970-01-01 00:00:00 GMT)
220 *
221 * @param int unix timestamp
222 * @return string mysql datetime format
223 */
224function ts_to_mysqldt($ts)
225{
226  return date('Y-m-d H:i:s', $ts);
227}
228
229/**
230 * creates a Unix timestamp (number of seconds since 1970-01-01 00:00:00
231 * GMT) from a MySQL datetime format (2005-07-14 23:01:37)
232 *
233 * @param string mysql datetime format
234 * @return int timestamp
235 */
236function mysqldt_to_ts($mysqldt)
237{
238  $date = explode_mysqldt($mysqldt);
239  return mktime($date['hour'], $date['minute'], $date['second'],
240                $date['month'], $date['day'], $date['year']);
241}
242
243/**
244 * creates an ISO 8601 format date (2003-01-20T18:05:41+04:00) from Unix
245 * timestamp (number of seconds since 1970-01-01 00:00:00 GMT)
246 *
247 * function copied from Dotclear project http://dotclear.net
248 *
249 * @param int timestamp
250 * @return string ISO 8601 date format
251 */
252function ts_to_iso8601($ts)
253{
254  $tz = date('O',$ts);
255  $tz = substr($tz, 0, -2).':'.substr($tz, -2);
256  return date('Y-m-d\\TH:i:s',$ts).$tz;
257}
258
259// +-----------------------------------------------------------------------+
260// |                            initialization                             |
261// +-----------------------------------------------------------------------+
262
263// clean $user array (include/user.inc.php has been executed)
264$user = array();
265
266// echo '<pre>'.generate_key(50).'</pre>';
267if (isset($_GET['feed'])
268    and preg_match('/^[A-Za-z0-9]{50}$/', $_GET['feed']))
269{
270  $query = '
271SELECT user_id AS id,
272       status,
273       last_feed_check
274  FROM '.USER_INFOS_TABLE.'
275  WHERE feed_id = \''.$_GET['feed'].'\'
276;';
277  $user = mysql_fetch_array(pwg_query($query));
278}
279else
280{
281  $user = array('id' => $conf['guest_id'],
282                'status' => 'guest');
283}
284
285$user['forbidden_categories'] = calculate_permissions($user['id'],
286                                                      $user['status']);
287if ('' == $user['forbidden_categories'])
288{
289  $user['forbidden_categories'] = '-1';
290}
291
292list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
293
294include_once(PHPWG_ROOT_PATH.'include/feedcreator.class.php');
295
296$rss = new UniversalFeedCreator();
297// $rss->useCached(); // use cached version if age<1 hour
298$rss->title = 'PhpWebGallery notifications';
299$rss->link = 'http://phpwebgallery.net';
300
301// +-----------------------------------------------------------------------+
302// |                            Feed creation                              |
303// +-----------------------------------------------------------------------+
304
305if ($conf['guest_id'] != $user['id'])
306{
307  $news = news($user['last_feed_check'], $dbnow);
308
309  if (count($news) > 0)
310  {
311    // echo '<pre>';
312    // print_r($news);
313    // echo '</pre>';
314   
315    $item = new FeedItem(); 
316    $item->title = sprintf(l10n('New on %s'), $dbnow);
317    $item->link = 'http://phpwebgallery.net';
318   
319    // content creation
320    $item->description = '<ul>';
321    foreach ($news as $line)
322    {
323      $item->description.= '<li>'.$line.'</li>';
324    }
325    $item->description.= '</ul>';
326    $item->descriptionHtmlSyndicated = true;
327   
328    $item->date = $dbnow; 
329    $item->author = 'PhpWebGallery notifier'; 
330 
331    $rss->addItem($item);
332  }
333
334  $query = '
335UPDATE '.USER_INFOS_TABLE.'
336  SET last_feed_check = \''.$dbnow.'\'
337  WHERE user_id = '.$user['id'].'
338;';
339  pwg_query($query);
340}
341else
342{
343  // The feed is filled with periodical blocks of informations. Date
344  // "checkpoints" cut the blocks. The first step is to find those
345  // checkpoints according to the configured feed period.
346  //
347  // checkpoints are first calculated in Unix timestamp (number of seconds
348  // since 1970-01-01 00:00:00 GMT) and then converted to MySQL datetime
349  // format.
350
351  $now = explode_mysqldt($dbnow);
352
353  $checkpoints = array();
354  $checkpoints[0] = mysqldt_to_ts($dbnow);
355
356  // if the feed period was not configured the right way (ie among the list
357  // of possible values), the configuration is overloaded here.
358  if (!in_array($conf['feed_period'],
359                array('hour', 'half day', 'day', 'week', 'month')))
360  {
361    $conf['feed_period'] = 'week';
362  }
363
364  // foreach feed_period possible, we need to find the beginning of the
365  // current period. The variable $timeshift contains the shift to apply to
366  // each checkpoint to find the previous one with strtotime function
367  switch ($conf['feed_period'])
368  {
369    // 2005-07-14 23:36:19 => 2005-07-14 23:00:00
370    case 'hour' :
371    {
372      $checkpoints[1] = mktime($now['hour'],0,0,
373                               $now['month'],$now['day'],$now['year']);
374      $timeshift = '1 hour ago';
375      break;
376    }
377    // 2005-07-14 23:36:19 => 2005-07-14 12:00:00
378    case 'half day' :
379    {
380      $checkpoints[1] = mktime(($now['hour'] < 12) ? 0 : 12,0,0,
381                               $now['month'],$now['day'],$now['year']);
382      $timeshift = '12 hours ago';
383      break;
384    }
385    // 2005-07-14 23:36:19 => 2005-07-14 00:00:00
386    case 'day' :
387    {
388      $checkpoints[1] = mktime(0,0,0,$now['month'],$now['day'],$now['year']);
389      $timeshift = '1 day ago';
390      break;
391    }
392    // 2005-07-14 23:36:19 => 2005-07-11 00:00:00
393    case 'week' :
394    {
395      $checkpoints[1] = strtotime('last monday', $checkpoints[0]);
396      $timeshift = '1 week ago';
397      break;
398    }
399    // 2005-07-14 23:36:19 => 2005-07-01 00:00:00
400    case 'month' :
401    {
402      $checkpoints[1] = mktime(0,0,0,$now['month'],1,$now['year']);
403      $timeshift = '1 month ago';
404      break;
405    }
406  }
407
408  for ($i = 2; $i <= 11; $i++)
409  {
410    $checkpoints[$i] = strtotime($timeshift, $checkpoints[$i-1]);
411  }
412
413  // converts all timestamp values to MySQL datetime format
414  $checkpoints = array_map('ts_to_mysqldt', $checkpoints);
415
416  for ($i = 1; $i <= max(array_keys($checkpoints)); $i++)
417  {
418    $news = news($checkpoints[$i], $checkpoints[$i-1]);
419
420    if (count($news) > 0)
421    {
422      $item = new FeedItem(); 
423      $item->title = sprintf(l10n('New from %s to %s'),
424                             $checkpoints[$i],
425                             $checkpoints[$i-1]);
426      $item->link = 'http://phpwebgallery.net';
427     
428      // content creation
429      $item->description = '<ul>';
430      foreach ($news as $line)
431      {
432        $item->description.= '<li>'.$line.'</li>';
433      }
434      $item->description.= '</ul>';
435      $item->descriptionHtmlSyndicated = true;
436     
437      $item->date = ts_to_iso8601(mysqldt_to_ts($checkpoints[$i-1]));
438      $item->author = 'PhpWebGallery notifier'; 
439     
440      $rss->addItem($item);
441    }
442  }
443}
444
445// send XML feed
446echo $rss->saveFeed('RSS2.0', '', true);
447?>
Note: See TracBrowser for help on using the repository browser.