source: trunk/picture.php @ 429

Last change on this file since 429 was 420, checked in by z0rglub, 20 years ago

field comments.date becomes a datetime MySQL field type (instead of int)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// |                              picture.php                              |
4// +-----------------------------------------------------------------------+
5// | application   : PhpWebGallery <http://phpwebgallery.net>              |
6// | branch        : BSF (Best So Far)                                     |
7// +-----------------------------------------------------------------------+
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-05-20 21:22:44 +0000 (Thu, 20 May 2004) $
10// | last modifier : $Author: z0rglub $
11// | revision      : $Revision: 420 $
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//----------------------------------------------------------- include
29define('PHPWG_ROOT_PATH','./');
30include_once( PHPWG_ROOT_PATH.'include/common.inc.php' );   
31//-------------------------------------------------- access authorization check
32check_cat_id( $_GET['cat'] );
33check_login_authorization();
34if ( isset( $page['cat'] ) and is_numeric( $page['cat'] ) )
35{
36  check_restrictions( $page['cat'] );
37}
38//---------------------------------------- incrementation of the number of hits
39$query = 'UPDATE '.IMAGES_TABLE.' SET hit=hit+1';
40$query.= ' WHERE id='.$_GET['image_id'];
41$query.= ';';
42@mysql_query( $query );
43//-------------------------------------------------------------- initialization
44initialize_category( 'picture' );
45
46// if this image_id doesn't correspond to this category, an error message is
47// displayed, and execution is stopped
48if ( 0 )
49{
50  echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
51  echo '<a href="'.add_session_id( PHPWG_ROOT_PATH.'category.php' ).'">';
52  echo $lang['thumbnails'].'</a></div>';
53  exit();
54}
55
56// retrieving the number of the picture in its category (in order)
57$query = 'SELECT DISTINCT(id)';
58$query.= ' FROM '.IMAGES_TABLE;
59$query.= ' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic';
60$query.= ' ON id = ic.image_id';
61$query.= $page['where'];
62$query.= $conf['order_by'];
63$query.= ';';
64$result = mysql_query( $query );
65$page['num'] = 0;
66$row = mysql_fetch_array( $result );
67while ( $row['id'] != $_GET['image_id'] )
68{
69  $page['num']++;
70  $row = mysql_fetch_array( $result );
71}
72
73//------------------------------------- prev, current & next picture management
74$picture = array();
75$picture['prev']['name'] = '';
76$picture['next']['name'] = '';
77$picture['prev']['thumbnail'] = '';
78$picture['next']['thumbnail'] = '';
79$picture['prev']['url'] = '';
80$picture['next']['url'] = '';
81
82$next = $page['num'] + 1;
83$prev = $page['num'] - 1;
84
85if ( $page['num'] == $page['cat_nb_images'] - 1 )
86{
87  $next = 0;
88}
89
90$query = 'SELECT *';
91$query.= ' FROM '.IMAGES_TABLE;
92$query.= ' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic';
93$query.= ' ON id=ic.image_id';
94$query.= $page['where'];
95$query.= $conf['order_by'];
96
97if ( $prev < 0 )
98{
99  $query.= ' LIMIT 0,2';
100}
101else
102{
103  $query.= ' LIMIT '.$prev.',3';
104}
105$query.= ';';
106
107$result = mysql_query( $query );
108$nb_row = mysql_num_rows( $result );
109$index = array('prev','current','next');
110
111for ( $i = 0; $i < $nb_row; $i++ )
112{
113  $j=($prev<0)?$index[$i+1]:$index[$i];
114  $row = mysql_fetch_array($result);
115  $picture[$j] = $row;
116 
117  if ( !isset($array_cat_directories[$row['storage_category_id']]))
118  {
119    $array_cat_directories[$row['storage_category_id']] =
120      get_complete_dir( $row['storage_category_id'] );
121  }
122  $cat_directory = $array_cat_directories[$row['storage_category_id']];
123  $file = substr ( $row['file'], 0, strrpos ( $row['file'], ".") );
124
125  $picture[$j]['src'] = $cat_directory.$row['file'];
126
127  $picture[$j]['thumbnail'] = $cat_directory.'thumbnail/';
128  $picture[$j]['thumbnail'].= $conf['prefix_thumbnail'].$file;
129  $picture[$j]['thumbnail'].= '.'.$row['tn_ext'];
130 
131  if ( !empty( $row['name'] ) )
132  {
133    $picture[$j]['name'] = $row['name'];
134  }
135  else
136  {
137    $picture[$j]['name'] = str_replace( '_', ' ', $file );
138  }
139
140  $picture[$j]['url'] = PHPWG_ROOT_PATH.'picture.php?image_id='.$row['id'];
141  $picture[$j]['url'].= '&amp;cat='.$page['cat'];
142  if ( $page['cat'] == 'search' )
143  {
144    $picture[$j]['url'].= '&amp;search='.$_GET['search'];
145    $picture[$j]['url'].= '&amp;mode='.$_GET['mode'];
146  }
147}
148
149$url_home = PHPWG_ROOT_PATH.'category.php?cat='.$page['cat'].'&amp;';
150$url_home.= 'num='.$page['num']; 
151if ( $page['cat'] == 'search' )
152{
153  $url_home.= "&amp;search=".$_GET['search'].'&amp;mode='.$_GET['mode'];
154}
155
156$url_admin = PHPWG_ROOT_PATH.'admin.php?page=picture_modify';
157$url_admin.= '&amp;cat_id='.$page['cat'];
158$url_admin.= '&amp;image_id='.$_GET['image_id'];
159 
160//--------------------------------------------------------- favorite management
161if ( isset( $_GET['add_fav'] ) )
162{
163  $query = 'DELETE FROM '.FAVORITES_TABLE;
164  $query.= ' WHERE user_id = '.$user['id'];
165  $query.= ' AND image_id = '.$picture['current']['id'];
166  $query.= ';';
167  $result = mysql_query( $query );
168 
169  if ( $_GET['add_fav'] == 1 )
170  {
171    $query = 'INSERT INTO '.FAVORITES_TABLE;
172    $query.= ' (image_id,user_id) VALUES';
173    $query.= ' ('.$picture['current']['id'].','.$user['id'].')';
174    $query.= ';';
175    $result = mysql_query( $query );
176  }
177  if ( !$_GET['add_fav'] and $page['cat'] == 'fav' )
178  {
179    if ( $prev < 0 and $nb_row == 1 )
180    {
181      // there is no favorite picture anymore we redirect the user to the
182      // category page
183      $url = add_session_id( $url_home );
184      redirect( $url );
185    }
186    else if ( $prev < 0 )
187    {
188      $url = str_replace( '&amp;', '&', $picture['next']['url'] );
189      $url = add_session_id( $url, true);
190    }
191    else
192    {
193      $url = str_replace('&amp;', '&', $picture['prev']['url'] );
194      $url = add_session_id( $url, true);
195    }
196    redirect( $url );
197  }
198}
199
200//------------------------------------------------------  comment registeration
201if ( isset( $_POST['content'] ) && !empty($_POST['content']) )
202{
203  $register_comment = true;
204  $author = !empty($_POST['author'])?$_POST['author']:$lang['guest'];
205  // if a guest try to use the name of an already existing user, he must be
206  // rejected
207  if ( $author != $user['username'] )
208  {
209    $query = 'SELECT COUNT(*) AS user_exists';
210    $query.= ' FROM '.USERS_TABLE;
211    $query.= " WHERE username = '".$author."'";
212    $query.= ';';
213    $row = mysql_fetch_array( mysql_query( $query ) );
214    if ( $row['user_exists'] == 1 )
215    {
216      $template->assign_block_vars(
217        'information',
218        array('INFORMATION'=>$lang['comment_user_exists']));
219      $register_comment = false;
220    }
221  }
222 
223  if ( $register_comment )
224  {
225    // anti-flood system
226    $reference_date = time() - $conf['anti-flood_time'];
227    $query = 'SELECT id FROM '.COMMENTS_TABLE;
228    $query.= ' WHERE date > FROM_UNIXTIME('.$reference_date.')';
229    $query.= " AND author = '".$author."'";
230    $query.= ';';
231    if ( mysql_num_rows( mysql_query( $query ) ) == 0
232         or $conf['anti-flood_time'] == 0 )
233    {
234      $query = 'INSERT INTO '.COMMENTS_TABLE;
235      $query.= ' (author,date,image_id,content,validated) VALUES (';
236      $query.= "'".$author."'";
237      $query.= ',NOW(),'.$_GET['image_id'];
238      $query.= ",'".htmlspecialchars( $_POST['content'], ENT_QUOTES)."'";
239      if ( !$conf['comments_validation'] or $user['status'] == 'admin' )
240      {       
241        $query.= ",'true'";
242      }
243      else
244      {
245        $query.= ",'false'";
246      }
247      $query.= ');';
248      mysql_query( $query );
249      // information message
250      $message = $lang['comment_added'];
251      if ( $conf['comments_validation'] and $user['status'] != 'admin' )
252      {
253        $message.= '<br />'.$lang['comment_to_validate'];
254      }
255      $template->assign_block_vars('information',
256                                   array('INFORMATION'=>$message));
257      // notification to the administrators
258      if ( $conf['mail_notification'] )
259      {
260        $cat_name = get_cat_display_name( $page['cat_name'], ' > ', '' );
261        $cat_name = strip_tags( $cat_name );
262        notify( 'comment', $cat_name.' > '.$picture['current']['name']);
263      }
264    }
265    else
266    {
267      // information message
268      $template->assign_block_vars(
269        'information',
270        array('INFORMATION'=>$lang['comment_anti-flood']));
271    }
272  }
273}
274// comment deletion
275if ( isset( $_GET['del'] )
276     and is_numeric( $_GET['del'] )
277     and $user['status'] == 'admin' )
278{
279  $query = 'DELETE FROM '.COMMENTS_TABLE;
280  $query.= ' WHERE id = '.$_GET['del'];
281  $query.= ';';
282  mysql_query( $query );
283}
284
285//
286// Start output of page
287//
288
289$title =  $picture['current']['name'];
290$refresh = 0;
291if ( isset( $_GET['slideshow'] ) and $next )
292{
293  $refresh= $_GET['slideshow'];
294  $url_link = $picture['next']['url'].'&amp;slideshow='.$refresh;
295}
296
297$title_img = $picture['current']['name'];
298$title_nb = '';
299if (is_numeric( $page['cat'] )) 
300{
301  $title_img = replace_space(get_cat_display_name( $page['cat_name'], " &gt; "));
302  $n = $page['num'] + 1;
303  $title_nb = "Photo".' '.$n.'/';
304  $title_nb.= $page['cat_nb_images'];
305  //$title_img.= $picture['current']['name'];
306}
307else if ( $page['cat'] == 'search' )
308{
309  $title_img = replace_search( $title_img, $_GET['search'] );
310}
311
312// calculation of width and height
313if ( empty($picture['current']['width']))
314{
315  $taille_image = @getimagesize( $lien_image );
316  $original_width = $taille_image[0];
317  $original_height = $taille_image[1];
318}
319else
320{
321  $original_width = $picture['current']['width'];
322  $original_height = $picture['current']['height'];
323}
324
325$picture_size = get_picture_size( $original_width, $original_height,
326                                  $user['maxwidth'], $user['maxheight'] );
327                                 
328include(PHPWG_ROOT_PATH.'include/page_header.php');
329$template->set_filenames(array('picture'=>'picture.tpl'));
330
331$template->assign_vars(array(
332  'CATEGORY' => $title_img,
333  'PHOTO' => $title_nb,
334  'TITLE' => $picture['current']['name'],
335  'PREV_TITLE_IMG' => $picture['prev']['name'],
336  'NEXT_TITLE_IMG' => $picture['next']['name'],
337  'PREV_IMG' => $picture['prev']['thumbnail'],
338  'NEXT_IMG' => $picture['next']['thumbnail'],
339  'SRC_IMG' => $picture['current']['src'],
340  'ALT_IMG' => $picture['current']['file'],
341  'WIDTH_IMG' => $picture_size[0],
342  'HEIGHT_IMG' => $picture_size[1],
343  'COMMENT_IMG' => $picture['current']['comment'],
344
345  'L_SLIDESHOW' => $lang['slideshow'],
346  'L_TIME' => $lang['period_seconds'],
347  'L_STOP_SLIDESHOW' => $lang['slideshow_stop'],
348  'L_PREV_IMG' =>$lang['previous_image'].' : ',
349  'L_ADMIN' =>$lang['link_info_image'],
350  'L_BACK' =>$lang['back'],
351  'L_COMMENT_TITLE' =>$lang['comments_title'],
352  'L_ADD_COMMENT' =>$lang['comments_add'],
353  'L_DELETE_COMMENT' =>$lang['comments_del'],
354  'L_DELETE' =>$lang['delete'],
355  'L_SUBMIT' =>$lang['submit'],
356  'L_AUTHOR' =>$lang['author'],
357  'L_COMMENT' =>$lang['comment'],
358 
359  'T_DEL_IMG' =>PHPWG_ROOT_PATH.'template/'.$user['template'].'/theme/delete.gif',
360 
361  'U_PREV_IMG' => add_session_id($picture['prev']['url']),
362  'U_NEXT_IMG' => add_session_id($picture['next']['url']),
363  'U_HOME' => add_session_id($url_home),
364  'U_ADMIN' => add_session_id($url_admin),
365  'U_ADD_COMMENT' => add_session_id(str_replace( '&', '&amp;', $_SERVER['REQUEST_URI'] ))
366  )
367);
368
369//-------------------------------------------------------- slideshow management
370if ( isset( $_GET['slideshow'] ) )
371{
372  if ( !is_numeric( $_GET['slideshow'] ) ) $_GET['slideshow'] = $conf['slideshow_period'][0];
373       
374  $template->assign_block_vars('stop_slideshow', array(
375  'U_SLIDESHOW'=>add_session_id( $picture['current']['url'] )
376  ));
377}
378else
379{
380  $template->assign_block_vars('start_slideshow', array());
381  foreach ( $conf['slideshow_period'] as $option ) 
382  {
383    $template->assign_block_vars('start_slideshow.second', array(
384          'SLIDESHOW_SPEED'=>$option,
385          'U_SLIDESHOW'=>add_session_id( $picture['current']['url'].'&amp;slideshow='.$option)
386          ));
387  }
388}
389
390if ($prev>=0) $template->assign_block_vars('previous', array());
391if ($next) $template->assign_block_vars('next', array());
392
393//--------------------------------------------------------- picture information
394// legend
395if ( !empty($picture['current']['comment']) )
396{
397  $template->assign_block_vars('legend', array());
398}
399
400// author
401if ( !empty($picture['current']['author']) )
402{
403  $template->assign_block_vars('info_line', array(
404          'INFO'=>$lang['author'],
405          'VALUE'=>$picture['current']['author']
406          ));
407}
408// creation date
409if ( !empty($picture['current']['date_creation']) )
410{
411  $template->assign_block_vars('info_line', array(
412          'INFO'=>$lang['creation_date'],
413          'VALUE'=>format_date( $picture['current']['date_creation'] ) 
414          ));
415}
416// date of availability
417$template->assign_block_vars('info_line', array(
418          'INFO'=>$lang['registration_date'],
419          'VALUE'=>format_date( $picture['current']['date_available'] ) 
420          ));
421// size in pixels
422if ( $original_width != $picture_size[0] or $original_height != $picture_size[1] )
423{
424  $content = '[ <a href="'.$picture['current']['url'].'" title="'.$lang['true_size'].'">';
425  $content.= $original_width.'*'.$original_height.'</a> ]';
426}
427else
428{
429  $content = $original_width.'*'.$original_height;
430}
431$template->assign_block_vars('info_line', array(
432          'INFO'=>$lang['size'],
433          'VALUE'=>$content 
434          ));
435// file
436$template->assign_block_vars('info_line', array(
437          'INFO'=>$lang['file'],
438          'VALUE'=>$picture['current']['file'] 
439          ));
440// filesize
441if ( empty($picture['current']['filesize']))
442{
443  $poids = floor ( filesize( $picture['current']['url'] ) / 1024 );
444}
445else
446{
447  $poids = $picture['current']['filesize'];
448}
449
450$template->assign_block_vars('info_line', array(
451          'INFO'=>$lang['filesize'],
452          'VALUE'=>$poids.' KB'
453          ));
454// keywords
455if ( !empty($picture['current']['keywords']))
456{
457  $keywords = explode( ',', $picture['current']['keywords'] );
458  $content = '';
459  $url = PHPWG_ROOT_PATH.'category.php?cat=search';
460  $url.= '&amp;mode=OR&amp;search=';
461  foreach ( $keywords as $i => $keyword ) {
462    $local_url = add_session_id( $url.$keyword );
463    if ( $i > 0 ) $content.= ',';
464    $content.= '<a href="'.$local_url.'">'.$keyword.'</a>';
465  }
466  $template->assign_block_vars('info_line', array(
467    'INFO'=>$lang['keywords'],
468    'VALUE'=>$content
469    ));
470}
471// number of visits
472$template->assign_block_vars('info_line', array(
473    'INFO'=>$lang['visited'],
474    'VALUE'=>$picture['current']['hit'].' '.$lang['times']
475    ));
476
477//------------------------------------------------------- favorite manipulation
478if ( !$user['is_the_guest'] )
479{
480  // verify if the picture is already in the favorite of the user
481  $query = 'SELECT COUNT(*) AS nb_fav';
482  $query.= ' FROM '.FAVORITES_TABLE.' WHERE image_id = '.$_GET['image_id'];
483  $query.= ' AND user_id = '.$user['id'].';';
484  $result = mysql_query( $query );
485  $row = mysql_fetch_array( $result );
486  if (!$row['nb_fav'])
487  {
488    $url = PHPWG_ROOT_PATH.'picture.php?cat='.$page['cat'].'&amp;image_id='.$_GET['image_id'];
489    $url.='&amp;add_fav=1';
490    if ( $page['cat'] == 'search' )
491    {
492      $url.= '&amp;search='.$_GET['search'].'&amp;mode='.$_GET['mode'];
493    }
494        $template->assign_block_vars('favorite', array(
495      'FAVORITE_IMG' => PHPWG_ROOT_PATH.'template/'.$user['template'].'/theme/favorite.gif',
496          'FAVORITE_HINT' =>$lang['add_favorites_hint'],
497          'FAVORITE_ALT' =>'[ '.$lang['add_favorites_alt'].' ]',
498      'U_FAVORITE'=> add_session_id( $url )
499    ));
500  }
501  else
502  {
503    $url = PHPWG_ROOT_PATH.'picture.php?cat='.$page['cat'].'&amp;image_id='.$_GET['image_id'];
504    $url.= '&amp;add_fav=0';
505        $template->assign_block_vars('favorite', array(
506      'FAVORITE_IMG' => PHPWG_ROOT_PATH.'template/'.$user['template'].'/theme/del_favorite.gif',
507          'FAVORITE_HINT' =>$lang['del_favorites_hint'],
508          'FAVORITE_ALT' =>'[ '.$lang['del_favorites_alt'].' ]',
509      'U_FAVORITE'=> add_session_id( $url )
510    ));
511  }
512}
513//------------------------------------ admin link for information modifications
514if ( $user['status'] == 'admin' )
515{
516  $template->assign_block_vars('modification', array());
517}
518
519//---------------------------------------------------- users's comments display
520if ( $conf['show_comments'] )
521{
522  // number of comment for this picture
523  $query = 'SELECT COUNT(*) AS nb_comments';
524  $query.= ' FROM '.COMMENTS_TABLE.' WHERE image_id = '.$_GET['image_id'];
525  $query.= " AND validated = 'true'";
526  $query.= ';';
527  $row = mysql_fetch_array( mysql_query( $query ) );
528 
529  // navigation bar creation
530  $url = PHPWG_ROOT_PATH.'picture.php?cat='.$page['cat'].'&amp;image_id='.$_GET['image_id'];
531  if ( $page['cat'] == 'search' )
532  {
533    $url.= '&amp;search='.$_GET['search'].'&amp;mode='.$_GET['mode'];
534  }
535  if( !isset( $_GET['start'] )
536      or !is_numeric( $_GET['start'] )
537      or ( is_numeric( $_GET['start'] ) and $_GET['start'] < 0 ) )
538  {
539    $page['start'] = 0;
540  }
541  else
542  {
543    $page['start'] = $_GET['start'];
544  }
545  $page['navigation_bar'] = create_navigation_bar( $url, $row['nb_comments'],
546                                                   $page['start'],
547                                                   $conf['nb_comment_page'],
548                                                   '' );
549  $template->assign_block_vars('comments', array(
550    'NB_COMMENT'=>$row['nb_comments'],
551    'NAV_BAR'=>$page['navigation_bar']));
552
553  $query = 'SELECT id,author,date,image_id,content';
554  $query.= ' FROM '.COMMENTS_TABLE.' WHERE image_id = '.$_GET['image_id'];
555  $query.= " AND validated = 'true'";
556  $query.= ' ORDER BY date ASC';
557  $query.= ' LIMIT '.$page['start'].', '.$conf['nb_comment_page'].';';
558  $result = mysql_query( $query );
559               
560  while ( $row = mysql_fetch_array( $result ) )
561  {
562    $content = nl2br( $row['content'] );
563
564    // replace _word_ by an underlined word
565    $pattern = '/_([^\s]*)_/';
566    $replacement = '<span style="text-decoration:underline;">\1</span>';
567    $content = preg_replace( $pattern, $replacement, $content );
568
569    // replace *word* by a bolded word
570    $pattern = '/\*([^\s]*)\*/';
571    $replacement = '<span style="font-weight:bold;">\1</span>';
572    $content = preg_replace( $pattern, $replacement, $content );
573
574    // replace /word/ by an italic word
575    $pattern = '/\/([^\s]*)\//';
576    $replacement = '<span style="font-style:italic;">\1</span>';
577    $content = preg_replace( $pattern, $replacement, $content );
578       
579    $template->assign_block_vars('comments.comment', array(
580    'COMMENT_AUTHOR'=>empty($row['author'])?$lang['guest']:$row['author'],
581    'COMMENT_DATE'=>format_date( $row['date'], 'mysql_datetime', true ),
582        'COMMENT'=>$content
583        ));
584       
585    if ( $user['status'] == 'admin' )
586    {
587          $template->assign_block_vars('comments.comment.delete', array('U_COMMENT_DELETE'=>add_session_id( $url.'&amp;del='.$row['id'] )));
588    }
589  }
590
591  if ( !$user['is_the_guest']||( $user['is_the_guest'] and $conf['comments_forall'] ) )
592  {
593    $template->assign_block_vars('comments.add_comment', array());
594    // display author field if the user is not logged in
595    if ( !$user['is_the_guest'] )
596    {
597      $template->assign_block_vars('comments.add_comment.author_known', array('KNOWN_AUTHOR'=>$user['username']));
598        }
599    else
600    {
601      $template->assign_block_vars('comments.add_comment.author_field', array());
602    }
603  }
604}
605//------------------------------------------------------------ log informations
606pwg_log( 'picture', $title_img, $picture['current']['file'] );
607mysql_close();
608
609$template->pparse('picture');
610include(PHPWG_ROOT_PATH.'include/page_tail.php');
611?>
Note: See TracBrowser for help on using the repository browser.