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

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

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

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