source: trunk/picture.php @ 2048

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