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

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