source: branches/branch-1_7/include/section_init.inc.php @ 1997

Last change on this file since 1997 was 1997, checked in by rvelices, 17 years ago

merge r1996 from trunk to branch-1_7
correct bug on one of my previous commits (flat for picture page)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 16.2 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-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: section_init.inc.php 1997 2007-05-02 23:08:45Z rvelices $
8// | last update   : $Date: 2007-05-02 23:08:45 +0000 (Wed, 02 May 2007) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 1997 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27/**
28 * This included page checks section related parameter and provides
29 * following informations:
30 *
31 * - $page['title']
32 *
33 * - $page['items']: ordered list of items to display
34 *
35 */
36
37// "index.php?/category/12-foo/start-24" or
38// "index.php/category/12-foo/start-24"
39// must return :
40//
41// array(
42//   'section'  => 'categories',
43//   'category' => array('id'=>12, ...),
44//   'start'    => 24
45//   );
46
47$page['items'] = array();
48
49// some ISPs set PATH_INFO to empty string or to SCRIPT_FILENAME while in the
50// default apache implementation it is not set
51if ( $conf['question_mark_in_urls']==false and
52     isset($_SERVER["PATH_INFO"]) and !empty($_SERVER["PATH_INFO"]) )
53{
54  $rewritten = $_SERVER["PATH_INFO"];
55  $rewritten = str_replace('//', '/', $rewritten);
56  $path_count = count( explode('/', $rewritten) );
57  $page['root_path'] = PHPWG_ROOT_PATH.str_repeat('../', $path_count-1);
58}
59else
60{
61  $rewritten = '';
62  foreach (array_keys($_GET) as $keynum => $key)
63  {
64    $rewritten = $key;
65    break;
66  }
67  $page['root_path'] = PHPWG_ROOT_PATH;
68}
69
70// deleting first "/" if displayed
71$tokens = explode(
72  '/',
73  preg_replace('#^/#', '', $rewritten)
74  );
75// $tokens = array(
76//   0 => category,
77//   1 => 12-foo,
78//   2 => start-24
79//   );
80
81$next_token = 0;
82if (script_basename() == 'picture') // basename without file extention
83{ // the first token must be the identifier for the picture
84  if ( isset($_GET['image_id'])
85       and isset($_GET['cat']) and is_numeric($_GET['cat']) )
86  {// url compatibility with versions below 1.6
87    $url = make_picture_url( array(
88        'section' => 'categories',
89        'category' => get_cat_info($_GET['cat']),
90        'image_id' => $_GET['image_id']
91      ) );
92    redirect($url);
93  }
94  $token = $tokens[$next_token];
95  $next_token++;
96  if ( is_numeric($token) )
97  {
98    $page['image_id'] = $token;
99  }
100  else
101  {
102    preg_match('/^(\d+-)?(.*)?$/', $token, $matches);
103    if (isset($matches[1]) and is_numeric($matches[1]=rtrim($matches[1],'-')) )
104    {
105      $page['image_id'] = $matches[1];
106      if ( !empty($matches[2]) )
107      {
108        $page['image_file'] = $matches[2];
109      }
110    }
111    else
112    {
113      if ( !empty($matches[2]) )
114      {
115        $page['image_file'] = $matches[2];
116      }
117      else
118      {
119        bad_request('picture identifier is missing');
120      }
121    }
122  }
123}
124
125$page = array_merge( $page, parse_section_url( $tokens, $next_token) );
126if ( !isset($page['section']) )
127{
128  $page['section'] = 'categories';
129
130  switch (script_basename())
131  {
132    case 'picture':
133      break;
134    case 'index':
135    {
136      // No section defined, go to selected url
137      if (!empty($conf['random_index_redirect']) and empty($tokens[$next_token]) )
138      {
139        $random_index_redirect = array();
140        foreach ($conf['random_index_redirect'] as $random_url => $random_url_condition)
141        {
142          if (empty($random_url_condition) or eval($random_url_condition))
143          {
144            $random_index_redirect[] = $random_url;
145          }
146        }
147        if (!empty($random_index_redirect))
148        {
149          redirect($random_index_redirect[mt_rand(0, count($random_index_redirect)-1)]);
150        }
151      }
152      break;
153    }
154    default:
155      trigger_error('script_basename "'.script_basename().'" unknown',
156        E_USER_WARNING);
157  }
158}
159
160
161$page = array_merge( $page, parse_well_known_params_url( $tokens, $next_token) );
162
163
164if ( script_basename()=='picture' and 'categories'==$page['section'] and
165      !isset($page['category']) and !isset($page['chronology_field']) )
166{ //access a picture only by id, file or id-file without given section
167  $page['flat']=true;
168}
169
170
171// $page['nb_image_page'] is the number of picture to display on this page
172// By default, it is the same as the $user['nb_image_page']
173$page['nb_image_page'] = $user['nb_image_page'];
174
175if (pwg_get_session_var('image_order',0) > 0)
176{
177  $orders = get_category_preferred_image_orders();
178
179  $conf['order_by'] = str_replace(
180    'ORDER BY ',
181    'ORDER BY '.$orders[ pwg_get_session_var('image_order',0) ][1].',',
182    $conf['order_by']
183    );
184  $page['super_order_by'] = true;
185}
186
187$forbidden = get_sql_condition_FandF(
188      array
189        (
190          'forbidden_categories' => 'category_id',
191          'visible_categories' => 'category_id',
192          'visible_images' => 'id'
193        ),
194      'AND'
195  );
196
197// +-----------------------------------------------------------------------+
198// |                              category                                 |
199// +-----------------------------------------------------------------------+
200if ('categories' == $page['section'])
201{
202  if (isset($page['category']))
203  {
204    $page = array_merge(
205      $page,
206      array(
207        'comment'           => $page['category']['comment'],
208        'title'             =>
209          get_cat_display_name($page['category']['upper_names'], '', false),
210        )
211      );
212  }
213  else
214  {
215    $page['title'] = $lang['no_category'];
216  }
217
218  if
219    (
220      (!isset($page['chronology_field'])) and
221      (
222        (isset($page['category'])) or
223        (isset($page['flat']))
224      )
225    )
226  {
227    if ( !empty($page['category']['image_order']) and !isset($page['super_order_by']) )
228    {
229      $conf[ 'order_by' ] = ' ORDER BY '.$page['category']['image_order'];
230    }
231
232    if (isset($page['flat']))
233    {// flat categories mode
234      if ( isset($page['category']) )
235      {
236        $subcat_ids = get_subcat_ids( array($page['category']['id']) );
237        $where_sql = 'category_id IN ('.implode(',',$subcat_ids).')';
238      }
239      else
240      {
241        $where_sql = '1=1';
242      }
243    }
244    else
245    {// Normal mode
246      $where_sql = 'category_id = '.$page['category']['id'];
247    }
248
249    // Main query
250    $query = '
251SELECT DISTINCT(image_id)
252  FROM '.IMAGE_CATEGORY_TABLE.'
253    INNER JOIN '.IMAGES_TABLE.' ON id = image_id
254  WHERE
255    '.$where_sql.'
256'.$forbidden.'
257  '.$conf['order_by'].'
258;';
259
260    $page['items'] = array_from_query($query, 'image_id');
261  } //otherwise the calendar will requery all subitems
262}
263// special sections
264else
265{
266// +-----------------------------------------------------------------------+
267// |                            tags section                               |
268// +-----------------------------------------------------------------------+
269  if ($page['section'] == 'tags')
270  {
271    $page['tag_ids'] = array();
272    foreach ($page['tags'] as $tag)
273    {
274      array_push($page['tag_ids'], $tag['id']);
275    }
276
277    $items = get_image_ids_for_tags($page['tag_ids']);
278
279    // permissions depends on category, so to only keep images that are
280    // reachable to the connected user, we need to check category
281    // associations
282    if (!empty($items) )
283    {
284      $query = '
285SELECT image_id
286  FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.IMAGES_TABLE.' ON image_id=id
287  WHERE image_id IN ('.implode(',', $items).')
288    '.$forbidden.
289    $conf['order_by'].'
290;';
291      $items = array_unique(
292        array_from_query($query, 'image_id')
293        );
294    }
295
296    $title = get_tags_content_title();
297
298    $page = array_merge(
299      $page,
300      array(
301        'title' => $title,
302        'items' => array_values($items),
303        )
304      );
305  }
306// +-----------------------------------------------------------------------+
307// |                           search section                              |
308// +-----------------------------------------------------------------------+
309  if ($page['section'] == 'search')
310  {
311    include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' );
312
313    $search_result = get_search_results($page['search']);
314    if ( !empty($search_result['items']) and !isset($search_result['as_is']) )
315    {
316      $query = '
317SELECT DISTINCT(id)
318  FROM '.IMAGES_TABLE.'
319    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
320  WHERE id IN ('.implode(',', $search_result['items']).')
321    '.$forbidden.'
322  '.$conf['order_by'].'
323;';
324      $page['items'] = array_from_query($query, 'id');
325    }
326    else
327    {
328      $page['items'] = $search_result['items'];
329    }
330
331    $page = array_merge(
332      $page,
333      array(
334        'title' => $lang['search_result'],
335        )
336      );
337  }
338// +-----------------------------------------------------------------------+
339// |                           favorite section                            |
340// +-----------------------------------------------------------------------+
341  else if ($page['section'] == 'favorites')
342  {
343    check_user_favorites();
344
345    $query = '
346SELECT image_id
347  FROM '.FAVORITES_TABLE.'
348    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
349  WHERE user_id = '.$user['id'].'
350'.get_sql_condition_FandF
351  (
352    array
353      (
354        'visible_images' => 'image_id'
355      ),
356    'AND'
357  ).'
358  '.$conf['order_by'].'
359;';
360
361    $page = array_merge(
362      $page,
363      array(
364        'title' => $lang['favorites'],
365        'items' => array_from_query($query, 'image_id'),
366        )
367      );
368  }
369// +-----------------------------------------------------------------------+
370// |                       recent pictures section                         |
371// +-----------------------------------------------------------------------+
372  else if ($page['section'] == 'recent_pics')
373  {
374    $query = '
375SELECT DISTINCT(id)
376  FROM '.IMAGES_TABLE.'
377    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
378  WHERE
379    date_available >= SUBDATE(
380      CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY)
381    '.$forbidden.'
382  '.$conf['order_by'].'
383;';
384
385    $page = array_merge(
386      $page,
387      array(
388        'title' => '<a href="'.duplicate_index_url().'">'
389                  .$lang['recent_pics_cat'].'</a>',
390        'items' => array_from_query($query, 'id'),
391        )
392      );
393  }
394// +-----------------------------------------------------------------------+
395// |                 recently updated categories section                   |
396// +-----------------------------------------------------------------------+
397  else if ($page['section'] == 'recent_cats')
398  {
399    $page = array_merge(
400      $page,
401      array(
402        'title' => $lang['recent_cats_cat'],
403        )
404      );
405  }
406// +-----------------------------------------------------------------------+
407// |                        most visited section                           |
408// +-----------------------------------------------------------------------+
409  else if ($page['section'] == 'most_visited')
410  {
411    $page['super_order_by'] = true;
412    $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
413    $query = '
414SELECT DISTINCT(id)
415  FROM '.IMAGES_TABLE.'
416    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
417  WHERE hit > 0
418    '.$forbidden.'
419    '.$conf['order_by'].'
420  LIMIT 0, '.$conf['top_number'].'
421;';
422
423    $page = array_merge(
424      $page,
425      array(
426        'title' => '<a href="'.duplicate_index_url().'">'
427                  .$conf['top_number'].' '.$lang['most_visited_cat'].'</a>',
428        'items' => array_from_query($query, 'id'),
429        )
430      );
431  }
432// +-----------------------------------------------------------------------+
433// |                          best rated section                           |
434// +-----------------------------------------------------------------------+
435  else if ($page['section'] == 'best_rated')
436  {
437    $page['super_order_by'] = true;
438    $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC';
439
440    $query ='
441SELECT DISTINCT(id)
442  FROM '.IMAGES_TABLE.'
443    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
444  WHERE average_rate IS NOT NULL
445    '.$forbidden.'
446    '.$conf['order_by'].'
447  LIMIT 0, '.$conf['top_number'].'
448;';
449    $page = array_merge(
450      $page,
451      array(
452        'title' => '<a href="'.duplicate_index_url().'">'
453                  .$conf['top_number'].' '.$lang['best_rated_cat'].'</a>',
454        'items' => array_from_query($query, 'id'),
455        )
456      );
457  }
458// +-----------------------------------------------------------------------+
459// |                             list section                              |
460// +-----------------------------------------------------------------------+
461  else if ($page['section'] == 'list')
462  {
463    $query ='
464SELECT DISTINCT(id)
465  FROM '.IMAGES_TABLE.'
466    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
467  WHERE image_id IN ('.implode(',', $page['list']).')
468    '.$forbidden.'
469  '.$conf['order_by'].'
470;';
471
472    $page = array_merge(
473      $page,
474      array(
475        'title' => '<a href="'.duplicate_index_url().'">'
476                    .$lang['random_cat'].'</a>',
477        'items' => array_from_query($query, 'id'),
478        )
479      );
480  }
481}
482
483// +-----------------------------------------------------------------------+
484// |                             chronology                                |
485// +-----------------------------------------------------------------------+
486
487if (isset($page['chronology_field']))
488{
489  include_once( PHPWG_ROOT_PATH.'include/functions_calendar.inc.php' );
490  initialize_calendar();
491}
492
493if (script_basename() == 'picture'
494    and !isset($page['image_id']) )
495{
496  if ( !empty($page['items']) )
497  {
498    $query = '
499SELECT id,file
500  FROM '.IMAGES_TABLE .'
501  WHERE id IN ('.implode(',',$page['items']).')
502  AND file LIKE "' . $page['image_file'] . '.%" ESCAPE "|"'
503;
504    $result = pwg_query($query);
505    if (mysql_num_rows($result)>0)
506    {
507      list($page['image_id'], $page['image_file']) = mysql_fetch_row($result);
508    }
509  }
510  if ( !isset($page['image_id']) )
511  {
512    $page['image_id'] = -1; // will fail in picture.php
513  }
514}
515
516// add meta robots noindex, nofollow to avoid unnecesary robot crawls
517$page['meta_robots']=array();
518if ( isset($page['chronology_field']) or isset($page['flat'])
519      or 'list'==$page['section'] or 'recent_pics'==$page['section'] )
520{
521  $page['meta_robots']=array('noindex'=>1, 'nofollow'=>1);
522}
523elseif ('tags' == $page['section'])
524{
525  if ( count($page['tag_ids'])>1 )
526  {
527    $page['meta_robots']=array('noindex'=>1, 'nofollow'=>1);
528  }
529}
530elseif ('recent_cats'==$page['section'])
531{
532  $page['meta_robots']['nofollow']=1;
533}
534if ( $filter['enabled'] )
535{
536  $page['meta_robots']['noindex']=1;
537}
538
539// see if we need a redirect because of a permalink
540if ( 'categories'==$page['section'] and isset($page['category']) )
541{
542  $need_redirect=false;
543  if ( empty($page['category']['permalink']) )
544  {
545    if ( $conf['category_url_style'] == 'id-name' and
546        @$page['hit_by']['cat_url_name'] !== str2url($page['category']['name']) )
547    {
548      $need_redirect=true;
549    }
550  }
551  else
552  {
553    if ( $page['category']['permalink'] !== @$page['hit_by']['cat_permalink'] )
554    {
555      $need_redirect=true;
556    }
557  }
558
559  if ($need_redirect)
560  {
561    $redirect_url = ( script_basename()=='picture'
562        ? duplicate_picture_url()
563          : duplicate_index_url()
564      );
565    if (!headers_sent())
566    { // this is a permanent redirection
567      set_status_header(301);
568      redirect_http( $redirect_url );
569    }
570    redirect( $redirect_url );
571  }
572  unset( $need_redirect, $page['hit_by'] );
573}
574
575trigger_action('loc_end_section_init');
576?>
Note: See TracBrowser for help on using the repository browser.