source: tags/release-1_7_1/include/section_init.inc.php @ 4202

Last change on this file since 4202 was 2174, checked in by patdenice, 17 years ago

Add second parameter for render_category_description to differentiate both triggers in category_cats.inc.php and section_init.inc.php.
Add render_page_banner trigger.

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