source: trunk/include/section_init.inc.php @ 1094

Last change on this file since 1094 was 1094, checked in by rvelices, 19 years ago

URL rewrite: 3 options in the config file define behaviour (question mark
removal, file name for picture and .php extension removal)

fix: added unsigned for column in install sql - for the sake of uniformization

change: add_url_param is now add_url_params and takes an array as parameter
instead of a string

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.8 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: 2006-03-23 01:49:04 +0000 (Thu, 23 Mar 2006) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1094 $
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"]) )
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//phpinfo();
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 last token must be the identifier for the picture
88  $token = array_pop($tokens);
89  if ( is_numeric($token) )
90  {
91    $page['image_id'] = $token;
92  }
93  else
94  {
95    preg_match('/^(\d+-)?((.*)[_\.]html?)?$/', $token, $matches);
96    if (isset($matches[1]) and is_numeric($matches[1]=rtrim($matches[1],'-')) )
97    {
98      $page['image_id'] = $matches[1];
99      if ( !empty($matches[3]) )
100      {
101        $page['image_file'] = $matches[3];
102      }
103
104    }
105    else
106    {
107      if ( !empty($matches[3]) )
108      {
109        $page['image_file'] = $matches[3];
110      }
111      else
112      {
113        die('Fatal: picture identifier is missing');
114      }
115    }
116  }
117}
118
119if (0 === strpos($tokens[$next_token], 'cat'))
120{
121  $page['section'] = 'categories';
122  $next_token++;
123
124  if (isset($tokens[$next_token])
125      and preg_match('/^(\d+)/', $tokens[$next_token], $matches))
126  {
127    $page['category'] = $matches[1];
128    $next_token++;
129  }
130}
131else if (0 === strpos($tokens[$next_token], 'tag'))
132{
133  $page['section'] = 'tags';
134  $page['tags'] = array();
135
136  $next_token++;
137
138  for ($i = $next_token; ; $i++)
139  {
140    if (!isset($tokens[$i]))
141    {
142      break;
143    }
144
145    preg_match('/^(\d+)/', $tokens[$i], $matches);
146    if (!isset($matches[1]))
147    {
148      if (0 == count($page['tags']))
149      {
150        die('Fatal: at least one tag required');
151      }
152      else
153      {
154        break;
155      }
156    }
157    array_push($page['tags'], $matches[1]);
158  }
159
160  $next_token = $i;
161}
162else if (0 === strpos($tokens[$next_token], 'fav'))
163{
164  $page['section'] = 'favorites';
165  $next_token++;
166}
167else if ('most_visited' == $tokens[$next_token])
168{
169  $page['section'] = 'most_visited';
170  $next_token++;
171}
172else if ('best_rated' == $tokens[$next_token])
173{
174  $page['section'] = 'best_rated';
175  $next_token++;
176}
177else if ('recent_pics' == $tokens[$next_token])
178{
179  $page['section'] = 'recent_pics';
180  $next_token++;
181}
182else if ('recent_cats' == $tokens[$next_token])
183{
184  $page['section'] = 'recent_cats';
185  $next_token++;
186}
187else if ('search' == $tokens[$next_token])
188{
189  $page['section'] = 'search';
190  $next_token++;
191
192  preg_match('/(\d+)/', $tokens[$next_token], $matches);
193  if (!isset($matches[1]))
194  {
195    die('Fatal: search identifier is missing');
196  }
197  $page['search'] = $matches[1];
198  $next_token++;
199}
200else if ('list' == $tokens[$next_token])
201{
202  $page['section'] = 'list';
203  $next_token++;
204
205  $page['list'] = array();
206  if (!preg_match('/^\d+(,\d+)*$/', $tokens[$next_token]))
207  {
208    die('wrong format on list GET parameter');
209  }
210  foreach (explode(',', $tokens[$next_token]) as $image_id)
211  {
212    array_push($page['list'], $image_id);
213  }
214  $next_token++;
215}
216else
217{
218  $page['section'] = 'categories';
219  $next_token++;
220}
221
222for ($i = $next_token; ; $i++)
223{
224  if (!isset($tokens[$i]))
225  {
226    break;
227  }
228
229  if (preg_match('/^start-(\d+)/', $tokens[$i], $matches))
230  {
231    $page['start'] = $matches[1];
232  }
233
234  if (preg_match('/^posted|created/', $tokens[$i] ))
235  {
236    $chronology_tokens = explode('-', $tokens[$i] );
237    $page['chronology_field'] = $chronology_tokens[0];
238    array_shift($chronology_tokens);
239    $page['chronology_style'] = $chronology_tokens[0];
240    array_shift($chronology_tokens);
241    if ( count($chronology_tokens)>0 )
242    {
243      if ('list'==$chronology_tokens[0] or
244          'calendar'==$chronology_tokens[0])
245      {
246        $page['chronology_view'] = $chronology_tokens[0];
247        array_shift($chronology_tokens);
248      }
249      $page['chronology_date'] = $chronology_tokens;
250    }
251  }
252}
253
254
255// $page['nb_image_page'] is the number of picture to display on this page
256// By default, it is the same as the $user['nb_image_page']
257$page['nb_image_page'] = $user['nb_image_page'];
258
259if (isset($_COOKIE['pwg_image_order'])
260    and is_numeric($_COOKIE['pwg_image_order'])
261    and $_COOKIE['pwg_image_order'] > 0)
262{
263  $orders = get_category_preferred_image_orders();
264
265  $conf['order_by'] = str_replace(
266    'ORDER BY ',
267    'ORDER BY '.$orders[ $_COOKIE['pwg_image_order'] ][1].',',
268    $conf['order_by']
269    );
270  $page['super_order_by'] = true;
271}
272
273// +-----------------------------------------------------------------------+
274// |                              category                                 |
275// +-----------------------------------------------------------------------+
276if ('categories' == $page['section'])
277{
278  if (isset($page['category']))
279  {
280    $result = get_cat_info($page['category']);
281
282    $page = array_merge(
283      $page,
284      array(
285        'comment'          => $result['comment'],
286        'cat_dir'          => $result['dir'],
287        'cat_name'         => $result['name'],
288        'cat_nb_images'    => $result['nb_images'],
289        'cat_site_id'      => $result['site_id'],
290        'cat_uploadable'   => $result['uploadable'],
291        'cat_commentable'  => $result['commentable'],
292        'cat_id_uppercat'  => $result['id_uppercat'],
293        'uppercats'        => $result['uppercats'],
294
295        'title' => get_cat_display_name($result['name'], null, false),
296        )
297      );
298
299    if (!isset($page['chronology_field']))
300    {
301      $query = '
302SELECT image_id
303  FROM '.IMAGE_CATEGORY_TABLE.'
304    INNER JOIN '.IMAGES_TABLE.' ON id = image_id
305  WHERE category_id = '.$page['category'].'
306  '.$conf['order_by'].'
307;';
308      $page['items'] = array_from_query($query, 'image_id');
309
310      $page['thumbnails_include'] =
311        $result['nb_images'] > 0
312        ? 'include/category_default.inc.php'
313        : 'include/category_subcats.inc.php';
314    } //otherwise the calendar will requery all subitems
315  }
316  else
317  {
318    $page['title'] = $lang['no_category'];
319    $page['thumbnails_include'] = 'include/category_subcats.inc.php';
320  }
321}
322// special sections
323else
324{
325  if (!empty($user['forbidden_categories']))
326  {
327    $forbidden =
328      ' category_id NOT IN ('.$user['forbidden_categories'].')';
329  }
330  else
331  {
332    $forbidden = ' 1 = 1';
333  }
334// +-----------------------------------------------------------------------+
335// |                           search section                              |
336// +-----------------------------------------------------------------------+
337  if ($page['section'] == 'search')
338  {
339    $query = '
340SELECT DISTINCT(id)
341  FROM '.IMAGES_TABLE.'
342    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
343  WHERE '.get_sql_search_clause($page['search']).'
344    AND '.$forbidden.'
345  '.$conf['order_by'].'
346;';
347
348    $page = array_merge(
349      $page,
350      array(
351        'title' => $lang['search_result'],
352        'items' => array_from_query($query, 'id'),
353        'thumbnails_include' => 'include/category_default.inc.php',
354        )
355      );
356  }
357// +-----------------------------------------------------------------------+
358// |                           favorite section                            |
359// +-----------------------------------------------------------------------+
360  else if ($page['section'] == 'favorites')
361  {
362    check_user_favorites();
363
364    $query = '
365SELECT image_id
366  FROM '.FAVORITES_TABLE.'
367    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
368  WHERE user_id = '.$user['id'].'
369  '.$conf['order_by'].'
370;';
371
372    $page = array_merge(
373      $page,
374      array(
375        'title' => $lang['favorites'],
376        'items' => array_from_query($query, 'image_id'),
377        'thumbnails_include' => 'include/category_default.inc.php',
378        )
379      );
380  }
381// +-----------------------------------------------------------------------+
382// |                       recent pictures section                         |
383// +-----------------------------------------------------------------------+
384  else if ($page['section'] == 'recent_pics')
385  {
386    $query = '
387SELECT DISTINCT(id)
388  FROM '.IMAGES_TABLE.'
389    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
390  WHERE date_available > \''.
391      date('Y-m-d', time() - 60*60*24*$user['recent_period']).'\'
392    AND '.$forbidden.'
393  '.$conf['order_by'].'
394;';
395
396    $page = array_merge(
397      $page,
398      array(
399        'title' => $lang['recent_pics_cat'],
400        'items' => array_from_query($query, 'id'),
401        'thumbnails_include' => 'include/category_default.inc.php',
402        )
403      );
404  }
405// +-----------------------------------------------------------------------+
406// |                 recently updated categories section                   |
407// +-----------------------------------------------------------------------+
408  else if ($page['section'] == 'recent_cats')
409  {
410    $page = array_merge(
411      $page,
412      array(
413        'title' => $lang['recent_cats_cat'],
414        'cat_nb_images' => 0,
415        'thumbnails_include' => 'include/category_recent_cats.inc.php',
416        )
417      );
418  }
419// +-----------------------------------------------------------------------+
420// |                        most visited section                           |
421// +-----------------------------------------------------------------------+
422  else if ($page['section'] == 'most_visited')
423  {
424    $page['super_order_by'] = true;
425    $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
426    $query = '
427SELECT DISTINCT(id)
428  FROM '.IMAGES_TABLE.'
429    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
430  WHERE hit > 0
431    AND '.$forbidden.'
432    '.$conf['order_by'].'
433  LIMIT 0, '.$conf['top_number'].'
434;';
435
436    $page = array_merge(
437      $page,
438      array(
439        'title' => $conf['top_number'].' '.$lang['most_visited_cat'],
440        'items' => array_from_query($query, 'id'),
441        'thumbnails_include' => 'include/category_default.inc.php',
442        )
443      );
444  }
445// +-----------------------------------------------------------------------+
446// |                          best rated section                           |
447// +-----------------------------------------------------------------------+
448  else if ($page['section'] == 'best_rated')
449  {
450    $page['super_order_by'] = true;
451    $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC';
452
453    $query ='
454SELECT DISTINCT(id)
455  FROM '.IMAGES_TABLE.'
456    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
457  WHERE average_rate IS NOT NULL
458    AND '.$forbidden.'
459    '.$conf['order_by'].'
460  LIMIT 0, '.$conf['top_number'].'
461;';
462    $page = array_merge(
463      $page,
464      array(
465        'title' => $conf['top_number'].' '.$lang['best_rated_cat'],
466        'items' => array_from_query($query, 'id'),
467        'thumbnails_include' => 'include/category_default.inc.php',
468        )
469      );
470  }
471// +-----------------------------------------------------------------------+
472// |                             list section                              |
473// +-----------------------------------------------------------------------+
474  else if ($page['section'] == 'list')
475  {
476    $query ='
477SELECT DISTINCT(id)
478  FROM '.IMAGES_TABLE.'
479    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
480  WHERE image_id IN ('.implode(',', $page['list']).')
481    AND '.$forbidden.'
482  '.$conf['order_by'].'
483;';
484
485    $page = array_merge(
486      $page,
487      array(
488        'title' => $lang['random_cat'],
489        'items' => array_from_query($query, 'id'),
490        'thumbnails_include' => 'include/category_default.inc.php',
491        )
492      );
493  }
494
495  if (!isset($page['cat_nb_images']))
496  {
497    $page['cat_nb_images'] = count($page['items']);
498  }
499}
500
501// +-----------------------------------------------------------------------+
502// |                             chronology                                |
503// +-----------------------------------------------------------------------+
504
505if (isset($page['chronology_field']))
506{
507  include_once( PHPWG_ROOT_PATH.'include/functions_calendar.inc.php' );
508  initialize_calendar();
509}
510
511if (basename($_SERVER['SCRIPT_FILENAME']) == 'picture.php'
512    and !isset($page['image_id']) )
513{
514  if ( !empty($page['items']) )
515  {
516    $query = '
517SELECT id,file
518  FROM '.IMAGES_TABLE .'
519  WHERE id IN ('.implode(',',$page['items']).')
520  AND file LIKE "' . $page['image_file'] . '.%" ESCAPE "|"'
521;
522    $result = pwg_query($query);
523    if (mysql_num_rows($result)>0)
524    {
525      list($page['image_id'], $page['image_file']) = mysql_fetch_row($result);
526    }
527  }
528  if ( !isset($page['image_id']) )
529  {
530    $page['image_id'] = -1; // will fail in picture.php
531  }
532}
533?>
Note: See TracBrowser for help on using the repository browser.