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

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

moved category.php to index.php

split url functions from functions.inc.php to functions_url.inc.php

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