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

Last change on this file since 1985 was 1983, checked in by rvelices, 17 years ago

category image order not correct

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 16.2 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 1983 2007-04-27 04:20:56Z rvelices $
8// | last update   : $Date: 2007-04-27 04:20:56 +0000 (Fri, 27 Apr 2007) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 1983 $
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['chronology_field']) )
166{ //access a picture only by id, file or id-file without given section
167  $page['flat']=true;
168}
169
170// $page['nb_image_page'] is the number of picture to display on this page
171// By default, it is the same as the $user['nb_image_page']
172$page['nb_image_page'] = $user['nb_image_page'];
173
174if (pwg_get_session_var('image_order',0) > 0)
175{
176  $orders = get_category_preferred_image_orders();
177
178  $conf['order_by'] = str_replace(
179    'ORDER BY ',
180    'ORDER BY '.$orders[ pwg_get_session_var('image_order',0) ][1].',',
181    $conf['order_by']
182    );
183  $page['super_order_by'] = true;
184}
185
186$forbidden = get_sql_condition_FandF(
187      array
188        (
189          'forbidden_categories' => 'category_id',
190          'visible_categories' => 'category_id',
191          'visible_images' => 'id'
192        ),
193      'AND'
194  );
195
196// +-----------------------------------------------------------------------+
197// |                              category                                 |
198// +-----------------------------------------------------------------------+
199if ('categories' == $page['section'])
200{
201  if (isset($page['category']))
202  {
203    $page = array_merge(
204      $page,
205      array(
206        'comment'           => $page['category']['comment'],
207        'title'             =>
208          get_cat_display_name($page['category']['upper_names'], '', false),
209        )
210      );
211  }
212  else
213  {
214    $page['title'] = $lang['no_category'];
215  }
216
217  if
218    (
219      (!isset($page['chronology_field'])) and
220      (
221        (isset($page['category'])) or
222        (isset($page['flat']))
223      )
224    )
225  {
226    if ( !empty($page['category']['image_order']) and !isset($page['super_order_by']) )
227    {
228      $conf[ 'order_by' ] = ' ORDER BY '.$page['category']['image_order'];
229    }
230
231    if (isset($page['flat']))
232    {// flat categories mode
233      if ( isset($page['category']) )
234      {
235        $subcat_ids = get_subcat_ids( array($page['category']['id']) );
236        $where_sql = 'category_id IN ('.implode(',',$subcat_ids).')';
237      }
238      else
239      {
240        $where_sql = '1=1';
241      }
242    }
243    else
244    {// Normal mode
245      $where_sql = 'category_id = '.$page['category']['id'];
246    }
247
248    // Main query
249    $query = '
250SELECT DISTINCT(image_id)
251  FROM '.IMAGE_CATEGORY_TABLE.'
252    INNER JOIN '.IMAGES_TABLE.' ON id = image_id
253  WHERE
254    '.$where_sql.'
255'.$forbidden.'
256  '.$conf['order_by'].'
257;';
258
259    $page['items'] = array_from_query($query, 'image_id');
260  } //otherwise the calendar will requery all subitems
261}
262// special sections
263else
264{
265// +-----------------------------------------------------------------------+
266// |                            tags section                               |
267// +-----------------------------------------------------------------------+
268  if ($page['section'] == 'tags')
269  {
270    $page['tag_ids'] = array();
271    foreach ($page['tags'] as $tag)
272    {
273      array_push($page['tag_ids'], $tag['id']);
274    }
275
276    $items = get_image_ids_for_tags($page['tag_ids']);
277
278    // permissions depends on category, so to only keep images that are
279    // reachable to the connected user, we need to check category
280    // associations
281    if (!empty($items) )
282    {
283      $query = '
284SELECT image_id
285  FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.IMAGES_TABLE.' ON image_id=id
286  WHERE image_id IN ('.implode(',', $items).')
287    '.$forbidden.
288    $conf['order_by'].'
289;';
290      $items = array_unique(
291        array_from_query($query, 'image_id')
292        );
293    }
294
295    $title = get_tags_content_title();
296
297    $page = array_merge(
298      $page,
299      array(
300        'title' => $title,
301        'items' => array_values($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    }
329
330    $page = array_merge(
331      $page,
332      array(
333        'title' => $lang['search_result'],
334        )
335      );
336  }
337// +-----------------------------------------------------------------------+
338// |                           favorite section                            |
339// +-----------------------------------------------------------------------+
340  else if ($page['section'] == 'favorites')
341  {
342    check_user_favorites();
343
344    $query = '
345SELECT image_id
346  FROM '.FAVORITES_TABLE.'
347    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
348  WHERE user_id = '.$user['id'].'
349'.get_sql_condition_FandF
350  (
351    array
352      (
353        'visible_images' => 'image_id'
354      ),
355    'AND'
356  ).'
357  '.$conf['order_by'].'
358;';
359
360    $page = array_merge(
361      $page,
362      array(
363        'title' => $lang['favorites'],
364        'items' => array_from_query($query, 'image_id'),
365        )
366      );
367  }
368// +-----------------------------------------------------------------------+
369// |                       recent pictures section                         |
370// +-----------------------------------------------------------------------+
371  else if ($page['section'] == 'recent_pics')
372  {
373    $query = '
374SELECT DISTINCT(id)
375  FROM '.IMAGES_TABLE.'
376    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
377  WHERE
378    date_available >= SUBDATE(
379      CURRENT_DATE,INTERVAL '.$user['recent_period'].' DAY)
380    '.$forbidden.'
381  '.$conf['order_by'].'
382;';
383
384    $page = array_merge(
385      $page,
386      array(
387        'title' => '<a href="'.duplicate_index_url().'">'
388                  .$lang['recent_pics_cat'].'</a>',
389        'items' => array_from_query($query, 'id'),
390        )
391      );
392  }
393// +-----------------------------------------------------------------------+
394// |                 recently updated categories section                   |
395// +-----------------------------------------------------------------------+
396  else if ($page['section'] == 'recent_cats')
397  {
398    $page = array_merge(
399      $page,
400      array(
401        'title' => $lang['recent_cats_cat'],
402        )
403      );
404  }
405// +-----------------------------------------------------------------------+
406// |                        most visited section                           |
407// +-----------------------------------------------------------------------+
408  else if ($page['section'] == 'most_visited')
409  {
410    $page['super_order_by'] = true;
411    $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
412    $query = '
413SELECT DISTINCT(id)
414  FROM '.IMAGES_TABLE.'
415    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
416  WHERE hit > 0
417    '.$forbidden.'
418    '.$conf['order_by'].'
419  LIMIT 0, '.$conf['top_number'].'
420;';
421
422    $page = array_merge(
423      $page,
424      array(
425        'title' => '<a href="'.duplicate_index_url().'">'
426                  .$conf['top_number'].' '.$lang['most_visited_cat'].'</a>',
427        'items' => array_from_query($query, 'id'),
428        )
429      );
430  }
431// +-----------------------------------------------------------------------+
432// |                          best rated section                           |
433// +-----------------------------------------------------------------------+
434  else if ($page['section'] == 'best_rated')
435  {
436    $page['super_order_by'] = true;
437    $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC';
438
439    $query ='
440SELECT DISTINCT(id)
441  FROM '.IMAGES_TABLE.'
442    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
443  WHERE average_rate IS NOT NULL
444    '.$forbidden.'
445    '.$conf['order_by'].'
446  LIMIT 0, '.$conf['top_number'].'
447;';
448    $page = array_merge(
449      $page,
450      array(
451        'title' => '<a href="'.duplicate_index_url().'">'
452                  .$conf['top_number'].' '.$lang['best_rated_cat'].'</a>',
453        'items' => array_from_query($query, 'id'),
454        )
455      );
456  }
457// +-----------------------------------------------------------------------+
458// |                             list section                              |
459// +-----------------------------------------------------------------------+
460  else if ($page['section'] == 'list')
461  {
462    $query ='
463SELECT DISTINCT(id)
464  FROM '.IMAGES_TABLE.'
465    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
466  WHERE image_id IN ('.implode(',', $page['list']).')
467    '.$forbidden.'
468  '.$conf['order_by'].'
469;';
470
471    $page = array_merge(
472      $page,
473      array(
474        'title' => '<a href="'.duplicate_index_url().'">'
475                    .$lang['random_cat'].'</a>',
476        'items' => array_from_query($query, 'id'),
477        )
478      );
479  }
480}
481
482// +-----------------------------------------------------------------------+
483// |                             chronology                                |
484// +-----------------------------------------------------------------------+
485
486if (isset($page['chronology_field']))
487{
488  include_once( PHPWG_ROOT_PATH.'include/functions_calendar.inc.php' );
489  initialize_calendar();
490}
491
492if (script_basename() == 'picture'
493    and !isset($page['image_id']) )
494{
495  if ( !empty($page['items']) )
496  {
497    $query = '
498SELECT id,file
499  FROM '.IMAGES_TABLE .'
500  WHERE id IN ('.implode(',',$page['items']).')
501  AND file LIKE "' . $page['image_file'] . '.%" ESCAPE "|"'
502;
503    $result = pwg_query($query);
504    if (mysql_num_rows($result)>0)
505    {
506      list($page['image_id'], $page['image_file']) = mysql_fetch_row($result);
507    }
508  }
509  if ( !isset($page['image_id']) )
510  {
511    $page['image_id'] = -1; // will fail in picture.php
512  }
513}
514
515// add meta robots noindex, nofollow to avoid unnecesary robot crawls
516$page['meta_robots']=array();
517if ( isset($page['chronology_field']) or isset($page['flat'])
518      or 'list'==$page['section'] or 'recent_pics'==$page['section'] )
519{
520  $page['meta_robots']=array('noindex'=>1, 'nofollow'=>1);
521}
522elseif ('tags' == $page['section'])
523{
524  if ( count($page['tag_ids'])>1 )
525  {
526    $page['meta_robots']=array('noindex'=>1, 'nofollow'=>1);
527  }
528}
529elseif ('recent_cats'==$page['section'])
530{
531  $page['meta_robots']['nofollow']=1;
532}
533if ( $filter['enabled'] )
534{
535  $page['meta_robots']['noindex']=1;
536}
537
538// see if we need a redirect because of a permalink
539if ( 'categories'==$page['section'] and isset($page['category']) )
540{
541  $need_redirect=false;
542  if ( empty($page['category']['permalink']) )
543  {
544    if ( $conf['category_url_style'] == 'id-name' and
545        @$page['hit_by']['cat_url_name'] !== str2url($page['category']['name']) )
546    {
547      $need_redirect=true;
548    }
549  }
550  else
551  {
552    if ( $page['category']['permalink'] !== @$page['hit_by']['cat_permalink'] )
553    {
554      $need_redirect=true;
555    }
556  }
557
558  if ($need_redirect)
559  {
560    $redirect_url = ( script_basename()=='picture'
561        ? duplicate_picture_url()
562          : duplicate_index_url()
563      );
564    if (!headers_sent())
565    { // this is a permanent redirection
566      set_status_header(301);
567      redirect_http( $redirect_url );
568    }
569    redirect( $redirect_url );
570  }
571  unset( $need_redirect, $page['hit_by'] );
572}
573
574trigger_action('loc_end_section_init');
575?>
Note: See TracBrowser for help on using the repository browser.