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

Last change on this file since 2297 was 2297, checked in by plg, 17 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

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