source: branches/branch-1_6/include/section_init.inc.php @ 1287

Last change on this file since 1287 was 1287, checked in by rvelices, 18 years ago

bug 349: Nicer display messages instead of "die" when urls cannot be
solved (also set 404 status code for bots)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.9 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-2006 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $Id: section_init.inc.php 1287 2006-04-28 05:11:46Z rvelices $
9// | last update   : $Date: 2006-04-28 05:11:46 +0000 (Fri, 28 Apr 2006) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1287 $
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/**
29 * This included page checks section related parameter and provides
30 * following informations:
31 *
32 * - $page['title']
33 *
34 * - $page['items']: ordered list of items to display
35 *
36 * - $page['cat_nb_images']: number of items in the section (should be equal
37 * to count($page['items']))
38 *
39 * - $page['thumbnails_include']: include page managing thumbnails to
40 * display
41 */
42
43// "index.php?/category/12-foo/start-24&action=fill_caddie" or
44// "index.php/category/12-foo/start-24&action=fill_caddie"
45// must return :
46//
47// array(
48//   'section'  => 'categories',
49//   'category' => 12,
50//   'start'    => 24
51//   'action'   => 'fill_caddie'
52//   );
53
54$page['section'] = 'categories';
55
56if ( isset($_SERVER["PATH_INFO"]) and !empty($_SERVER["PATH_INFO"]) )
57{
58  $rewritten = $_SERVER["PATH_INFO"];
59  $rewritten = str_replace('//', '/', $rewritten);
60  $path_count = count( explode('/', $rewritten) );
61  $page['root_path'] = PHPWG_ROOT_PATH.str_repeat('../', $path_count-1);
62}
63else
64{
65  $rewritten = '';
66  foreach (array_keys($_GET) as $keynum => $key)
67  {
68    $rewritten = $key;
69    break;
70  }
71  $page['root_path'] = PHPWG_ROOT_PATH;
72}
73
74// deleting first "/" if displayed
75$tokens = explode(
76  '/',
77  preg_replace('#^/#', '', $rewritten)
78  );
79// $tokens = array(
80//   0 => category,
81//   1 => 12-foo,
82//   2 => start-24
83//   );
84
85$next_token = 0;
86if (basename($_SERVER['SCRIPT_FILENAME']) == 'picture.php')
87{ // the first token must be the identifier for the picture
88  if ( isset($_GET['image_id'])
89       and isset($_GET['cat']) and is_numeric($_GET['cat']) )
90  {// url compatibility with versions below 1.6
91    $url = make_picture_url( array(
92        'section' => 'categories',
93        'category' => $_GET['cat'],
94        'image_id' => $_GET['image_id']
95      ) );
96    redirect($url);
97  }
98  $token = $tokens[$next_token];
99  $next_token++;
100  if ( is_numeric($token) )
101  {
102    $page['image_id'] = $token;
103  }
104  else
105  {
106    preg_match('/^(\d+-)?(.*)?$/', $token, $matches);
107    if (isset($matches[1]) and is_numeric($matches[1]=rtrim($matches[1],'-')) )
108    {
109      $page['image_id'] = $matches[1];
110      if ( !empty($matches[2]) )
111      {
112        $page['image_file'] = $matches[2];
113      }
114
115    }
116    else
117    {
118      if ( !empty($matches[2]) )
119      {
120        $page['image_file'] = $matches[2];
121      }
122      else
123      {
124        die('Fatal: picture identifier is missing');
125      }
126    }
127  }
128}
129
130if (0 === strpos($tokens[$next_token], 'categor'))
131{
132  $page['section'] = 'categories';
133  $next_token++;
134
135  if (isset($tokens[$next_token])
136      and preg_match('/^(\d+)/', $tokens[$next_token], $matches))
137  {
138    $page['category'] = $matches[1];
139    $next_token++;
140  }
141}
142else if (0 === strpos($tokens[$next_token], 'tag'))
143{
144  $page['section'] = 'tags';
145  $page['tags'] = array();
146
147  $next_token++;
148  $i = $next_token;
149
150  $requested_tag_ids = array();
151  $requested_tag_url_names = array();
152
153  while (isset($tokens[$i]))
154  {
155    if ( preg_match('/^(created-|posted-|start-(\d)+)/', $tokens[$i]) )
156      break;
157
158    if ( preg_match('/^(\d+)(?:-(.*))?/', $tokens[$i], $matches) )
159    {
160      array_push($requested_tag_ids, $matches[1]);
161    }
162    else
163    {
164      array_push($requested_tag_url_names, "'".$tokens[$i]."'");
165    }
166    $i++;
167  }
168  $next_token = $i;
169
170  if ( empty($requested_tag_ids) && empty($requested_tag_url_names) )
171  {
172    die('Fatal: at least one tag required');
173  }
174  // tag infos
175  $query = '
176SELECT name, url_name, id
177  FROM '.TAGS_TABLE.'
178  WHERE ';
179  if ( !empty($requested_tag_ids) )
180  {
181    $query.= 'id IN ('.implode(',', $requested_tag_ids ).')';
182  }
183  if ( !empty($requested_tag_url_names) )
184  {
185    if ( !empty($requested_tag_ids) )
186    {
187      $query.= ' OR ';
188    }
189    $query.= 'url_name IN ('.implode(',', $requested_tag_url_names ).')';
190  }
191  $result = pwg_query($query);
192  $tag_infos = array();
193  while ($row = mysql_fetch_array($result))
194  {
195    $tag_infos[ $row['id'] ] = $row;
196    array_push($page['tags'], $row );//we loose given tag order; is it important?
197  }
198  if ( empty($page['tags']) )
199  {
200    page_not_found('Requested tag does not exist', get_root_url().'tags.php' );
201  }
202}
203else if (0 === strpos($tokens[$next_token], 'fav'))
204{
205  $page['section'] = 'favorites';
206  $next_token++;
207}
208else if ('most_visited' == $tokens[$next_token])
209{
210  $page['section'] = 'most_visited';
211  $next_token++;
212}
213else if ('best_rated' == $tokens[$next_token])
214{
215  $page['section'] = 'best_rated';
216  $next_token++;
217}
218else if ('recent_pics' == $tokens[$next_token])
219{
220  $page['section'] = 'recent_pics';
221  $next_token++;
222}
223else if ('recent_cats' == $tokens[$next_token])
224{
225  $page['section'] = 'recent_cats';
226  $next_token++;
227}
228else if ('search' == $tokens[$next_token])
229{
230  $page['section'] = 'search';
231  $next_token++;
232
233  preg_match('/(\d+)/', $tokens[$next_token], $matches);
234  if (!isset($matches[1]))
235  {
236    die('Fatal: search identifier is missing');
237  }
238  $page['search'] = $matches[1];
239  $next_token++;
240}
241else if ('list' == $tokens[$next_token])
242{
243  $page['section'] = 'list';
244  $next_token++;
245
246  $page['list'] = array();
247  if (!preg_match('/^\d+(,\d+)*$/', $tokens[$next_token]))
248  {
249    die('wrong format on list GET parameter');
250  }
251  foreach (explode(',', $tokens[$next_token]) as $image_id)
252  {
253    array_push($page['list'], $image_id);
254  }
255  $next_token++;
256}
257
258$i = $next_token;
259
260while (isset($tokens[$i]))
261{
262  if (preg_match('/^start-(\d+)/', $tokens[$i], $matches))
263  {
264    $page['start'] = $matches[1];
265  }
266
267  if (preg_match('/^(posted|created)/', $tokens[$i] ))
268  {
269    $chronology_tokens = explode('-', $tokens[$i] );
270
271    $page['chronology_field'] = $chronology_tokens[0];
272
273    array_shift($chronology_tokens);
274    $page['chronology_style'] = $chronology_tokens[0];
275
276    array_shift($chronology_tokens);
277    if ( count($chronology_tokens)>0 )
278    {
279      if ('list'==$chronology_tokens[0] or
280          'calendar'==$chronology_tokens[0])
281      {
282        $page['chronology_view'] = $chronology_tokens[0];
283        array_shift($chronology_tokens);
284      }
285      $page['chronology_date'] = $chronology_tokens;
286    }
287  }
288
289  $i++;
290}
291
292
293// $page['nb_image_page'] is the number of picture to display on this page
294// By default, it is the same as the $user['nb_image_page']
295$page['nb_image_page'] = $user['nb_image_page'];
296
297if (isset($_COOKIE['pwg_image_order'])
298    and is_numeric($_COOKIE['pwg_image_order'])
299    and $_COOKIE['pwg_image_order'] > 0)
300{
301  $orders = get_category_preferred_image_orders();
302
303  $conf['order_by'] = str_replace(
304    'ORDER BY ',
305    'ORDER BY '.$orders[ $_COOKIE['pwg_image_order'] ][1].',',
306    $conf['order_by']
307    );
308  $page['super_order_by'] = true;
309}
310
311// +-----------------------------------------------------------------------+
312// |                              category                                 |
313// +-----------------------------------------------------------------------+
314if ('categories' == $page['section'])
315{
316  if (isset($page['category']))
317  {
318    $result = get_cat_info($page['category']);
319    if (empty($result))
320    {
321      page_not_found('Requested category does not exist' );
322    }
323
324    $page = array_merge(
325      $page,
326      array(
327        'comment'          => $result['comment'],
328        'cat_dir'          => $result['dir'],
329        'cat_name'         => $result['name'],
330        'cat_site_id'      => $result['site_id'],
331        'cat_uploadable'   => $result['uploadable'],
332        'cat_commentable'  => $result['commentable'],
333        'cat_id_uppercat'  => $result['id_uppercat'],
334        'uppercats'        => $result['uppercats'],
335
336        'title' => get_cat_display_name($result['name'], '', false),
337        )
338      );
339
340    if (!isset($page['chronology_field']))
341    {
342      $query = '
343SELECT image_id
344  FROM '.IMAGE_CATEGORY_TABLE.'
345    INNER JOIN '.IMAGES_TABLE.' ON id = image_id
346  WHERE category_id = '.$page['category'].'
347  '.$conf['order_by'].'
348;';
349      $page['items'] = array_from_query($query, 'image_id');
350
351      $page['thumbnails_include'] =
352        $result['nb_images'] > 0
353        ? 'include/category_default.inc.php'
354        : 'include/category_subcats.inc.php';
355    } //otherwise the calendar will requery all subitems
356  }
357  else
358  {
359    $page['title'] = $lang['no_category'];
360    $page['thumbnails_include'] = 'include/category_subcats.inc.php';
361  }
362}
363// special sections
364else
365{
366  if (!empty($user['forbidden_categories']))
367  {
368    $forbidden =
369      ' category_id NOT IN ('.$user['forbidden_categories'].')';
370  }
371  else
372  {
373    $forbidden = ' 1 = 1';
374  }
375// +-----------------------------------------------------------------------+
376// |                            tags section                               |
377// +-----------------------------------------------------------------------+
378  if ($page['section'] == 'tags')
379  {
380    $page['tag_ids'] = array();
381    foreach ($page['tags'] as $tag)
382    {
383      array_push($page['tag_ids'], $tag['id']);
384    }
385
386    $items = get_image_ids_for_tags($page['tag_ids']);
387
388    // permissions depends on category, so to only keep images that are
389    // reachable to the connected user, we need to check category
390    // associations
391    if (!empty($items) )
392    {
393      $query = '
394SELECT image_id
395  FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.IMAGES_TABLE.' ON image_id=id
396  WHERE image_id IN ('.implode(',', $items).')
397    AND '.$forbidden.
398    $conf['order_by'].'
399;';
400      $items = array_unique(
401        array_from_query($query, 'image_id')
402        );
403    }
404
405    $title = count($page['tags']) > 1 ? l10n('Tags') : l10n('Tag');
406    $title.= ' ';
407
408    $tag_num = 1;
409    foreach ($page['tag_ids'] as $tag_id)
410    {
411      $title.=
412        ($tag_num++ > 1 ? ' + ' : '')
413        .'<a href="'
414        .make_index_url(
415          array(
416            'tags' => array(
417              array(
418                'id' => $tag_id,
419                'url_name' => $tag_infos[$tag_id]['url_name'],
420                ),
421              )
422            )
423          )
424        .'">'
425        .$tag_infos[$tag_id]['name']
426        .'</a>';
427    }
428
429    $page = array_merge(
430      $page,
431      array(
432        'title' => $title,
433        'items' => array_values($items),
434        'thumbnails_include' => 'include/category_default.inc.php',
435        )
436      );
437  }
438// +-----------------------------------------------------------------------+
439// |                           search section                              |
440// +-----------------------------------------------------------------------+
441  if ($page['section'] == 'search')
442  {
443    include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' );
444
445    $search_items = get_search_items($page['search']);
446    if ( !empty($search_items) )
447    {
448      $query = '
449SELECT DISTINCT(id)
450  FROM '.IMAGES_TABLE.'
451    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
452  WHERE id IN ('.implode(',', $search_items).')
453    AND '.$forbidden.'
454  '.$conf['order_by'].'
455;';
456      $page['items'] = array_from_query($query, 'id');
457    }
458    else
459    {
460      $page['items'] = array();
461    }
462
463    $page = array_merge(
464      $page,
465      array(
466        'title' => $lang['search_result'],
467        'thumbnails_include' => 'include/category_default.inc.php',
468        )
469      );
470  }
471// +-----------------------------------------------------------------------+
472// |                           favorite section                            |
473// +-----------------------------------------------------------------------+
474  else if ($page['section'] == 'favorites')
475  {
476    check_user_favorites();
477
478    $query = '
479SELECT image_id
480  FROM '.FAVORITES_TABLE.'
481    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
482  WHERE user_id = '.$user['id'].'
483  '.$conf['order_by'].'
484;';
485
486    $page = array_merge(
487      $page,
488      array(
489        'title' => $lang['favorites'],
490        'items' => array_from_query($query, 'image_id'),
491        'thumbnails_include' => 'include/category_default.inc.php',
492        )
493      );
494  }
495// +-----------------------------------------------------------------------+
496// |                       recent pictures section                         |
497// +-----------------------------------------------------------------------+
498  else if ($page['section'] == 'recent_pics')
499  {
500    $query = '
501SELECT DISTINCT(id)
502  FROM '.IMAGES_TABLE.'
503    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
504  WHERE date_available > \''.
505      date('Y-m-d', time() - 60*60*24*$user['recent_period']).'\'
506    AND '.$forbidden.'
507  '.$conf['order_by'].'
508;';
509
510    $page = array_merge(
511      $page,
512      array(
513        'title' => $lang['recent_pics_cat'],
514        'items' => array_from_query($query, 'id'),
515        'thumbnails_include' => 'include/category_default.inc.php',
516        )
517      );
518  }
519// +-----------------------------------------------------------------------+
520// |                 recently updated categories section                   |
521// +-----------------------------------------------------------------------+
522  else if ($page['section'] == 'recent_cats')
523  {
524    $page = array_merge(
525      $page,
526      array(
527        'title' => $lang['recent_cats_cat'],
528        'thumbnails_include' => 'include/category_recent_cats.inc.php',
529        )
530      );
531  }
532// +-----------------------------------------------------------------------+
533// |                        most visited section                           |
534// +-----------------------------------------------------------------------+
535  else if ($page['section'] == 'most_visited')
536  {
537    $page['super_order_by'] = true;
538    $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
539    $query = '
540SELECT DISTINCT(id)
541  FROM '.IMAGES_TABLE.'
542    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
543  WHERE hit > 0
544    AND '.$forbidden.'
545    '.$conf['order_by'].'
546  LIMIT 0, '.$conf['top_number'].'
547;';
548
549    $page = array_merge(
550      $page,
551      array(
552        'title' => $conf['top_number'].' '.$lang['most_visited_cat'],
553        'items' => array_from_query($query, 'id'),
554        'thumbnails_include' => 'include/category_default.inc.php',
555        )
556      );
557  }
558// +-----------------------------------------------------------------------+
559// |                          best rated section                           |
560// +-----------------------------------------------------------------------+
561  else if ($page['section'] == 'best_rated')
562  {
563    $page['super_order_by'] = true;
564    $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC';
565
566    $query ='
567SELECT DISTINCT(id)
568  FROM '.IMAGES_TABLE.'
569    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
570  WHERE average_rate IS NOT NULL
571    AND '.$forbidden.'
572    '.$conf['order_by'].'
573  LIMIT 0, '.$conf['top_number'].'
574;';
575    $page = array_merge(
576      $page,
577      array(
578        'title' => $conf['top_number'].' '.$lang['best_rated_cat'],
579        'items' => array_from_query($query, 'id'),
580        'thumbnails_include' => 'include/category_default.inc.php',
581        )
582      );
583  }
584// +-----------------------------------------------------------------------+
585// |                             list section                              |
586// +-----------------------------------------------------------------------+
587  else if ($page['section'] == 'list')
588  {
589    $query ='
590SELECT DISTINCT(id)
591  FROM '.IMAGES_TABLE.'
592    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
593  WHERE image_id IN ('.implode(',', $page['list']).')
594    AND '.$forbidden.'
595  '.$conf['order_by'].'
596;';
597
598    $page = array_merge(
599      $page,
600      array(
601        'title' => $lang['random_cat'],
602        'items' => array_from_query($query, 'id'),
603        'thumbnails_include' => 'include/category_default.inc.php',
604        )
605      );
606  }
607}
608
609// +-----------------------------------------------------------------------+
610// |                             chronology                                |
611// +-----------------------------------------------------------------------+
612
613if (isset($page['chronology_field']))
614{
615  include_once( PHPWG_ROOT_PATH.'include/functions_calendar.inc.php' );
616  initialize_calendar();
617}
618
619$page['cat_nb_images'] = isset($page['items']) ? count($page['items']) : 0;
620
621if (basename($_SERVER['SCRIPT_FILENAME']) == 'picture.php'
622    and !isset($page['image_id']) )
623{
624  if ( !empty($page['items']) )
625  {
626    $query = '
627SELECT id,file
628  FROM '.IMAGES_TABLE .'
629  WHERE id IN ('.implode(',',$page['items']).')
630  AND file LIKE "' . $page['image_file'] . '.%" ESCAPE "|"'
631;
632    $result = pwg_query($query);
633    if (mysql_num_rows($result)>0)
634    {
635      list($page['image_id'], $page['image_file']) = mysql_fetch_row($result);
636    }
637  }
638  if ( !isset($page['image_id']) )
639  {
640    $page['image_id'] = -1; // will fail in picture.php
641  }
642}
643?>
Note: See TracBrowser for help on using the repository browser.