source: tags/release-1_6_0RC1/include/section_init.inc.php @ 12523

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

bug 345: cannot browse categories (Club Internet modified web server provides
PATH_INFO even if it is empty)

fix 339: also added in create_listing_file.php

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.7 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 1228 2006-04-21 00:03:46Z rvelices $
9// | last update   : $Date: 2006-04-21 00:03:46 +0000 (Fri, 21 Apr 2006) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1228 $
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    die('Fatal: no existing tag');
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
320    $page = array_merge(
321      $page,
322      array(
323        'comment'          => $result['comment'],
324        'cat_dir'          => $result['dir'],
325        'cat_name'         => $result['name'],
326        'cat_site_id'      => $result['site_id'],
327        'cat_uploadable'   => $result['uploadable'],
328        'cat_commentable'  => $result['commentable'],
329        'cat_id_uppercat'  => $result['id_uppercat'],
330        'uppercats'        => $result['uppercats'],
331
332        'title' => get_cat_display_name($result['name'], '', false),
333        )
334      );
335
336    if (!isset($page['chronology_field']))
337    {
338      $query = '
339SELECT image_id
340  FROM '.IMAGE_CATEGORY_TABLE.'
341    INNER JOIN '.IMAGES_TABLE.' ON id = image_id
342  WHERE category_id = '.$page['category'].'
343  '.$conf['order_by'].'
344;';
345      $page['items'] = array_from_query($query, 'image_id');
346
347      $page['thumbnails_include'] =
348        $result['nb_images'] > 0
349        ? 'include/category_default.inc.php'
350        : 'include/category_subcats.inc.php';
351    } //otherwise the calendar will requery all subitems
352  }
353  else
354  {
355    $page['title'] = $lang['no_category'];
356    $page['thumbnails_include'] = 'include/category_subcats.inc.php';
357  }
358}
359// special sections
360else
361{
362  if (!empty($user['forbidden_categories']))
363  {
364    $forbidden =
365      ' category_id NOT IN ('.$user['forbidden_categories'].')';
366  }
367  else
368  {
369    $forbidden = ' 1 = 1';
370  }
371// +-----------------------------------------------------------------------+
372// |                            tags section                               |
373// +-----------------------------------------------------------------------+
374  if ($page['section'] == 'tags')
375  {
376    $page['tag_ids'] = array();
377    foreach ($page['tags'] as $tag)
378    {
379      array_push($page['tag_ids'], $tag['id']);
380    }
381
382    $items = get_image_ids_for_tags($page['tag_ids']);
383
384    // permissions depends on category, so to only keep images that are
385    // reachable to the connected user, we need to check category
386    // associations
387    if (!empty($items) )
388    {
389      $query = '
390SELECT image_id
391  FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.IMAGES_TABLE.' ON image_id=id
392  WHERE image_id IN ('.implode(',', $items).')
393    AND '.$forbidden.
394    $conf['order_by'].'
395;';
396      $items = array_unique(
397        array_from_query($query, 'image_id')
398        );
399    }
400
401    $title = count($page['tags']) > 1 ? l10n('Tags') : l10n('Tag');
402    $title.= ' ';
403
404    $tag_num = 1;
405    foreach ($page['tag_ids'] as $tag_id)
406    {
407      $title.=
408        ($tag_num++ > 1 ? ' + ' : '')
409        .'<a href="'
410        .make_index_url(
411          array(
412            'tags' => array(
413              array(
414                'id' => $tag_id,
415                'url_name' => $tag_infos[$tag_id]['url_name'],
416                ),
417              )
418            )
419          )
420        .'">'
421        .$tag_infos[$tag_id]['name']
422        .'</a>';
423    }
424
425    $page = array_merge(
426      $page,
427      array(
428        'title' => $title,
429        'items' => array_values($items),
430        'thumbnails_include' => 'include/category_default.inc.php',
431        )
432      );
433  }
434// +-----------------------------------------------------------------------+
435// |                           search section                              |
436// +-----------------------------------------------------------------------+
437  if ($page['section'] == 'search')
438  {
439    include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' );
440
441    $search_items = get_search_items($page['search']);
442    if ( !empty($search_items) )
443    {
444      $query = '
445SELECT DISTINCT(id)
446  FROM '.IMAGES_TABLE.'
447    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
448  WHERE id IN ('.implode(',', $search_items).')
449    AND '.$forbidden.'
450  '.$conf['order_by'].'
451;';
452      $page['items'] = array_from_query($query, 'id');
453    }
454    else
455    {
456      $page['items'] = array();
457    }
458
459    $page = array_merge(
460      $page,
461      array(
462        'title' => $lang['search_result'],
463        'thumbnails_include' => 'include/category_default.inc.php',
464        )
465      );
466  }
467// +-----------------------------------------------------------------------+
468// |                           favorite section                            |
469// +-----------------------------------------------------------------------+
470  else if ($page['section'] == 'favorites')
471  {
472    check_user_favorites();
473
474    $query = '
475SELECT image_id
476  FROM '.FAVORITES_TABLE.'
477    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
478  WHERE user_id = '.$user['id'].'
479  '.$conf['order_by'].'
480;';
481
482    $page = array_merge(
483      $page,
484      array(
485        'title' => $lang['favorites'],
486        'items' => array_from_query($query, 'image_id'),
487        'thumbnails_include' => 'include/category_default.inc.php',
488        )
489      );
490  }
491// +-----------------------------------------------------------------------+
492// |                       recent pictures section                         |
493// +-----------------------------------------------------------------------+
494  else if ($page['section'] == 'recent_pics')
495  {
496    $query = '
497SELECT DISTINCT(id)
498  FROM '.IMAGES_TABLE.'
499    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
500  WHERE date_available > \''.
501      date('Y-m-d', time() - 60*60*24*$user['recent_period']).'\'
502    AND '.$forbidden.'
503  '.$conf['order_by'].'
504;';
505
506    $page = array_merge(
507      $page,
508      array(
509        'title' => $lang['recent_pics_cat'],
510        'items' => array_from_query($query, 'id'),
511        'thumbnails_include' => 'include/category_default.inc.php',
512        )
513      );
514  }
515// +-----------------------------------------------------------------------+
516// |                 recently updated categories section                   |
517// +-----------------------------------------------------------------------+
518  else if ($page['section'] == 'recent_cats')
519  {
520    $page = array_merge(
521      $page,
522      array(
523        'title' => $lang['recent_cats_cat'],
524        'thumbnails_include' => 'include/category_recent_cats.inc.php',
525        )
526      );
527  }
528// +-----------------------------------------------------------------------+
529// |                        most visited section                           |
530// +-----------------------------------------------------------------------+
531  else if ($page['section'] == 'most_visited')
532  {
533    $page['super_order_by'] = true;
534    $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
535    $query = '
536SELECT DISTINCT(id)
537  FROM '.IMAGES_TABLE.'
538    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
539  WHERE hit > 0
540    AND '.$forbidden.'
541    '.$conf['order_by'].'
542  LIMIT 0, '.$conf['top_number'].'
543;';
544
545    $page = array_merge(
546      $page,
547      array(
548        'title' => $conf['top_number'].' '.$lang['most_visited_cat'],
549        'items' => array_from_query($query, 'id'),
550        'thumbnails_include' => 'include/category_default.inc.php',
551        )
552      );
553  }
554// +-----------------------------------------------------------------------+
555// |                          best rated section                           |
556// +-----------------------------------------------------------------------+
557  else if ($page['section'] == 'best_rated')
558  {
559    $page['super_order_by'] = true;
560    $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC';
561
562    $query ='
563SELECT DISTINCT(id)
564  FROM '.IMAGES_TABLE.'
565    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
566  WHERE average_rate IS NOT NULL
567    AND '.$forbidden.'
568    '.$conf['order_by'].'
569  LIMIT 0, '.$conf['top_number'].'
570;';
571    $page = array_merge(
572      $page,
573      array(
574        'title' => $conf['top_number'].' '.$lang['best_rated_cat'],
575        'items' => array_from_query($query, 'id'),
576        'thumbnails_include' => 'include/category_default.inc.php',
577        )
578      );
579  }
580// +-----------------------------------------------------------------------+
581// |                             list section                              |
582// +-----------------------------------------------------------------------+
583  else if ($page['section'] == 'list')
584  {
585    $query ='
586SELECT DISTINCT(id)
587  FROM '.IMAGES_TABLE.'
588    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
589  WHERE image_id IN ('.implode(',', $page['list']).')
590    AND '.$forbidden.'
591  '.$conf['order_by'].'
592;';
593
594    $page = array_merge(
595      $page,
596      array(
597        'title' => $lang['random_cat'],
598        'items' => array_from_query($query, 'id'),
599        'thumbnails_include' => 'include/category_default.inc.php',
600        )
601      );
602  }
603}
604
605// +-----------------------------------------------------------------------+
606// |                             chronology                                |
607// +-----------------------------------------------------------------------+
608
609if (isset($page['chronology_field']))
610{
611  include_once( PHPWG_ROOT_PATH.'include/functions_calendar.inc.php' );
612  initialize_calendar();
613}
614
615$page['cat_nb_images'] = isset($page['items']) ? count($page['items']) : 0;
616
617if (basename($_SERVER['SCRIPT_FILENAME']) == 'picture.php'
618    and !isset($page['image_id']) )
619{
620  if ( !empty($page['items']) )
621  {
622    $query = '
623SELECT id,file
624  FROM '.IMAGES_TABLE .'
625  WHERE id IN ('.implode(',',$page['items']).')
626  AND file LIKE "' . $page['image_file'] . '.%" ESCAPE "|"'
627;
628    $result = pwg_query($query);
629    if (mysql_num_rows($result)>0)
630    {
631      list($page['image_id'], $page['image_file']) = mysql_fetch_row($result);
632    }
633  }
634  if ( !isset($page['image_id']) )
635  {
636    $page['image_id'] = -1; // will fail in picture.php
637  }
638}
639?>
Note: See TracBrowser for help on using the repository browser.