source: trunk/picture.php @ 1723

Last change on this file since 1723 was 1711, checked in by rvelices, 18 years ago
  • better code in filter.inc.php (remove unused code + filter is not reseted

when going to an unfiltered page)

  • removed unnecessary filtered pages from config_default (especially admin !!!)
  • removed flat recent category icon from irrelevant pages
  • mysterious code comment appeared in picture.php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.1 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: picture.php 1711 2007-01-11 05:10:16Z rvelices $
9// | last update   : $Date: 2007-01-11 05:10:16 +0000 (Thu, 11 Jan 2007) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1711 $
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
28define('PHPWG_ROOT_PATH','./');
29include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
30include(PHPWG_ROOT_PATH.'include/section_init.inc.php');
31include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
32
33// Check Access and exit when user status is not ok
34check_status(ACCESS_GUEST);
35
36// access authorization check
37if (isset($page['category']))
38{
39  check_restrictions($page['category']);
40}
41
42// if this image_id doesn't correspond to this category, an error message is
43// displayed, and execution is stopped
44if (!in_array($page['image_id'], $page['items']))
45{
46  page_not_found('The requested image does not belong to this image set',
47      duplicate_index_url() );
48}
49
50// add default event handler for rendering element content
51add_event_handler('render_element_content', 'default_picture_content',
52  EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
53trigger_action('loc_begin_picture');
54
55// this is the default handler that generates the display for the element
56function default_picture_content($content, $element_info)
57{
58  if ( !empty($content) )
59  {// someone hooked us - so we skip;
60    return $content;
61  }
62  if (!isset($element_info['image_url']))
63  { // nothing to do
64    return $content;
65  }
66  global $user;
67  $my_template = new Template(PHPWG_ROOT_PATH.'template/'.$user['template'],
68    $user['theme'] );
69  $my_template->set_filenames( array('default_content'=>'picture_content.tpl') );
70
71  if (isset($element_info['high_url']))
72  {
73    $uuid = uniqid(rand());
74    $my_template->assign_block_vars(
75      'high',
76      array(
77        'U_HIGH' => $element_info['high_url'],
78        'UUID'   => $uuid,
79        )
80      );
81  }
82  $my_template->assign_vars( array(
83      'SRC_IMG' => $element_info['image_url'],
84      'ALT_IMG' => $element_info['file'],
85      'WIDTH_IMG' => @$element_info['scaled_width'],
86      'HEIGHT_IMG' => @$element_info['scaled_height'],
87      )
88    );
89  return $my_template->parse( 'default_content', true);
90}
91
92
93
94// +-----------------------------------------------------------------------+
95// |                            initialization                             |
96// +-----------------------------------------------------------------------+
97
98$page['rank_of'] = array_flip($page['items']);
99
100// caching first_rank, last_rank, current_rank in the displayed
101// section. This should also help in readability.
102$page['first_rank']   = 0;
103$page['last_rank']    = count($page['items']) - 1;
104$page['current_rank'] = $page['rank_of'][ $page['image_id'] ];
105
106// caching current item : readability purpose
107$page['current_item'] = $page['image_id'];
108
109if ($page['current_rank'] != $page['first_rank'])
110{
111  // caching first & previous item : readability purpose
112  $page['previous_item'] = $page['items'][ $page['current_rank'] - 1 ];
113  $page['first_item'] = $page['items'][ $page['first_rank'] ];
114}
115
116if ($page['current_rank'] != $page['last_rank'])
117{
118  // caching next & last item : readability purpose
119  $page['next_item'] = $page['items'][ $page['current_rank'] + 1 ];
120  $page['last_item'] = $page['items'][ $page['last_rank'] ];
121}
122
123$url_up = duplicate_index_url(
124  array(
125    'start' =>
126      floor($page['current_rank'] / $user['nb_image_page'])
127      * $user['nb_image_page']
128    ),
129  array(
130    'start',
131    )
132  );
133
134$url_self = duplicate_picture_url();
135
136// +-----------------------------------------------------------------------+
137// |                                actions                                |
138// +-----------------------------------------------------------------------+
139
140/**
141 * Actions are favorite adding, user comment deletion, setting the picture
142 * as representative of the current category...
143 *
144 * Actions finish by a redirection
145 */
146
147if (isset($_GET['action']))
148{
149  switch ($_GET['action'])
150  {
151    case 'add_to_favorites' :
152    {
153      $query = '
154INSERT INTO '.FAVORITES_TABLE.'
155  (image_id,user_id)
156  VALUES
157  ('.$page['image_id'].','.$user['id'].')
158;';
159      pwg_query($query);
160
161      redirect($url_self);
162
163      break;
164    }
165    case 'remove_from_favorites' :
166    {
167      $query = '
168DELETE FROM '.FAVORITES_TABLE.'
169  WHERE user_id = '.$user['id'].'
170    AND image_id = '.$page['image_id'].'
171;';
172      pwg_query($query);
173
174      if ('favorites' == $page['section'])
175      {
176        redirect($url_up);
177      }
178      else
179      {
180        redirect($url_self);
181      }
182
183      break;
184    }
185    case 'set_as_representative' :
186    {
187      if (is_admin() and !is_adviser() and isset($page['category']))
188      {
189        $query = '
190UPDATE '.CATEGORIES_TABLE.'
191  SET representative_picture_id = '.$page['image_id'].'
192  WHERE id = '.$page['category'].'
193;';
194        pwg_query($query);
195      }
196
197      redirect($url_self);
198
199      break;
200    }
201    case 'toggle_metadata' :
202    {
203      break;
204    }
205    case 'add_to_caddie' :
206    {
207      fill_caddie(array($page['image_id']));
208      redirect($url_self);
209      break;
210    }
211    case 'rate' :
212    {
213      include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php');
214      rate_picture($page['image_id'],
215          isset($_POST['rate']) ? $_POST['rate'] : $_GET['rate'] );
216      redirect($url_self);
217    }
218    case 'delete_comment' :
219    {
220      if (isset($_GET['comment_to_delete'])
221          and is_numeric($_GET['comment_to_delete'])
222          and is_admin() and !is_adviser() )
223      {
224        $query = '
225DELETE FROM '.COMMENTS_TABLE.'
226  WHERE id = '.$_GET['comment_to_delete'].'
227;';
228        pwg_query( $query );
229      }
230
231      redirect($url_self);
232    }
233  }
234}
235
236// incrementation of the number of hits, we do this only if no action
237$query = '
238UPDATE
239  '.IMAGES_TABLE.'
240  SET hit = hit+1
241  WHERE id = '.$page['image_id'].'
242;';
243pwg_query($query);
244
245//---------------------------------------------------------- related categories
246$query = '
247SELECT category_id,uppercats,commentable,global_rank
248  FROM '.IMAGE_CATEGORY_TABLE.'
249    INNER JOIN '.CATEGORIES_TABLE.' ON category_id = id
250  WHERE image_id = '.$page['image_id'].'
251'.get_sql_condition_FandF
252  (
253    array
254      (
255        'forbidden_categories' => 'category_id',
256        'visible_categories' => 'category_id'
257      ),
258    'AND'
259  ).'
260;';
261$result = pwg_query($query);
262$related_categories = array();
263while ($row = mysql_fetch_array($result))
264{
265  array_push($related_categories, $row);
266}
267usort($related_categories, 'global_rank_compare');
268//-------------------------first, prev, current, next & last picture management
269$picture = array();
270
271$ids = array($page['image_id']);
272if (isset($page['previous_item']))
273{
274  array_push($ids, $page['previous_item']);
275  array_push($ids, $page['first_item']);
276}
277if (isset($page['next_item']))
278{
279  array_push($ids, $page['next_item']);
280  array_push($ids, $page['last_item']);
281}
282
283$query = '
284SELECT *
285  FROM '.IMAGES_TABLE.'
286  WHERE id IN ('.implode(',', $ids).')
287;';
288
289$result = pwg_query($query);
290
291while ($row = mysql_fetch_assoc($result))
292{
293  if (isset($page['previous_item']) and $row['id'] == $page['previous_item'])
294  {
295    $i = 'previous';
296  }
297  else if (isset($page['next_item']) and $row['id'] == $page['next_item'])
298  {
299    $i = 'next';
300  }
301  else if (isset($page['first_item']) and $row['id'] == $page['first_item'])
302  {
303    $i = 'first';
304  }
305  else if (isset($page['last_item']) and $row['id'] == $page['last_item'])
306  {
307    $i = 'last';
308  }
309  else
310  {
311    $i = 'current';
312  }
313
314  $picture[$i] = $row;
315
316  $picture[$i]['is_picture'] = false;
317  if (in_array(get_extension($row['file']), $conf['picture_ext']))
318  {
319    $picture[$i]['is_picture'] = true;
320  }
321
322  // ------ build element_path and element_url
323  $picture[$i]['element_path'] = get_element_path($picture[$i]);
324  $picture[$i]['element_url'] = get_element_url($picture[$i]);
325
326  // ------ build image_path and image_url
327  if ($i=='current' or $i=='next')
328  {
329    $picture[$i]['image_path'] = get_image_path( $picture[$i] );
330    $picture[$i]['image_url'] = get_image_url( $picture[$i] );
331  }
332
333  if ($i=='current')
334  {
335    if ( $picture[$i]['is_picture'] )
336    {
337      if ( $user['enabled_high']=='true' )
338      {
339        $hi_url=get_high_url($picture[$i]);
340        if ( !empty($hi_url) )
341        {
342          $picture[$i]['high_url'] = $hi_url;
343          $picture[$i]['download_url'] = get_download_url('h',$picture[$i]);
344        }
345      }
346    }
347    else
348    { // not a pic - need download link
349      $picture[$i]['download_url'] = get_download_url('e',$picture[$i]);
350    }
351  }
352
353  $picture[$i]['thumbnail'] = get_thumbnail_url($row);
354
355  if ( !empty( $row['name'] ) )
356  {
357    $picture[$i]['name'] = $row['name'];
358  }
359  else
360  {
361    $file_wo_ext = get_filename_wo_extension($row['file']);
362    $picture[$i]['name'] = str_replace('_', ' ', $file_wo_ext);
363  }
364
365  $picture[$i]['url'] = duplicate_picture_url(
366    array(
367      'image_id' => $row['id'],
368      'image_file' => $row['file'],
369      ),
370    array(
371      'start',
372      )
373    );
374
375  if ('previous'==$i and $page['previous_item']==$page['first_item'])
376  {
377    $picture['first'] = $picture[$i];
378  }
379  if ('next'==$i and $page['next_item']==$page['last_item'])
380  {
381    $picture['last'] = $picture[$i];
382  }
383}
384
385// calculation of width and height for the current picture
386if (empty($picture['current']['width']))
387{
388  $taille_image = @getimagesize($picture['current']['image_path']);
389  if ($taille_image!==false)
390  {
391    $picture['current']['width'] = $taille_image[0];
392    $picture['current']['height']= $taille_image[1];
393  }
394}
395
396if (!empty($picture['current']['width']))
397{
398  list($picture['current']['scaled_width'],$picture['current']['scaled_height']) =
399    get_picture_size(
400      $picture['current']['width'],
401      $picture['current']['height'],
402      @$user['maxwidth'],
403      @$user['maxheight']
404    );
405}
406
407$url_admin =
408  get_root_url().'admin.php?page=picture_modify'
409  .'&amp;cat_id='.(isset($page['category']) ? $page['category'] : '')
410  .'&amp;image_id='.$page['image_id']
411;
412
413$url_slide = add_url_params(
414  $picture['current']['url'],
415  array( 'slideshow'=>$conf['slideshow_period'] )
416  );
417
418$title =  $picture['current']['name'];
419$refresh = 0;
420if ( isset( $_GET['slideshow'] ) and isset($page['next_item']) )
421{
422  // $redirect_msg, $refresh, $url_link and $title are required for creating an automated
423  // refresh page in header.tpl
424  $refresh= $_GET['slideshow'];
425  $url_link = add_url_params(
426      $picture['next']['url'],
427      array('slideshow'=>$refresh)
428    );
429  $redirect_msg = nl2br(l10n('redirect_msg'));
430  $page['meta_robots']=array('noindex'=>1, 'nofollow'=>1);
431}
432
433$title_nb = ($page['current_rank'] + 1).'/'.$page['cat_nb_images'];
434
435// metadata
436$url_metadata = duplicate_picture_url();
437
438// do we have a plugin that can show metadata for something else than images?
439$metadata_showable = trigger_event('get_element_metadata_available',
440    (
441      ($conf['show_exif'] or $conf['show_iptc'])
442      and isset($picture['current']['image_path'])
443    ),
444    $picture['current']['path'] );
445if ($metadata_showable)
446{
447  if ( !isset($_GET['metadata']) )
448  {
449    $url_metadata = add_url_params( $url_metadata, array('metadata'=>null) );
450  }
451  else
452  {
453    $page['meta_robots']=array('noindex'=>1, 'nofollow'=>1);
454  }
455}
456
457$page['body_id'] = 'thePicturePage';
458
459// maybe someone wants a special display (call it before page_header so that they
460// can add stylesheets)
461$element_content = trigger_event('render_element_content',
462                      '', $picture['current'] );
463
464if ( isset($picture['next']['image_url'])
465      and isset($picture['next']['is_picture']) )
466{
467  $template->assign_block_vars( 'prefetch',
468    array (
469      'URL' => $picture['next']['image_url']
470    )
471  );
472}
473$template->set_filenames(array('picture'=>'picture.tpl'));
474
475//------------------------------------------------------- navigation management
476foreach ( array('first','previous','next','last') as $which_image )
477{
478  if (isset($picture[$which_image]))
479  {
480    $template->assign_block_vars(
481      $which_image,
482      array(
483        'TITLE_IMG' => $picture[$which_image]['name'],
484        'IMG' => $picture[$which_image]['thumbnail'],
485        'U_IMG' => $picture[$which_image]['url'],
486        )
487      );
488  }
489}
490
491$template->assign_vars(
492  array(
493    'SECTION_TITLE' => $page['title'],
494    'PICTURE_TITLE' => $picture['current']['name'],
495    'PHOTO' => $title_nb,
496    'TITLE' => $picture['current']['name'],
497    'ELEMENT_CONTENT' => $element_content,
498
499    'LEVEL_SEPARATOR' => $conf['level_separator'],
500
501    'U_HOME' => make_index_url(),
502    'U_UP' => $url_up,
503    'U_METADATA' => $url_metadata,
504    'U_ADMIN' => $url_admin,
505    'U_SLIDESHOW'=> $url_slide,
506    'U_ADD_COMMENT' => $url_self,
507    )
508  );
509
510if ($conf['show_picture_name_on_title'])
511{
512  $template->assign_block_vars('title', array());
513}
514
515//------------------------------------------------------- upper menu management
516
517// download link
518if ( isset($picture['current']['download_url']) )
519{
520  $template->assign_block_vars(
521    'download',
522    array(
523      'U_DOWNLOAD' => $picture['current']['download_url']
524      )
525    );
526}
527
528// button to set the current picture as representative
529if (is_admin() and isset($page['category']))
530{
531  $template->assign_block_vars(
532    'representative',
533    array(
534      'URL' => add_url_params($url_self,
535                  array('action'=>'set_as_representative')
536               )
537      )
538    );
539}
540
541// caddie button
542if (is_admin())
543{
544  $template->assign_block_vars(
545    'caddie',
546    array(
547      'URL' => add_url_params($url_self,
548                  array('action'=>'add_to_caddie')
549               )
550      )
551    );
552}
553
554// favorite manipulation
555if (!$user['is_the_guest'])
556{
557  // verify if the picture is already in the favorite of the user
558  $query = '
559SELECT COUNT(*) AS nb_fav
560  FROM '.FAVORITES_TABLE.'
561  WHERE image_id = '.$page['image_id'].'
562    AND user_id = '.$user['id'].'
563;';
564  $result = pwg_query($query);
565  $row = mysql_fetch_array($result);
566
567  if ($row['nb_fav'] == 0)
568  {
569    $template->assign_block_vars(
570      'favorite',
571      array(
572        'FAVORITE_IMG'  => get_root_url().get_themeconf('icon_dir').'/favorite.png',
573        'FAVORITE_HINT' => $lang['add_favorites_hint'],
574        'FAVORITE_ALT'  => $lang['add_favorites_alt'],
575        'U_FAVORITE'    => add_url_params(
576                              $url_self,
577                              array('action'=>'add_to_favorites')
578                           ),
579        )
580      );
581  }
582  else
583  {
584    $template->assign_block_vars(
585      'favorite',
586      array(
587        'FAVORITE_IMG'  => get_root_url().get_themeconf('icon_dir').'/del_favorite.png',
588        'FAVORITE_HINT' => $lang['del_favorites_hint'],
589        'FAVORITE_ALT'  => $lang['del_favorites_alt'],
590        'U_FAVORITE'    => add_url_params(
591                              $url_self,
592                              array('action'=>'remove_from_favorites')
593                           )
594        )
595      );
596  }
597}
598//------------------------------------ admin link for information modifications
599if ( is_admin() )
600{
601  $template->assign_block_vars('admin', array());
602}
603
604//--------------------------------------------------------- picture information
605$header_infos = array();        //for html header use
606// legend
607if (isset($picture['current']['comment'])
608    and !empty($picture['current']['comment']))
609{
610  $template->assign_block_vars(
611    'legend',
612    array(
613      'COMMENT_IMG' => nl2br($picture['current']['comment'])
614      ));
615  $header_infos['COMMENT'] = strip_tags($picture['current']['comment']);
616}
617
618$infos = array();
619
620// author
621if (!empty($picture['current']['author']))
622{
623  $infos['INFO_AUTHOR'] =
624    // FIXME because of search engine partial rewrite, giving the author
625    // name threw GET is not supported anymore. This feature should come
626    // back later, with a better design
627//     '<a href="'.
628//       PHPWG_ROOT_PATH.'category.php?cat=search'.
629//       '&amp;search=author:'.$picture['current']['author']
630//       .'">'.$picture['current']['author'].'</a>';
631    $picture['current']['author'];
632  $header_infos['INFO_AUTHOR'] = $picture['current']['author'];
633}
634else
635{
636  $infos['INFO_AUTHOR'] = l10n('N/A');
637}
638
639// creation date
640if (!empty($picture['current']['date_creation']))
641{
642  $val = format_date($picture['current']['date_creation']);
643  $url = make_index_url(
644        array(
645          'chronology_field'=>'created',
646          'chronology_style'=>'monthly',
647          'chronology_view'=>'list',
648          'chronology_date' => explode('-', $picture['current']['date_creation'])
649        )
650      );
651  $infos['INFO_CREATION_DATE'] = '<a href="'.$url.'" rel="nofollow">'.$val.'</a>';
652}
653else
654{
655  $infos['INFO_CREATION_DATE'] = l10n('N/A');
656}
657
658// date of availability
659$val = format_date($picture['current']['date_available'], 'mysql_datetime');
660$url = make_index_url(
661      array(
662        'chronology_field'=>'posted',
663        'chronology_style'=>'monthly',
664        'chronology_view'=>'list',
665        'chronology_date'=>explode('-', substr($picture['current']['date_available'],0,10))
666      )
667    );
668$infos['INFO_POSTED_DATE'] = '<a href="'.$url.'" rel="nofollow">'.$val.'</a>';
669
670// size in pixels
671if ($picture['current']['is_picture'] and isset($picture['current']['width']) )
672{
673  if ($picture['current']['scaled_width'] !== $picture['current']['width'] )
674  {
675    $infos['INFO_DIMENSIONS'] =
676      '<a href="'.$picture['current']['image_url'].'" title="'.
677      l10n('Original dimensions').'">'.
678      $picture['current']['width'].'*'.$picture['current']['height'].'</a>';
679  }
680  else
681  {
682    $infos['INFO_DIMENSIONS'] =
683      $picture['current']['width'].'*'.$picture['current']['height'];
684  }
685}
686else
687{
688  $infos['INFO_DIMENSIONS'] = l10n('N/A');
689}
690
691// filesize
692if (!empty($picture['current']['filesize']))
693{
694  $infos['INFO_FILESIZE'] =
695    sprintf(l10n('%d Kb'), $picture['current']['filesize']);
696}
697else
698{
699  $infos['INFO_FILESIZE'] = l10n('N/A');
700}
701
702// number of visits
703$infos['INFO_VISITS'] = $picture['current']['hit'];
704
705// file
706$infos['INFO_FILE'] = $picture['current']['file'];
707
708// tags
709$query = '
710SELECT id, name, url_name
711  FROM '.IMAGE_TAG_TABLE.'
712    INNER JOIN '.TAGS_TABLE.' ON tag_id = id
713  WHERE image_id = '.$page['image_id'].'
714;';
715$result = pwg_query($query);
716
717if (mysql_num_rows($result) > 0)
718{
719  $tags = array();
720  $tag_names = array();
721
722  while ($row = mysql_fetch_array($result))
723  {
724    array_push(
725      $tags,
726      '<a href="'
727      .make_index_url(
728        array(
729          'tags' => array(
730            array(
731              'id' => $row['id'],
732              'url_name' => $row['url_name'],
733              ),
734            )
735          )
736        )
737      .'">'.$row['name'].'</a>'
738      );
739    array_push( $tag_names, $row['name'] );
740  }
741
742  $infos['INFO_TAGS'] = implode(', ', $tags);
743  $header_infos['INFO_TAGS'] = implode(', ', $tag_names);
744}
745else
746{
747  $infos['INFO_TAGS'] = l10n('N/A');
748}
749
750$template->assign_vars($infos);
751
752// related categories
753foreach ($related_categories as $category)
754{
755  $template->assign_block_vars(
756    'category',
757    array(
758      'LINE' => count($related_categories) > 3
759        ? get_cat_display_name_cache($category['uppercats'])
760        : get_cat_display_name_from_id($category['category_id'])
761      )
762    );
763}
764
765//slideshow end
766if (isset($_GET['slideshow']))
767{
768  if (!is_numeric($_GET['slideshow']))
769  {
770    $_GET['slideshow'] = $conf['slideshow_period'];
771  }
772
773  $template->assign_block_vars(
774    'stop_slideshow',
775    array(
776      'U_SLIDESHOW' => $picture['current']['url'],
777      )
778    );
779}
780
781// +-----------------------------------------------------------------------+
782// |                               sub pages                               |
783// +-----------------------------------------------------------------------+
784
785include(PHPWG_ROOT_PATH.'include/picture_rate.inc.php');
786include(PHPWG_ROOT_PATH.'include/picture_comment.inc.php');
787if ($metadata_showable and isset($_GET['metadata']))
788{
789  include(PHPWG_ROOT_PATH.'include/picture_metadata.inc.php');
790}
791//------------------------------------------------------------ log informations
792pwg_log('picture', $page['title'], $picture['current']['file']);
793
794include(PHPWG_ROOT_PATH.'include/page_header.php');
795$template->parse('picture');
796include(PHPWG_ROOT_PATH.'include/page_tail.php');
797?>
Note: See TracBrowser for help on using the repository browser.