source: trunk/picture.php @ 1627

Last change on this file since 1627 was 1627, checked in by chrisaga, 17 years ago

improve page header : slightly prettier title and

first implementation of meta tags and rel links (see the wiki specs)
some code improvements are still need.

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