source: trunk/picture.php @ 1135

Last change on this file since 1135 was 1135, checked in by rvelices, 18 years ago

merge -r1134 from branches/branch-1_6 into trunk

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