source: tags/release-2_0_0RC1/include/section_init.inc.php @ 13792

Last change on this file since 13792 was 2517, checked in by plg, 16 years ago

feature 169 added: ability to manually sort images inside a category. A
dedicated screen let the administrator sort the images (pur HTML, no
JavaScript yet). The "rank" sort order is available only for a category
without flat mode. New database column image_category.rank.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 17.6 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    if ($page['image_id']==0)
97    {
98      bad_request('invalid picture identifier');
99    }
100  }
101  else
102  {
103    preg_match('/^(\d+-)?(.*)?$/', $token, $matches);
104    if (isset($matches[1]) and is_numeric($matches[1]=rtrim($matches[1],'-')) )
105    {
106      $page['image_id'] = $matches[1];
107      if ( !empty($matches[2]) )
108      {
109        $page['image_file'] = $matches[2];
110      }
111    }
112    else
113    {
114      if ( !empty($matches[2]) )
115      {
116        $page['image_file'] = $matches[2];
117      }
118      else
119      {
120        bad_request('picture identifier is missing');
121      }
122    }
123  }
124}
125
126$page = array_merge( $page, parse_section_url( $tokens, $next_token) );
127if ( !isset($page['section']) )
128{
129  $page['section'] = 'categories';
130
131  switch (script_basename())
132  {
133    case 'picture':
134      break;
135    case 'index':
136    {
137      // No section defined, go to selected url
138      if (!empty($conf['random_index_redirect']) and empty($tokens[$next_token]) )
139      {
140        $random_index_redirect = array();
141        foreach ($conf['random_index_redirect'] as $random_url => $random_url_condition)
142        {
143          if (empty($random_url_condition) or eval($random_url_condition))
144          {
145            $random_index_redirect[] = $random_url;
146          }
147        }
148        if (!empty($random_index_redirect))
149        {
150          redirect($random_index_redirect[mt_rand(0, count($random_index_redirect)-1)]);
151        }
152      }
153      break;
154    }
155    default:
156      trigger_error('script_basename "'.script_basename().'" unknown',
157        E_USER_WARNING);
158  }
159}
160
161
162$page = array_merge( $page, parse_well_known_params_url( $tokens, $next_token) );
163
164
165if ( script_basename()=='picture' and 'categories'==$page['section'] and
166      !isset($page['category']) and !isset($page['chronology_field']) )
167{ //access a picture only by id, file or id-file without given section
168  $page['flat']=true;
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
175// if flat mode is active, we must consider the image set as a standard set
176// and not as a category set because we can't use the #image_category.rank :
177// displayed images are not directly linked to the displayed category
178if ('categories' == $page['section'] and !isset($page['flat']))
179{
180  $conf['order_by'] = $conf['order_by_inside_category'];
181}
182
183if (pwg_get_session_var('image_order',0) > 0)
184{
185  $image_order_id = pwg_get_session_var('image_order');
186 
187  $orders = get_category_preferred_image_orders();
188
189  // the current session stored image_order might be not compatible with
190  // current image set, for example if the current image_order is the rank
191  // and that we are displaying images related to a tag.
192  //
193  // In case of incompatibility, the session stored image_order is removed.
194  if ($orders[$image_order_id][2])
195  {
196    $conf['order_by'] = str_replace(
197      'ORDER BY ',
198      'ORDER BY '.$orders[$image_order_id][1].',',
199      $conf['order_by']
200    );
201    $page['super_order_by'] = true;
202
203  }
204  else
205  {
206    pwg_unset_session_var('image_order');
207    $page['super_order_by'] = false;
208  }
209}
210
211$forbidden = get_sql_condition_FandF(
212      array
213        (
214          'forbidden_categories' => 'category_id',
215          'visible_categories' => 'category_id',
216          'visible_images' => 'id'
217        ),
218      'AND'
219  );
220
221// +-----------------------------------------------------------------------+
222// |                              category                                 |
223// +-----------------------------------------------------------------------+
224if ('categories' == $page['section'])
225{
226  if (isset($page['category']))
227  {
228    $page = array_merge(
229      $page,
230      array(
231        'comment'           =>
232            trigger_event(
233              'render_category_description',
234              $page['category']['comment'],
235              'main_page_category_description'
236            ),
237        'title'             =>
238          get_cat_display_name($page['category']['upper_names'], '', false),
239        )
240      );
241  }
242  else
243  {
244    $page['title'] = l10n('no_category');
245  }
246
247  if
248    (
249      (!isset($page['chronology_field'])) and
250      (
251        (isset($page['category'])) or
252        (isset($page['flat']))
253      )
254    )
255  {
256    if ( !empty($page['category']['image_order']) and !isset($page['super_order_by']) )
257    {
258      $conf[ 'order_by' ] = ' ORDER BY '.$page['category']['image_order'];
259    }
260
261    if (isset($page['flat']))
262    {// flat categories mode
263      if ( isset($page['category']) )
264      { // get all allowed sub-categories
265        $query = '
266SELECT id
267  FROM '.CATEGORIES_TABLE.'
268  WHERE
269    uppercats LIKE "'.$page['category']['uppercats'].',%" '
270    .get_sql_condition_FandF(
271      array
272        (
273          'forbidden_categories' => 'id',
274          'visible_categories' => 'id',
275        ),
276      "\n  AND"
277          );
278        $subcat_ids = array_from_query($query, 'id');
279        $subcat_ids[] = $page['category']['id'];
280        $where_sql = 'category_id IN ('.implode(',',$subcat_ids).')';
281        // remove categories from forbidden because just checked above
282        $forbidden = get_sql_condition_FandF(
283              array( 'visible_images' => 'id' ),
284              'AND'
285          );
286      }
287      else
288      {
289        $where_sql = '1=1';
290      }
291    }
292    else
293    {// Normal mode
294      $where_sql = 'category_id = '.$page['category']['id'];
295    }
296
297    // Main query
298    $query = '
299SELECT DISTINCT(image_id)
300  FROM '.IMAGE_CATEGORY_TABLE.'
301    INNER JOIN '.IMAGES_TABLE.' ON id = image_id
302  WHERE
303    '.$where_sql.'
304'.$forbidden.'
305  '.$conf['order_by'].'
306;';
307
308    $page['items'] = array_from_query($query, 'image_id');
309  } //otherwise the calendar will requery all subitems
310}
311// special sections
312else
313{
314// +-----------------------------------------------------------------------+
315// |                            tags section                               |
316// +-----------------------------------------------------------------------+
317  if ($page['section'] == 'tags')
318  {
319    $page['tag_ids'] = array();
320    foreach ($page['tags'] as $tag)
321    {
322      array_push($page['tag_ids'], $tag['id']);
323    }
324
325    $items = get_image_ids_for_tags($page['tag_ids']);
326
327    // permissions depends on category, so to only keep images that are
328    // reachable to the connected user, we need to check category
329    // associations
330    if (!empty($items) )
331    {
332      $query = '
333SELECT DISTINCT image_id
334  FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.IMAGES_TABLE.' ON image_id=id
335  WHERE image_id IN ('.implode(',', $items).')
336    '.$forbidden.
337    $conf['order_by'].'
338;';
339      $items =  array_from_query($query, 'image_id');
340    }
341
342    $title = get_tags_content_title();
343
344    $page = array_merge(
345      $page,
346      array(
347        'title' => $title,
348        'items' => $items,
349        )
350      );
351  }
352// +-----------------------------------------------------------------------+
353// |                           search section                              |
354// +-----------------------------------------------------------------------+
355  if ($page['section'] == 'search')
356  {
357    include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' );
358
359    $search_result = get_search_results($page['search'], @$page['super_order_by'] );
360    if ( isset($search_result['qs']) )
361    {//save the details of the query search
362      $page['qsearch_details'] = $search_result['qs'];
363    }
364
365    $page = array_merge(
366      $page,
367      array(
368        'items' => $search_result['items'],
369        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
370                  .l10n('search_result').'</a>',
371        )
372      );
373  }
374// +-----------------------------------------------------------------------+
375// |                           favorite section                            |
376// +-----------------------------------------------------------------------+
377  else if ($page['section'] == 'favorites')
378  {
379    check_user_favorites();
380
381    $query = '
382SELECT image_id
383  FROM '.FAVORITES_TABLE.'
384    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
385  WHERE user_id = '.$user['id'].'
386'.get_sql_condition_FandF
387  (
388    array
389      (
390        'visible_images' => 'id'
391      ),
392    'AND'
393  ).'
394  '.$conf['order_by'].'
395;';
396
397    $page = array_merge(
398      $page,
399      array(
400        'title' => l10n('favorites'),
401        'items' => array_from_query($query, 'image_id'),
402        )
403      );
404  }
405// +-----------------------------------------------------------------------+
406// |                       recent pictures section                         |
407// +-----------------------------------------------------------------------+
408  else if ($page['section'] == 'recent_pics')
409  {
410    if ( !isset($page['super_order_by']) )
411    {
412      $conf['order_by'] = str_replace(
413        'ORDER BY ',
414        'ORDER BY date_available DESC,',
415        $conf['order_by']
416        );
417    }
418
419    $query = '
420SELECT DISTINCT(id)
421  FROM '.IMAGES_TABLE.'
422    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
423  WHERE
424    date_available >= SUBDATE(
425      CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY)
426    '.$forbidden.'
427  '.$conf['order_by'].'
428;';
429
430    $page = array_merge(
431      $page,
432      array(
433        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
434                  .l10n('recent_pics_cat').'</a>',
435        'items' => array_from_query($query, 'id'),
436        )
437      );
438  }
439// +-----------------------------------------------------------------------+
440// |                 recently updated categories section                   |
441// +-----------------------------------------------------------------------+
442  else if ($page['section'] == 'recent_cats')
443  {
444    $page = array_merge(
445      $page,
446      array(
447        'title' => l10n('recent_cats_cat'),
448        )
449      );
450  }
451// +-----------------------------------------------------------------------+
452// |                        most visited section                           |
453// +-----------------------------------------------------------------------+
454  else if ($page['section'] == 'most_visited')
455  {
456    $page['super_order_by'] = true;
457    $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
458    $query = '
459SELECT DISTINCT(id)
460  FROM '.IMAGES_TABLE.'
461    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
462  WHERE hit > 0
463    '.$forbidden.'
464    '.$conf['order_by'].'
465  LIMIT 0, '.$conf['top_number'].'
466;';
467
468    $page = array_merge(
469      $page,
470      array(
471        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
472                  .$conf['top_number'].' '.l10n('most_visited_cat').'</a>',
473        'items' => array_from_query($query, 'id'),
474        )
475      );
476  }
477// +-----------------------------------------------------------------------+
478// |                          best rated section                           |
479// +-----------------------------------------------------------------------+
480  else if ($page['section'] == 'best_rated')
481  {
482    $page['super_order_by'] = true;
483    $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC';
484
485    $query ='
486SELECT DISTINCT(id)
487  FROM '.IMAGES_TABLE.'
488    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
489  WHERE average_rate IS NOT NULL
490    '.$forbidden.'
491    '.$conf['order_by'].'
492  LIMIT 0, '.$conf['top_number'].'
493;';
494    $page = array_merge(
495      $page,
496      array(
497        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
498                  .$conf['top_number'].' '.l10n('best_rated_cat').'</a>',
499        'items' => array_from_query($query, 'id'),
500        )
501      );
502  }
503// +-----------------------------------------------------------------------+
504// |                             list section                              |
505// +-----------------------------------------------------------------------+
506  else if ($page['section'] == 'list')
507  {
508    $query ='
509SELECT DISTINCT(id)
510  FROM '.IMAGES_TABLE.'
511    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
512  WHERE image_id IN ('.implode(',', $page['list']).')
513    '.$forbidden.'
514  '.$conf['order_by'].'
515;';
516
517    $page = array_merge(
518      $page,
519      array(
520        'title' => '<a href="'.duplicate_index_url(array('start'=>0)).'">'
521                    .l10n('random_cat').'</a>',
522        'items' => array_from_query($query, 'id'),
523        )
524      );
525  }
526}
527
528// +-----------------------------------------------------------------------+
529// |                             chronology                                |
530// +-----------------------------------------------------------------------+
531
532if (isset($page['chronology_field']))
533{
534  include_once( PHPWG_ROOT_PATH.'include/functions_calendar.inc.php' );
535  initialize_calendar();
536}
537
538if (script_basename() == 'picture'
539    and !isset($page['image_id']) )
540{
541  $page['image_id'] = 0; // more work in picture.php
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.