source: trunk/picture.php @ 1113

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

fix: php 5 errors and warnings

improve: put back update remote site from local listing.xml functionality
(allow_url_fopen is false on several ISPs)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.5 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-03-28 01:26:37 +0000 (Tue, 28 Mar 2006) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1107 $
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_img = $picture['current']['name'];
374if ( isset( $page['category'] ) )
375{
376  if (is_numeric( $page['category'] ))
377  {
378    $title_img = replace_space(get_cat_display_name($page['cat_name']));
379  }
380  else if ( $page['cat'] == 'search' )
381  { // ??? TODO -remove or change some remainings from old variables
382    $title_img = replace_search( $title_img, $_GET['search'] );
383  }
384}
385$title_nb = ($page['current_rank'] + 1).'/'.$page['cat_nb_images'];
386
387// calculation of width and height
388if (empty($picture['current']['width']))
389{
390  $taille_image = @getimagesize($picture['current']['src_file_system']);
391  $original_width = $taille_image[0];
392  $original_height = $taille_image[1];
393}
394else
395{
396  $original_width = $picture['current']['width'];
397  $original_height = $picture['current']['height'];
398}
399
400$picture_size = get_picture_size(
401  $original_width,
402  $original_height,
403  @$user['maxwidth'],
404  @$user['maxheight']
405  );
406
407// metadata
408$url_metadata = duplicate_picture_URL();
409if ($conf['show_exif'] or $conf['show_iptc'])
410{
411  $metadata_showable = true;
412  if ( !isset($_GET['metadata']) )
413  {
414    $url_metadata = add_url_params( $url_metadata, array('metadata'=>null) );
415  }
416}
417else
418{
419  $metadata_showable = false;
420}
421
422$page['body_id'] = 'thePicturePage';
423//------------------------------------------------------- navigation management
424foreach ( array('first','previous','next','last') as $which_image )
425{
426  if (isset($picture[$which_image]))
427  {
428    $template->assign_block_vars(
429      $which_image,
430      array(
431        'TITLE_IMG' => $picture[$which_image]['name'],
432        'IMG' => $picture[$which_image]['thumbnail'],
433        'U_IMG' => $picture[$which_image]['url'],
434        'U_IMG_SRC' => $picture[$which_image]['src']
435        )
436      );
437  }
438}
439
440include(PHPWG_ROOT_PATH.'include/page_header.php');
441$template->set_filenames(array('picture'=>'picture.tpl'));
442
443$template->assign_vars(
444  array(
445    'CATEGORY' => $title_img,
446    'PHOTO' => $title_nb,
447    'TITLE' => $picture['current']['name'],
448    'SRC_IMG' => $picture['current']['src'],
449    'ALT_IMG' => $picture['current']['file'],
450    'WIDTH_IMG' => $picture_size[0],
451    'HEIGHT_IMG' => $picture_size[1],
452
453    'LEVEL_SEPARATOR' => $conf['level_separator'],
454
455    'L_HOME' => $lang['home'],
456    'L_SLIDESHOW' => $lang['slideshow'],
457    'L_STOP_SLIDESHOW' => $lang['slideshow_stop'],
458    'L_PREV_IMG' =>$lang['previous_page'].' : ',
459    'L_NEXT_IMG' =>$lang['next_page'].' : ',
460    'L_ADMIN' =>$lang['link_info_image'],
461    'L_COMMENT_TITLE' =>$lang['comments_title'],
462    'L_ADD_COMMENT' =>$lang['comments_add'],
463    'L_DELETE_COMMENT' =>$lang['comments_del'],
464    'L_DELETE' =>$lang['delete'],
465    'L_SUBMIT' =>$lang['submit'],
466    'L_AUTHOR' =>  $lang['upload_author'],
467    'L_COMMENT' =>$lang['comment'],
468    'L_DOWNLOAD' => $lang['download'],
469    'L_DOWNLOAD_HINT' => $lang['download_hint'],
470    'L_PICTURE_METADATA' => $lang['picture_show_metadata'],
471    'L_PICTURE_HIGH' => $lang['picture_high'],
472    'L_UP_HINT' => $lang['home_hint'],
473    'L_UP_ALT' => $lang['home'],
474
475    'U_HOME' => make_index_URL(),
476    'U_UP' => $url_up,
477    'U_METADATA' => $url_metadata,
478    'U_ADMIN' => $url_admin,
479    'U_SLIDESHOW'=> $url_slide,
480    'U_ADD_COMMENT' => $url_self,
481    )
482  );
483
484if ($conf['show_picture_name_on_title'])
485{
486  $template->assign_block_vars('title', array());
487}
488
489//------------------------------------------------------- upper menu management
490
491// download link if file is not a picture
492if (!$picture['current']['is_picture'])
493{
494  $template->assign_block_vars(
495    'download',
496    array(
497      'U_DOWNLOAD' => $picture['current']['download']
498      )
499    );
500}
501
502// display a high quality link if present
503if (isset($picture['current']['high']))
504{
505  $uuid = uniqid(rand());
506
507  $template->assign_block_vars(
508    'high',
509    array(
510      'U_HIGH' => $picture['current']['high'],
511      'UUID'   => $uuid,
512      )
513    );
514
515  $template->assign_block_vars(
516    'download',
517    array(
518      'U_DOWNLOAD' => get_root_url().'action.php?dwn='
519      .$picture['current']['high_file_system']
520      )
521    );
522}
523
524// button to set the current picture as representative
525if (is_admin() and isset($page['category']))
526{
527  $template->assign_block_vars(
528    'representative',
529    array(
530      'URL' => add_url_params($url_self,
531                  array('action'=>'set_as_representative')
532               )
533      )
534    );
535}
536
537// caddie button
538if (is_admin())
539{
540  $template->assign_block_vars(
541    'caddie',
542    array(
543      'URL' => add_url_params($url_self,
544                  array('action'=>'add_to_caddie')
545               )
546      )
547    );
548}
549
550// favorite manipulation
551if (!$user['is_the_guest'])
552{
553  // verify if the picture is already in the favorite of the user
554  $query = '
555SELECT COUNT(*) AS nb_fav
556  FROM '.FAVORITES_TABLE.'
557  WHERE image_id = '.$page['image_id'].'
558    AND user_id = '.$user['id'].'
559;';
560  $result = pwg_query($query);
561  $row = mysql_fetch_array($result);
562
563  if ($row['nb_fav'] == 0)
564  {
565    $template->assign_block_vars(
566      'favorite',
567      array(
568        'FAVORITE_IMG'  => get_root_url().get_themeconf('icon_dir').'/favorite.png',
569        'FAVORITE_HINT' => $lang['add_favorites_hint'],
570        'FAVORITE_ALT'  => $lang['add_favorites_alt'],
571        'U_FAVORITE'    => add_url_params(
572                              $url_self,
573                              array('action'=>'add_to_favorites')
574                           ),
575        )
576      );
577  }
578  else
579  {
580    $template->assign_block_vars(
581      'favorite',
582      array(
583        'FAVORITE_IMG'  => get_root_url().get_themeconf('icon_dir').'/del_favorite.png',
584        'FAVORITE_HINT' => $lang['del_favorites_hint'],
585        'FAVORITE_ALT'  => $lang['del_favorites_alt'],
586        'U_FAVORITE'    => add_url_params(
587                              $url_self,
588                              array('action'=>'remove_from_favorites')
589                           )
590        )
591      );
592  }
593}
594//------------------------------------ admin link for information modifications
595if ( is_admin() )
596{
597  $template->assign_block_vars('admin', array());
598}
599
600//--------------------------------------------------------- picture information
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}
611
612$infos = array();
613
614// author
615if (!empty($picture['current']['author']))
616{
617  $infos['INFO_AUTHOR'] =
618    // FIXME because of search engine partial rewrite, giving the author
619    // name threw GET is not supported anymore. This feature should come
620    // back later, with a better design
621//     '<a href="'.
622//       PHPWG_ROOT_PATH.'category.php?cat=search'.
623//       '&amp;search=author:'.$picture['current']['author']
624//       .'">'.$picture['current']['author'].'</a>';
625    $picture['current']['author'];
626}
627else
628{
629  $infos['INFO_AUTHOR'] = l10n('N/A');
630}
631
632// creation date
633if (!empty($picture['current']['date_creation']))
634{
635  $val = format_date($picture['current']['date_creation']);
636  $url = make_index_URL(
637        array(
638          'chronology_field'=>'created',
639          'chronology_style'=>'monthly',
640          'chronology_view'=>'list',
641          'chronology_date' => explode('-', $picture['current']['date_creation'])
642        )
643      );
644  $infos['INFO_CREATION_DATE'] = '<a href="'.$url.'">'.$val.'</a>';
645}
646else
647{
648  $infos['INFO_CREATION_DATE'] = l10n('N/A');
649}
650
651// date of availability
652$val = format_date($picture['current']['date_available'], 'mysql_datetime');
653$url = make_index_URL(
654      array(
655        'chronology_field'=>'posted',
656        'chronology_style'=>'monthly',
657        'chronology_view'=>'list',
658        'chronology_date'=>explode('-', substr($picture['current']['date_available'],0,10))
659      )
660    );
661$infos['INFO_POSTED_DATE'] = '<a href="'.$url.'">'.$val.'</a>';
662
663// size in pixels
664if ($picture['current']['is_picture'])
665{
666  if ($original_width != $picture_size[0]
667      or $original_height != $picture_size[1])
668  {
669    $infos['INFO_DIMENSIONS'] =
670      '<a href="'.$picture['current']['src'].'" title="'.
671      l10n('Original dimensions').'">'.
672      $original_width.'*'.$original_height.'</a>';
673  }
674  else
675  {
676    $infos['INFO_DIMENSIONS'] = $original_width.'*'.$original_height;
677  }
678}
679else
680{
681  $infos['INFO_DIMENSIONS'] = l10n('N/A');
682}
683
684// filesize
685if (!empty($picture['current']['filesize']))
686{
687  $infos['INFO_FILESIZE'] =
688    sprintf(l10n('%d Kb'), $picture['current']['filesize']);
689}
690else
691{
692  $infos['INFO_FILESIZE'] = l10n('N/A');
693}
694
695// number of visits
696$infos['INFO_VISITS'] = $picture['current']['hit'];
697
698// file
699$infos['INFO_FILE'] = $picture['current']['file'];
700
701// keywords
702if (!empty($picture['current']['keywords']))
703{
704  $infos['INFO_KEYWORDS'] =
705    // FIXME because of search engine partial rewrite, giving the author
706    // name threw GET is not supported anymore. This feature should come
707    // back later, with a better design (tag classification).
708//     preg_replace(
709//       '/([^,]+)/',
710//       '<a href="'.
711//         PHPWG_ROOT_PATH.'category.php?cat=search&amp;search=keywords:$1'
712//         .'">$1</a>',
713//       $picture['current']['keywords']
714//       );
715    $picture['current']['keywords'];
716}
717else
718{
719  $infos['INFO_KEYWORDS'] = l10n('N/A');
720}
721
722$template->assign_vars($infos);
723
724// related categories
725foreach ($related_categories as $category)
726{
727  $template->assign_block_vars(
728    'category',
729    array(
730      'LINE' => count($related_categories) > 3
731        ? get_cat_display_name_cache($category['uppercats'])
732        : get_cat_display_name_from_id($category['category_id'])
733      )
734    );
735}
736
737//slideshow end
738if (isset($_GET['slideshow']))
739{
740  if (!is_numeric($_GET['slideshow']))
741  {
742    $_GET['slideshow'] = $conf['slideshow_period'];
743  }
744
745  $template->assign_block_vars(
746    'stop_slideshow',
747    array(
748      'U_SLIDESHOW' => $picture['current']['url'],
749      )
750    );
751}
752
753// +-----------------------------------------------------------------------+
754// |                               sub pages                               |
755// +-----------------------------------------------------------------------+
756
757include(PHPWG_ROOT_PATH.'include/picture_rate.inc.php');
758include(PHPWG_ROOT_PATH.'include/picture_comment.inc.php');
759if ($metadata_showable and isset($_GET['metadata']))
760{
761  include(PHPWG_ROOT_PATH.'include/picture_metadata.inc.php');
762}
763//------------------------------------------------------------ log informations
764pwg_log( 'picture', $title_img, $picture['current']['file'] );
765
766$template->parse('picture');
767include(PHPWG_ROOT_PATH.'include/page_tail.php');
768?>
Note: See TracBrowser for help on using the repository browser.