source: trunk/picture.php @ 1086

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

URL rewrite for chronology: uses $pagechronology and
$pagechronology_date. $pagechronology is an array with 'field',
'style' and 'view' keys. This is step 1.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.9 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-17 04:13:19 +0000 (Fri, 17 Mar 2006) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1086 $
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  // 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']))
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']) and !is_adviser())
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      if (!is_adviser())
163      {
164        fill_caddie(array($page['image_id']));
165      }
166      redirect($url_self);
167      break;
168    }
169    case 'rate' :
170    {
171      rate_picture($user['id'], $page['image_id'], $_GET['rate']);
172      redirect($url_self);
173    }
174    case 'delete_comment' :
175    {
176      if (isset($_GET['comment_to_delete'])
177          and is_numeric($_GET['comment_to_delete'])
178          and is_admin())
179      {
180        $query = '
181DELETE FROM '.COMMENTS_TABLE.'
182  WHERE id = '.$_GET['comment_to_delete'].'
183;';
184        pwg_query( $query );
185      }
186
187      redirect($url_self);
188    }
189  }
190}
191
192// incrementation of the number of hits, we do this only if no action
193$query = '
194UPDATE
195  '.IMAGES_TABLE.'
196  SET hit = hit+1
197  WHERE id = '.$page['image_id'].'
198;';
199pwg_query($query);
200
201//---------------------------------------------------------- related categories
202$query = '
203SELECT category_id,uppercats,commentable,global_rank
204  FROM '.IMAGE_CATEGORY_TABLE.'
205    INNER JOIN '.CATEGORIES_TABLE.' ON category_id = id
206  WHERE image_id = '.$page['image_id'].'
207    AND category_id NOT IN ('.$user['forbidden_categories'].')
208;';
209$result = pwg_query($query);
210$related_categories = array();
211while ($row = mysql_fetch_array($result))
212{
213  array_push($related_categories, $row);
214}
215usort($related_categories, 'global_rank_compare');
216//-------------------------first, prev, current, next & last picture management
217$picture = array();
218
219$ids = array($page['image_id']);
220if (isset($page['previous_item']))
221{
222  array_push($ids, $page['previous_item']);
223  array_push($ids, $page['first_item']);
224}
225if (isset($page['next_item']))
226{
227  array_push($ids, $page['next_item']);
228  array_push($ids, $page['last_item']);
229}
230
231$query = '
232SELECT *
233  FROM '.IMAGES_TABLE.'
234  WHERE id IN ('.implode(',', $ids).')
235;';
236
237$result = pwg_query($query);
238
239while ($row = mysql_fetch_array($result))
240{
241  if (isset($page['previous_item']) and $row['id'] == $page['previous_item'])
242  {
243    $i = 'previous';
244  }
245  else if (isset($page['next_item']) and $row['id'] == $page['next_item'])
246  {
247    $i = 'next';
248  }
249  else if (isset($page['first_item']) and $row['id'] == $page['first_item'])
250  {
251    $i = 'first';
252  }
253  else if (isset($page['last_item']) and $row['id'] == $page['last_item'])
254  {
255    $i = 'last';
256  }
257  else
258  {
259    $i = 'current';
260  }
261
262  foreach (array_keys($row) as $key)
263  {
264    if (!is_numeric($key))
265    {
266      $picture[$i][$key] = $row[$key];
267    }
268  }
269
270  $picture[$i]['is_picture'] = false;
271  if (in_array(get_extension($row['file']), $conf['picture_ext']))
272  {
273    $picture[$i]['is_picture'] = true;
274  }
275
276  $cat_directory = dirname($row['path']);
277  $file_wo_ext = get_filename_wo_extension($row['file']);
278
279  $icon = get_themeconf('mime_icon_dir');
280  $icon.= strtolower(get_extension($row['file'])).'.png';
281
282  if (isset($row['representative_ext']) and $row['representative_ext'] != '')
283  {
284    $picture[$i]['src'] =
285      $cat_directory.'/pwg_representative/'
286      .$file_wo_ext.'.'.$row['representative_ext'];
287  }
288  else
289  {
290    $picture[$i]['src'] = $icon;
291  }
292  // special case for picture files
293  if ($picture[$i]['is_picture'])
294  {
295    $picture[$i]['src'] = $row['path'];
296    // if we are working on the "current" element, we search if there is a
297    // high quality picture
298    if ($i == 'current')
299    {
300      if (($row['has_high'] == 'true') and ($user['enabled_high'] == 'true'))
301      {
302        $url_high=$cat_directory.'/pwg_high/'.$row['file'];
303        $picture[$i]['high'] = $url_high;
304      }
305    }
306  }
307
308  // if picture is not a file, we need the download link
309  if (!$picture[$i]['is_picture'])
310  {
311    $picture[$i]['download'] = $row['path'];
312  }
313
314  $picture[$i]['thumbnail'] = get_thumbnail_src($row['path'], @$row['tn_ext']);
315
316  if ( !empty( $row['name'] ) )
317  {
318    $picture[$i]['name'] = $row['name'];
319  }
320  else
321  {
322    $picture[$i]['name'] = str_replace('_', ' ', $file_wo_ext);
323  }
324
325  $picture[$i]['url'] = duplicate_picture_URL(
326    array(
327      'image_id' => $row['id'],
328      ),
329    array(
330      'start',
331      )
332    );
333
334  if ('previous'==$i and $page['previous_item']==$page['first_item'])
335  {
336    $picture['first'] = $picture[$i];
337  }
338  if ('next'==$i and $page['next_item']==$page['last_item'])
339  {
340    $picture['last'] = $picture[$i];
341  }
342}
343
344$url_admin =
345  PHPWG_ROOT_PATH.'admin.php?page=picture_modify'
346  .'&amp;cat_id='.(isset($page['category']) ? $page['category'] : '')
347  .'&amp;image_id='.$page['image_id']
348;
349
350$url_slide =
351  $picture['current']['url']
352  .'&amp;slideshow='.$conf['slideshow_period']
353;
354
355$title =  $picture['current']['name'];
356$refresh = 0;
357if ( isset( $_GET['slideshow'] ) and isset($page['next_item']) )
358{
359  $refresh= $_GET['slideshow'];
360  $url_link = $picture['next']['url'].'&amp;slideshow='.$refresh;
361}
362
363$title_img = $picture['current']['name'];
364if ( isset( $page['cat'] ) )
365{
366  if (is_numeric( $page['cat'] ))
367  {
368    $title_img = replace_space(get_cat_display_name($page['cat_name']));
369  }
370  else if ( $page['cat'] == 'search' )
371  {
372    $title_img = replace_search( $title_img, $_GET['search'] );
373  }
374}
375$title_nb = ($page['current_rank'] + 1).'/'.$page['cat_nb_images'];
376
377// calculation of width and height
378if (empty($picture['current']['width']))
379{
380  $taille_image = @getimagesize($picture['current']['src']);
381  $original_width = $taille_image[0];
382  $original_height = $taille_image[1];
383}
384else
385{
386  $original_width = $picture['current']['width'];
387  $original_height = $picture['current']['height'];
388}
389
390$picture_size = get_picture_size(
391  $original_width,
392  $original_height,
393  @$user['maxwidth'],
394  @$user['maxheight']
395  );
396
397// metadata
398if ($conf['show_exif'] or $conf['show_iptc'])
399{
400  $metadata_showable = true;
401}
402else
403{
404  $metadata_showable = false;
405}
406
407// $url_metadata = PHPWG_ROOT_PATH.'picture.php';
408// $url_metadata .=  get_query_string_diff(array('add_fav', 'slideshow', 'show_metadata'));
409// if ($metadata_showable and !isset($_GET['show_metadata']))
410// {
411//   $url_metadata.= '&amp;show_metadata=1';
412// }
413
414// TODO: rewrite metadata display to toggle on/off user_infos.show_metadata
415$url_metadata = duplicate_picture_URL();
416
417$page['body_id'] = 'thePicturePage';
418//------------------------------------------------------- navigation management
419foreach ( array('first','previous','next','last') as $which_image )
420{
421  if (isset($picture[$which_image]))
422  {
423    $template->assign_block_vars(
424      $which_image,
425      array(
426        'TITLE_IMG' => $picture[$which_image]['name'],
427        'IMG' => $picture[$which_image]['thumbnail'],
428        'U_IMG' => $picture[$which_image]['url'],
429        'U_IMG_SRC' => $picture[$which_image]['src']
430        )
431      );
432  }
433}
434
435include(PHPWG_ROOT_PATH.'include/page_header.php');
436$template->set_filenames(array('picture'=>'picture.tpl'));
437
438$template->assign_vars(
439  array(
440    'CATEGORY' => $title_img,
441    'PHOTO' => $title_nb,
442    'TITLE' => $picture['current']['name'],
443    'SRC_IMG' => $picture['current']['src'],
444    'ALT_IMG' => $picture['current']['file'],
445    'WIDTH_IMG' => $picture_size[0],
446    'HEIGHT_IMG' => $picture_size[1],
447
448    'LEVEL_SEPARATOR' => $conf['level_separator'],
449
450    'L_HOME' => $lang['home'],
451    'L_SLIDESHOW' => $lang['slideshow'],
452    'L_STOP_SLIDESHOW' => $lang['slideshow_stop'],
453    'L_PREV_IMG' =>$lang['previous_page'].' : ',
454    'L_NEXT_IMG' =>$lang['next_page'].' : ',
455    'L_ADMIN' =>$lang['link_info_image'],
456    'L_COMMENT_TITLE' =>$lang['comments_title'],
457    'L_ADD_COMMENT' =>$lang['comments_add'],
458    'L_DELETE_COMMENT' =>$lang['comments_del'],
459    'L_DELETE' =>$lang['delete'],
460    'L_SUBMIT' =>$lang['submit'],
461    'L_AUTHOR' =>  $lang['upload_author'],
462    'L_COMMENT' =>$lang['comment'],
463    'L_DOWNLOAD' => $lang['download'],
464    'L_DOWNLOAD_HINT' => $lang['download_hint'],
465    'L_PICTURE_METADATA' => $lang['picture_show_metadata'],
466    'L_PICTURE_HIGH' => $lang['picture_high'],
467    'L_UP_HINT' => $lang['home_hint'],
468    'L_UP_ALT' => $lang['home'],
469
470    'U_HOME' => make_index_URL(),
471    'U_UP' => $url_up,
472    'U_METADATA' => $url_metadata,
473    'U_ADMIN' => $url_admin,
474    'U_SLIDESHOW'=> $url_slide,
475    'U_ADD_COMMENT' => $url_self,
476    )
477  );
478
479if ($conf['show_picture_name_on_title'])
480{
481  $template->assign_block_vars('title', array());
482}
483
484//------------------------------------------------------- upper menu management
485
486// download link if file is not a picture
487if (!$picture['current']['is_picture'])
488{
489  $template->assign_block_vars(
490    'download',
491    array(
492      'U_DOWNLOAD' => $picture['current']['download']
493      )
494    );
495}
496
497// display a high quality link if present
498if (isset($picture['current']['high']))
499{
500  $uuid = uniqid(rand());
501
502  $template->assign_block_vars(
503    'high',
504    array(
505      'U_HIGH' => $picture['current']['high'],
506      'UUID'   => $uuid,
507      )
508    );
509
510  $template->assign_block_vars(
511    'download',
512    array(
513      'U_DOWNLOAD' => PHPWG_ROOT_PATH.'action.php?dwn='
514      .$picture['current']['high']
515      )
516    );
517}
518
519// button to set the current picture as representative
520if (is_admin() and isset($page['category']))
521{
522  $template->assign_block_vars(
523    'representative',
524    array(
525      'URL' => $url_self.'&amp;action=set_as_representative'
526      )
527    );
528}
529
530// caddie button
531if (is_admin())
532{
533  $template->assign_block_vars(
534    'caddie',
535    array(
536      'URL' => $url_self.'&amp;action=add_to_caddie'
537      )
538    );
539}
540
541// favorite manipulation
542if (!$user['is_the_guest'])
543{
544  // verify if the picture is already in the favorite of the user
545  $query = '
546SELECT COUNT(*) AS nb_fav
547  FROM '.FAVORITES_TABLE.'
548  WHERE image_id = '.$page['image_id'].'
549    AND user_id = '.$user['id'].'
550;';
551  $result = pwg_query($query);
552  $row = mysql_fetch_array($result);
553
554  if ($row['nb_fav'] == 0)
555  {
556    $url = $url_self.'&amp;action=add_to_favorites';
557
558    $template->assign_block_vars(
559      'favorite',
560      array(
561        'FAVORITE_IMG'  => get_themeconf('icon_dir').'/favorite.png',
562        'FAVORITE_HINT' => $lang['add_favorites_hint'],
563        'FAVORITE_ALT'  => $lang['add_favorites_alt'],
564        'U_FAVORITE'    => $url_self.'&amp;action=add_to_favorites',
565        )
566      );
567  }
568  else
569  {
570    $template->assign_block_vars(
571      'favorite',
572      array(
573        'FAVORITE_IMG'  => get_themeconf('icon_dir').'/del_favorite.png',
574        'FAVORITE_HINT' => $lang['del_favorites_hint'],
575        'FAVORITE_ALT'  => $lang['del_favorites_alt'],
576        'U_FAVORITE'    => $url_self.'&amp;action=remove_from_favorites',
577        )
578      );
579  }
580}
581//------------------------------------ admin link for information modifications
582if ( is_admin() )
583{
584  $template->assign_block_vars('admin', array());
585}
586
587//--------------------------------------------------------- picture information
588// legend
589if (isset($picture['current']['comment'])
590    and !empty($picture['current']['comment']))
591{
592  $template->assign_block_vars(
593    'legend',
594    array(
595      'COMMENT_IMG' => nl2br($picture['current']['comment'])
596      ));
597}
598
599$infos = array();
600
601// author
602if (!empty($picture['current']['author']))
603{
604  $infos['INFO_AUTHOR'] =
605    // FIXME because of search engine partial rewrite, giving the author
606    // name threw GET is not supported anymore. This feature should come
607    // back later, with a better design
608//     '<a href="'.
609//       PHPWG_ROOT_PATH.'category.php?cat=search'.
610//       '&amp;search=author:'.$picture['current']['author']
611//       .'">'.$picture['current']['author'].'</a>';
612    $picture['current']['author'];
613}
614else
615{
616  $infos['INFO_AUTHOR'] = l10n('N/A');
617}
618
619// creation date
620if (!empty($picture['current']['date_creation']))
621{
622  $val = format_date($picture['current']['date_creation']);
623  $url = make_index_URL(
624          array(
625            'chronology' =>
626              array(
627                'field'=>'created',
628                'style'=>'monthly',
629                'view'=>'list',
630              ),
631             'chronology_date' => explode('-', $picture['current']['date_creation'])
632           )
633         );
634  $infos['INFO_CREATION_DATE'] = '<a href="'.$url.'">'.$val.'</a>';
635}
636else
637{
638  $infos['INFO_CREATION_DATE'] = l10n('N/A');
639}
640
641// date of availability
642$val = format_date($picture['current']['date_available'], 'mysql_datetime');
643$url = make_index_URL(
644        array(
645          'chronology' =>
646            array(
647              'field'=>'posted',
648              'style'=>'monthly',
649              'view'=>'list',
650            ),
651           'chronology_date' => explode('-', substr($picture['current']['date_available'],0,10))
652         )
653       );
654$infos['INFO_POSTED_DATE'] = '<a href="'.$url.'">'.$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.