source: tags/release-1_7_0RC1/include/section_init.inc.php @ 21250

Last change on this file since 21250 was 1820, checked in by rvelices, 17 years ago
  • feature 642: display both subcategory thumbnails and element thumbnails (if a

category has both) in the index page

flat category view

  • web service fixes for categories.getList
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.4 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// | branch        : BSF (Best So Far)
8// | file          : $Id: section_init.inc.php 1820 2007-02-15 00:10:41Z rvelices $
9// | last update   : $Date: 2007-02-15 00:10:41 +0000 (Thu, 15 Feb 2007) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1820 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28/**
29 * This included page checks section related parameter and provides
30 * following informations:
31 *
32 * - $page['title']
33 *
34 * - $page['items']: ordered list of items to display
35 *
36 */
37
38// "index.php?/category/12-foo/start-24&action=fill_caddie" or
39// "index.php/category/12-foo/start-24&action=fill_caddie"
40// must return :
41//
42// array(
43//   'section'  => 'categories',
44//   'category' => 12,
45//   'start'    => 24
46//   'action'   => 'fill_caddie'
47//   );
48
49$page['items'] = array();
50
51// some ISPs set PATH_INFO to empty string or to SCRIPT_FILENAME while in the
52// default apache implementation it is not set
53if ( $conf['question_mark_in_urls']==false and
54     isset($_SERVER["PATH_INFO"]) and !empty($_SERVER["PATH_INFO"]) )
55{
56  $rewritten = $_SERVER["PATH_INFO"];
57  $rewritten = str_replace('//', '/', $rewritten);
58  $path_count = count( explode('/', $rewritten) );
59  $page['root_path'] = PHPWG_ROOT_PATH.str_repeat('../', $path_count-1);
60}
61else
62{
63  $rewritten = '';
64  foreach (array_keys($_GET) as $keynum => $key)
65  {
66    $rewritten = $key;
67    break;
68  }
69  $page['root_path'] = PHPWG_ROOT_PATH;
70}
71
72// deleting first "/" if displayed
73$tokens = explode(
74  '/',
75  preg_replace('#^/#', '', $rewritten)
76  );
77// $tokens = array(
78//   0 => category,
79//   1 => 12-foo,
80//   2 => start-24
81//   );
82
83$next_token = 0;
84if (script_basename() == 'picture') // basename without file extention
85{ // the first token must be the identifier for the picture
86  if ( isset($_GET['image_id'])
87       and isset($_GET['cat']) and is_numeric($_GET['cat']) )
88  {// url compatibility with versions below 1.6
89    $url = make_picture_url( array(
90        'section' => 'categories',
91        'category' => $_GET['cat'],
92        'image_id' => $_GET['image_id']
93      ) );
94    redirect($url);
95  }
96  $token = $tokens[$next_token];
97  $next_token++;
98  if ( is_numeric($token) )
99  {
100    $page['image_id'] = $token;
101  }
102  else
103  {
104    preg_match('/^(\d+-)?(.*)?$/', $token, $matches);
105    if (isset($matches[1]) and is_numeric($matches[1]=rtrim($matches[1],'-')) )
106    {
107      $page['image_id'] = $matches[1];
108      if ( !empty($matches[2]) )
109      {
110        $page['image_file'] = $matches[2];
111      }
112
113    }
114    else
115    {
116      if ( !empty($matches[2]) )
117      {
118        $page['image_file'] = $matches[2];
119      }
120      else
121      {
122        die('Fatal: picture identifier is missing');
123      }
124    }
125  }
126}
127
128if (0 === strpos(@$tokens[$next_token], 'categor'))
129{
130  $page['section'] = 'categories';
131  $next_token++;
132
133  if (isset($tokens[$next_token])
134      and preg_match('/^(\d+)/', $tokens[$next_token], $matches))
135  {
136    $page['category'] = $matches[1];
137    $next_token++;
138  }
139}
140else if (0 === strpos(@$tokens[$next_token], 'tag'))
141{
142  $page['section'] = 'tags';
143  $page['tags'] = array();
144
145  $next_token++;
146  $i = $next_token;
147
148  $requested_tag_ids = array();
149  $requested_tag_url_names = array();
150
151  while (isset($tokens[$i]))
152  {
153    if ( preg_match('/^(created-|posted-|start-(\d)+)/', $tokens[$i]) )
154      break;
155
156    if ( preg_match('/^(\d+)(?:-(.*))?/', $tokens[$i], $matches) )
157    {
158      array_push($requested_tag_ids, $matches[1]);
159    }
160    else
161    {
162      array_push($requested_tag_url_names, "'".$tokens[$i]."'");
163    }
164    $i++;
165  }
166  $next_token = $i;
167
168  if ( empty($requested_tag_ids) && empty($requested_tag_url_names) )
169  {
170    die('Fatal: at least one tag required');
171  }
172  // tag infos
173  $query = '
174SELECT name, url_name, id
175  FROM '.TAGS_TABLE.'
176  WHERE ';
177  if ( !empty($requested_tag_ids) )
178  {
179    $query.= 'id IN ('.implode(',', $requested_tag_ids ).')';
180  }
181  if ( !empty($requested_tag_url_names) )
182  {
183    if ( !empty($requested_tag_ids) )
184    {
185      $query.= ' OR ';
186    }
187    $query.= 'url_name IN ('.implode(',', $requested_tag_url_names ).')';
188  }
189  $result = pwg_query($query);
190  $tag_infos = array();
191  while ($row = mysql_fetch_assoc($result))
192  {
193    $tag_infos[ $row['id'] ] = $row;
194    array_push($page['tags'], $row );//we loose given tag order; is it important?
195  }
196  if ( empty($page['tags']) )
197  {
198    page_not_found('Requested tag does not exist', get_root_url().'tags.php' );
199  }
200}
201else if (0 === strpos(@$tokens[$next_token], 'fav'))
202{
203  $page['section'] = 'favorites';
204  $next_token++;
205}
206else if ('most_visited' == @$tokens[$next_token])
207{
208  $page['section'] = 'most_visited';
209  $next_token++;
210}
211else if ('best_rated' == @$tokens[$next_token])
212{
213  $page['section'] = 'best_rated';
214  $next_token++;
215}
216else if ('recent_pics' == @$tokens[$next_token])
217{
218  $page['section'] = 'recent_pics';
219  $next_token++;
220}
221else if ('recent_cats' == @$tokens[$next_token])
222{
223  $page['section'] = 'recent_cats';
224  $next_token++;
225}
226else if ('search' == @$tokens[$next_token])
227{
228  $page['section'] = 'search';
229  $next_token++;
230
231  preg_match('/(\d+)/', $tokens[$next_token], $matches);
232  if (!isset($matches[1]))
233  {
234    die('Fatal: search identifier is missing');
235  }
236  $page['search'] = $matches[1];
237  $next_token++;
238}
239else if ('list' == @$tokens[$next_token])
240{
241  $page['section'] = 'list';
242  $next_token++;
243
244  $page['list'] = array();
245
246  // No pictures
247  if (empty($tokens[$next_token]))
248  {
249    // Add dummy element list
250    array_push($page['list'], -1);
251  }
252  // With pictures list
253  else
254  {
255    if (!preg_match('/^\d+(,\d+)*$/', $tokens[$next_token]))
256    {
257      die('wrong format on list GET parameter');
258    }
259    foreach (explode(',', $tokens[$next_token]) as $image_id)
260    {
261      array_push($page['list'], $image_id);
262    }
263  }
264  $next_token++;
265}
266else
267{
268  $page['section'] = 'categories';
269
270  switch (script_basename())
271  {
272    case 'picture':
273    {
274      //access a picture only by id, file or id-file without given section
275      $page['flat'] = true;
276      break;
277    }
278    case 'index':
279    {
280      // No section defined, go to selected url
281      if (!empty($conf['random_index_redirect']) and empty($tokens[$next_token]) )
282      {
283        $random_index_redirect = array();
284        foreach ($conf['random_index_redirect'] as $random_url => $random_url_condition)
285        {
286          if (empty($random_url_condition) or eval($random_url_condition))
287          {
288            $random_index_redirect[] = $random_url;
289          }
290        }
291        if (!empty($random_index_redirect))
292        {
293          redirect($random_index_redirect[mt_rand(0, count($random_index_redirect)-1)]);
294        }
295      }
296      break;
297    }
298  }
299}
300
301$i = $next_token;
302
303while (isset($tokens[$i]))
304{
305  if (preg_match('/^start-(\d+)/', $tokens[$i], $matches))
306  {
307    $page['start'] = $matches[1];
308  }
309
310  if ('categories' == $page['section'] and
311      'flat' == $tokens[$i])
312  {
313    // indicate a special list of images
314    $page['flat'] = true;
315  }
316
317  if (preg_match('/^(posted|created)/', $tokens[$i] ))
318  {
319    $chronology_tokens = explode('-', $tokens[$i] );
320
321    $page['chronology_field'] = $chronology_tokens[0];
322
323    array_shift($chronology_tokens);
324    $page['chronology_style'] = $chronology_tokens[0];
325
326    array_shift($chronology_tokens);
327    if ( count($chronology_tokens)>0 )
328    {
329      if ('list'==$chronology_tokens[0] or
330          'calendar'==$chronology_tokens[0])
331      {
332        $page['chronology_view'] = $chronology_tokens[0];
333        array_shift($chronology_tokens);
334      }
335      $page['chronology_date'] = $chronology_tokens;
336    }
337  }
338
339  $i++;
340}
341
342
343// $page['nb_image_page'] is the number of picture to display on this page
344// By default, it is the same as the $user['nb_image_page']
345$page['nb_image_page'] = $user['nb_image_page'];
346
347if (pwg_get_session_var('image_order',0) > 0)
348{
349  $orders = get_category_preferred_image_orders();
350
351  $conf['order_by'] = str_replace(
352    'ORDER BY ',
353    'ORDER BY '.$orders[ pwg_get_session_var('image_order',0) ][1].',',
354    $conf['order_by']
355    );
356  $page['super_order_by'] = true;
357}
358
359$forbidden = get_sql_condition_FandF(
360      array
361        (
362          'forbidden_categories' => 'category_id',
363          'visible_categories' => 'category_id',
364          'visible_images' => 'id'
365        ),
366      'AND'
367  );
368
369// +-----------------------------------------------------------------------+
370// |                              category                                 |
371// +-----------------------------------------------------------------------+
372if ('categories' == $page['section'])
373{
374  if (isset($page['category']))
375  {
376    $result = get_cat_info($page['category']);
377    if (empty($result))
378    {
379      page_not_found('Requested category does not exist' );
380    }
381
382    $page = array_merge(
383      $page,
384      array(
385        'comment'            => $result['comment'],
386        'cat_dir'            => $result['dir'],
387        'cat_name'           => $result['name'],
388        'cat_site_id'        => $result['site_id'],
389        'cat_uploadable'     => $result['uploadable'],
390        'cat_commentable'    => $result['commentable'],
391        'cat_id_uppercat'    => $result['id_uppercat'],
392        'uppercats'          => $result['uppercats'],
393        'title'             =>
394          get_cat_display_name($result['name'], '', false),
395        )
396      );
397  }
398  else
399  {
400    $page['title'] = $lang['no_category'];
401  }
402
403  if
404    (
405      (!isset($page['chronology_field'])) and
406      (
407        (isset($page['category'])) or
408        (isset($page['flat']))
409      )
410    )
411  {
412    if ( !empty($result['image_order']) and !isset($page['super_order_by']) )
413    {
414      $conf[ 'order_by' ] = ' ORDER BY '.$result['image_order'];
415    }
416
417    if (isset($page['flat']))
418    {// flat categories mode
419      if ( isset($page['category']) )
420      {
421        $subcat_ids = get_subcat_ids( array($page['category']) );
422        $where_sql = 'category_id IN ('.implode(',',$subcat_ids).')';
423      }
424      else
425      {
426        $where_sql = '1=1';
427      }
428    }
429    else
430    {// Normal mode
431      $where_sql = 'category_id = '.$page['category'];
432    }
433
434    // Main query
435    $query = '
436SELECT DISTINCT(image_id)
437  FROM '.IMAGE_CATEGORY_TABLE.'
438    INNER JOIN '.IMAGES_TABLE.' ON id = image_id
439  WHERE
440    '.$where_sql.'
441'.$forbidden.'
442  '.$conf['order_by'].'
443;';
444
445    $page['items'] = array_from_query($query, 'image_id');
446  } //otherwise the calendar will requery all subitems
447}
448// special sections
449else
450{
451// +-----------------------------------------------------------------------+
452// |                            tags section                               |
453// +-----------------------------------------------------------------------+
454  if ($page['section'] == 'tags')
455  {
456    $page['tag_ids'] = array();
457    foreach ($page['tags'] as $tag)
458    {
459      array_push($page['tag_ids'], $tag['id']);
460    }
461
462    $items = get_image_ids_for_tags($page['tag_ids']);
463
464    // permissions depends on category, so to only keep images that are
465    // reachable to the connected user, we need to check category
466    // associations
467    if (!empty($items) )
468    {
469      $query = '
470SELECT image_id
471  FROM '.IMAGE_CATEGORY_TABLE.' INNER JOIN '.IMAGES_TABLE.' ON image_id=id
472  WHERE image_id IN ('.implode(',', $items).')
473    '.$forbidden.
474    $conf['order_by'].'
475;';
476      $items = array_unique(
477        array_from_query($query, 'image_id')
478        );
479    }
480
481    $title = get_tags_content_title();
482
483    $page = array_merge(
484      $page,
485      array(
486        'title' => $title,
487        'items' => array_values($items),
488        )
489      );
490  }
491// +-----------------------------------------------------------------------+
492// |                           search section                              |
493// +-----------------------------------------------------------------------+
494  if ($page['section'] == 'search')
495  {
496    include_once( PHPWG_ROOT_PATH .'include/functions_search.inc.php' );
497
498    $search_result = get_search_results($page['search']);
499    if ( !empty($search_result['items']) and !isset($search_result['as_is']) )
500    {
501      $query = '
502SELECT DISTINCT(id)
503  FROM '.IMAGES_TABLE.'
504    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
505  WHERE id IN ('.implode(',', $search_result['items']).')
506    '.$forbidden.'
507  '.$conf['order_by'].'
508;';
509      $page['items'] = array_from_query($query, 'id');
510    }
511    else
512    {
513      $page['items'] = $search_result['items'];
514    }
515
516    $page = array_merge(
517      $page,
518      array(
519        'title' => $lang['search_result'],
520        )
521      );
522  }
523// +-----------------------------------------------------------------------+
524// |                           favorite section                            |
525// +-----------------------------------------------------------------------+
526  else if ($page['section'] == 'favorites')
527  {
528    check_user_favorites();
529
530    $query = '
531SELECT image_id
532  FROM '.FAVORITES_TABLE.'
533    INNER JOIN '.IMAGES_TABLE.' ON image_id = id
534  WHERE user_id = '.$user['id'].'
535'.get_sql_condition_FandF
536  (
537    array
538      (
539        'visible_images' => 'image_id'
540      ),
541    'AND'
542  ).'
543  '.$conf['order_by'].'
544;';
545
546    $page = array_merge(
547      $page,
548      array(
549        'title' => $lang['favorites'],
550        'items' => array_from_query($query, 'image_id'),
551        )
552      );
553  }
554// +-----------------------------------------------------------------------+
555// |                       recent pictures section                         |
556// +-----------------------------------------------------------------------+
557  else if ($page['section'] == 'recent_pics')
558  {
559    $query = '
560SELECT DISTINCT(id)
561  FROM '.IMAGES_TABLE.'
562    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
563  WHERE date_available > \''.
564      date('Y-m-d', time() - 60*60*24*$user['recent_period']).'\'
565    '.$forbidden.'
566  '.$conf['order_by'].'
567;';
568
569    $page = array_merge(
570      $page,
571      array(
572        'title' => '<a href="'.duplicate_index_url().'">'
573                  .$lang['recent_pics_cat'].'</a>',
574        'items' => array_from_query($query, 'id'),
575        )
576      );
577  }
578// +-----------------------------------------------------------------------+
579// |                 recently updated categories section                   |
580// +-----------------------------------------------------------------------+
581  else if ($page['section'] == 'recent_cats')
582  {
583    $page = array_merge(
584      $page,
585      array(
586        'title' => $lang['recent_cats_cat'],
587        )
588      );
589  }
590// +-----------------------------------------------------------------------+
591// |                        most visited section                           |
592// +-----------------------------------------------------------------------+
593  else if ($page['section'] == 'most_visited')
594  {
595    $page['super_order_by'] = true;
596    $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
597    $query = '
598SELECT DISTINCT(id)
599  FROM '.IMAGES_TABLE.'
600    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
601  WHERE hit > 0
602    '.$forbidden.'
603    '.$conf['order_by'].'
604  LIMIT 0, '.$conf['top_number'].'
605;';
606
607    $page = array_merge(
608      $page,
609      array(
610        'title' => '<a href="'.duplicate_index_url().'">'
611                  .$conf['top_number'].' '.$lang['most_visited_cat'].'</a>',
612        'items' => array_from_query($query, 'id'),
613        )
614      );
615  }
616// +-----------------------------------------------------------------------+
617// |                          best rated section                           |
618// +-----------------------------------------------------------------------+
619  else if ($page['section'] == 'best_rated')
620  {
621    $page['super_order_by'] = true;
622    $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC';
623
624    $query ='
625SELECT DISTINCT(id)
626  FROM '.IMAGES_TABLE.'
627    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
628  WHERE average_rate IS NOT NULL
629    '.$forbidden.'
630    '.$conf['order_by'].'
631  LIMIT 0, '.$conf['top_number'].'
632;';
633    $page = array_merge(
634      $page,
635      array(
636        'title' => '<a href="'.duplicate_index_url().'">'
637                  .$conf['top_number'].' '.$lang['best_rated_cat'].'</a>',
638        'items' => array_from_query($query, 'id'),
639        )
640      );
641  }
642// +-----------------------------------------------------------------------+
643// |                             list section                              |
644// +-----------------------------------------------------------------------+
645  else if ($page['section'] == 'list')
646  {
647    $query ='
648SELECT DISTINCT(id)
649  FROM '.IMAGES_TABLE.'
650    INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id
651  WHERE image_id IN ('.implode(',', $page['list']).')
652    '.$forbidden.'
653  '.$conf['order_by'].'
654;';
655
656    $page = array_merge(
657      $page,
658      array(
659        'title' => '<a href="'.duplicate_index_url().'">'
660                    .$lang['random_cat'].'</a>',
661        'items' => array_from_query($query, 'id'),
662        )
663      );
664  }
665}
666
667// +-----------------------------------------------------------------------+
668// |                             chronology                                |
669// +-----------------------------------------------------------------------+
670
671if (isset($page['chronology_field']))
672{
673  include_once( PHPWG_ROOT_PATH.'include/functions_calendar.inc.php' );
674  initialize_calendar();
675}
676
677if (script_basename() == 'picture'
678    and !isset($page['image_id']) )
679{
680  if ( !empty($page['items']) )
681  {
682    $query = '
683SELECT id,file
684  FROM '.IMAGES_TABLE .'
685  WHERE id IN ('.implode(',',$page['items']).')
686  AND file LIKE "' . $page['image_file'] . '.%" ESCAPE "|"'
687;
688    $result = pwg_query($query);
689    if (mysql_num_rows($result)>0)
690    {
691      list($page['image_id'], $page['image_file']) = mysql_fetch_row($result);
692    }
693  }
694  if ( !isset($page['image_id']) )
695  {
696    $page['image_id'] = -1; // will fail in picture.php
697  }
698}
699
700// add meta robots noindex, nofollow to avoid unnecesary robot crawls
701$page['meta_robots']=array();
702if ( isset($page['chronology_field']) or isset($page['flat'])
703      or 'list'==$page['section'] or 'recent_pics'==$page['section'] )
704{
705  $page['meta_robots']=array('noindex'=>1, 'nofollow'=>1);
706}
707elseif ('tags' == $page['section'])
708{
709  if ( count($page['tag_ids'])>1 )
710  {
711    $page['meta_robots']=array('noindex'=>1, 'nofollow'=>1);
712  }
713}
714elseif ('recent_cats'==$page['section'])
715{
716  $page['meta_robots']['nofollow']=1;
717}
718if ( $filter['enabled'] )
719{
720  $page['meta_robots']['noindex']=1;
721}
722
723trigger_action('loc_end_section_init');
724?>
Note: See TracBrowser for help on using the repository browser.