source: branches/2.0/include/section_init.inc.php @ 18245

Last change on this file since 18245 was 5938, checked in by laurent.duretz, 14 years ago

Issue 1521 : report trunk on branch 2.0

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