source: trunk/picture.php @ 1690

Last change on this file since 1690 was 1682, checked in by rub, 18 years ago

Fix Feature Issue ID 0000585.

Convergence of exif configuration between local site and remote site.

Notes added on administration page where configuration is not OK.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.0 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-12-28 00:06:06 +0000 (Thu, 28 Dec 2006) $
10// | last modifier : $Author: rub $
11// | revision      : $Revision: 1682 $
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}
431
432$title_nb = ($page['current_rank'] + 1).'/'.$page['cat_nb_images'];
433
434// metadata
435$url_metadata = duplicate_picture_url();
436
437// do we have a plugin that can show metadata for something else than images?
438$metadata_showable = trigger_event('get_element_metadata_available',
439    (
440      ($conf['show_exif'] or $conf['show_iptc'])
441      and isset($picture['current']['image_path'])
442    ),
443    $picture['current']['path'] );
444if ($metadata_showable)
445{
446  if ( !isset($_GET['metadata']) )
447  {
448    $url_metadata = add_url_params( $url_metadata, array('metadata'=>null) );
449  }
450}
451
452$page['body_id'] = 'thePicturePage';
453
454// maybe someone wants a special display (call it before page_header so that they
455// can add stylesheets)
456$element_content = trigger_event('render_element_content',
457                      '', $picture['current'] );
458
459if ( isset($picture['next']['image_url'])
460      and isset($picture['next']['is_picture']) )
461{
462  $template->assign_block_vars( 'prefetch',
463    array (
464      'URL' => $picture['next']['image_url']
465    )
466  );
467}
468$template->set_filenames(array('picture'=>'picture.tpl'));
469
470//------------------------------------------------------- navigation management
471foreach ( array('first','previous','next','last') as $which_image )
472{
473  if (isset($picture[$which_image]))
474  {
475    $template->assign_block_vars(
476      $which_image,
477      array(
478        'TITLE_IMG' => $picture[$which_image]['name'],
479        'IMG' => $picture[$which_image]['thumbnail'],
480        'U_IMG' => $picture[$which_image]['url'],
481        )
482      );
483  }
484}
485
486$template->assign_vars(
487  array(
488    'SECTION_TITLE' => $page['title'],
489    'PICTURE_TITLE' => $picture['current']['name'],
490    'PHOTO' => $title_nb,
491    'TITLE' => $picture['current']['name'],
492    'ELEMENT_CONTENT' => $element_content,
493
494    'LEVEL_SEPARATOR' => $conf['level_separator'],
495
496    'U_HOME' => make_index_url(),
497    'U_UP' => $url_up,
498    'U_METADATA' => $url_metadata,
499    'U_ADMIN' => $url_admin,
500    'U_SLIDESHOW'=> $url_slide,
501    'U_ADD_COMMENT' => $url_self,
502    )
503  );
504
505if ($conf['show_picture_name_on_title'])
506{
507  $template->assign_block_vars('title', array());
508}
509
510//------------------------------------------------------- upper menu management
511
512// download link
513if ( isset($picture['current']['download_url']) )
514{
515  $template->assign_block_vars(
516    'download',
517    array(
518      'U_DOWNLOAD' => $picture['current']['download_url']
519      )
520    );
521}
522
523// button to set the current picture as representative
524if (is_admin() and isset($page['category']))
525{
526  $template->assign_block_vars(
527    'representative',
528    array(
529      'URL' => add_url_params($url_self,
530                  array('action'=>'set_as_representative')
531               )
532      )
533    );
534}
535
536// caddie button
537if (is_admin())
538{
539  $template->assign_block_vars(
540    'caddie',
541    array(
542      'URL' => add_url_params($url_self,
543                  array('action'=>'add_to_caddie')
544               )
545      )
546    );
547}
548
549// favorite manipulation
550if (!$user['is_the_guest'])
551{
552  // verify if the picture is already in the favorite of the user
553  $query = '
554SELECT COUNT(*) AS nb_fav
555  FROM '.FAVORITES_TABLE.'
556  WHERE image_id = '.$page['image_id'].'
557    AND user_id = '.$user['id'].'
558;';
559  $result = pwg_query($query);
560  $row = mysql_fetch_array($result);
561
562  if ($row['nb_fav'] == 0)
563  {
564    $template->assign_block_vars(
565      'favorite',
566      array(
567        'FAVORITE_IMG'  => get_root_url().get_themeconf('icon_dir').'/favorite.png',
568        'FAVORITE_HINT' => $lang['add_favorites_hint'],
569        'FAVORITE_ALT'  => $lang['add_favorites_alt'],
570        'U_FAVORITE'    => add_url_params(
571                              $url_self,
572                              array('action'=>'add_to_favorites')
573                           ),
574        )
575      );
576  }
577  else
578  {
579    $template->assign_block_vars(
580      'favorite',
581      array(
582        'FAVORITE_IMG'  => get_root_url().get_themeconf('icon_dir').'/del_favorite.png',
583        'FAVORITE_HINT' => $lang['del_favorites_hint'],
584        'FAVORITE_ALT'  => $lang['del_favorites_alt'],
585        'U_FAVORITE'    => add_url_params(
586                              $url_self,
587                              array('action'=>'remove_from_favorites')
588                           )
589        )
590      );
591  }
592}
593//------------------------------------ admin link for information modifications
594if ( is_admin() )
595{
596  $template->assign_block_vars('admin', array());
597}
598
599//--------------------------------------------------------- picture information
600$header_infos = array();        //for html header use
601// legend
602if (isset($picture['current']['comment'])
603    and !empty($picture['current']['comment']))
604{
605  $template->assign_block_vars(
606    'legend',
607    array(
608      'COMMENT_IMG' => nl2br($picture['current']['comment'])
609      ));
610  $header_infos['COMMENT'] = strip_tags($picture['current']['comment']);
611}
612
613$infos = array();
614
615// author
616if (!empty($picture['current']['author']))
617{
618  $infos['INFO_AUTHOR'] =
619    // FIXME because of search engine partial rewrite, giving the author
620    // name threw GET is not supported anymore. This feature should come
621    // back later, with a better design
622//     '<a href="'.
623//       PHPWG_ROOT_PATH.'category.php?cat=search'.
624//       '&amp;search=author:'.$picture['current']['author']
625//       .'">'.$picture['current']['author'].'</a>';
626    $picture['current']['author'];
627  $header_infos['INFO_AUTHOR'] = $picture['current']['author'];
628}
629else
630{
631  $infos['INFO_AUTHOR'] = l10n('N/A');
632}
633
634// creation date
635if (!empty($picture['current']['date_creation']))
636{
637  $val = format_date($picture['current']['date_creation']);
638  $url = make_index_url(
639        array(
640          'chronology_field'=>'created',
641          'chronology_style'=>'monthly',
642          'chronology_view'=>'list',
643          'chronology_date' => explode('-', $picture['current']['date_creation'])
644        )
645      );
646  $infos['INFO_CREATION_DATE'] = '<a href="'.$url.'" rel="nofollow">'.$val.'</a>';
647}
648else
649{
650  $infos['INFO_CREATION_DATE'] = l10n('N/A');
651}
652
653// date of availability
654$val = format_date($picture['current']['date_available'], 'mysql_datetime');
655$url = make_index_url(
656      array(
657        'chronology_field'=>'posted',
658        'chronology_style'=>'monthly',
659        'chronology_view'=>'list',
660        'chronology_date'=>explode('-', substr($picture['current']['date_available'],0,10))
661      )
662    );
663$infos['INFO_POSTED_DATE'] = '<a href="'.$url.'" rel="nofollow">'.$val.'</a>';
664
665// size in pixels
666if ($picture['current']['is_picture'] and isset($picture['current']['width']) )
667{
668  if ($picture['current']['scaled_width'] !== $picture['current']['width'] )
669  {
670    $infos['INFO_DIMENSIONS'] =
671      '<a href="'.$picture['current']['image_url'].'" title="'.
672      l10n('Original dimensions').'">'.
673      $picture['current']['width'].'*'.$picture['current']['height'].'</a>';
674  }
675  else
676  {
677    $infos['INFO_DIMENSIONS'] =
678      $picture['current']['width'].'*'.$picture['current']['height'];
679  }
680}
681else
682{
683  $infos['INFO_DIMENSIONS'] = l10n('N/A');
684}
685
686// filesize
687if (!empty($picture['current']['filesize']))
688{
689  $infos['INFO_FILESIZE'] =
690    sprintf(l10n('%d Kb'), $picture['current']['filesize']);
691}
692else
693{
694  $infos['INFO_FILESIZE'] = l10n('N/A');
695}
696
697// number of visits
698$infos['INFO_VISITS'] = $picture['current']['hit'];
699
700// file
701$infos['INFO_FILE'] = $picture['current']['file'];
702
703// tags
704$query = '
705SELECT id, name, url_name
706  FROM '.IMAGE_TAG_TABLE.'
707    INNER JOIN '.TAGS_TABLE.' ON tag_id = id
708  WHERE image_id = '.$page['image_id'].'
709;';
710$result = pwg_query($query);
711
712if (mysql_num_rows($result) > 0)
713{
714  $tags = array();
715  $tag_names = array();
716
717  while ($row = mysql_fetch_array($result))
718  {
719    array_push(
720      $tags,
721      '<a href="'
722      .make_index_url(
723        array(
724          'tags' => array(
725            array(
726              'id' => $row['id'],
727              'url_name' => $row['url_name'],
728              ),
729            )
730          )
731        )
732      .'">'.$row['name'].'</a>'
733      );
734    array_push( $tag_names, $row['name'] );
735  }
736
737  $infos['INFO_TAGS'] = implode(', ', $tags);
738  $header_infos['INFO_TAGS'] = implode(', ', $tag_names);
739}
740else
741{
742  $infos['INFO_TAGS'] = l10n('N/A');
743}
744
745$template->assign_vars($infos);
746
747// related categories
748foreach ($related_categories as $category)
749{
750  $template->assign_block_vars(
751    'category',
752    array(
753      'LINE' => count($related_categories) > 3
754        ? get_cat_display_name_cache($category['uppercats'])
755        : get_cat_display_name_from_id($category['category_id'])
756      )
757    );
758}
759
760//slideshow end
761if (isset($_GET['slideshow']))
762{
763  if (!is_numeric($_GET['slideshow']))
764  {
765    $_GET['slideshow'] = $conf['slideshow_period'];
766  }
767
768  $template->assign_block_vars(
769    'stop_slideshow',
770    array(
771      'U_SLIDESHOW' => $picture['current']['url'],
772      )
773    );
774}
775
776// +-----------------------------------------------------------------------+
777// |                               sub pages                               |
778// +-----------------------------------------------------------------------+
779
780include(PHPWG_ROOT_PATH.'include/picture_rate.inc.php');
781include(PHPWG_ROOT_PATH.'include/picture_comment.inc.php');
782//if ($metadata_showable and isset($_GET['metadata']))
783{
784  include(PHPWG_ROOT_PATH.'include/picture_metadata.inc.php');
785}
786//------------------------------------------------------------ log informations
787pwg_log('picture', $page['title'], $picture['current']['file']);
788
789include(PHPWG_ROOT_PATH.'include/page_header.php');
790$template->parse('picture');
791include(PHPWG_ROOT_PATH.'include/page_tail.php');
792?>
Note: See TracBrowser for help on using the repository browser.