source: trunk/picture.php @ 1052

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

fix: permissioning not working (2 bugs)

fix: locked gallery cannot be unlocked (impossible to login)

improvement: nicer display in redirect.tpl

improvement: when a page is not accessible because of permissions (accessed
through bookmark or email), redirect to identification.php and after
identification to the initially requested page

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.7 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2006-02-23 05:12:32 +0000 (Thu, 23 Feb 2006) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1052 $
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
28$rate_items = array(0,1,2,3,4,5);
29//--------------------------------------------------------------------- include
30define('PHPWG_ROOT_PATH','./');
31include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
32
33include(PHPWG_ROOT_PATH.'include/section_init.inc.php');
34//-------------------------------------------------- access authorization check
35if (isset($page['cat']) and is_numeric($page['cat']))
36{
37  check_restrictions($page['cat']);
38}
39//-------------------------------------------------------------- initialization
40// if this image_id doesn't correspond to this category, an error message is
41// displayed, and execution is stopped
42if (!in_array($_GET['image_id'], $page['items']))
43{
44  echo '
45<div style="text-align:center;">'.$lang['access_forbiden'].'<br />
46  <a href="'.PHPWG_ROOT_PATH.'category.php'.'">'.$lang['thumbnails'].'</a>
47</div>';
48  exit();
49}
50
51$page['rank_of'] = array_flip($page['items']);
52
53// caching first_rank, last_rank, current_rank in the displayed
54// section. This should also help in readability.
55$page['first_rank']   = 0;
56$page['last_rank']    = count($page['items']) - 1;
57$page['current_rank'] = $page['rank_of'][ $_GET['image_id'] ];
58
59// caching current item : readability purpose
60$page['current_item'] = $_GET['image_id'];
61
62if ($page['current_rank'] != $page['first_rank'])
63{
64  // "go to first picture of this section" link is displayed only if the
65  // displayed item is not the first.
66  $template->assign_block_vars(
67    'first',
68    array(
69      'U_IMG' =>
70        PHPWG_ROOT_PATH.'picture.php'.
71        get_query_string_diff(
72          array('image_id', 'add_fav', 'slideshow')
73          ).
74        '&amp;image_id='.$page['items'][ $page['first_rank'] ],
75      )
76    );
77
78  // caching previous item : readability purpose
79  $page['previous_item'] = $page['items'][ $page['current_rank'] - 1 ];
80}
81
82if ($page['current_rank'] != $page['last_rank'])
83{
84  // "go to last picture of this section" link is displayed only if the
85  // displayed item is not the last.
86  $template->assign_block_vars(
87    'last',
88    array(
89      'U_IMG' =>
90        PHPWG_ROOT_PATH.'picture.php'.
91        get_query_string_diff(
92          array('image_id', 'add_fav', 'slideshow')
93          ).
94        '&amp;image_id='.$page['items'][ $page['last_rank'] ],
95      )
96    );
97
98  // caching next item : readability purpose
99  $page['next_item'] = $page['items'][ $page['current_rank'] + 1 ];
100}
101
102//---------------------------------------- incrementation of the number of hits
103if ( count(array_intersect(
104             array_keys($_GET),
105             array('add_fav', 'caddie', 'rate', 'representative', 'del') )
106          )==0 )
107{
108  $query = '
109  UPDATE '.IMAGES_TABLE.'
110    SET hit = hit+1
111    WHERE id = '.$_GET['image_id'].'
112  ;';
113  @pwg_query( $query );
114}
115
116//-------------------------------------------------------------- representative
117if ('admin' == $user['status'] and isset($_GET['representative']))
118{
119  $query = '
120UPDATE '.CATEGORIES_TABLE.'
121  SET representative_picture_id = '.$_GET['image_id'].'
122  WHERE id = '.$page['cat'].'
123;';
124  pwg_query($query);
125
126  $url =
127    PHPWG_ROOT_PATH
128    .'picture.php'
129    .get_query_string_diff(array('representative'));
130  redirect($url);
131}
132
133//-------------------------------------------------------------- caddie filling
134
135if (isset($_GET['caddie']))
136{
137  fill_caddie(array($_GET['image_id']));
138
139  $url =
140    PHPWG_ROOT_PATH
141    .'picture.php'
142    .get_query_string_diff(array('caddie'));
143  redirect($url);
144}
145
146
147//----------------------------------------------------------- rate registration
148if (isset($_GET['rate'])
149    and $conf['rate']
150    and ( !$user['is_the_guest'] or $conf['rate_anonymous'] )
151    and in_array($_GET['rate'], $rate_items))
152{
153  if ($user['is_the_guest'])
154  {
155    $ip_components = explode('.', $_SERVER["REMOTE_ADDR"]);
156    if ( count($ip_components)>3 )
157    {
158      array_pop($ip_components);
159    }
160    $anonymous_id = implode ('.', $ip_components);
161
162    if ( isset($_COOKIE['pwg_anonymous_rater']) )
163    {
164      if ($anonymous_id != $_COOKIE['pwg_anonymous_rater'] )
165      { // client has changed his IP adress or he's trying to fool us
166        $query = '
167SELECT element_id FROM '. RATE_TABLE . '
168  WHERE user_id=' . $user['id'] . '
169  AND anonymous_id=\'' . $anonymous_id . '\'';
170        $result = pwg_query($query);
171        $already_there = array();
172        while ( $row = mysql_fetch_array($result) )
173        {
174          array_push( $already_there, $row['element_id'] );
175        }
176       
177        if ( count($already_there)>0 )
178        {
179          $query = '
180DELETE FROM '. RATE_TABLE . '
181  WHERE user_id=' . $user['id'] . '
182  AND anonymous_id=\'' . $_COOKIE['pwg_anonymous_rater'] . '\'
183  AND element_id NOT IN (' . implode(',',$already_there) . ')';
184          pwg_query($query);
185        }
186       
187        $query = '
188UPDATE '. RATE_TABLE . '
189  SET anonymous_id=\'' . $anonymous_id . '\'
190  WHERE user_id=' . $user['id'] . '
191  AND anonymous_id=\'' . $_COOKIE['pwg_anonymous_rater'] . '\'';
192        pwg_query($query);
193
194        setcookie('pwg_anonymous_rater', $anonymous_id, 
195                   strtotime('+10 years'), cookie_path() );
196      }
197    }
198    else
199    {
200      setcookie('pwg_anonymous_rater', $anonymous_id, 
201                 strtotime('+10 years'), cookie_path() );
202    }
203  }
204
205  $query = '
206DELETE FROM '.RATE_TABLE.'
207  WHERE element_id = '.$_GET['image_id'] . '
208  AND user_id = '.$user['id']
209;
210  if (isset($anonymous_id))
211  {
212    $query.= ' AND anonymous_id=\'' . $anonymous_id .'\'';
213  }
214  pwg_query($query);
215  $query = '
216INSERT INTO '.RATE_TABLE.'
217  (user_id,anonymous_id,element_id,rate,date)
218  VALUES
219  ('.$user['id'].','.(isset($anonymous_id)?'\''.$anonymous_id.'\'':"''").','.
220   $_GET['image_id'].','.$_GET['rate'].',NOW())
221;';
222  pwg_query($query);
223
224  // update of images.average_rate field
225  $query = '
226SELECT ROUND(AVG(rate),2) AS average_rate
227  FROM '.RATE_TABLE.'
228  WHERE element_id = '.$_GET['image_id'].'
229;';
230  $row = mysql_fetch_array(pwg_query($query));
231  $query = '
232UPDATE '.IMAGES_TABLE.'
233  SET average_rate = '.$row['average_rate'].'
234  WHERE id = '.$_GET['image_id'].'
235;';
236  pwg_query($query);
237  $url =
238    PHPWG_ROOT_PATH
239    .'picture.php'
240    .get_query_string_diff(array('rate'));
241  redirect($url);
242}
243
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 = '.$_GET['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//------------------------------------- prev, current & next picture management
261$picture = array();
262
263$ids = array($_GET['image_id']);
264if (isset($page['previous_item']))
265{
266  array_push($ids, $page['previous_item']);
267}
268if (isset($page['next_item']))
269{
270  array_push($ids, $page['next_item']);
271}
272
273$query = '
274SELECT *
275  FROM '.IMAGES_TABLE.'
276  WHERE id IN ('.implode(',', $ids).')
277;';
278
279$result = pwg_query($query);
280
281while ($row = mysql_fetch_array($result))
282{
283  if (isset($page['previous_item']) and $row['id'] == $page['previous_item'])
284  {
285    $i = 'prev';
286  }
287  else if (isset($page['next_item']) and $row['id'] == $page['next_item'])
288  {
289    $i = 'next';
290  }
291  else
292  {
293    $i = 'current';
294  }
295 
296  foreach (array_keys($row) as $key)
297  {
298    if (!is_numeric($key))
299    {
300      $picture[$i][$key] = $row[$key];
301    }
302  }
303
304  $picture[$i]['is_picture'] = false;
305  if (in_array(get_extension($row['file']), $conf['picture_ext']))
306  {
307    $picture[$i]['is_picture'] = true;
308  }
309 
310  $cat_directory = dirname($row['path']);
311  $file_wo_ext = get_filename_wo_extension($row['file']);
312
313  $icon = get_themeconf('mime_icon_dir');
314  $icon.= strtolower(get_extension($row['file'])).'.png';
315
316  if (isset($row['representative_ext']) and $row['representative_ext'] != '')
317  {
318    $picture[$i]['src'] =
319      $cat_directory.'/pwg_representative/'
320      .$file_wo_ext.'.'.$row['representative_ext'];
321  }
322  else
323  {
324    $picture[$i]['src'] = $icon;
325  }
326  // special case for picture files
327  if ($picture[$i]['is_picture'])
328  {
329    $picture[$i]['src'] = $row['path'];
330    // if we are working on the "current" element, we search if there is a
331    // high quality picture
332    if ($i == 'current')
333    {
334      if ($row['has_high']=='true')
335      {
336        $url_high=$cat_directory.'/pwg_high/'.$row['file'];
337        $picture[$i]['high'] = $url_high;
338      }
339    }
340  }
341
342  // if picture is not a file, we need the download link
343  if (!$picture[$i]['is_picture'])
344  {
345    $picture[$i]['download'] = $row['path'];
346  }
347
348  $picture[$i]['thumbnail'] = get_thumbnail_src($row['path'], @$row['tn_ext']);
349 
350  if ( !empty( $row['name'] ) )
351  {
352    $picture[$i]['name'] = $row['name'];
353  }
354  else
355  {
356    $picture[$i]['name'] = str_replace('_', ' ', $file_wo_ext);
357  }
358
359  $picture[$i]['url'] =
360    PHPWG_ROOT_PATH.'picture.php'
361    .get_query_string_diff(array('image_id', 'add_fav', 'slideshow'))
362    .'&amp;image_id='.$row['id'];
363}
364
365$url_up = PHPWG_ROOT_PATH.'category.php?';
366if ( isset($page['cat']) )
367{
368  $url_up .= 'cat='.$page['cat'];
369}
370elseif ( isset($_GET['calendar']) )
371{
372  $url_up .= 'calendar='.$_GET['calendar'];
373}
374
375$url_up_start = floor( $page['current_rank'] / $user['nb_image_page'] );
376$url_up_start *= $user['nb_image_page'];
377if ($url_up_start>0)
378{
379  $url_up .= '&amp;start='.$url_up_start; 
380}
381
382if ( $page['cat'] == 'search' )
383{
384  $url_up.= '&amp;search='.$_GET['search'];
385}
386if ( $page['cat'] == 'list' )
387{
388  $url_up.= '&amp;list='.$_GET['list'];
389}
390
391$url_admin =
392  PHPWG_ROOT_PATH.'admin.php?page=picture_modify'
393  .'&amp;cat_id='.$page['cat']
394  .'&amp;image_id='.$_GET['image_id'];
395
396$url_slide =
397  $picture['current']['url'].'&amp;slideshow='.$conf['slideshow_period'];
398
399//--------------------------------------------------------- favorite management
400if ( isset( $_GET['add_fav'] ) )
401{
402  $query = 'DELETE FROM '.FAVORITES_TABLE;
403  $query.= ' WHERE user_id = '.$user['id'];
404  $query.= ' AND image_id = '.$picture['current']['id'];
405  $query.= ';';
406  $result = pwg_query( $query );
407 
408  if ( $_GET['add_fav'] == 1 )
409  {
410    $query = 'INSERT INTO '.FAVORITES_TABLE;
411    $query.= ' (image_id,user_id) VALUES';
412    $query.= ' ('.$picture['current']['id'].','.$user['id'].')';
413    $query.= ';';
414    $result = pwg_query( $query );
415  }
416  if ( !$_GET['add_fav'] and $page['cat'] == 'fav' )
417  {
418    if (!isset($page['previous_item']) and !isset($page['next_item']))
419    {
420      // there is no favorite picture anymore we redirect the user to the
421      // category page
422      redirect($url_up);
423    }
424    else if (!isset($page['previous_item']))
425    {
426      $url = str_replace( '&amp;', '&', $picture['next']['url'] );
427      redirect( $url );
428    }
429    else
430    {
431      $url = str_replace('&amp;', '&', $picture['prev']['url'] );
432      redirect( $url );
433    }
434    redirect( $url );
435  }
436}
437
438//------------------------------------------------------  comment registeration
439if ( isset( $_POST['content'] ) && !empty($_POST['content']) )
440{
441  $register_comment = true;
442  $author = !empty($_POST['author'])?$_POST['author']:$lang['guest'];
443  // if a guest try to use the name of an already existing user, he must be
444  // rejected
445  if ( $author != $user['username'] )
446  {
447    $query = 'SELECT COUNT(*) AS user_exists';
448    $query.= ' FROM '.USERS_TABLE;
449    $query.= ' WHERE '.$conf['user_fields']['username']." = '".$author."'";
450    $query.= ';';
451    $row = mysql_fetch_array( pwg_query( $query ) );
452    if ( $row['user_exists'] == 1 )
453    {
454      $template->assign_block_vars(
455        'information',
456        array('INFORMATION'=>$lang['comment_user_exists']));
457      $register_comment = false;
458    }
459  }
460 
461  if ( $register_comment )
462  {
463    // anti-flood system
464    $reference_date = time() - $conf['anti-flood_time'];
465    $query = 'SELECT id FROM '.COMMENTS_TABLE;
466    $query.= ' WHERE date > FROM_UNIXTIME('.$reference_date.')';
467    $query.= " AND author = '".$author."'";
468    $query.= ';';
469    if ( mysql_num_rows( pwg_query( $query ) ) == 0
470         or $conf['anti-flood_time'] == 0 )
471    {
472      list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
473
474      $data = array();
475      $data{'author'} = $author;
476      $data{'date'} = $dbnow;
477      $data{'image_id'} = $_GET['image_id'];
478      $data{'content'} = htmlspecialchars( $_POST['content'], ENT_QUOTES);
479     
480      if (!$conf['comments_validation'] or $user['status'] == 'admin')
481      {
482        $data{'validated'} = 'true';
483        $data{'validation_date'} = $dbnow;
484      }
485      else
486      {
487        $data{'validated'} = 'false';
488      }
489     
490      include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
491      $fields = array('author', 'date', 'image_id', 'content', 'validated',
492                      'validation_date');
493      mass_inserts(COMMENTS_TABLE, $fields, array($data));
494     
495      // information message
496      $message = $lang['comment_added'];
497
498      if (!$conf['comments_validation'] or $user['status'] == 'admin')
499     
500      if ( $conf['comments_validation'] and $user['status'] != 'admin' )
501      {
502        $message.= '<br />'.$lang['comment_to_validate'];
503      }
504      $template->assign_block_vars('information',
505                                   array('INFORMATION'=>$message));
506    }
507    else
508    {
509      // information message
510      $template->assign_block_vars(
511        'information',
512        array('INFORMATION'=>$lang['comment_anti-flood']));
513    }
514  }
515}
516// comment deletion
517if ( isset( $_GET['del'] )
518     and is_numeric( $_GET['del'] )
519     and $user['status'] == 'admin' )
520{
521  $query = 'DELETE FROM '.COMMENTS_TABLE;
522  $query.= ' WHERE id = '.$_GET['del'];
523  $query.= ';';
524  pwg_query( $query );
525}
526
527//
528// Start output of page
529//
530
531$title =  $picture['current']['name'];
532$refresh = 0;
533if ( isset( $_GET['slideshow'] ) and isset($page['next_item']) )
534{
535  $refresh= $_GET['slideshow'];
536  $url_link = $picture['next']['url'].'&amp;slideshow='.$refresh;
537}
538
539$title_img = $picture['current']['name'];
540if (is_numeric( $page['cat'] )) 
541{
542  $title_img = replace_space(get_cat_display_name($page['cat_name']));
543}
544else if ( $page['cat'] == 'search' )
545{
546  $title_img = replace_search( $title_img, $_GET['search'] );
547}
548$title_nb = ($page['current_rank'] + 1).'/'.$page['cat_nb_images'];
549
550// calculation of width and height
551if (empty($picture['current']['width']))
552{
553  $taille_image = @getimagesize($picture['current']['src']);
554  $original_width = $taille_image[0];
555  $original_height = $taille_image[1];
556}
557else
558{
559  $original_width = $picture['current']['width'];
560  $original_height = $picture['current']['height'];
561}
562
563$picture_size = get_picture_size($original_width, $original_height,
564                                 @$user['maxwidth'], @$user['maxheight']);
565
566// metadata
567if ($conf['show_exif'] or $conf['show_iptc'])
568{
569  $metadata_showable = true;
570}
571else
572{
573  $metadata_showable = false;
574}
575
576$url_metadata = PHPWG_ROOT_PATH.'picture.php';
577$url_metadata .=  get_query_string_diff(array('add_fav', 'slideshow', 'show_metadata'));
578if ($metadata_showable and !isset($_GET['show_metadata']))
579{
580  $url_metadata.= '&amp;show_metadata=1';
581}
582
583$page['body_id'] = 'thePicturePage';
584//------------------------------------------------------- navigation management
585if (isset($page['previous_item']))
586{
587  $template->assign_block_vars(
588    'previous',
589    array(
590      'TITLE_IMG' => $picture['prev']['name'],
591      'IMG' => $picture['prev']['thumbnail'],
592      'U_IMG' => $picture['prev']['url'],
593      'U_IMG_SRC' => $picture['prev']['src']
594      )
595    );
596}
597
598if (isset($page['next_item']))
599{
600  $template->assign_block_vars(
601    'next',
602    array(
603      'TITLE_IMG' => $picture['next']['name'],
604      'IMG' => $picture['next']['thumbnail'],
605      'U_IMG' => $picture['next']['url'],
606      'U_IMG_SRC' => $picture['next']['src'] // allow navigator to preload
607      )
608    );
609}
610
611include(PHPWG_ROOT_PATH.'include/page_header.php');
612$template->set_filenames(array('picture'=>'picture.tpl'));
613
614$template->assign_vars(array(
615  'CATEGORY' => $title_img,
616  'PHOTO' => $title_nb,
617  'TITLE' => $picture['current']['name'],
618  'SRC_IMG' => $picture['current']['src'],
619  'ALT_IMG' => $picture['current']['file'],
620  'WIDTH_IMG' => $picture_size[0],
621  'HEIGHT_IMG' => $picture_size[1],
622
623  'LEVEL_SEPARATOR' => $conf['level_separator'],
624
625  'L_HOME' => $lang['home'],
626  'L_SLIDESHOW' => $lang['slideshow'],
627  'L_STOP_SLIDESHOW' => $lang['slideshow_stop'],
628  'L_PREV_IMG' =>$lang['previous_page'].' : ',
629  'L_NEXT_IMG' =>$lang['next_page'].' : ',
630  'L_ADMIN' =>$lang['link_info_image'],
631  'L_COMMENT_TITLE' =>$lang['comments_title'],
632  'L_ADD_COMMENT' =>$lang['comments_add'],
633  'L_DELETE_COMMENT' =>$lang['comments_del'],
634  'L_DELETE' =>$lang['delete'],
635  'L_SUBMIT' =>$lang['submit'],
636  'L_AUTHOR' =>  $lang['upload_author'],
637  'L_COMMENT' =>$lang['comment'],
638  'L_DOWNLOAD' => $lang['download'],
639  'L_DOWNLOAD_HINT' => $lang['download_hint'],
640  'L_PICTURE_METADATA' => $lang['picture_show_metadata'],
641  'L_PICTURE_HIGH' => $lang['picture_high'],
642  'L_UP_HINT' => $lang['home_hint'],
643  'L_UP_ALT' => $lang['home'],
644 
645  'U_HOME' => (PHPWG_ROOT_PATH.'category.php'),
646  'U_UP' => $url_up,
647  'U_METADATA' => $url_metadata,
648  'U_ADMIN' => $url_admin,
649  'U_SLIDESHOW'=> $url_slide,
650  'U_ADD_COMMENT' => str_replace( '&', '&amp;', $_SERVER['REQUEST_URI'] )
651  )
652);
653
654if ($conf['show_picture_name_on_title'])
655{
656  $template->assign_block_vars('title', array());
657}
658
659//------------------------------------------------------- upper menu management
660// download link if file is not a picture
661if (!$picture['current']['is_picture'])
662{
663  $template->assign_block_vars(
664    'download',
665    array('U_DOWNLOAD' => $picture['current']['download']));
666}
667// display a high quality link if present
668if (isset($picture['current']['high']))
669{
670  $uuid = uniqid(rand());
671  $template->assign_block_vars('high', array(
672    'U_HIGH' => $picture['current']['high'],
673    'UUID'=>$uuid
674  ));
675  $template->assign_block_vars(
676    'download',
677    array('U_DOWNLOAD' => PHPWG_ROOT_PATH.'action.php?dwn='
678          .$picture['current']['high']
679    )
680  );
681}
682// button to set the current picture as representative
683if ('admin' == $user['status'] and is_numeric($page['cat']))
684{
685  $template->assign_block_vars(
686    'representative',
687    array(
688      'URL' =>
689        PHPWG_ROOT_PATH.'picture.php'
690        .get_query_string_diff(array('add_fav'))
691        .'&amp;representative=1'
692      )
693    );
694}
695
696if ('admin' == $user['status'])
697{
698  $template->assign_block_vars(
699    'caddie',
700    array(
701      'URL' =>
702        PHPWG_ROOT_PATH.'picture.php'
703        .get_query_string_diff(array('add_fav')).'&amp;caddie=1')
704    );
705}
706
707//------------------------------------------------------- favorite manipulation
708if ( !$user['is_the_guest'] )
709{
710  // verify if the picture is already in the favorite of the user
711  $query = 'SELECT COUNT(*) AS nb_fav';
712  $query.= ' FROM '.FAVORITES_TABLE.' WHERE image_id = '.$_GET['image_id'];
713  $query.= ' AND user_id = '.$user['id'].';';
714  $result = pwg_query( $query );
715  $row = mysql_fetch_array( $result );
716  if (!$row['nb_fav'])
717  {
718    $url = PHPWG_ROOT_PATH.'picture.php';
719    $url.= get_query_string_diff(array('add_fav'));
720    $url.= '&amp;add_fav=1';
721
722    $template->assign_block_vars(
723      'favorite',
724      array(
725        'FAVORITE_IMG' => get_themeconf('icon_dir').'/favorite.png',
726        'FAVORITE_HINT' =>$lang['add_favorites_hint'],
727        'FAVORITE_ALT' =>$lang['add_favorites_alt'],
728        'U_FAVORITE' => $url
729        ));
730  }
731  else
732  {
733    $url = PHPWG_ROOT_PATH.'picture.php';
734    $url.= get_query_string_diff(array('add_fav'));
735    $url.= '&amp;add_fav=0';
736   
737    $template->assign_block_vars(
738      'favorite',
739      array(
740        'FAVORITE_IMG' => get_themeconf('icon_dir').'/del_favorite.png',
741        'FAVORITE_HINT' =>$lang['del_favorites_hint'],
742        'FAVORITE_ALT' =>$lang['del_favorites_alt'],
743        'U_FAVORITE'=> $url
744        ));
745  }
746}
747//------------------------------------ admin link for information modifications
748if ( $user['status'] == 'admin' )
749{
750  $template->assign_block_vars('admin', array());
751}
752
753//--------------------------------------------------------- picture information
754// legend
755if (isset($picture['current']['comment'])
756    and !empty($picture['current']['comment']))
757{
758  $template->assign_block_vars(
759    'legend',
760    array(
761      'COMMENT_IMG' => nl2br($picture['current']['comment'])
762      ));
763}
764
765$infos = array();
766
767// author
768if (!empty($picture['current']['author']))
769{
770  $infos['INFO_AUTHOR'] =
771    // FIXME because of search engine partial rewrite, giving the author
772    // name threw GET is not supported anymore. This feature should come
773    // back later, with a better design
774//     '<a href="'.
775//       PHPWG_ROOT_PATH.'category.php?cat=search'.
776//       '&amp;search=author:'.$picture['current']['author']
777//       .'">'.$picture['current']['author'].'</a>';
778    $picture['current']['author'];
779}
780else
781{
782  $infos['INFO_AUTHOR'] = l10n('N/A');
783}
784
785// creation date
786if (!empty($picture['current']['date_creation']))
787{
788  $val = format_date($picture['current']['date_creation']);
789  if ( $conf['calendar_datefield'] == 'date_creation' )
790  {
791    $infos['INFO_CREATION_DATE'] = '<a href="'.
792       PHPWG_ROOT_PATH.'category.php?calendar=c-'.
793       $picture['current']['date_creation'].'">'.$val.'</a>';
794  }
795  else
796  {
797     $infos['INFO_CREATION_DATE'] = $val;
798  }
799}
800else
801{
802  $infos['INFO_CREATION_DATE'] = l10n('N/A');
803}
804
805// date of availability
806$val = format_date($picture['current']['date_available'], 'mysql_datetime');
807if ( $conf['calendar_datefield'] == 'date_available' )
808{
809  $infos['INFO_AVAILABILITY_DATE'] = '<a href="'.
810     PHPWG_ROOT_PATH.'category.php?calendar=c-'.
811     substr($picture['current']['date_available'],0,10).'">'.$val.'</a>';
812}
813else
814{
815   $infos['INFO_AVAILABILITY_DATE'] = $val;
816}
817
818// size in pixels
819if ($picture['current']['is_picture'])
820{
821  if ($original_width != $picture_size[0]
822      or $original_height != $picture_size[1])
823  {
824    $infos['INFO_DIMENSIONS'] =
825      '<a href="'.$picture['current']['src'].'" title="'.
826      l10n('Original dimensions').'">'.
827      $original_width.'*'.$original_height.'</a>';
828  }
829  else
830  {
831    $infos['INFO_DIMENSIONS'] = $original_width.'*'.$original_height;
832  }
833}
834else
835{
836  $infos['INFO_DIMENSIONS'] = l10n('N/A');
837}
838
839// filesize
840if (!empty($picture['current']['filesize']))
841{
842  $infos['INFO_FILESIZE'] =
843    sprintf(l10n('%d Kb'), $picture['current']['filesize']);
844}
845else
846{
847  $infos['INFO_FILESIZE'] = l10n('N/A');
848}
849
850// number of visits
851$infos['INFO_VISITS'] = $picture['current']['hit'];
852
853// file
854$infos['INFO_FILE'] = $picture['current']['file'];
855
856// keywords
857if (!empty($picture['current']['keywords']))
858{
859  $infos['INFO_KEYWORDS'] =
860    // FIXME because of search engine partial rewrite, giving the author
861    // name threw GET is not supported anymore. This feature should come
862    // back later, with a better design (tag classification).
863//     preg_replace(
864//       '/([^,]+)/',
865//       '<a href="'.
866//         PHPWG_ROOT_PATH.'category.php?cat=search&amp;search=keywords:$1'
867//         .'">$1</a>',
868//       $picture['current']['keywords']
869//       );
870    $picture['current']['keywords'];
871}
872else
873{
874  $infos['INFO_KEYWORDS'] = l10n('N/A');
875}
876
877$template->assign_vars($infos);
878
879// related categories
880foreach ($related_categories as $category)
881{
882  $template->assign_block_vars(
883    'category',
884    array(
885      'LINE' => count($related_categories) > 3
886        ? get_cat_display_name_cache($category['uppercats'])
887        : get_cat_display_name_from_id($category['category_id'])
888      )
889    );
890}
891
892//-------------------------------------------------------------------  metadata
893if ($metadata_showable and isset($_GET['show_metadata']))
894{
895  include_once(PHPWG_ROOT_PATH.'/include/functions_metadata.inc.php');
896  $template->assign_block_vars('metadata', array());
897  if ($conf['show_exif'])
898  {
899    if (!function_exists('read_exif_data'))
900    {
901      die('Exif extension not available, admin should disable exif display');
902    }
903   
904    if ($exif = @read_exif_data($picture['current']['src']))
905    {
906      $template->assign_block_vars(
907        'metadata.headline',
908        array('TITLE' => 'EXIF Metadata')
909        );
910
911      foreach ($conf['show_exif_fields'] as $field)
912      {
913        if (strpos($field, ';') === false)
914        {
915          if (isset($exif[$field]))
916          {
917            $key = $field;
918            if (isset($lang['exif_field_'.$field]))
919            {
920              $key = $lang['exif_field_'.$field];
921            }
922           
923            $template->assign_block_vars(
924              'metadata.line',
925              array(
926                'KEY' => $key,
927                'VALUE' => $exif[$field]
928                )
929              );
930          }
931        }
932        else
933        {
934          $tokens = explode(';', $field);
935          if (isset($exif[$tokens[0]][$tokens[1]]))
936          {
937            $key = $tokens[1];
938            if (isset($lang['exif_field_'.$tokens[1]]))
939            {
940              $key = $lang['exif_field_'.$tokens[1]];
941            }
942           
943            $template->assign_block_vars(
944              'metadata.line',
945              array(
946                'KEY' => $key,
947                'VALUE' => $exif[$tokens[0]][$tokens[1]]
948                )
949              );
950          }
951        }
952      }
953    }
954  }
955  if ($conf['show_iptc'])
956  {
957    $iptc = get_iptc_data($picture['current']['src'],
958                          $conf['show_iptc_mapping']);
959
960    if (count($iptc) > 0)
961    {
962      $template->assign_block_vars(
963        'metadata.headline',
964        array('TITLE' => 'IPTC Metadata')
965        );
966    }
967   
968    foreach ($iptc as $field => $value)
969    {
970      $key = $field;
971      if (isset($lang[$field]))
972      {
973        $key = $lang[$field];
974      }
975     
976      $template->assign_block_vars(
977        'metadata.line',
978        array(
979          'KEY' => $key,
980          'VALUE' => $value
981          )
982        );
983    }
984  }
985}
986//slideshow end
987if ( isset( $_GET['slideshow'] ) )
988{
989  if ( !is_numeric( $_GET['slideshow'] ) ) $_GET['slideshow'] = $conf['slideshow_period'];
990 
991  $template->assign_block_vars('stop_slideshow', array(
992  'U_SLIDESHOW'=>$picture['current']['url']
993  ));
994}
995
996//------------------------------------------------------------------- rating
997if ($conf['rate'])
998{
999  $query = '
1000SELECT COUNT(rate) AS count
1001     , ROUND(AVG(rate),2) AS average
1002     , ROUND(STD(rate),2) AS STD
1003  FROM '.RATE_TABLE.'
1004  WHERE element_id = '.$picture['current']['id'].'
1005;';
1006  $row = mysql_fetch_array(pwg_query($query));
1007  if ($row['count'] == 0)
1008  {
1009    $value = $lang['no_rate'];
1010  }
1011  else
1012  {
1013    $value = sprintf(
1014      l10n('%.2f (rated %d times, standard deviation = %.2f)'),
1015      $row['average'],
1016      $row['count'],
1017      $row['STD']
1018      );
1019  }
1020
1021  if ($conf['rate_anonymous'] or !$user['is_the_guest'])
1022  {
1023    if ($row['count']>0)
1024    {
1025      $query = 'SELECT rate
1026      FROM '.RATE_TABLE.'
1027      WHERE element_id = '.$_GET['image_id'] . '
1028      AND user_id = '.$user['id'] ;
1029 
1030      if ($user['is_the_guest'])
1031      {
1032        $ip_components = explode('.', $_SERVER['REMOTE_ADDR']);
1033        if ( count($ip_components)>3 )
1034        {
1035          array_pop($ip_components);
1036        }
1037        $anonymous_id = implode ('.', $ip_components);
1038        $query .= ' AND anonymous_id = \''.$anonymous_id . '\'';
1039      }
1040     
1041      $result = pwg_query($query);
1042      if (mysql_num_rows($result) > 0)
1043      {
1044        $row = mysql_fetch_array($result);
1045        $sentence = $lang['already_rated'];
1046        $sentence.= ' ('.$row['rate'].'). ';
1047        $sentence.= $lang['update_rate'];
1048      }
1049      else
1050      {
1051        $sentence = $lang['never_rated'].'. '.$lang['Rate'];
1052      }
1053    }
1054    else 
1055    {
1056      $sentence = $lang['never_rated'].'. '.$lang['Rate'];
1057    }
1058    $template->assign_block_vars(
1059      'rate',
1060      array(
1061        'CONTENT' => $value,
1062        'SENTENCE' => $sentence
1063        ));
1064
1065    $template->assign_block_vars('info_rate', array('CONTENT' => $value));
1066
1067    $template->assign_vars(
1068      array(
1069        'INFO_RATE' => $value
1070        )
1071      );
1072
1073    foreach ($rate_items as $num => $mark)
1074    {
1075      if ($num > 0)
1076      {
1077        $separator = '|';
1078      }
1079      else
1080      {
1081        $separator = '';
1082      }
1083
1084      $url = PHPWG_ROOT_PATH.'picture.php';
1085      $url.= get_query_string_diff(array('add_fav'));
1086      $url.= '&amp;rate='.$mark;
1087
1088      $template->assign_block_vars(
1089        'rate.rate_option',
1090        array(
1091          'OPTION' => $mark,
1092          'URL' => $url,
1093          'SEPARATOR' => $separator
1094          ));
1095    }
1096  }
1097}
1098
1099//---------------------------------------------------- users's comments display
1100
1101// the picture is commentable if it belongs at least to one category which
1102// is commentable
1103$page['show_comments'] = false;
1104foreach ($related_categories as $category)
1105{
1106  if ($category['commentable'] == 'true')
1107  {
1108    $page['show_comments'] = true;
1109  }
1110}
1111
1112if ($page['show_comments'])
1113{
1114  // number of comment for this picture
1115  $query = 'SELECT COUNT(*) AS nb_comments';
1116  $query.= ' FROM '.COMMENTS_TABLE.' WHERE image_id = '.$_GET['image_id'];
1117  $query.= " AND validated = 'true'";
1118  $query.= ';';
1119  $row = mysql_fetch_array( pwg_query( $query ) );
1120 
1121  // navigation bar creation
1122  $url = PHPWG_ROOT_PATH.'picture.php';
1123  $url.= get_query_string_diff(array('add_fav','start'));
1124
1125  if (!isset( $_GET['start'] )
1126      or !is_numeric( $_GET['start'] )
1127      or ( is_numeric( $_GET['start'] ) and $_GET['start'] < 0 ) )
1128  {
1129    $page['start'] = 0;
1130  }
1131  else
1132  {
1133    $page['start'] = $_GET['start'];
1134  }
1135  $page['navigation_bar'] = create_navigation_bar( $url, $row['nb_comments'],
1136                                                   $page['start'],
1137                                                   $conf['nb_comment_page'],
1138                                                   '' );
1139  $template->assign_block_vars('comments', array(
1140    'NB_COMMENT'=>$row['nb_comments'],
1141    'NAV_BAR'=>$page['navigation_bar']));
1142
1143  if ($row['nb_comments']>0)
1144  {
1145    $query = 'SELECT id,author,date,image_id,content';
1146    $query.= ' FROM '.COMMENTS_TABLE.' WHERE image_id = '.$_GET['image_id'];
1147    $query.= " AND validated = 'true'";
1148    $query.= ' ORDER BY date ASC';
1149    $query.= ' LIMIT '.$page['start'].', '.$conf['nb_comment_page'].';';
1150    $result = pwg_query( $query );
1151
1152    while ( $row = mysql_fetch_array( $result ) )
1153    {
1154      $template->assign_block_vars(
1155        'comments.comment',
1156        array(
1157          'COMMENT_AUTHOR'=>empty($row['author'])?$lang['guest']:$row['author'],
1158          'COMMENT_DATE'=>format_date($row['date'], 'mysql_datetime', true),
1159    'COMMENT'=>parse_comment_content($row['content'])
1160    ));
1161
1162      if ( $user['status'] == 'admin' )
1163      {
1164        $template->assign_block_vars(
1165          'comments.comment.delete',
1166          array('U_COMMENT_DELETE'=> $url.'&amp;del='.$row['id']
1167            ));
1168      }
1169    }
1170  }
1171 
1172  if (!$user['is_the_guest']
1173      or ($user['is_the_guest'] and $conf['comments_forall']))
1174  {
1175    $template->assign_block_vars('comments.add_comment', array());
1176    // display author field if the user is not logged in
1177    if (!$user['is_the_guest'])
1178    {
1179      $template->assign_block_vars(
1180        'comments.add_comment.author_known',
1181        array('KNOWN_AUTHOR'=>$user['username'])
1182        );
1183    }
1184    else
1185    {
1186      $template->assign_block_vars(
1187        'comments.add_comment.author_field', array()
1188        );
1189    }
1190  }
1191}
1192//------------------------------------------------------------ log informations
1193pwg_log( 'picture', $title_img, $picture['current']['file'] );
1194
1195$template->parse('picture');
1196include(PHPWG_ROOT_PATH.'include/page_tail.php');
1197?>
Note: See TracBrowser for help on using the repository browser.