source: trunk/category.php @ 148

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

If informations (filesize,width,height) are not given in the database about
a picture, they can be found with the file itself

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.5 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 148 2003-09-20 21:44:57Z 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// about link
191$vtp->addSession( $handle, 'summary' );
192$vtp->setVar( $handle, 'summary.url', './about.php?'.
193              str_replace( '&', '&amp;', $_SERVER['QUERY_STRING'] ) );
194$vtp->setVar( $handle, 'summary.title', $lang['hint_about'] );
195$vtp->setVar( $handle, 'summary.name', replace_space( $lang['about'] ) );
196$vtp->closeSession( $handle, 'summary' );
197// administration link
198if ( $user['status'] == 'admin' )
199{
200  $vtp->addSession( $handle, 'summary' );
201  $vtp->setVar( $handle, 'summary.url',
202                add_session_id( './admin/admin.php' ) );
203  $vtp->setVar( $handle, 'summary.title', $lang['hint_admin'] );
204  $vtp->setVar( $handle, 'summary.name', replace_space( $lang['admin'] ) );
205  $vtp->closeSession( $handle, 'summary' );
206}
207//-------------------------------------------------------------- category title
208if ( isset ( $page['cat'] ) )
209{
210  if ( is_numeric( $page['cat'] ) )
211  {
212    $cat_title = get_cat_display_name( $page['cat_name'], '<br />',
213                                    'font-style:italic;' );
214    $vtp->setGlobalVar( $handle, "cat_title", $cat_title );
215  }
216  else
217  {
218    if ( $page['cat'] == 'search' )
219    {
220      $page['title'].= ' : <span style="font-style:italic;">';
221      $page['title'].= $_GET['search']."</span>";
222    }
223    $page['title'] = replace_space( $page['title'] );
224    $vtp->setGlobalVar( $handle, "cat_title", $page['title'] );
225  }
226}
227else
228{
229  $vtp->setGlobalVar( $handle, "cat_title",
230                      replace_space( $lang['no_category'] ) );
231}
232//------------------------------------------------------------------ thumbnails
233if ( isset( $page['cat'] ) and $page['cat_nb_images'] != 0 )
234{
235  $array_cat_directories = array();
236 
237  $query = 'SELECT distinct(id),file,date_available,tn_ext,name,filesize';
238  $query.= ',storage_category_id';
239  $query.= ' FROM '.PREFIX_TABLE.'images AS i';
240  $query.= ' LEFT JOIN '.PREFIX_TABLE.'image_category AS ic ON id=ic.image_id';
241  $query.= $page['where'];
242  $query.= $conf['order_by'];
243  $query.= ' LIMIT '.$page['start'].','.$page['nb_image_page'];
244  $query.= ';';
245  $result = mysql_query( $query );
246
247  $vtp->addSession( $handle, 'thumbnails' );
248  $vtp->addSession( $handle, 'line' );
249  // iteration counter to use a new <tr> every "$nb_image_line" pictures
250  $cell_number = 1;
251  // iteration counter to be sure not to create too much lines in the table
252  $line_number = 1;
253  while ( $row = mysql_fetch_array( $result ) )
254  {
255    // retrieving the storage dir of the picture
256    if ( $array_cat_directories[$row['storage_category_id']] == '' )
257    {
258      $array_cat_directories[$row['storage_category_id']] =
259        get_complete_dir( $row['storage_category_id'] );
260    }
261    $cat_directory = $array_cat_directories[$row['storage_category_id']];
262
263    $file = get_filename_wo_extension( $row['file'] );
264    // name of the picture
265    if ( $row['name'] != '' ) $name = $row['name'];
266    else                      $name = str_replace( '_', ' ', $file );
267
268    if ( $page['cat'] == 'search' )
269    {
270      $name = replace_search( $name, $_GET['search'] );
271    }
272    // thumbnail url
273    $thumbnail_url = $cat_directory;
274    $thumbnail_url.= 'thumbnail/'.$conf['prefix_thumbnail'];
275    $thumbnail_url.= $file.'.'.$row['tn_ext'];
276    // message in title for the thumbnail
277    $thumbnail_title = $row['file'];
278    if ( $row['filesize'] == '' )
279      $poids = floor( filesize( $cat_directory.$row['file'] ) / 1024 );
280    else
281      $poids = $row['filesize'];
282    $thumbnail_title .= ' : '.$poids.' KB';
283    // url link on picture.php page
284    $url_link = './picture.php?cat='.$page['cat'];
285    $url_link.= '&amp;image_id='.$row['id'].'&amp;expand='.$page['expand'];
286    if ( $page['cat'] == 'search' )
287    {
288      $url_link.= '&amp;search='.$_GET['search'].'&amp;mode='.$_GET['mode'];
289    }
290    // date of availability for creation icon
291    list( $year,$month,$day ) = explode( '-', $row['date_available'] );
292    $date = mktime( 0, 0, 0, $month, $day, $year );
293    // sending vars to display
294    $vtp->addSession( $handle, 'thumbnail' );
295    $vtp->setVar( $handle, 'thumbnail.url', add_session_id( $url_link ) );
296    $vtp->setVar( $handle, 'thumbnail.src', $thumbnail_url );
297    $vtp->setVar( $handle, 'thumbnail.alt', $row['file'] );
298    $vtp->setVar( $handle, 'thumbnail.title', $thumbnail_title );
299    $vtp->setVar( $handle, 'thumbnail.name', $name );
300    $vtp->setVar( $handle, 'thumbnail.icon', get_icon( $date ) );
301
302    if ( $conf['show_comments'] and $user['show_nb_comments'] )
303    {
304      $vtp->addSession( $handle, 'nb_comments' );
305      $query = 'SELECT COUNT(*) AS nb_comments';
306      $query.= ' FROM '.PREFIX_TABLE.'comments';
307      $query.= ' WHERE image_id = '.$row['id'];
308      $query.= " AND validated = 'true'";
309      $query.= ';';
310      $row = mysql_fetch_array( mysql_query( $query ) );
311      $vtp->setVar( $handle, 'nb_comments.nb', $row['nb_comments'] );
312      $vtp->closeSession( $handle, 'nb_comments' );
313    }
314   
315    $vtp->closeSession( $handle, 'thumbnail' );
316   
317    if ( $cell_number++ == $user['nb_image_line'] )
318    {
319      // creating a new line
320      $vtp->closeSession( $handle, 'line' );
321      // the number of the next cell is 1
322      $cell_number = 1;
323      // we only create a new line if it does not exceed the maximum line
324      // per page for the logged user
325      if ( $line_number++ < $user['nb_line_page'] )
326      {
327        $vtp->addSession( $handle, 'line' );
328      }
329    }
330  }
331  $vtp->closeSession( $handle, 'thumbnails' );
332}
333//-------------------------------------------------------------- empty category
334elseif ( ( isset( $page['cat'] )
335           and is_numeric( $page['cat'] )
336           and $page['cat_nb_images'] == 0 )
337         or $_GET['cat'] == '' )
338{
339  $vtp->addSession( $handle, 'thumbnails' );
340  $vtp->addSession( $handle, 'line' );
341
342  $subcats = get_non_empty_subcat_ids( $page['cat'] );
343  $cell_number = 1;
344  $i = 0;
345  foreach ( $subcats as $subcat_id => $non_empty_id ) {
346    $name = '<img src="'.$user['lien_collapsed'].'" style="border:none;"';
347    $name.= ' alt="&gt;"/> ';
348    $name.= '[ <span style="font-weight:bold;">';
349    $name.= $page['plain_structure'][$subcat_id]['name'];
350    $name.= '</span> ]';
351
352    // searching the representative picture of the category
353    $query = 'SELECT representative_picture_id';
354    $query.= ' FROM '.PREFIX_TABLE.'categories';
355    $query.= ' WHERE id = '.$non_empty_id;
356    $row = mysql_fetch_array( mysql_query( $query ) );
357   
358    $query = 'SELECT file,tn_ext,storage_category_id';
359    $query.= ' FROM '.PREFIX_TABLE.'images';
360    $query.= ' LEFT JOIN '.PREFIX_TABLE.'image_category ON id = image_id';
361    $query.= ' WHERE category_id = '.$non_empty_id;
362    // if the category has a representative picture, this is its thumbnail
363    // tha will be displayed !
364    if ( $row['representative_picture_id'] != '' )
365      $query.= ' AND id = '.$row['representative_picture_id'];
366    else
367      $query.= ' ORDER BY RAND()';
368    $query.= ' LIMIT 0,1';
369    $query.= ';';
370    $image_result = mysql_query( $query );
371    $image_row    = mysql_fetch_array( $image_result );
372
373    $file = get_filename_wo_extension( $image_row['file'] );
374
375    // creating links for thumbnail and associated category
376    $thumbnail_link = get_complete_dir( $image_row['storage_category_id'] );
377    $thumbnail_link.= 'thumbnail/'.$conf['prefix_thumbnail'];
378    $thumbnail_link.= $file.'.'.$image_row['tn_ext'];
379
380    $thumbnail_title = $lang['hint_category'];
381
382    $url_link = './category.php?cat='.$subcat_id;
383    if ( !in_array( $page['cat'], $page['tab_expand'] ) )
384    {
385      array_push( $page['tab_expand'], $page['cat'] );
386      $page['expand'] = implode( ',', $page['tab_expand'] );
387    }
388    $url_link.= '&amp;expand='.$page['expand'];
389    // we add the category to explore in the expand list
390    if ( $page['expand'] != '' ) $url_link.= ',';
391    $url_link.= $subcat_id;
392
393    $date = $page['plain_structure'][$subcat_id]['date_last'];
394
395    // sending vars to display
396    $vtp->addSession( $handle, 'thumbnail' );
397    $vtp->setVar( $handle, 'thumbnail.url', add_session_id( $url_link ) );
398    $vtp->setVar( $handle, 'thumbnail.src', $thumbnail_link );
399    $vtp->setVar( $handle, 'thumbnail.alt', $image_row['file'] );
400    $vtp->setVar( $handle, 'thumbnail.title', $thumbnail_title );
401    $vtp->setVar( $handle, 'thumbnail.name', $name );
402    $vtp->setVar( $handle, 'thumbnail.icon', get_icon( $date ) );
403    $vtp->closeSession( $handle, 'thumbnail' );
404
405    if ( $cell_number++ == $user['nb_image_line'] )
406    {
407      $vtp->closeSession( $handle, 'line' );
408      $cell_number = 1;
409      // we open a new line if the subcat was not the last one
410      if ( $i++ < count( $subcats ) - 1 )
411      {
412        $vtp->addSession( $handle, 'line' );
413      }
414    }
415  }
416  if ( $i < count( $subcats ) - 1 )
417  {
418    $vtp->closeSession( $handle, 'line' );
419  }
420  $vtp->closeSession( $handle, 'thumbnails' );
421}
422//------------------------------------------------------- category informations
423if ( isset ( $page['cat'] ) )
424{
425  $vtp->addSession( $handle, 'cat_infos' );
426  // navigation bar
427  if ( $page['navigation_bar'] != '' )
428  {
429    $vtp->addSession( $handle, 'navigation' );
430    $vtp->closeSession( $handle, 'navigation' );
431  }
432  // category comment
433  if ( isset( $page['comment'] ) and $page['comment'] != '' )
434  {
435    $vtp->addSession( $handle, 'comment' );
436    $vtp->setVar( $handle, 'comment.cat_comment', $page['comment'] );
437    $vtp->closeSession( $handle, 'comment' );
438  }
439  // total number of pictures in the category
440  if ( is_numeric( $page['cat'] ) )
441  {
442    $vtp->setVar( $handle, 'cat_infos.cat_name',
443                  get_cat_display_name( $page['cat_name'], ' - ',
444                                        'font-style:italic;' ) );
445  }
446  else
447  {
448    $vtp->setVar( $handle, 'cat_infos.cat_name', $page['title'] );
449  }
450  // upload a picture in the category
451  if ( $page['cat_site_id'] == 1
452       and $conf['upload_available']
453       and $page['cat_uploadable'] )
454  {
455    $vtp->addSession( $handle, 'upload' );
456    $url = './upload.php?cat='.$page['cat'].'&amp;expand='.$page['expand'];
457    $vtp->setVar( $handle, 'upload.url', add_session_id( $url ) );
458    $vtp->closeSession( $handle, 'upload' );
459  }
460  $vtp->closeSession( $handle, 'cat_infos' );
461}
462//------------------------------------------------------------ log informations
463pwg_log( 'category', $page['title'] );
464mysql_close();
465//------------------------------------------------------------- generation time
466$time = get_elapsed_time( $t2, get_moment() );
467$vtp->setGlobalVar( $handle, 'time', $time );
468//----------------------------------------------------------- html code display
469$code = $vtp->Display( $handle, 0 );
470echo $code;
471?>
Note: See TracBrowser for help on using the repository browser.