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

Last change on this file since 18392 was 18392, checked in by mistic100, 12 years ago

feature 2614: pagination on albums, save one query on picture page

  • Property svn:eol-style set to LF
File size: 19.4 KB
RevLine 
[1036]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2297]4// +-----------------------------------------------------------------------+
[12922]5// | Copyright(C) 2008-2012 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
[18165]44// exemple of dynamic nb_categories_page (%2 for nice display)
45// $conf['nb_categories_page'] = 2*round($user['nb_image_page']/4);
[1820]46
[18165]47$page['items'] = $page['categories'] = array();
48$page['start'] = $page['starta'] = 0;
49
[1306]50// some ISPs set PATH_INFO to empty string or to SCRIPT_FILENAME while in the
51// default apache implementation it is not set
52if ( $conf['question_mark_in_urls']==false and
53     isset($_SERVER["PATH_INFO"]) and !empty($_SERVER["PATH_INFO"]) )
[1036]54{
[1090]55  $rewritten = $_SERVER["PATH_INFO"];
56  $rewritten = str_replace('//', '/', $rewritten);
57  $path_count = count( explode('/', $rewritten) );
58  $page['root_path'] = PHPWG_ROOT_PATH.str_repeat('../', $path_count-1);
59}
60else
61{
62  $rewritten = '';
63  foreach (array_keys($_GET) as $keynum => $key)
[1036]64  {
[1090]65    $rewritten = $key;
66    break;
67  }
[11893]68
[6906]69  // the $_GET keys are not protected in include/common.inc.php, only the values
70  $rewritten = pwg_db_real_escape_string($rewritten);
[1090]71  $page['root_path'] = PHPWG_ROOT_PATH;
72}
[1131]73
[13240]74if ( strncmp($page['root_path'], './', 2) == 0 )
75{
[13258]76  $page['root_path'] = substr($page['root_path'], 2);
[13240]77}
78
[1090]79// deleting first "/" if displayed
[2773]80$tokens = explode('/', ltrim($rewritten, '/') );
[1090]81// $tokens = array(
82//   0 => category,
83//   1 => 12-foo,
84//   2 => start-24
85//   );
[1082]86
[1090]87$next_token = 0;
[1690]88if (script_basename() == 'picture') // basename without file extention
[1109]89{ // the first token must be the identifier for the picture
90  if ( isset($_GET['image_id'])
91       and isset($_GET['cat']) and is_numeric($_GET['cat']) )
92  {// url compatibility with versions below 1.6
93    $url = make_picture_url( array(
94        'section' => 'categories',
[1861]95        'category' => get_cat_info($_GET['cat']),
[1109]96        'image_id' => $_GET['image_id']
97      ) );
98    redirect($url);
99  }
100  $token = $tokens[$next_token];
101  $next_token++;
[1092]102  if ( is_numeric($token) )
[1090]103  {
[1092]104    $page['image_id'] = $token;
[2430]105    if ($page['image_id']==0)
106    {
107      bad_request('invalid picture identifier');
108    }
[1090]109  }
[1092]110  else
111  {
[1109]112    preg_match('/^(\d+-)?(.*)?$/', $token, $matches);
[1094]113    if (isset($matches[1]) and is_numeric($matches[1]=rtrim($matches[1],'-')) )
[1092]114    {
115      $page['image_id'] = $matches[1];
[1109]116      if ( !empty($matches[2]) )
[1092]117      {
[1109]118        $page['image_file'] = $matches[2];
[1092]119      }
120    }
121    else
122    {
[3167]123      $page['image_id'] = 0; // more work in picture.php
[1109]124      if ( !empty($matches[2]) )
[1092]125      {
[1109]126        $page['image_file'] = $matches[2];
[1092]127      }
128      else
129      {
[1852]130        bad_request('picture identifier is missing');
[1092]131      }
132    }
133  }
[1090]134}
[1086]135
[1980]136$page = array_merge( $page, parse_section_url( $tokens, $next_token) );
[4385]137
[1980]138if ( !isset($page['section']) )
[1090]139{
140  $page['section'] = 'categories';
[1086]141
[1792]142  switch (script_basename())
[1788]143  {
[1792]144    case 'picture':
145      break;
146    case 'index':
147    {
148      // No section defined, go to selected url
149      if (!empty($conf['random_index_redirect']) and empty($tokens[$next_token]) )
[1788]150      {
[1792]151        $random_index_redirect = array();
152        foreach ($conf['random_index_redirect'] as $random_url => $random_url_condition)
153        {
154          if (empty($random_url_condition) or eval($random_url_condition))
155          {
156            $random_index_redirect[] = $random_url;
157          }
158        }
159        if (!empty($random_index_redirect))
160        {
161          redirect($random_index_redirect[mt_rand(0, count($random_index_redirect)-1)]);
162        }
[1788]163      }
[13458]164      $page['is_homepage'] = true;
[1792]165      break;
[1788]166    }
[1880]167    default:
168      trigger_error('script_basename "'.script_basename().'" unknown',
169        E_USER_WARNING);
[1788]170  }
171}
172
[1980]173$page = array_merge( $page, parse_well_known_params_url( $tokens, $next_token) );
174if ( script_basename()=='picture' and 'categories'==$page['section'] and
[1996]175      !isset($page['category']) and !isset($page['chronology_field']) )
[1980]176{ //access a picture only by id, file or id-file without given section
177  $page['flat']=true;
[1036]178}
179
[1047]180// $page['nb_image_page'] is the number of picture to display on this page
181// By default, it is the same as the $user['nb_image_page']
182$page['nb_image_page'] = $user['nb_image_page'];
[1036]183
[2517]184// if flat mode is active, we must consider the image set as a standard set
185// and not as a category set because we can't use the #image_category.rank :
186// displayed images are not directly linked to the displayed category
187if ('categories' == $page['section'] and !isset($page['flat']))
188{
189  $conf['order_by'] = $conf['order_by_inside_category'];
190}
191
[1623]192if (pwg_get_session_var('image_order',0) > 0)
[1051]193{
[2517]194  $image_order_id = pwg_get_session_var('image_order');
[2773]195
[1051]196  $orders = get_category_preferred_image_orders();
197
[2517]198  // the current session stored image_order might be not compatible with
199  // current image set, for example if the current image_order is the rank
200  // and that we are displaying images related to a tag.
201  //
202  // In case of incompatibility, the session stored image_order is removed.
203  if ($orders[$image_order_id][2])
204  {
205    $conf['order_by'] = str_replace(
206      'ORDER BY ',
207      'ORDER BY '.$orders[$image_order_id][1].',',
208      $conf['order_by']
[1051]209    );
[2517]210    $page['super_order_by'] = true;
211
212  }
213  else
214  {
215    pwg_unset_session_var('image_order');
216    $page['super_order_by'] = false;
217  }
[1051]218}
219
[1711]220$forbidden = get_sql_condition_FandF(
221      array
222        (
223          'forbidden_categories' => 'category_id',
224          'visible_categories' => 'category_id',
[1820]225          'visible_images' => 'id'
[1711]226        ),
227      'AND'
228  );
229
[1036]230// +-----------------------------------------------------------------------+
231// |                              category                                 |
232// +-----------------------------------------------------------------------+
[1082]233if ('categories' == $page['section'])
234{
235  if (isset($page['category']))
[1036]236  {
237    $page = array_merge(
238      $page,
239      array(
[2117]240        'comment'           =>
241            trigger_event(
242              'render_category_description',
[2175]243              $page['category']['comment'],
244              'main_page_category_description'
[2117]245            ),
[6411]246        'title'             => get_cat_display_name($page['category']['upper_names'], '', false),
[1051]247        )
248      );
[1677]249  }
[6411]250  else
251    $page['title'] = ''; // will be set later
[1086]252
[18165]253   
254  // GET IMAGES LIST
[1703]255  if
[1677]256    (
[18165]257      $page['starta'] == 0 and
[1677]258      (!isset($page['chronology_field'])) and
259      (
[1703]260        (isset($page['category'])) or
[1800]261        (isset($page['flat']))
[1677]262      )
263    )
264  {
[1983]265    if ( !empty($page['category']['image_order']) and !isset($page['super_order_by']) )
[1051]266    {
[1983]267      $conf[ 'order_by' ] = ' ORDER BY '.$page['category']['image_order'];
[1677]268    }
269
[1800]270    if (isset($page['flat']))
[1820]271    {// flat categories mode
272      if ( isset($page['category']) )
[2327]273      { // get all allowed sub-categories
274        $query = '
[2424]275SELECT id
[2327]276  FROM '.CATEGORIES_TABLE.'
[2424]277  WHERE
[6664]278    uppercats LIKE \''.$page['category']['uppercats'].',%\' '
[2327]279    .get_sql_condition_FandF(
280      array
281        (
282          'forbidden_categories' => 'id',
283          'visible_categories' => 'id',
284        ),
285      "\n  AND"
286          );
287        $subcat_ids = array_from_query($query, 'id');
288        $subcat_ids[] = $page['category']['id'];
[1820]289        $where_sql = 'category_id IN ('.implode(',',$subcat_ids).')';
[2327]290        // remove categories from forbidden because just checked above
291        $forbidden = get_sql_condition_FandF(
292              array( 'visible_images' => 'id' ),
293              'AND'
294          );
[1500]295      }
[1820]296      else
297      {
[13872]298        unset($page['is_homepage']);
[1820]299        $where_sql = '1=1';
300      }
[1677]301    }
302    else
[1820]303    {// Normal mode
[1861]304      $where_sql = 'category_id = '.$page['category']['id'];
[1677]305    }
[1500]306
[1820]307    // Main query
308    $query = '
[6668]309SELECT DISTINCT(image_id)
[1051]310  FROM '.IMAGE_CATEGORY_TABLE.'
311    INNER JOIN '.IMAGES_TABLE.' ON id = image_id
[1677]312  WHERE
313    '.$where_sql.'
[1711]314'.$forbidden.'
[1051]315  '.$conf['order_by'].'
316;';
[1677]317
[1820]318    $page['items'] = array_from_query($query, 'image_id');
[1677]319  } //otherwise the calendar will requery all subitems
[18165]320 
321  // GET CATEGORIES LIST
[18392]322  if ( script_basename()=='index'
323    and 0==$page['start']
[18165]324    and !isset($page['flat'])
325    and !isset($page['chronology_field'])
326    and ('recent_cats'==$page['section'] or 'categories'==$page['section'])
327    and (!isset($page['category']['count_categories']) or $page['category']['count_categories']>0 )
328  )
329  {
330    $query = '
331SELECT c.id
332  FROM '.CATEGORIES_TABLE.' c
333    INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ucc
334    ON id = cat_id
335    AND user_id = '.$user['id'];
336
337    if ('recent_cats' == $page['section'])
338    {
339      $query.= '
340      WHERE date_last >= '.pwg_db_get_recent_period_expression($user['recent_period']);
341    }
342    else
343    {
344      $query.= '
345      WHERE id_uppercat '.(!isset($page['category']) ? 'is NULL' : '= '.$page['category']['id']);
346    }
347
348    $query.= '
349        '.get_sql_condition_FandF(
350          array('visible_categories' => 'id'),
351          'AND'
352          );
353
354    $query.= '
355    ;';
356
357    $page['categories'] = array_from_query($query, 'id');
358  }
[1082]359}
360// special sections
361else
362{
[1036]363// +-----------------------------------------------------------------------+
[1119]364// |                            tags section                               |
365// +-----------------------------------------------------------------------+
366  if ($page['section'] == 'tags')
367  {
368    $page['tag_ids'] = array();
369    foreach ($page['tags'] as $tag)
370    {
371      array_push($page['tag_ids'], $tag['id']);
372    }
373
374    $items = get_image_ids_for_tags($page['tag_ids']);
375
376    $page = array_merge(
377      $page,
378      array(
[2773]379        'title' => get_tags_content_title(),
[2296]380        'items' => $items,
[1119]381        )
382      );
383  }
384// +-----------------------------------------------------------------------+
[1036]385// |                           search section                              |
386// +-----------------------------------------------------------------------+
[1082]387  if ($page['section'] == 'search')
388  {
[1113]389    include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' );
[1119]390
[2451]391    $search_result = get_search_results($page['search'], @$page['super_order_by'] );
392    if ( isset($search_result['qs']) )
393    {//save the details of the query search
394      $page['qsearch_details'] = $search_result['qs'];
[1120]395    }
[1036]396
[1082]397    $page = array_merge(
398      $page,
399      array(
[2451]400        'items' => $search_result['items'],
[2117]401        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
[5021]402                  .l10n('Search results').'</a>',
[1082]403        )
404      );
405  }
[1036]406// +-----------------------------------------------------------------------+
407// |                           favorite section                            |
408// +-----------------------------------------------------------------------+
[1082]409  else if ($page['section'] == 'favorites')
410  {
411    check_user_favorites();
[1036]412
[3037]413    $page = array_merge(
414      $page,
415      array(
[6411]416        'title' => l10n('Favorites')
417      )
[3037]418    );
419
[3108]420    if (!empty($_GET['action']) && ($_GET['action'] == 'remove_all_from_favorites'))
[3037]421    {
422      $query = '
423DELETE FROM '.FAVORITES_TABLE.'
424  WHERE user_id = '.$user['id'].'
425;';
426      pwg_query($query);
[3108]427      redirect(make_index_url( array('section'=>'favorites') ));
[3037]428    }
[3108]429    else
[3037]430    {
431      $query = '
[1036]432SELECT image_id
433  FROM '.FAVORITES_TABLE.'
434    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
435  WHERE user_id = '.$user['id'].'
[1677]436'.get_sql_condition_FandF
437  (
438    array
439      (
[2451]440        'visible_images' => 'id'
[1677]441      ),
442    'AND'
443  ).'
[1036]444  '.$conf['order_by'].'
445;';
[3037]446      $page = array_merge(
447        $page,
448        array(
449          'items' => array_from_query($query, 'image_id'),
450         )
451      );
[1036]452
[3108]453      if (count($page['items'])>0)
[3037]454      {
455        $template->assign(
456          'favorite',
457          array(
458            'U_FAVORITE'    => add_url_params(
[3108]459              make_index_url( array('section'=>'favorites') ),
[3037]460              array('action'=>'remove_all_from_favorites')
461               ),
462             )
463           );
464      }
465    }
[1082]466  }
[1036]467// +-----------------------------------------------------------------------+
468// |                       recent pictures section                         |
469// +-----------------------------------------------------------------------+
[1082]470  else if ($page['section'] == 'recent_pics')
471  {
[2424]472    if ( !isset($page['super_order_by']) )
473    {
474      $conf['order_by'] = str_replace(
475        'ORDER BY ',
476        'ORDER BY date_available DESC,',
477        $conf['order_by']
478        );
479    }
480
[1082]481    $query = '
[6668]482SELECT DISTINCT(id)
[1036]483  FROM '.IMAGES_TABLE.'
484    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
[1876]485  WHERE
[4367]486    date_available >= '.pwg_db_get_recent_period_expression($user['recent_period']).'
[1677]487    '.$forbidden.'
[1036]488  '.$conf['order_by'].'
489;';
490
[1082]491    $page = array_merge(
492      $page,
493      array(
[2117]494        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
[8665]495                  .l10n('Recent photos').'</a>',
[1082]496        'items' => array_from_query($query, 'id'),
497        )
498      );
499  }
[1036]500// +-----------------------------------------------------------------------+
501// |                 recently updated categories section                   |
502// +-----------------------------------------------------------------------+
[1082]503  else if ($page['section'] == 'recent_cats')
504  {
505    $page = array_merge(
506      $page,
507      array(
[6951]508        'title' => l10n('Recent albums'),
[1082]509        )
510      );
511  }
[1036]512// +-----------------------------------------------------------------------+
513// |                        most visited section                           |
514// +-----------------------------------------------------------------------+
[1082]515  else if ($page['section'] == 'most_visited')
516  {
517    $page['super_order_by'] = true;
[11893]518    $conf['order_by'] = ' ORDER BY hit DESC, id DESC';
[1082]519    $query = '
[11893]520SELECT DISTINCT(id)
[1036]521  FROM '.IMAGES_TABLE.'
522    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
523  WHERE hit > 0
[1677]524    '.$forbidden.'
[1082]525    '.$conf['order_by'].'
[4334]526  LIMIT '.$conf['top_number'].'
[1036]527;';
[1086]528
[1082]529    $page = array_merge(
530      $page,
531      array(
[2117]532        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
[5021]533                  .$conf['top_number'].' '.l10n('Most visited').'</a>',
[1082]534        'items' => array_from_query($query, 'id'),
535        )
536      );
537  }
[1036]538// +-----------------------------------------------------------------------+
539// |                          best rated section                           |
540// +-----------------------------------------------------------------------+
[1082]541  else if ($page['section'] == 'best_rated')
542  {
543    $page['super_order_by'] = true;
[11893]544    $conf['order_by'] = ' ORDER BY rating_score DESC, id DESC';
[1086]545
[1082]546    $query ='
[11893]547SELECT DISTINCT(id)
[1036]548  FROM '.IMAGES_TABLE.'
549    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
[11893]550  WHERE rating_score IS NOT NULL
[1677]551    '.$forbidden.'
[1082]552    '.$conf['order_by'].'
[4334]553  LIMIT '.$conf['top_number'].'
[1036]554;';
[1082]555    $page = array_merge(
556      $page,
557      array(
[2117]558        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
[5021]559                  .$conf['top_number'].' '.l10n('Best rated').'</a>',
[1082]560        'items' => array_from_query($query, 'id'),
561        )
562      );
563  }
[1036]564// +-----------------------------------------------------------------------+
565// |                             list section                              |
566// +-----------------------------------------------------------------------+
[1082]567  else if ($page['section'] == 'list')
568  {
569    $query ='
[6668]570SELECT DISTINCT(id)
[1036]571  FROM '.IMAGES_TABLE.'
572    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
[1082]573  WHERE image_id IN ('.implode(',', $page['list']).')
[1677]574    '.$forbidden.'
[1036]575  '.$conf['order_by'].'
576;';
[1086]577
[1082]578    $page = array_merge(
579      $page,
580      array(
[2117]581        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
[8665]582                    .l10n('Random photos').'</a>',
[1082]583        'items' => array_from_query($query, 'id'),
584        )
585      );
[1036]586  }
587}
[1082]588
[1036]589// +-----------------------------------------------------------------------+
[1082]590// |                             chronology                                |
[1036]591// +-----------------------------------------------------------------------+
[1047]592
[1090]593if (isset($page['chronology_field']))
[1047]594{
[13872]595  unset($page['is_homepage']);
[1047]596  include_once( PHPWG_ROOT_PATH.'include/functions_calendar.inc.php' );
597  initialize_calendar();
598}
599
[6411]600// title update
601if (isset($page['title']))
602{
603  if (!empty($page['title']))
604        {
605          $page['title'] = $conf['level_separator'].$page['title'];
606        }
607  $page['title'] = '<a href="'.get_gallery_home_url().'">'.l10n('Home').'</a>'.$page['title'];
608}
609
[1703]610// add meta robots noindex, nofollow to avoid unnecesary robot crawls
611$page['meta_robots']=array();
[2135]612if ( isset($page['chronology_field'])
613      or ( isset($page['flat']) and isset($page['category']) )
[1703]614      or 'list'==$page['section'] or 'recent_pics'==$page['section'] )
615{
616  $page['meta_robots']=array('noindex'=>1, 'nofollow'=>1);
617}
618elseif ('tags' == $page['section'])
619{
620  if ( count($page['tag_ids'])>1 )
621  {
622    $page['meta_robots']=array('noindex'=>1, 'nofollow'=>1);
623  }
624}
625elseif ('recent_cats'==$page['section'])
626{
[2138]627  $page['meta_robots']['noindex']=1;
628}
629elseif ('search'==$page['section'])
630{
[1703]631  $page['meta_robots']['nofollow']=1;
632}
633if ( $filter['enabled'] )
634{
635  $page['meta_robots']['noindex']=1;
636}
637
[1866]638// see if we need a redirect because of a permalink
639if ( 'categories'==$page['section'] and isset($page['category']) )
640{
641  $need_redirect=false;
642  if ( empty($page['category']['permalink']) )
643  {
644    if ( $conf['category_url_style'] == 'id-name' and
645        @$page['hit_by']['cat_url_name'] !== str2url($page['category']['name']) )
646    {
647      $need_redirect=true;
648    }
649  }
650  else
651  {
652    if ( $page['category']['permalink'] !== @$page['hit_by']['cat_permalink'] )
653    {
654      $need_redirect=true;
655    }
656  }
657
658  if ($need_redirect)
659  {
660    $redirect_url = ( script_basename()=='picture'
661        ? duplicate_picture_url()
662          : duplicate_index_url()
663      );
664    if (!headers_sent())
665    { // this is a permanent redirection
[1950]666      set_status_header(301);
[1866]667      redirect_http( $redirect_url );
668    }
669    redirect( $redirect_url );
670  }
671  unset( $need_redirect, $page['hit_by'] );
672}
673
[1604]674trigger_action('loc_end_section_init');
[6615]675?>
Note: See TracBrowser for help on using the repository browser.