source: trunk/picture.php @ 1612

Last change on this file since 1612 was 1612, checked in by rvelices, 17 years ago
  • plugins can have full control over the path/url of the element/image/

thumbnail/high (it is possible now to have secure images, on the fly
watermarking, mod download and media integrator plugins working together in
any combination and without touching PWG core)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.6 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-2006 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2006-11-17 04:26:10 +0000 (Fri, 17 Nov 2006) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1612 $
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    AND category_id NOT IN ('.$user['forbidden_categories'].')
252;';
253$result = pwg_query($query);
254$related_categories = array();
255while ($row = mysql_fetch_array($result))
256{
257  array_push($related_categories, $row);
258}
259usort($related_categories, 'global_rank_compare');
260//-------------------------first, prev, current, next & last picture management
261$picture = array();
262
263$ids = array($page['image_id']);
264if (isset($page['previous_item']))
265{
266  array_push($ids, $page['previous_item']);
267  array_push($ids, $page['first_item']);
268}
269if (isset($page['next_item']))
270{
271  array_push($ids, $page['next_item']);
272  array_push($ids, $page['last_item']);
273}
274
275$query = '
276SELECT *
277  FROM '.IMAGES_TABLE.'
278  WHERE id IN ('.implode(',', $ids).')
279;';
280
281$result = pwg_query($query);
282
283while ($row = mysql_fetch_assoc($result))
284{
285  if (isset($page['previous_item']) and $row['id'] == $page['previous_item'])
286  {
287    $i = 'previous';
288  }
289  else if (isset($page['next_item']) and $row['id'] == $page['next_item'])
290  {
291    $i = 'next';
292  }
293  else if (isset($page['first_item']) and $row['id'] == $page['first_item'])
294  {
295    $i = 'first';
296  }
297  else if (isset($page['last_item']) and $row['id'] == $page['last_item'])
298  {
299    $i = 'last';
300  }
301  else
302  {
303    $i = 'current';
304  }
305
306  $picture[$i] = $row;
307
308  $picture[$i]['is_picture'] = false;
309  if (in_array(get_extension($row['file']), $conf['picture_ext']))
310  {
311    $picture[$i]['is_picture'] = true;
312  }
313
314  // ------ build element_path and element_url
315  $picture[$i]['element_path'] = get_element_path($picture[$i]);
316  $picture[$i]['element_url'] = get_element_url($picture[$i]);
317
318  // ------ build image_path and image_url
319  if ($i=='current' or $i=='next')
320  {
321    $picture[$i]['image_path'] = get_image_path( $picture[$i] );
322    $picture[$i]['image_url'] = get_image_url( $picture[$i] );
323  }
324
325  if ($i=='current')
326  {
327    if ( $picture[$i]['is_picture'] )
328    {
329      if ( $user['enabled_high']=='true' )
330      {
331        $hi_url=get_high_url($picture[$i]);
332        if ( !empty($hi_url) )
333        {
334          $picture[$i]['high_url'] = $hi_url;
335          $picture[$i]['download_url'] = get_download_url('h',$picture[$i]);
336        }
337      }
338    }
339    else
340    { // not a pic - need download link
341      $picture[$i]['download_url'] = get_download_url('e',$picture[$i]);
342    }
343  }
344
345  $picture[$i]['thumbnail'] = get_thumbnail_url($row);
346
347  if ( !empty( $row['name'] ) )
348  {
349    $picture[$i]['name'] = $row['name'];
350  }
351  else
352  {
353    $file_wo_ext = get_filename_wo_extension($row['file']);
354    $picture[$i]['name'] = str_replace('_', ' ', $file_wo_ext);
355  }
356
357  $picture[$i]['url'] = duplicate_picture_url(
358    array(
359      'image_id' => $row['id'],
360      'image_file' => $row['file'],
361      ),
362    array(
363      'start',
364      )
365    );
366
367  if ('previous'==$i and $page['previous_item']==$page['first_item'])
368  {
369    $picture['first'] = $picture[$i];
370  }
371  if ('next'==$i and $page['next_item']==$page['last_item'])
372  {
373    $picture['last'] = $picture[$i];
374  }
375}
376
377// calculation of width and height for the current picture
378if (empty($picture['current']['width']))
379{
380  $taille_image = @getimagesize($picture['current']['image_path']);
381  if ($taille_image!==false)
382  {
383    $picture['current']['width'] = $taille_image[0];
384    $picture['current']['height']= $taille_image[1];
385  }
386}
387
388if (!empty($picture['current']['width']))
389{
390  list($picture['current']['scaled_width'],$picture['current']['scaled_height']) =
391    get_picture_size(
392      $picture['current']['width'],
393      $picture['current']['height'],
394      @$user['maxwidth'],
395      @$user['maxheight']
396    );
397}
398
399$url_admin =
400  get_root_url().'admin.php?page=picture_modify'
401  .'&amp;cat_id='.(isset($page['category']) ? $page['category'] : '')
402  .'&amp;image_id='.$page['image_id']
403;
404
405$url_slide = add_url_params(
406  $picture['current']['url'],
407  array( 'slideshow'=>$conf['slideshow_period'] )
408  );
409
410$title =  $picture['current']['name'];
411$refresh = 0;
412if ( isset( $_GET['slideshow'] ) and isset($page['next_item']) )
413{
414  // $redirect_msg, $refresh, $url_link and $title are required for creating an automated
415  // refresh page in header.tpl
416  $refresh= $_GET['slideshow'];
417  $url_link = add_url_params(
418      $picture['next']['url'],
419      array('slideshow'=>$refresh)
420    );
421  $redirect_msg = nl2br(l10n('redirect_msg'));
422}
423
424$title_nb = ($page['current_rank'] + 1).'/'.$page['cat_nb_images'];
425
426// metadata
427$url_metadata = duplicate_picture_url();
428
429// do we have a plugin that can show metadata for something else than images?
430$metadata_showable = trigger_event('get_element_metadata_available',
431    (
432      ($conf['show_exif'] or $conf['show_iptc'])
433      and isset($picture['current']['image_path'])
434    ),
435    $picture['current']['path'] );
436if ($metadata_showable)
437{
438  if ( !isset($_GET['metadata']) )
439  {
440    $url_metadata = add_url_params( $url_metadata, array('metadata'=>null) );
441  }
442}
443
444$page['body_id'] = 'thePicturePage';
445
446// maybe someone wants a special display (call it before page_header so that they
447// can add stylesheets)
448$element_content = trigger_event('render_element_content',
449                      '', $picture['current'] );
450
451if ( isset($picture['next']['image_url'])
452      and isset($picture['next']['is_picture']) )
453{
454  $template->assign_block_vars( 'prefetch',
455    array (
456      'URL' => $picture['next']['image_url']
457    )
458  );
459}
460include(PHPWG_ROOT_PATH.'include/page_header.php');
461$template->set_filenames(array('picture'=>'picture.tpl'));
462
463//------------------------------------------------------- navigation management
464foreach ( array('first','previous','next','last') as $which_image )
465{
466  if (isset($picture[$which_image]))
467  {
468    $template->assign_block_vars(
469      $which_image,
470      array(
471        'TITLE_IMG' => $picture[$which_image]['name'],
472        'IMG' => $picture[$which_image]['thumbnail'],
473        'U_IMG' => $picture[$which_image]['url'],
474        )
475      );
476  }
477}
478
479$template->assign_vars(
480  array(
481    'SECTION_TITLE' => $page['title'],
482    'PICTURE_TITLE' => $picture['current']['name'],
483    'PHOTO' => $title_nb,
484    'TITLE' => $picture['current']['name'],
485    'ELEMENT_CONTENT' => $element_content,
486
487    'LEVEL_SEPARATOR' => $conf['level_separator'],
488
489    'U_HOME' => make_index_url(),
490    'U_UP' => $url_up,
491    'U_METADATA' => $url_metadata,
492    'U_ADMIN' => $url_admin,
493    'U_SLIDESHOW'=> $url_slide,
494    'U_ADD_COMMENT' => $url_self,
495    )
496  );
497
498if ($conf['show_picture_name_on_title'])
499{
500  $template->assign_block_vars('title', array());
501}
502
503//------------------------------------------------------- upper menu management
504
505// download link
506if ( isset($picture['current']['download_url']) )
507{
508  $template->assign_block_vars(
509    'download',
510    array(
511      'U_DOWNLOAD' => $picture['current']['download_url']
512      )
513    );
514}
515
516// button to set the current picture as representative
517if (is_admin() and isset($page['category']))
518{
519  $template->assign_block_vars(
520    'representative',
521    array(
522      'URL' => add_url_params($url_self,
523                  array('action'=>'set_as_representative')
524               )
525      )
526    );
527}
528
529// caddie button
530if (is_admin())
531{
532  $template->assign_block_vars(
533    'caddie',
534    array(
535      'URL' => add_url_params($url_self,
536                  array('action'=>'add_to_caddie')
537               )
538      )
539    );
540}
541
542// favorite manipulation
543if (!$user['is_the_guest'])
544{
545  // verify if the picture is already in the favorite of the user
546  $query = '
547SELECT COUNT(*) AS nb_fav
548  FROM '.FAVORITES_TABLE.'
549  WHERE image_id = '.$page['image_id'].'
550    AND user_id = '.$user['id'].'
551;';
552  $result = pwg_query($query);
553  $row = mysql_fetch_array($result);
554
555  if ($row['nb_fav'] == 0)
556  {
557    $template->assign_block_vars(
558      'favorite',
559      array(
560        'FAVORITE_IMG'  => get_root_url().get_themeconf('icon_dir').'/favorite.png',
561        'FAVORITE_HINT' => $lang['add_favorites_hint'],
562        'FAVORITE_ALT'  => $lang['add_favorites_alt'],
563        'U_FAVORITE'    => add_url_params(
564                              $url_self,
565                              array('action'=>'add_to_favorites')
566                           ),
567        )
568      );
569  }
570  else
571  {
572    $template->assign_block_vars(
573      'favorite',
574      array(
575        'FAVORITE_IMG'  => get_root_url().get_themeconf('icon_dir').'/del_favorite.png',
576        'FAVORITE_HINT' => $lang['del_favorites_hint'],
577        'FAVORITE_ALT'  => $lang['del_favorites_alt'],
578        'U_FAVORITE'    => add_url_params(
579                              $url_self,
580                              array('action'=>'remove_from_favorites')
581                           )
582        )
583      );
584  }
585}
586//------------------------------------ admin link for information modifications
587if ( is_admin() )
588{
589  $template->assign_block_vars('admin', array());
590}
591
592//--------------------------------------------------------- picture information
593// legend
594if (isset($picture['current']['comment'])
595    and !empty($picture['current']['comment']))
596{
597  $template->assign_block_vars(
598    'legend',
599    array(
600      'COMMENT_IMG' => nl2br($picture['current']['comment'])
601      ));
602}
603
604$infos = array();
605
606// author
607if (!empty($picture['current']['author']))
608{
609  $infos['INFO_AUTHOR'] =
610    // FIXME because of search engine partial rewrite, giving the author
611    // name threw GET is not supported anymore. This feature should come
612    // back later, with a better design
613//     '<a href="'.
614//       PHPWG_ROOT_PATH.'category.php?cat=search'.
615//       '&amp;search=author:'.$picture['current']['author']
616//       .'">'.$picture['current']['author'].'</a>';
617    $picture['current']['author'];
618}
619else
620{
621  $infos['INFO_AUTHOR'] = l10n('N/A');
622}
623
624// creation date
625if (!empty($picture['current']['date_creation']))
626{
627  $val = format_date($picture['current']['date_creation']);
628  $url = make_index_url(
629        array(
630          'chronology_field'=>'created',
631          'chronology_style'=>'monthly',
632          'chronology_view'=>'list',
633          'chronology_date' => explode('-', $picture['current']['date_creation'])
634        )
635      );
636  $infos['INFO_CREATION_DATE'] = '<a href="'.$url.'" rel="nofollow">'.$val.'</a>';
637}
638else
639{
640  $infos['INFO_CREATION_DATE'] = l10n('N/A');
641}
642
643// date of availability
644$val = format_date($picture['current']['date_available'], 'mysql_datetime');
645$url = make_index_url(
646      array(
647        'chronology_field'=>'posted',
648        'chronology_style'=>'monthly',
649        'chronology_view'=>'list',
650        'chronology_date'=>explode('-', substr($picture['current']['date_available'],0,10))
651      )
652    );
653$infos['INFO_POSTED_DATE'] = '<a href="'.$url.'" rel="nofollow">'.$val.'</a>';
654
655// size in pixels
656if ($picture['current']['is_picture'] and isset($picture['current']['width']) )
657{
658  if ($picture['current']['scaled_width'] !== $picture['current']['width'] )
659  {
660    $infos['INFO_DIMENSIONS'] =
661      '<a href="'.$picture['current']['image_url'].'" title="'.
662      l10n('Original dimensions').'">'.
663      $picture['current']['width'].'*'.$picture['current']['height'].'</a>';
664  }
665  else
666  {
667    $infos['INFO_DIMENSIONS'] =
668      $picture['current']['width'].'*'.$picture['current']['height'];
669  }
670}
671else
672{
673  $infos['INFO_DIMENSIONS'] = l10n('N/A');
674}
675
676// filesize
677if (!empty($picture['current']['filesize']))
678{
679  $infos['INFO_FILESIZE'] =
680    sprintf(l10n('%d Kb'), $picture['current']['filesize']);
681}
682else
683{
684  $infos['INFO_FILESIZE'] = l10n('N/A');
685}
686
687// number of visits
688$infos['INFO_VISITS'] = $picture['current']['hit'];
689
690// file
691$infos['INFO_FILE'] = $picture['current']['file'];
692
693// tags
694$query = '
695SELECT id, name, url_name
696  FROM '.IMAGE_TAG_TABLE.'
697    INNER JOIN '.TAGS_TABLE.' ON tag_id = id
698  WHERE image_id = '.$page['image_id'].'
699;';
700$result = pwg_query($query);
701
702if (mysql_num_rows($result) > 0)
703{
704  $tags = array();
705
706  while ($row = mysql_fetch_array($result))
707  {
708    array_push(
709      $tags,
710      '<a href="'
711      .make_index_url(
712        array(
713          'tags' => array(
714            array(
715              'id' => $row['id'],
716              'url_name' => $row['url_name'],
717              ),
718            )
719          )
720        )
721      .'">'.$row['name'].'</a>'
722      );
723  }
724
725  $infos['INFO_TAGS'] = implode(', ', $tags);
726}
727else
728{
729  $infos['INFO_TAGS'] = l10n('N/A');
730}
731
732$template->assign_vars($infos);
733
734// related categories
735foreach ($related_categories as $category)
736{
737  $template->assign_block_vars(
738    'category',
739    array(
740      'LINE' => count($related_categories) > 3
741        ? get_cat_display_name_cache($category['uppercats'])
742        : get_cat_display_name_from_id($category['category_id'])
743      )
744    );
745}
746
747//slideshow end
748if (isset($_GET['slideshow']))
749{
750  if (!is_numeric($_GET['slideshow']))
751  {
752    $_GET['slideshow'] = $conf['slideshow_period'];
753  }
754
755  $template->assign_block_vars(
756    'stop_slideshow',
757    array(
758      'U_SLIDESHOW' => $picture['current']['url'],
759      )
760    );
761}
762
763// +-----------------------------------------------------------------------+
764// |                               sub pages                               |
765// +-----------------------------------------------------------------------+
766
767include(PHPWG_ROOT_PATH.'include/picture_rate.inc.php');
768include(PHPWG_ROOT_PATH.'include/picture_comment.inc.php');
769if ($metadata_showable and isset($_GET['metadata']))
770{
771  include(PHPWG_ROOT_PATH.'include/picture_metadata.inc.php');
772}
773//------------------------------------------------------------ log informations
774pwg_log('picture', $page['title'], $picture['current']['file']);
775
776$template->parse('picture');
777include(PHPWG_ROOT_PATH.'include/page_tail.php');
778?>
Note: See TracBrowser for help on using the repository browser.