source: branches/release-1_3/category.php @ 277

Last change on this file since 277 was 276, checked in by gweltas, 20 years ago

Minor corrections of PHP warnings

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