source: trunk/category.php @ 166

Last change on this file since 166 was 166, checked in by z0rglub, 21 years ago

Adding a page for non admin to see last users comments

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.8 KB
Line 
1<?php
2/***************************************************************************
3 *                               category.php                              *
4 *                            -------------------                          *
5 *   application   : PhpWebGallery 1.3 <http://phpwebgallery.net>          *
6 *   author        : Pierrick LE GALL <pierrick@z0rglub.com>               *
7 *                                                                         *
8 *   $Id: category.php 166 2003-10-04 16:08:53Z z0rglub $
9 *                                                                         *
10 ***************************************************************************/
11
12/***************************************************************************
13 *                                                                         *
14 *   This program is free software; you can redistribute it and/or modify  *
15 *   it under the terms of the GNU General Public License as published by  *
16 *   the Free Software Foundation;                                         *
17 *                                                                         *
18 ***************************************************************************/
19// determine the initial instant to indicate the generation time of this page
20$t1 = explode( ' ', microtime() );
21$t2 = explode( '.', $t1[0] );
22$t2 = $t1[1].'.'.$t2[1];
23//----------------------------------------------------------- personnal include
24include_once( './include/init.inc.php' );
25//---------------------------------------------------------------------- logout
26if ( $_GET['act'] == 'logout' and isset( $_COOKIE['id'] ) )
27{
28  // cookie deletion if exists
29  setcookie( 'id', '', 0, cookie_path() );
30  $url = 'category.php';
31  header( 'Request-URI: '.$url ); 
32  header( 'Content-Location: '.$url ); 
33  header( 'Location: '.$url );
34  exit();
35}
36//-------------------------------------------------- access authorization check
37// creating the plain structure : array of all the available categories and
38// their relative informations, see the definition of the function
39// get_plain_structure for further details.
40$page['plain_structure'] = get_plain_structure();
41
42check_cat_id( $_GET['cat'] );
43check_login_authorization();
44if ( isset( $page['cat'] ) and is_numeric( $page['cat'] ) )
45{
46  check_restrictions( $page['cat'] );
47}
48//-------------------------------------------------------------- initialization
49// creation of the array containing the cat ids to expand in the menu
50// $page['tab_expand'] contains an array with the category ids
51// $page['expand'] contains the string to display in URL with comma
52$page['tab_expand'] = array();
53if ( isset ( $_GET['expand'] ) and $_GET['expand'] != 'all' )
54{
55  $tab_expand = explode( ',', $_GET['expand'] );
56  foreach ( $tab_expand as $id ) {
57    if ( is_numeric( $id ) ) array_push( $page['tab_expand'], $id );
58  }
59  $page['expand'] = implode( ',', $page['tab_expand'] );
60}
61// in case of expanding all authorized cats
62// The $page['expand'] equals 'all' and
63// $page['tab_expand'] contains all the authorized cat ids
64if ( $user['expand'] or $_GET['expand'] == 'all' )
65{
66  $page['tab_expand'] = array();
67  $page['expand'] = 'all';
68}
69// detection of the start picture to display
70if ( !isset( $_GET['start'] )
71     or !is_numeric( $_GET['start'] )
72     or ( is_numeric( $_GET['start'] ) and $_GET['start'] < 0 ) )
73{
74  $page['start'] = 0;
75}
76else
77{
78  $page['start'] = $_GET['start'];
79}
80// Sometimes, a "num" is provided in the URL. It is the number
81// of the picture to show. This picture must be in the thumbnails page.
82// We have to find the right $page['start'] that show the num picture
83// in this category
84if ( is_numeric( $_GET['num'] ) and $_GET['num'] >= 0 )
85{
86  $page['start'] = floor( $_GET['num'] / $user['nb_image_page'] );
87  $page['start']*= $user['nb_image_page'];
88}
89// creating the structure of the categories (useful for displaying the menu)
90$page['structure'] = create_structure( '', $user['restrictions'] );
91$page['structure'] = update_structure( $page['structure'] );
92initialize_category();
93//----------------------------------------------------- template initialization
94$vtp = new VTemplate;
95$handle = $vtp->Open( './template/'.$user['template'].'/category.vtp' );
96initialize_template();
97$tpl = array(
98  'categories','hint_category','sub-cat','images_available','total',
99  'title_menu','nb_image_category','send_mail','title_send_mail',
100  'generation_time','connected_user','recent_image','days','generation_time',
101  'favorite_cat_hint','favorite_cat','stats','most_visited_cat_hint',
102  'most_visited_cat','recent_cat','recent_cat_hint','upload_picture',
103  'comments' );
104templatize_array( $tpl, 'lang', $handle );
105
106$tpl = array( 'mail_webmaster','webmaster','top_number','version','site_url' );
107templatize_array( $tpl, 'conf', $handle );
108
109$tpl = array( 'short_period','long_period','lien_collapsed', 'username' );
110templatize_array( $tpl, 'user', $handle );
111
112$tpl = array( 'title','navigation_bar','cat_comment','cat_nb_images' );
113templatize_array( $tpl, 'page', $handle );
114
115// special global template vars
116$vtp->setGlobalVar( $handle, 'icon_short', get_icon( time() ) );
117$icon_long = get_icon( time() - ( $user['short_period'] * 24 * 60 * 60 + 1 ) );
118$vtp->setGlobalVar( $handle, 'icon_long', $icon_long );
119$nb_total_pictures = count_images( $page['structure'] );
120$vtp->setGlobalVar( $handle, 'nb_total_pictures',$nb_total_pictures );
121//------------------------------------------------------------- categories menu
122// normal categories
123foreach ( $page['structure'] as $category ) {
124  // display category is a function relative to the template
125  display_category( $category, '&nbsp;', $handle );
126}
127// favorites cat
128if ( !$user['is_the_guest'] )
129{
130  $vtp->addSession( $handle, 'favorites' );
131  $url = './category.php?cat=fav&amp;expand='.$page['expand'];
132  $vtp->setVar( $handle, 'favorites.url', add_session_id( $url ) );
133  // searching the number of favorite picture
134  $query = 'SELECT COUNT(*) AS count';
135  $query.= ' FROM '.PREFIX_TABLE.'favorites';
136  $query.= ' WHERE user_id = '.$user['id'].';';
137  $result = mysql_query( $query );
138  $row = mysql_fetch_array( $result );
139  $vtp->setVar( $handle, 'favorites.nb_favorites', $row['count'] );
140  $vtp->closeSession( $handle, 'favorites' );
141}
142// most visited pictures category
143$url = './category.php?cat=most_visited&amp;expand='.$page['expand'];
144$vtp->setGlobalVar( $handle, 'most_visited_url', add_session_id( $url ) );
145// recent pictures
146$url = './category.php?cat=recent&amp;expand='.$page['expand'];
147$vtp->setGlobalVar( $handle, 'recent_url', add_session_id( $url ) );
148//--------------------------------------------------------------------- summary
149$vtp->addSession( $handle, 'summary' );
150$vtp->setVar( $handle, 'summary.url', './identification.php' );
151if ( !$user['is_the_guest'] )
152{
153  $vtp->setVar( $handle, 'summary.title', '' );
154  $vtp->setVar( $handle, 'summary.name',replace_space($lang['change_login']));
155}
156else
157{
158  $vtp->setVar( $handle, 'summary.title', $lang['hint_login'] );
159  $vtp->setVar( $handle, 'summary.name',  replace_space( $lang['login'] ) );
160}
161$vtp->closeSession( $handle, 'summary' );
162// links for registered users
163if ( !$user['is_the_guest'] )
164{
165  // logout link
166  $vtp->addSession( $handle, 'summary' );
167  $vtp->setVar( $handle, 'summary.url', './category.php?act=logout' );
168  $vtp->setVar( $handle, 'summary.title', '' );
169  $vtp->setVar( $handle, 'summary.name', replace_space( $lang['logout'] ) );
170  $vtp->closeSession( $handle, 'summary' );
171  // customization link
172  $vtp->addSession( $handle, 'summary' );
173  $url = './profile.php?cat='.$page['cat'];
174  $url.= '&amp;expand='.$page['expand'];
175  if ( $page['cat'] == 'search' )
176  {
177    $url.= '&amp;search='.$_GET['search'].'&amp;mode='.$_GET['mode'];
178  }
179  $vtp->setVar( $handle, 'summary.url', add_session_id( $url ) );
180  $vtp->setVar( $handle, 'summary.title', $lang['hint_customize'] );
181  $vtp->setVar( $handle, 'summary.name', replace_space( $lang['customize'] ) );
182  $vtp->closeSession( $handle, 'summary' );
183}
184// search link
185$vtp->addSession( $handle, 'summary' );
186$vtp->setVar( $handle, 'summary.url', add_session_id( './search.php' ) );
187$vtp->setVar( $handle, 'summary.title', $lang['hint_search'] );
188$vtp->setVar( $handle, 'summary.name', replace_space( $lang['search'] ) );
189$vtp->closeSession( $handle, 'summary' );
190// comments link
191$vtp->addSession( $handle, 'summary' );
192$vtp->setVar( $handle, 'summary.url', add_session_id( './comments.php' ) );
193$vtp->setVar( $handle, 'summary.title', $lang['hint_comments'] );
194$vtp->setVar( $handle, 'summary.name', replace_space( $lang['comments'] ) );
195$vtp->closeSession( $handle, 'summary' );
196// about link
197$vtp->addSession( $handle, 'summary' );
198$vtp->setVar( $handle, 'summary.url', './about.php?'.
199              str_replace( '&', '&amp;', $_SERVER['QUERY_STRING'] ) );
200$vtp->setVar( $handle, 'summary.title', $lang['hint_about'] );
201$vtp->setVar( $handle, 'summary.name', replace_space( $lang['about'] ) );
202$vtp->closeSession( $handle, 'summary' );
203// administration link
204if ( $user['status'] == 'admin' )
205{
206  $vtp->addSession( $handle, 'summary' );
207  $vtp->setVar( $handle, 'summary.url',
208                add_session_id( './admin/admin.php' ) );
209  $vtp->setVar( $handle, 'summary.title', $lang['hint_admin'] );
210  $vtp->setVar( $handle, 'summary.name', replace_space( $lang['admin'] ) );
211  $vtp->closeSession( $handle, 'summary' );
212}
213//-------------------------------------------------------------- category title
214if ( isset ( $page['cat'] ) )
215{
216  if ( is_numeric( $page['cat'] ) )
217  {
218    $cat_title = get_cat_display_name( $page['cat_name'], '<br />',
219                                    'font-style:italic;' );
220    $vtp->setGlobalVar( $handle, "cat_title", $cat_title );
221  }
222  else
223  {
224    if ( $page['cat'] == 'search' )
225    {
226      $page['title'].= ' : <span style="font-style:italic;">';
227      $page['title'].= $_GET['search']."</span>";
228    }
229    $page['title'] = replace_space( $page['title'] );
230    $vtp->setGlobalVar( $handle, "cat_title", $page['title'] );
231  }
232}
233else
234{
235  $vtp->setGlobalVar( $handle, "cat_title",
236                      replace_space( $lang['no_category'] ) );
237}
238//------------------------------------------------------------------ thumbnails
239if ( isset( $page['cat'] ) and $page['cat_nb_images'] != 0 )
240{
241  $array_cat_directories = array();
242 
243  $query = 'SELECT distinct(id),file,date_available,tn_ext,name,filesize';
244  $query.= ',storage_category_id';
245  $query.= ' FROM '.PREFIX_TABLE.'images AS i';
246  $query.= ' LEFT JOIN '.PREFIX_TABLE.'image_category AS ic ON id=ic.image_id';
247  $query.= $page['where'];
248  $query.= $conf['order_by'];
249  $query.= ' LIMIT '.$page['start'].','.$page['nb_image_page'];
250  $query.= ';';
251  $result = mysql_query( $query );
252
253  $vtp->addSession( $handle, 'thumbnails' );
254  $vtp->addSession( $handle, 'line' );
255  // iteration counter to use a new <tr> every "$nb_image_line" pictures
256  $cell_number = 1;
257  // iteration counter to be sure not to create too much lines in the table
258  $line_number = 1;
259  while ( $row = mysql_fetch_array( $result ) )
260  {
261    // retrieving the storage dir of the picture
262    if ( $array_cat_directories[$row['storage_category_id']] == '' )
263    {
264      $array_cat_directories[$row['storage_category_id']] =
265        get_complete_dir( $row['storage_category_id'] );
266    }
267    $cat_directory = $array_cat_directories[$row['storage_category_id']];
268
269    $file = get_filename_wo_extension( $row['file'] );
270    // name of the picture
271    if ( $row['name'] != '' ) $name = $row['name'];
272    else                      $name = str_replace( '_', ' ', $file );
273
274    if ( $page['cat'] == 'search' )
275    {
276      $name = replace_search( $name, $_GET['search'] );
277    }
278    // thumbnail url
279    $thumbnail_url = $cat_directory;
280    $thumbnail_url.= 'thumbnail/'.$conf['prefix_thumbnail'];
281    $thumbnail_url.= $file.'.'.$row['tn_ext'];
282    // message in title for the thumbnail
283    $thumbnail_title = $row['file'];
284    if ( $row['filesize'] == '' )
285      $poids = floor( filesize( $cat_directory.$row['file'] ) / 1024 );
286    else
287      $poids = $row['filesize'];
288    $thumbnail_title .= ' : '.$poids.' KB';
289    // url link on picture.php page
290    $url_link = './picture.php?cat='.$page['cat'];
291    $url_link.= '&amp;image_id='.$row['id'].'&amp;expand='.$page['expand'];
292    if ( $page['cat'] == 'search' )
293    {
294      $url_link.= '&amp;search='.$_GET['search'].'&amp;mode='.$_GET['mode'];
295    }
296    // date of availability for creation icon
297    list( $year,$month,$day ) = explode( '-', $row['date_available'] );
298    $date = mktime( 0, 0, 0, $month, $day, $year );
299    // sending vars to display
300    $vtp->addSession( $handle, 'thumbnail' );
301    $vtp->setVar( $handle, 'thumbnail.url', add_session_id( $url_link ) );
302    $vtp->setVar( $handle, 'thumbnail.src', $thumbnail_url );
303    $vtp->setVar( $handle, 'thumbnail.alt', $row['file'] );
304    $vtp->setVar( $handle, 'thumbnail.title', $thumbnail_title );
305    $vtp->setVar( $handle, 'thumbnail.name', $name );
306    $vtp->setVar( $handle, 'thumbnail.icon', get_icon( $date ) );
307
308    if ( $conf['show_comments'] and $user['show_nb_comments'] )
309    {
310      $vtp->addSession( $handle, 'nb_comments' );
311      $query = 'SELECT COUNT(*) AS nb_comments';
312      $query.= ' FROM '.PREFIX_TABLE.'comments';
313      $query.= ' WHERE image_id = '.$row['id'];
314      $query.= " AND validated = 'true'";
315      $query.= ';';
316      $row = mysql_fetch_array( mysql_query( $query ) );
317      $vtp->setVar( $handle, 'nb_comments.nb', $row['nb_comments'] );
318      $vtp->closeSession( $handle, 'nb_comments' );
319    }
320   
321    $vtp->closeSession( $handle, 'thumbnail' );
322   
323    if ( $cell_number++ == $user['nb_image_line'] )
324    {
325      // creating a new line
326      $vtp->closeSession( $handle, 'line' );
327      // the number of the next cell is 1
328      $cell_number = 1;
329      // we only create a new line if it does not exceed the maximum line
330      // per page for the logged user
331      if ( $line_number++ < $user['nb_line_page'] )
332      {
333        $vtp->addSession( $handle, 'line' );
334      }
335    }
336  }
337  $vtp->closeSession( $handle, 'thumbnails' );
338}
339//-------------------------------------------------------------- empty category
340elseif ( ( isset( $page['cat'] )
341           and is_numeric( $page['cat'] )
342           and $page['cat_nb_images'] == 0 )
343         or $_GET['cat'] == '' )
344{
345  $vtp->addSession( $handle, 'thumbnails' );
346  $vtp->addSession( $handle, 'line' );
347
348  $subcats = get_non_empty_subcat_ids( $page['cat'] );
349  $cell_number = 1;
350  $i = 0;
351  foreach ( $subcats as $subcat_id => $non_empty_id ) {
352    $name = '<img src="'.$user['lien_collapsed'].'" style="border:none;"';
353    $name.= ' alt="&gt;"/> ';
354    $name.= '[ <span style="font-weight:bold;">';
355    $name.= $page['plain_structure'][$subcat_id]['name'];
356    $name.= '</span> ]';
357
358    // searching the representative picture of the category
359    $query = 'SELECT representative_picture_id';
360    $query.= ' FROM '.PREFIX_TABLE.'categories';
361    $query.= ' WHERE id = '.$non_empty_id;
362    $row = mysql_fetch_array( mysql_query( $query ) );
363   
364    $query = 'SELECT file,tn_ext,storage_category_id';
365    $query.= ' FROM '.PREFIX_TABLE.'images';
366    $query.= ' LEFT JOIN '.PREFIX_TABLE.'image_category ON id = image_id';
367    $query.= ' WHERE category_id = '.$non_empty_id;
368    // if the category has a representative picture, this is its thumbnail
369    // tha will be displayed !
370    if ( $row['representative_picture_id'] != '' )
371      $query.= ' AND id = '.$row['representative_picture_id'];
372    else
373      $query.= ' ORDER BY RAND()';
374    $query.= ' LIMIT 0,1';
375    $query.= ';';
376    $image_result = mysql_query( $query );
377    $image_row    = mysql_fetch_array( $image_result );
378
379    $file = get_filename_wo_extension( $image_row['file'] );
380
381    // creating links for thumbnail and associated category
382    $thumbnail_link = get_complete_dir( $image_row['storage_category_id'] );
383    $thumbnail_link.= 'thumbnail/'.$conf['prefix_thumbnail'];
384    $thumbnail_link.= $file.'.'.$image_row['tn_ext'];
385
386    $thumbnail_title = $lang['hint_category'];
387
388    $url_link = './category.php?cat='.$subcat_id;
389    if ( !in_array( $page['cat'], $page['tab_expand'] ) )
390    {
391      array_push( $page['tab_expand'], $page['cat'] );
392      $page['expand'] = implode( ',', $page['tab_expand'] );
393    }
394    $url_link.= '&amp;expand='.$page['expand'];
395    // we add the category to explore in the expand list
396    if ( $page['expand'] != '' ) $url_link.= ',';
397    $url_link.= $subcat_id;
398
399    $date = $page['plain_structure'][$subcat_id]['date_last'];
400
401    // sending vars to display
402    $vtp->addSession( $handle, 'thumbnail' );
403    $vtp->setVar( $handle, 'thumbnail.url', add_session_id( $url_link ) );
404    $vtp->setVar( $handle, 'thumbnail.src', $thumbnail_link );
405    $vtp->setVar( $handle, 'thumbnail.alt', $image_row['file'] );
406    $vtp->setVar( $handle, 'thumbnail.title', $thumbnail_title );
407    $vtp->setVar( $handle, 'thumbnail.name', $name );
408    $vtp->setVar( $handle, 'thumbnail.icon', get_icon( $date ) );
409    $vtp->closeSession( $handle, 'thumbnail' );
410
411    if ( $cell_number++ == $user['nb_image_line'] )
412    {
413      $vtp->closeSession( $handle, 'line' );
414      $cell_number = 1;
415      // we open a new line if the subcat was not the last one
416      if ( $i++ < count( $subcats ) - 1 )
417      {
418        $vtp->addSession( $handle, 'line' );
419      }
420    }
421  }
422  if ( $i < count( $subcats ) - 1 )
423  {
424    $vtp->closeSession( $handle, 'line' );
425  }
426  $vtp->closeSession( $handle, 'thumbnails' );
427}
428//------------------------------------------------------- category informations
429if ( isset ( $page['cat'] ) )
430{
431  $vtp->addSession( $handle, 'cat_infos' );
432  // navigation bar
433  if ( $page['navigation_bar'] != '' )
434  {
435    $vtp->addSession( $handle, 'navigation' );
436    $vtp->closeSession( $handle, 'navigation' );
437  }
438  // category comment
439  if ( isset( $page['comment'] ) and $page['comment'] != '' )
440  {
441    $vtp->addSession( $handle, 'comment' );
442    $vtp->setVar( $handle, 'comment.cat_comment', $page['comment'] );
443    $vtp->closeSession( $handle, 'comment' );
444  }
445  // total number of pictures in the category
446  if ( is_numeric( $page['cat'] ) )
447  {
448    $vtp->setVar( $handle, 'cat_infos.cat_name',
449                  get_cat_display_name( $page['cat_name'], ' - ',
450                                        'font-style:italic;' ) );
451  }
452  else
453  {
454    $vtp->setVar( $handle, 'cat_infos.cat_name', $page['title'] );
455  }
456  // upload a picture in the category
457  if ( $page['cat_site_id'] == 1
458       and $conf['upload_available']
459       and $page['cat_uploadable'] )
460  {
461    $vtp->addSession( $handle, 'upload' );
462    $url = './upload.php?cat='.$page['cat'].'&amp;expand='.$page['expand'];
463    $vtp->setVar( $handle, 'upload.url', add_session_id( $url ) );
464    $vtp->closeSession( $handle, 'upload' );
465  }
466  $vtp->closeSession( $handle, 'cat_infos' );
467}
468//------------------------------------------------------------ log informations
469pwg_log( 'category', $page['title'] );
470mysql_close();
471//------------------------------------------------------------- generation time
472$time = get_elapsed_time( $t2, get_moment() );
473$vtp->setGlobalVar( $handle, 'time', $time );
474//----------------------------------------------------------- html code display
475$code = $vtp->Display( $handle, 0 );
476echo $code;
477?>
Note: See TracBrowser for help on using the repository browser.