source: trunk/picture.php @ 1092

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

URL rewriting: capable of fully working with urls without ?

URL rewriting: works with image file instead of image id (change
make_picture_url to generate urls with file name instead of image id)

URL rewriting: completely works with category/best_rated and
picture/best_rated/534 (change 'category.php?' to 'category' in make_index_url
and 'picture.php?' to 'picture' in make_picture_url to see it)

fix: picture category display in upper bar

fix: function rate_picture variables and use of the new user type

fix: caddie icon appears now on category page

fix: admin element_set sql query was using storage_category_id column
(column has moved to #image_categories)

fix: replaced some old $_GET[xxx] with $page[xxx]

fix: pictures have metadata url (use ? parameter - might change later)

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