source: branches/branch-1_6-hk/picture.php @ 2276

Last change on this file since 2276 was 1629, checked in by chrisaga, 18 years ago

merge from trunk r1626:1627 : meta and links in the header

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