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

Last change on this file since 28320 was 28320, checked in by rvelices, 10 years ago

removed url compatibility with phpwebgallery 1.6 and below

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