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

Last change on this file since 5679 was 5469, checked in by laurent.duretz, 15 years ago

Issue 1521 : get_absolute_root_url() missing in section_init.inc.php

File size: 18.4 KB
RevLine 
[1036]1<?php
2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
[5196]5// | Copyright(C) 2008-2010 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
[2773]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    {
[3167]111      $page['image_id'] = 0; // more work in picture.php
[1109]112      if ( !empty($matches[2]) )
[1092]113      {
[1109]114        $page['image_file'] = $matches[2];
[1092]115      }
116      else
117      {
[1852]118        bad_request('picture identifier is missing');
[1092]119      }
120    }
121  }
[1090]122}
[1086]123
[1980]124$page = array_merge( $page, parse_section_url( $tokens, $next_token) );
[4385]125
[1980]126if ( !isset($page['section']) )
[1090]127{
128  $page['section'] = 'categories';
[1086]129
[1792]130  switch (script_basename())
[1788]131  {
[1792]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]) )
[1788]138      {
[1792]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        }
[1788]151      }
[1792]152      break;
[1788]153    }
[1880]154    default:
155      trigger_error('script_basename "'.script_basename().'" unknown',
156        E_USER_WARNING);
[1788]157  }
158}
159
[1980]160$page = array_merge( $page, parse_well_known_params_url( $tokens, $next_token) );
161if ( script_basename()=='picture' and 'categories'==$page['section'] and
[1996]162      !isset($page['category']) and !isset($page['chronology_field']) )
[1980]163{ //access a picture only by id, file or id-file without given section
164  $page['flat']=true;
[1036]165}
166
[1047]167// $page['nb_image_page'] is the number of picture to display on this page
168// By default, it is the same as the $user['nb_image_page']
169$page['nb_image_page'] = $user['nb_image_page'];
[1036]170
[2517]171// if flat mode is active, we must consider the image set as a standard set
172// and not as a category set because we can't use the #image_category.rank :
173// displayed images are not directly linked to the displayed category
174if ('categories' == $page['section'] and !isset($page['flat']))
175{
176  $conf['order_by'] = $conf['order_by_inside_category'];
177}
178
[1623]179if (pwg_get_session_var('image_order',0) > 0)
[1051]180{
[2517]181  $image_order_id = pwg_get_session_var('image_order');
[2773]182
[1051]183  $orders = get_category_preferred_image_orders();
184
[2517]185  // the current session stored image_order might be not compatible with
186  // current image set, for example if the current image_order is the rank
187  // and that we are displaying images related to a tag.
188  //
189  // In case of incompatibility, the session stored image_order is removed.
190  if ($orders[$image_order_id][2])
191  {
192    $conf['order_by'] = str_replace(
193      'ORDER BY ',
194      'ORDER BY '.$orders[$image_order_id][1].',',
195      $conf['order_by']
[1051]196    );
[2517]197    $page['super_order_by'] = true;
198
199  }
200  else
201  {
202    pwg_unset_session_var('image_order');
203    $page['super_order_by'] = false;
204  }
[1051]205}
206
[1711]207$forbidden = get_sql_condition_FandF(
208      array
209        (
210          'forbidden_categories' => 'category_id',
211          'visible_categories' => 'category_id',
[1820]212          'visible_images' => 'id'
[1711]213        ),
214      'AND'
215  );
216
[1036]217// +-----------------------------------------------------------------------+
218// |                              category                                 |
219// +-----------------------------------------------------------------------+
[1082]220if ('categories' == $page['section'])
221{
222  if (isset($page['category']))
[1036]223  {
224    $page = array_merge(
225      $page,
226      array(
[2117]227        'comment'           =>
228            trigger_event(
229              'render_category_description',
[2175]230              $page['category']['comment'],
231              'main_page_category_description'
[2117]232            ),
[1703]233        'title'             =>
[1980]234          get_cat_display_name($page['category']['upper_names'], '', false),
[1051]235        )
236      );
[1677]237  }
238  else
239  {
[5469]240    $page['title'] = '<a href="'.get_absolute_root_url().$conf['home_page'].'">'.l10n('Home').'</a>';
[1677]241  }
[1086]242
[1703]243  if
[1677]244    (
245      (!isset($page['chronology_field'])) and
246      (
[1703]247        (isset($page['category'])) or
[1800]248        (isset($page['flat']))
[1677]249      )
250    )
251  {
[1983]252    if ( !empty($page['category']['image_order']) and !isset($page['super_order_by']) )
[1051]253    {
[1983]254      $conf[ 'order_by' ] = ' ORDER BY '.$page['category']['image_order'];
[1677]255    }
256
[1800]257    if (isset($page['flat']))
[1820]258    {// flat categories mode
259      if ( isset($page['category']) )
[2327]260      { // get all allowed sub-categories
261        $query = '
[2424]262SELECT id
[2327]263  FROM '.CATEGORIES_TABLE.'
[2424]264  WHERE
[2327]265    uppercats LIKE "'.$page['category']['uppercats'].',%" '
266    .get_sql_condition_FandF(
267      array
268        (
269          'forbidden_categories' => 'id',
270          'visible_categories' => 'id',
271        ),
272      "\n  AND"
273          );
274        $subcat_ids = array_from_query($query, 'id');
275        $subcat_ids[] = $page['category']['id'];
[1820]276        $where_sql = 'category_id IN ('.implode(',',$subcat_ids).')';
[2327]277        // remove categories from forbidden because just checked above
278        $forbidden = get_sql_condition_FandF(
279              array( 'visible_images' => 'id' ),
280              'AND'
281          );
[1500]282      }
[1820]283      else
284      {
285        $where_sql = '1=1';
286      }
[1677]287    }
288    else
[1820]289    {// Normal mode
[1861]290      $where_sql = 'category_id = '.$page['category']['id'];
[1677]291    }
[1500]292
[1820]293    // Main query
294    $query = '
[4387]295SELECT DISTINCT(image_id)'.get_extra_fields($conf['order_by']).'
[1051]296  FROM '.IMAGE_CATEGORY_TABLE.'
297    INNER JOIN '.IMAGES_TABLE.' ON id = image_id
[1677]298  WHERE
299    '.$where_sql.'
[1711]300'.$forbidden.'
[1051]301  '.$conf['order_by'].'
302;';
[1677]303
[1820]304    $page['items'] = array_from_query($query, 'image_id');
[1677]305  } //otherwise the calendar will requery all subitems
[1082]306}
307// special sections
308else
309{
[1036]310// +-----------------------------------------------------------------------+
[1119]311// |                            tags section                               |
312// +-----------------------------------------------------------------------+
313  if ($page['section'] == 'tags')
314  {
315    $page['tag_ids'] = array();
316    foreach ($page['tags'] as $tag)
317    {
318      array_push($page['tag_ids'], $tag['id']);
319    }
320
321    $items = get_image_ids_for_tags($page['tag_ids']);
322
323    // permissions depends on category, so to only keep images that are
324    // reachable to the connected user, we need to check category
325    // associations
[1131]326    if (!empty($items) )
[1119]327    {
328      $query = '
[4387]329SELECT DISTINCT image_id'.get_extra_fields($conf['order_by']).'
[1125]330  FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.IMAGES_TABLE.' ON image_id=id
[1119]331  WHERE image_id IN ('.implode(',', $items).')
[1677]332    '.$forbidden.
[1125]333    $conf['order_by'].'
[1119]334;';
[2296]335      $items =  array_from_query($query, 'image_id');
[1119]336    }
337
338    $page = array_merge(
339      $page,
340      array(
[2773]341        'title' => get_tags_content_title(),
[2296]342        'items' => $items,
[1119]343        )
344      );
345  }
346// +-----------------------------------------------------------------------+
[1036]347// |                           search section                              |
348// +-----------------------------------------------------------------------+
[1082]349  if ($page['section'] == 'search')
350  {
[1113]351    include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' );
[1119]352
[2451]353    $search_result = get_search_results($page['search'], @$page['super_order_by'] );
354    if ( isset($search_result['qs']) )
355    {//save the details of the query search
356      $page['qsearch_details'] = $search_result['qs'];
[1120]357    }
[1036]358
[1082]359    $page = array_merge(
360      $page,
361      array(
[2451]362        'items' => $search_result['items'],
[2117]363        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
[5021]364                  .l10n('Search results').'</a>',
[1082]365        )
366      );
367  }
[1036]368// +-----------------------------------------------------------------------+
369// |                           favorite section                            |
370// +-----------------------------------------------------------------------+
[1082]371  else if ($page['section'] == 'favorites')
372  {
373    check_user_favorites();
[1036]374
[3037]375    $page = array_merge(
376      $page,
377      array(
[5021]378        'title' => l10n('Favorites')
[3037]379            )
380    );
381
[3108]382    if (!empty($_GET['action']) && ($_GET['action'] == 'remove_all_from_favorites'))
[3037]383    {
384      $query = '
385DELETE FROM '.FAVORITES_TABLE.'
386  WHERE user_id = '.$user['id'].'
387;';
388      pwg_query($query);
[3108]389      redirect(make_index_url( array('section'=>'favorites') ));
[3037]390    }
[3108]391    else
[3037]392    {
393      $query = '
[1036]394SELECT image_id
395  FROM '.FAVORITES_TABLE.'
396    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
397  WHERE user_id = '.$user['id'].'
[1677]398'.get_sql_condition_FandF
399  (
400    array
401      (
[2451]402        'visible_images' => 'id'
[1677]403      ),
404    'AND'
405  ).'
[1036]406  '.$conf['order_by'].'
407;';
[3037]408      $page = array_merge(
409        $page,
410        array(
411          'items' => array_from_query($query, 'image_id'),
412         )
413      );
[1036]414
[3108]415      if (count($page['items'])>0)
[3037]416      {
417        $template->assign(
418          'favorite',
419          array(
420            'FAVORITE_IMG'  =>
421            get_root_url().get_themeconf('icon_dir').'/del_all_favorites.png',
422            'U_FAVORITE'    => add_url_params(
[3108]423              make_index_url( array('section'=>'favorites') ),
[3037]424              array('action'=>'remove_all_from_favorites')
425               ),
426             )
427           );
428      }
429    }
[1082]430  }
[1036]431// +-----------------------------------------------------------------------+
432// |                       recent pictures section                         |
433// +-----------------------------------------------------------------------+
[1082]434  else if ($page['section'] == 'recent_pics')
435  {
[2424]436    if ( !isset($page['super_order_by']) )
437    {
438      $conf['order_by'] = str_replace(
439        'ORDER BY ',
440        'ORDER BY date_available DESC,',
441        $conf['order_by']
442        );
443    }
444
[1082]445    $query = '
[4388]446SELECT DISTINCT(id)'.get_extra_fields($conf['order_by']).'
[1036]447  FROM '.IMAGES_TABLE.'
448    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
[1876]449  WHERE
[4367]450    date_available >= '.pwg_db_get_recent_period_expression($user['recent_period']).'
[1677]451    '.$forbidden.'
[1036]452  '.$conf['order_by'].'
453;';
454
[1082]455    $page = array_merge(
456      $page,
457      array(
[2117]458        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
[5021]459                  .l10n('Recent pictures').'</a>',
[1082]460        'items' => array_from_query($query, 'id'),
461        )
462      );
463  }
[1036]464// +-----------------------------------------------------------------------+
465// |                 recently updated categories section                   |
466// +-----------------------------------------------------------------------+
[1082]467  else if ($page['section'] == 'recent_cats')
468  {
469    $page = array_merge(
470      $page,
471      array(
[5021]472        'title' => l10n('Recent categories'),
[1082]473        )
474      );
475  }
[1036]476// +-----------------------------------------------------------------------+
477// |                        most visited section                           |
478// +-----------------------------------------------------------------------+
[1082]479  else if ($page['section'] == 'most_visited')
480  {
481    $page['super_order_by'] = true;
482    $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
483    $query = '
[4387]484SELECT DISTINCT(id), hit, file
[1036]485  FROM '.IMAGES_TABLE.'
486    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
487  WHERE hit > 0
[1677]488    '.$forbidden.'
[1082]489    '.$conf['order_by'].'
[4334]490  LIMIT '.$conf['top_number'].'
[1036]491;';
[1086]492
[1082]493    $page = array_merge(
494      $page,
495      array(
[2117]496        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
[5021]497                  .$conf['top_number'].' '.l10n('Most visited').'</a>',
[1082]498        'items' => array_from_query($query, 'id'),
499        )
500      );
501  }
[1036]502// +-----------------------------------------------------------------------+
503// |                          best rated section                           |
504// +-----------------------------------------------------------------------+
[1082]505  else if ($page['section'] == 'best_rated')
506  {
507    $page['super_order_by'] = true;
508    $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC';
[1086]509
[1082]510    $query ='
[4387]511SELECT DISTINCT(id), average_rate
[1036]512  FROM '.IMAGES_TABLE.'
513    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
514  WHERE average_rate IS NOT NULL
[1677]515    '.$forbidden.'
[1082]516    '.$conf['order_by'].'
[4334]517  LIMIT '.$conf['top_number'].'
[1036]518;';
[1082]519    $page = array_merge(
520      $page,
521      array(
[2117]522        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
[5021]523                  .$conf['top_number'].' '.l10n('Best rated').'</a>',
[1082]524        'items' => array_from_query($query, 'id'),
525        )
526      );
527  }
[1036]528// +-----------------------------------------------------------------------+
529// |                             list section                              |
530// +-----------------------------------------------------------------------+
[1082]531  else if ($page['section'] == 'list')
532  {
533    $query ='
[4388]534SELECT DISTINCT(id)'.get_extra_fields($conf['order_by']).'
[1036]535  FROM '.IMAGES_TABLE.'
536    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
[1082]537  WHERE image_id IN ('.implode(',', $page['list']).')
[1677]538    '.$forbidden.'
[1036]539  '.$conf['order_by'].'
540;';
[1086]541
[1082]542    $page = array_merge(
543      $page,
544      array(
[2117]545        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
[5021]546                    .l10n('Random pictures').'</a>',
[1082]547        'items' => array_from_query($query, 'id'),
548        )
549      );
[1036]550  }
551}
[1082]552
[1036]553// +-----------------------------------------------------------------------+
[1082]554// |                             chronology                                |
[1036]555// +-----------------------------------------------------------------------+
[1047]556
[1090]557if (isset($page['chronology_field']))
[1047]558{
559  include_once( PHPWG_ROOT_PATH.'include/functions_calendar.inc.php' );
560  initialize_calendar();
561}
562
[1703]563// add meta robots noindex, nofollow to avoid unnecesary robot crawls
564$page['meta_robots']=array();
[2135]565if ( isset($page['chronology_field'])
566      or ( isset($page['flat']) and isset($page['category']) )
[1703]567      or 'list'==$page['section'] or 'recent_pics'==$page['section'] )
568{
569  $page['meta_robots']=array('noindex'=>1, 'nofollow'=>1);
570}
571elseif ('tags' == $page['section'])
572{
573  if ( count($page['tag_ids'])>1 )
574  {
575    $page['meta_robots']=array('noindex'=>1, 'nofollow'=>1);
576  }
577}
578elseif ('recent_cats'==$page['section'])
579{
[2138]580  $page['meta_robots']['noindex']=1;
581}
582elseif ('search'==$page['section'])
583{
[1703]584  $page['meta_robots']['nofollow']=1;
585}
586if ( $filter['enabled'] )
587{
588  $page['meta_robots']['noindex']=1;
589}
590
[1866]591// see if we need a redirect because of a permalink
592if ( 'categories'==$page['section'] and isset($page['category']) )
593{
594  $need_redirect=false;
595  if ( empty($page['category']['permalink']) )
596  {
597    if ( $conf['category_url_style'] == 'id-name' and
598        @$page['hit_by']['cat_url_name'] !== str2url($page['category']['name']) )
599    {
600      $need_redirect=true;
601    }
602  }
603  else
604  {
605    if ( $page['category']['permalink'] !== @$page['hit_by']['cat_permalink'] )
606    {
607      $need_redirect=true;
608    }
609  }
610
611  if ($need_redirect)
612  {
613    $redirect_url = ( script_basename()=='picture'
614        ? duplicate_picture_url()
615          : duplicate_index_url()
616      );
617    if (!headers_sent())
618    { // this is a permanent redirection
[1950]619      set_status_header(301);
[1866]620      redirect_http( $redirect_url );
621    }
622    redirect( $redirect_url );
623  }
624  unset( $need_redirect, $page['hit_by'] );
625}
626
[1604]627trigger_action('loc_end_section_init');
[5441]628?>
Note: See TracBrowser for help on using the repository browser.