source: trunk/picture.php @ 1286

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

merge -r1285 in from branch-1_6 to trunk
bug 359: transform rating html A links to FORM because some bots rate
pictures (even if rel="nofollow" is set)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2006-04-28 04:38:36 +0000 (Fri, 28 Apr 2006) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1286 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28define('PHPWG_ROOT_PATH','./');
29include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
30include(PHPWG_ROOT_PATH.'include/section_init.inc.php');
31
32// Check Access and exit when user status is not ok
33check_status(ACCESS_GUEST);
34
35// access authorization check
36if (isset($page['category']))
37{
38  check_restrictions($page['category']);
39}
40
41// if this image_id doesn't correspond to this category, an error message is
42// displayed, and execution is stopped
43if (!in_array($page['image_id'], $page['items']))
44{
45  die('Fatal: this picture does not belong to this section');
46}
47
48// +-----------------------------------------------------------------------+
49// |                            initialization                             |
50// +-----------------------------------------------------------------------+
51
52$page['rank_of'] = array_flip($page['items']);
53
54// caching first_rank, last_rank, current_rank in the displayed
55// section. This should also help in readability.
56$page['first_rank']   = 0;
57$page['last_rank']    = count($page['items']) - 1;
58$page['current_rank'] = $page['rank_of'][ $page['image_id'] ];
59
60// caching current item : readability purpose
61$page['current_item'] = $page['image_id'];
62
63if ($page['current_rank'] != $page['first_rank'])
64{
65  // caching first & previous item : readability purpose
66  $page['previous_item'] = $page['items'][ $page['current_rank'] - 1 ];
67  $page['first_item'] = $page['items'][ $page['first_rank'] ];
68}
69
70if ($page['current_rank'] != $page['last_rank'])
71{
72  // caching next & last item : readability purpose
73  $page['next_item'] = $page['items'][ $page['current_rank'] + 1 ];
74  $page['last_item'] = $page['items'][ $page['last_rank'] ];
75}
76
77$url_up = duplicate_index_URL(
78  array(
79    'start' =>
80      floor($page['current_rank'] / $user['nb_image_page'])
81      * $user['nb_image_page']
82    ),
83  array(
84    'start',
85    )
86  );
87
88$url_self = duplicate_picture_URL();
89
90// +-----------------------------------------------------------------------+
91// |                                actions                                |
92// +-----------------------------------------------------------------------+
93
94/**
95 * Actions are favorite adding, user comment deletion, setting the picture
96 * as representative of the current category...
97 *
98 * Actions finish by a redirection
99 */
100
101if (isset($_GET['action']) and !is_adviser())
102{
103  switch ($_GET['action'])
104  {
105    case 'add_to_favorites' :
106    {
107      $query = '
108INSERT INTO '.FAVORITES_TABLE.'
109  (image_id,user_id)
110  VALUES
111  ('.$page['image_id'].','.$user['id'].')
112;';
113      pwg_query($query);
114
115      redirect($url_self);
116
117      break;
118    }
119    case 'remove_from_favorites' :
120    {
121      $query = '
122DELETE FROM '.FAVORITES_TABLE.'
123  WHERE user_id = '.$user['id'].'
124    AND image_id = '.$page['image_id'].'
125;';
126      pwg_query($query);
127
128      if ('favorites' == $page['section'])
129      {
130        redirect($url_up);
131      }
132      else
133      {
134        redirect($url_self);
135      }
136
137      break;
138    }
139    case 'set_as_representative' :
140    {
141      if (is_admin() and isset($page['category']))
142      {
143        $query = '
144UPDATE '.CATEGORIES_TABLE.'
145  SET representative_picture_id = '.$page['image_id'].'
146  WHERE id = '.$page['category'].'
147;';
148        pwg_query($query);
149      }
150
151      redirect($url_self);
152
153      break;
154    }
155    case 'toggle_metadata' :
156    {
157      break;
158    }
159    case 'add_to_caddie' :
160    {
161      fill_caddie(array($page['image_id']));
162      redirect($url_self);
163      break;
164    }
165    case 'rate' :
166    {
167      include_once(PHPWG_ROOT_PATH.'include/functions_rate.inc.php');
168      rate_picture($page['image_id'],
169          isset($_POST['rate']) ? $_POST['rate'] : $_GET['rate'] );
170      redirect($url_self);
171    }
172    case 'delete_comment' :
173    {
174      if (isset($_GET['comment_to_delete'])
175          and is_numeric($_GET['comment_to_delete'])
176          and is_admin())
177      {
178        $query = '
179DELETE FROM '.COMMENTS_TABLE.'
180  WHERE id = '.$_GET['comment_to_delete'].'
181;';
182        pwg_query( $query );
183      }
184
185      redirect($url_self);
186    }
187  }
188}
189
190// incrementation of the number of hits, we do this only if no action
191$query = '
192UPDATE
193  '.IMAGES_TABLE.'
194  SET hit = hit+1
195  WHERE id = '.$page['image_id'].'
196;';
197pwg_query($query);
198
199//---------------------------------------------------------- related categories
200$query = '
201SELECT category_id,uppercats,commentable,global_rank
202  FROM '.IMAGE_CATEGORY_TABLE.'
203    INNER JOIN '.CATEGORIES_TABLE.' ON category_id = id
204  WHERE image_id = '.$page['image_id'].'
205    AND category_id NOT IN ('.$user['forbidden_categories'].')
206;';
207$result = pwg_query($query);
208$related_categories = array();
209while ($row = mysql_fetch_array($result))
210{
211  array_push($related_categories, $row);
212}
213usort($related_categories, 'global_rank_compare');
214//-------------------------first, prev, current, next & last picture management
215$picture = array();
216
217$ids = array($page['image_id']);
218if (isset($page['previous_item']))
219{
220  array_push($ids, $page['previous_item']);
221  array_push($ids, $page['first_item']);
222}
223if (isset($page['next_item']))
224{
225  array_push($ids, $page['next_item']);
226  array_push($ids, $page['last_item']);
227}
228
229$query = '
230SELECT *
231  FROM '.IMAGES_TABLE.'
232  WHERE id IN ('.implode(',', $ids).')
233;';
234
235$result = pwg_query($query);
236
237while ($row = mysql_fetch_array($result))
238{
239  if (isset($page['previous_item']) and $row['id'] == $page['previous_item'])
240  {
241    $i = 'previous';
242  }
243  else if (isset($page['next_item']) and $row['id'] == $page['next_item'])
244  {
245    $i = 'next';
246  }
247  else if (isset($page['first_item']) and $row['id'] == $page['first_item'])
248  {
249    $i = 'first';
250  }
251  else if (isset($page['last_item']) and $row['id'] == $page['last_item'])
252  {
253    $i = 'last';
254  }
255  else
256  {
257    $i = 'current';
258  }
259
260  foreach (array_keys($row) as $key)
261  {
262    if (!is_numeric($key))
263    {
264      $picture[$i][$key] = $row[$key];
265    }
266  }
267
268  $picture[$i]['is_picture'] = false;
269  if (in_array(get_extension($row['file']), $conf['picture_ext']))
270  {
271    $picture[$i]['is_picture'] = true;
272  }
273
274  $cat_directory = dirname($row['path']);
275  $file_wo_ext = get_filename_wo_extension($row['file']);
276
277  if (isset($row['representative_ext']) and $row['representative_ext'] != '')
278  {
279    $picture[$i]['src'] =
280      $cat_directory.'/pwg_representative/'
281      .$file_wo_ext.'.'.$row['representative_ext'];
282  }
283  else
284  {
285    $icon = get_themeconf('mime_icon_dir');
286    $icon.= strtolower(get_extension($row['file'])).'.png';
287    $picture[$i]['src'] = $icon;
288  }
289  // special case for picture files
290  if ($picture[$i]['is_picture'])
291  {
292    $picture[$i]['src'] = $row['path'];
293    // if we are working on the "current" element, we search if there is a
294    // high quality picture
295    if ($i == 'current')
296    {
297      if (($row['has_high'] == 'true') and ($user['enabled_high'] == 'true'))
298      {
299        $url_high=$cat_directory.'/pwg_high/'.$row['file'];
300        $picture[$i]['high_file_system'] = $picture[$i]['high'] = $url_high;
301        if ( ! url_is_remote($picture[$i]['high']) )
302        {
303          $picture[$i]['high'] = get_root_url().$picture[$i]['high'];
304        }
305      }
306    }
307  }
308  $picture[$i]['src_file_system'] = $picture[$i]['src'];
309  if ( ! url_is_remote($picture[$i]['src']) )
310  {
311    $picture[$i]['src'] = get_root_url(). $picture[$i]['src'];
312  }
313
314  // if picture is not a file, we need the download link
315  if (!$picture[$i]['is_picture'])
316  {
317    $picture[$i]['download'] = url_is_remote($row['path']) ? '' : get_root_url();
318    $picture[$i]['download'].= $row['path'];
319  }
320
321  $picture[$i]['thumbnail'] = get_thumbnail_src($row['path'], @$row['tn_ext']);
322
323  if ( !empty( $row['name'] ) )
324  {
325    $picture[$i]['name'] = $row['name'];
326  }
327  else
328  {
329    $picture[$i]['name'] = str_replace('_', ' ', $file_wo_ext);
330  }
331
332  $picture[$i]['url'] = duplicate_picture_URL(
333    array(
334      'image_id' => $row['id'],
335      'image_file' => $row['file'],
336      ),
337    array(
338      'start',
339      )
340    );
341
342  if ('previous'==$i and $page['previous_item']==$page['first_item'])
343  {
344    $picture['first'] = $picture[$i];
345  }
346  if ('next'==$i and $page['next_item']==$page['last_item'])
347  {
348    $picture['last'] = $picture[$i];
349  }
350}
351
352$url_admin =
353  get_root_url().'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 = add_url_params(
359  $picture['current']['url'],
360  array( '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  // $redirect_msg, $refresh, $url_link and $title are required for creating an automated
368  // refresh page in header.tpl
369  $refresh= $_GET['slideshow'];
370  $url_link = add_url_params(
371      $picture['next']['url'],
372      array('slideshow'=>$refresh)
373    );
374  $redirect_msg = nl2br(l10n('redirect_msg'));
375}
376
377$title_nb = ($page['current_rank'] + 1).'/'.$page['cat_nb_images'];
378
379// calculation of width and height
380if (empty($picture['current']['width']))
381{
382  $taille_image = @getimagesize($picture['current']['src_file_system']);
383  $original_width = $taille_image[0];
384  $original_height = $taille_image[1];
385}
386else
387{
388  $original_width = $picture['current']['width'];
389  $original_height = $picture['current']['height'];
390}
391
392$picture_size = get_picture_size(
393  $original_width,
394  $original_height,
395  @$user['maxwidth'],
396  @$user['maxheight']
397  );
398
399// metadata
400$url_metadata = duplicate_picture_URL();
401if ($conf['show_exif'] or $conf['show_iptc'])
402{
403  $metadata_showable = true;
404  if ( !isset($_GET['metadata']) )
405  {
406    $url_metadata = add_url_params( $url_metadata, array('metadata'=>null) );
407  }
408}
409else
410{
411  $metadata_showable = false;
412}
413
414$page['body_id'] = 'thePicturePage';
415//------------------------------------------------------- navigation management
416foreach ( array('first','previous','next','last') as $which_image )
417{
418  if (isset($picture[$which_image]))
419  {
420    $template->assign_block_vars(
421      $which_image,
422      array(
423        'TITLE_IMG' => $picture[$which_image]['name'],
424        'IMG' => $picture[$which_image]['thumbnail'],
425        'U_IMG' => $picture[$which_image]['url'],
426        'U_IMG_SRC' => $picture[$which_image]['src']
427        )
428      );
429  }
430}
431
432include(PHPWG_ROOT_PATH.'include/page_header.php');
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// legend
595if (isset($picture['current']['comment'])
596    and !empty($picture['current']['comment']))
597{
598  $template->assign_block_vars(
599    'legend',
600    array(
601      'COMMENT_IMG' => nl2br($picture['current']['comment'])
602      ));
603}
604
605$infos = array();
606
607// author
608if (!empty($picture['current']['author']))
609{
610  $infos['INFO_AUTHOR'] =
611    // FIXME because of search engine partial rewrite, giving the author
612    // name threw GET is not supported anymore. This feature should come
613    // back later, with a better design
614//     '<a href="'.
615//       PHPWG_ROOT_PATH.'category.php?cat=search'.
616//       '&amp;search=author:'.$picture['current']['author']
617//       .'">'.$picture['current']['author'].'</a>';
618    $picture['current']['author'];
619}
620else
621{
622  $infos['INFO_AUTHOR'] = l10n('N/A');
623}
624
625// creation date
626if (!empty($picture['current']['date_creation']))
627{
628  $val = format_date($picture['current']['date_creation']);
629  $url = make_index_URL(
630        array(
631          'chronology_field'=>'created',
632          'chronology_style'=>'monthly',
633          'chronology_view'=>'list',
634          'chronology_date' => explode('-', $picture['current']['date_creation'])
635        )
636      );
637  $infos['INFO_CREATION_DATE'] = '<a href="'.$url.'" rel="nofollow">'.$val.'</a>';
638}
639else
640{
641  $infos['INFO_CREATION_DATE'] = l10n('N/A');
642}
643
644// date of availability
645$val = format_date($picture['current']['date_available'], 'mysql_datetime');
646$url = make_index_URL(
647      array(
648        'chronology_field'=>'posted',
649        'chronology_style'=>'monthly',
650        'chronology_view'=>'list',
651        'chronology_date'=>explode('-', substr($picture['current']['date_available'],0,10))
652      )
653    );
654$infos['INFO_POSTED_DATE'] = '<a href="'.$url.'" rel="nofollow">'.$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// tags
695$query = '
696SELECT id, name, url_name
697  FROM '.IMAGE_TAG_TABLE.'
698    INNER JOIN '.TAGS_TABLE.' ON tag_id = id
699  WHERE image_id = '.$page['image_id'].'
700;';
701$result = pwg_query($query);
702
703if (mysql_num_rows($result) > 0)
704{
705  $tags = array();
706
707  while ($row = mysql_fetch_array($result))
708  {
709    array_push(
710      $tags,
711      '<a href="'
712      .make_index_URL(
713        array(
714          'tags' => array(
715            array(
716              'id' => $row['id'],
717              'url_name' => $row['url_name'],
718              ),
719            )
720          )
721        )
722      .'">'.$row['name'].'</a>'
723      );
724  }
725
726  $infos['INFO_TAGS'] = implode(', ', $tags);
727}
728else
729{
730  $infos['INFO_TAGS'] = l10n('N/A');
731}
732
733$template->assign_vars($infos);
734
735// related categories
736foreach ($related_categories as $category)
737{
738  $template->assign_block_vars(
739    'category',
740    array(
741      'LINE' => count($related_categories) > 3
742        ? get_cat_display_name_cache($category['uppercats'])
743        : get_cat_display_name_from_id($category['category_id'])
744      )
745    );
746}
747
748//slideshow end
749if (isset($_GET['slideshow']))
750{
751  if (!is_numeric($_GET['slideshow']))
752  {
753    $_GET['slideshow'] = $conf['slideshow_period'];
754  }
755
756  $template->assign_block_vars(
757    'stop_slideshow',
758    array(
759      'U_SLIDESHOW' => $picture['current']['url'],
760      )
761    );
762}
763
764// +-----------------------------------------------------------------------+
765// |                               sub pages                               |
766// +-----------------------------------------------------------------------+
767
768include(PHPWG_ROOT_PATH.'include/picture_rate.inc.php');
769include(PHPWG_ROOT_PATH.'include/picture_comment.inc.php');
770if ($metadata_showable and isset($_GET['metadata']))
771{
772  include(PHPWG_ROOT_PATH.'include/picture_metadata.inc.php');
773}
774//------------------------------------------------------------ log informations
775pwg_log('picture', $page['title'], $picture['current']['file']);
776
777$template->parse('picture');
778include(PHPWG_ROOT_PATH.'include/page_tail.php');
779?>
Note: See TracBrowser for help on using the repository browser.