[801] | 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-11-26 20:15:50 +0000 (Sat, 26 Nov 2005) $ |
---|
| 10 | // | last modifier : $Author: plg $ |
---|
| 11 | // | revision : $Revision: 958 $ |
---|
| 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 | |
---|
| 28 | define('PHPWG_ROOT_PATH','./'); |
---|
| 29 | include_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 | */ |
---|
| 43 | function new_comments($start, $end) |
---|
| 44 | { |
---|
| 45 | global $user; |
---|
| 46 | |
---|
| 47 | $query = ' |
---|
| 48 | SELECT 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 | */ |
---|
| 67 | function unvalidated_comments($date) |
---|
| 68 | { |
---|
| 69 | $query = ' |
---|
| 70 | SELECT 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 | */ |
---|
| 87 | function new_elements($start, $end) |
---|
| 88 | { |
---|
| 89 | global $user; |
---|
| 90 | |
---|
| 91 | $query = ' |
---|
| 92 | SELECT 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 | */ |
---|
| 109 | function updated_categories($start, $end) |
---|
| 110 | { |
---|
| 111 | global $user; |
---|
| 112 | |
---|
| 113 | $query = ' |
---|
| 114 | SELECT 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 | */ |
---|
| 130 | function new_users($start, $end) |
---|
| 131 | { |
---|
| 132 | $query = ' |
---|
[808] | 133 | SELECT user_id |
---|
| 134 | FROM '.USER_INFOS_TABLE.' |
---|
[801] | 135 | WHERE registration_date > \''.$start.'\' |
---|
| 136 | AND registration_date <= \''.$end.'\' |
---|
| 137 | ;'; |
---|
[808] | 138 | return array_from_query($query, 'user_id'); |
---|
[801] | 139 | } |
---|
| 140 | |
---|
| 141 | /** |
---|
[864] | 142 | * currently waiting pictures |
---|
| 143 | * |
---|
| 144 | * @return array waiting ids |
---|
| 145 | */ |
---|
| 146 | function waiting_elements() |
---|
| 147 | { |
---|
| 148 | $query = ' |
---|
| 149 | SELECT id |
---|
| 150 | FROM '.WAITING_TABLE.' |
---|
| 151 | WHERE validated = \'false\' |
---|
| 152 | ;'; |
---|
| 153 | |
---|
| 154 | return array_from_query($query, 'id'); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
[801] | 158 | * What's new between two dates ? |
---|
| 159 | * |
---|
| 160 | * Informations : number of new comments, number of new elements, number of |
---|
| 161 | * updated categories. Administrators are also informed about : number of |
---|
| 162 | * unvalidated comments, number of new users (TODO : number of unvalidated |
---|
| 163 | * elements) |
---|
| 164 | * |
---|
| 165 | * @param string start date (mysql datetime format) |
---|
| 166 | * @param string end date (mysql datetime format) |
---|
| 167 | */ |
---|
| 168 | function news($start, $end) |
---|
| 169 | { |
---|
| 170 | global $user; |
---|
| 171 | |
---|
| 172 | $news = array(); |
---|
| 173 | |
---|
| 174 | $nb_new_comments = count(new_comments($start, $end)); |
---|
| 175 | if ($nb_new_comments > 0) |
---|
| 176 | { |
---|
| 177 | array_push($news, sprintf(l10n('%d new comments'), $nb_new_comments)); |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | $nb_new_elements = count(new_elements($start, $end)); |
---|
| 181 | if ($nb_new_elements > 0) |
---|
| 182 | { |
---|
| 183 | array_push($news, sprintf(l10n('%d new elements'), $nb_new_elements)); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | $nb_updated_categories = count(updated_categories($start, $end)); |
---|
| 187 | if ($nb_updated_categories > 0) |
---|
| 188 | { |
---|
| 189 | array_push($news, sprintf(l10n('%d categories updated'), |
---|
| 190 | $nb_updated_categories)); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | if ('admin' == $user['status']) |
---|
| 194 | { |
---|
| 195 | $nb_unvalidated_comments = count(unvalidated_comments($end)); |
---|
| 196 | if ($nb_unvalidated_comments > 0) |
---|
| 197 | { |
---|
| 198 | array_push($news, sprintf(l10n('%d comments to validate'), |
---|
| 199 | $nb_unvalidated_comments)); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | $nb_new_users = count(new_users($start, $end)); |
---|
| 203 | if ($nb_new_users > 0) |
---|
| 204 | { |
---|
| 205 | array_push($news, sprintf(l10n('%d new users'), $nb_new_users)); |
---|
| 206 | } |
---|
[864] | 207 | |
---|
| 208 | $nb_waiting_elements = count(waiting_elements()); |
---|
| 209 | if ($nb_waiting_elements > 0) |
---|
| 210 | { |
---|
| 211 | array_push( |
---|
| 212 | $news, |
---|
| 213 | sprintf( |
---|
| 214 | l10n('%d waiting elements'), |
---|
| 215 | $nb_waiting_elements |
---|
| 216 | ) |
---|
| 217 | ); |
---|
| 218 | } |
---|
[801] | 219 | } |
---|
| 220 | |
---|
| 221 | return $news; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | /** |
---|
| 225 | * explodes a MySQL datetime format (2005-07-14 23:01:37) in fields "year", |
---|
| 226 | * "month", "day", "hour", "minute", "second". |
---|
| 227 | * |
---|
| 228 | * @param string mysql datetime format |
---|
| 229 | * @return array |
---|
| 230 | */ |
---|
| 231 | function explode_mysqldt($mysqldt) |
---|
| 232 | { |
---|
| 233 | $date = array(); |
---|
| 234 | list($date['year'], |
---|
| 235 | $date['month'], |
---|
| 236 | $date['day'], |
---|
| 237 | $date['hour'], |
---|
| 238 | $date['minute'], |
---|
| 239 | $date['second']) |
---|
| 240 | = preg_split('/[-: ]/', $mysqldt); |
---|
| 241 | |
---|
| 242 | return $date; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | /** |
---|
| 246 | * creates a Unix timestamp (number of seconds since 1970-01-01 00:00:00 |
---|
| 247 | * GMT) from a MySQL datetime format (2005-07-14 23:01:37) |
---|
| 248 | * |
---|
| 249 | * @param string mysql datetime format |
---|
| 250 | * @return int timestamp |
---|
| 251 | */ |
---|
| 252 | function mysqldt_to_ts($mysqldt) |
---|
| 253 | { |
---|
| 254 | $date = explode_mysqldt($mysqldt); |
---|
| 255 | return mktime($date['hour'], $date['minute'], $date['second'], |
---|
| 256 | $date['month'], $date['day'], $date['year']); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | /** |
---|
| 260 | * creates an ISO 8601 format date (2003-01-20T18:05:41+04:00) from Unix |
---|
| 261 | * timestamp (number of seconds since 1970-01-01 00:00:00 GMT) |
---|
| 262 | * |
---|
| 263 | * function copied from Dotclear project http://dotclear.net |
---|
| 264 | * |
---|
| 265 | * @param int timestamp |
---|
| 266 | * @return string ISO 8601 date format |
---|
| 267 | */ |
---|
| 268 | function ts_to_iso8601($ts) |
---|
| 269 | { |
---|
| 270 | $tz = date('O',$ts); |
---|
| 271 | $tz = substr($tz, 0, -2).':'.substr($tz, -2); |
---|
| 272 | return date('Y-m-d\\TH:i:s',$ts).$tz; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | // +-----------------------------------------------------------------------+ |
---|
| 276 | // | initialization | |
---|
| 277 | // +-----------------------------------------------------------------------+ |
---|
| 278 | |
---|
| 279 | // clean $user array (include/user.inc.php has been executed) |
---|
| 280 | $user = array(); |
---|
| 281 | |
---|
| 282 | // echo '<pre>'.generate_key(50).'</pre>'; |
---|
| 283 | if (isset($_GET['feed']) |
---|
| 284 | and preg_match('/^[A-Za-z0-9]{50}$/', $_GET['feed'])) |
---|
| 285 | { |
---|
| 286 | $query = ' |
---|
[833] | 287 | SELECT uf.user_id AS id, |
---|
| 288 | ui.status, |
---|
| 289 | uf.last_check, |
---|
| 290 | u.'.$conf['user_fields']['username'].' AS username |
---|
| 291 | FROM '.USER_FEED_TABLE.' AS uf |
---|
| 292 | INNER JOIN '.USER_INFOS_TABLE.' AS ui |
---|
| 293 | ON ui.user_id = uf.user_id |
---|
| 294 | INNER JOIN '.USERS_TABLE.' AS u |
---|
| 295 | ON u.'.$conf['user_fields']['id'].' = uf.user_id |
---|
| 296 | WHERE uf.id = \''.$_GET['feed'].'\' |
---|
[801] | 297 | ;'; |
---|
| 298 | $user = mysql_fetch_array(pwg_query($query)); |
---|
| 299 | } |
---|
| 300 | else |
---|
| 301 | { |
---|
[833] | 302 | echo l10n('Unknown feed identifier'); |
---|
| 303 | exit(); |
---|
[801] | 304 | } |
---|
| 305 | |
---|
| 306 | $user['forbidden_categories'] = calculate_permissions($user['id'], |
---|
| 307 | $user['status']); |
---|
| 308 | if ('' == $user['forbidden_categories']) |
---|
| 309 | { |
---|
| 310 | $user['forbidden_categories'] = '-1'; |
---|
| 311 | } |
---|
| 312 | |
---|
| 313 | list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();')); |
---|
| 314 | |
---|
| 315 | include_once(PHPWG_ROOT_PATH.'include/feedcreator.class.php'); |
---|
| 316 | |
---|
| 317 | $rss = new UniversalFeedCreator(); |
---|
| 318 | |
---|
[833] | 319 | $rss->title = $conf['gallery_title'].', notifications'; |
---|
| 320 | $rss->title.= ' (as '.$user['username'].')'; |
---|
| 321 | |
---|
| 322 | $rss->link = $conf['gallery_url']; |
---|
| 323 | |
---|
[801] | 324 | // +-----------------------------------------------------------------------+ |
---|
| 325 | // | Feed creation | |
---|
| 326 | // +-----------------------------------------------------------------------+ |
---|
| 327 | |
---|
[833] | 328 | $news = news($user['last_check'], $dbnow); |
---|
| 329 | |
---|
| 330 | if (count($news) > 0) |
---|
[801] | 331 | { |
---|
[833] | 332 | $item = new FeedItem(); |
---|
| 333 | $item->title = sprintf(l10n('New on %s'), $dbnow); |
---|
[958] | 334 | $item->link = $conf['gallery_url']; |
---|
[833] | 335 | |
---|
| 336 | // content creation |
---|
| 337 | $item->description = '<ul>'; |
---|
| 338 | foreach ($news as $line) |
---|
[801] | 339 | { |
---|
[833] | 340 | $item->description.= '<li>'.$line.'</li>'; |
---|
| 341 | } |
---|
| 342 | $item->description.= '</ul>'; |
---|
| 343 | $item->descriptionHtmlSyndicated = true; |
---|
[801] | 344 | |
---|
[833] | 345 | $item->date = ts_to_iso8601(mysqldt_to_ts($dbnow)); |
---|
| 346 | $item->author = 'PhpWebGallery notifier'; |
---|
| 347 | |
---|
| 348 | $rss->addItem($item); |
---|
| 349 | } |
---|
[801] | 350 | |
---|
[833] | 351 | $query = ' |
---|
| 352 | UPDATE '.USER_FEED_TABLE.' |
---|
| 353 | SET last_check = \''.$dbnow.'\' |
---|
| 354 | WHERE id = \''.$_GET['feed'].'\' |
---|
[801] | 355 | ;'; |
---|
[833] | 356 | pwg_query($query); |
---|
[801] | 357 | |
---|
| 358 | // send XML feed |
---|
| 359 | echo $rss->saveFeed('RSS2.0', '', true); |
---|
| 360 | ?> |
---|