source: trunk/category.php @ 26

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

* empty log message *

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