source: trunk/picture.php @ 1084

Last change on this file since 1084 was 1084, checked in by plg, 18 years ago

bug fixed: create_navigation_bar take into account clean URL if requested.

deletion: argument link_class (HTML class of links) in function
create_navigation_bar was removed, useless since branch 1.5.

bug fixed: rate_items are now a configuration parameter (set in config file)

modification: new functions library functions_rate.inc.php to reduce
picture.php length.

bug fixed: categories were never expanded in the menu since clean URLs.

bug fixed: changing pictures sorting order in main page was always
rederecting to root category.

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