source: trunk/admin/include/functions.php @ 177

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

Bug in the deletion of favorites : even if the category was authorized, the
favorites where deleted for the user

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1<?php
2/***************************************************************************
3 *                               functions.php                             *
4 *                            -------------------                          *
5 *   application   : PhpWebGallery 1.3 <http://phpwebgallery.net>          *
6 *   author        : Pierrick LE GALL <pierrick@z0rglub.com>               *
7 *                                                                         *
8 *   $Id: functions.php 167 2003-10-04 17:13:19Z 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
20$tab_ext_create_TN = array ( 'jpg', 'png', 'JPG', 'PNG' );
21
22// is_image returns true if the given $filename (including the path) is a
23// picture according to its format and its extension.
24// As GD library can only generate pictures from jpeg and png files, if you
25// ask if the filename is an image for thumbnail creation (second parameter
26// set to true), the only authorized formats are jpeg and png.
27function is_image( $filename, $create_thumbnail = false )
28{
29  global $conf, $tab_ext_create_TN;
30
31  if ( is_file( $filename ) )
32  {
33    $size = getimagesize( $filename );
34    // $size[2] == 1 means GIF
35    // $size[2] == 2 means JPG
36    // $size[2] == 3 means PNG
37    if ( !$create_thumbnail )
38    {
39      if ( in_array( get_extension( $filename ), $conf['picture_ext'] )
40           and ( $size[2] == 1 or $size[2] == 2 or $size[2] == 3 ) )
41      {
42        return true;
43      }
44    }
45    else
46    {
47      if ( in_array( get_extension( $filename ), $tab_ext_create_TN )
48           and ( $size[2] == 2 or $size[2] == 3 ) )
49      {
50        return true;
51      }
52    }
53  }
54  return false;
55}
56       
57function TN_exists( $dir, $file )
58{
59  global $conf;
60
61  $filename = get_filename_wo_extension( $file );
62  foreach ( $conf['picture_ext'] as $ext ) {
63    $test = $dir.'/thumbnail/'.$conf['prefix_thumbnail'].$filename.'.'.$ext;
64    if ( is_file ( $test ) )
65    {
66      return $ext;
67    }
68  }
69  return false;
70}       
71       
72// The function delete_site deletes a site
73// and call the function delete_category for each primary category of the site
74function delete_site( $id )
75{
76  // destruction of the categories of the site
77  $query = 'SELECT id';
78  $query.= ' FROM '.PREFIX_TABLE.'categories';
79  $query.= ' WHERE site_id = '.$id;
80  $query.= ';';
81  $result = mysql_query( $query );
82  while ( $row = mysql_fetch_array( $result ) )
83  {
84    delete_category( $row['id'] );
85  }
86               
87  // destruction of the site
88  $query = 'DELETE FROM '.PREFIX_TABLE.'sites';
89  $query.= ' WHERE id = '.$id;
90  $query.= ';';
91  mysql_query( $query );
92}
93       
94// The function delete_category deletes the category identified by the $id
95// It also deletes (in the database) :
96//    - all the images of the images (thanks to delete_image, see further)
97//    - all the links between images and this category
98//    - all the restrictions linked to the category
99// The function works recursively.
100function delete_category( $id )
101{
102  // destruction of all the related images
103  $query = 'SELECT id';
104  $query.= ' FROM '.PREFIX_TABLE.'images';
105  $query.= ' WHERE storage_category_id = '.$id;
106  $query.= ';';
107  $result = mysql_query( $query );
108  while ( $row = mysql_fetch_array( $result ) )
109  {
110    delete_image( $row['id'] );
111  }
112
113  // destruction of the links between images and this category
114  $query = 'DELETE FROM '.PREFIX_TABLE.'image_category';
115  $query.= ' WHERE category_id = '.$id;
116  $query.= ';';
117  mysql_query( $query );
118
119  // destruction of the access linked to the category
120  $query = 'DELETE FROM '.PREFIX_TABLE.'user_access';
121  $query.= ' WHERE cat_id = '.$id;
122  $query.= ';';
123  mysql_query( $query );
124  $query = 'DELETE FROM '.PREFIX_TABLE.'group_access';
125  $query.= ' WHERE cat_id = '.$id;
126  $query.= ';';
127  mysql_query( $query );
128
129  // destruction of the sub-categories
130  $query = 'SELECT id';
131  $query.= ' FROM '.PREFIX_TABLE.'categories';
132  $query.= ' WHERE id_uppercat = '.$id;
133  $query.= ';';
134  $result = mysql_query( $query );
135  while( $row = mysql_fetch_array( $result ) )
136  {
137    delete_category( $row['id'] );
138  }
139
140  // destruction of the category
141  $query = 'DELETE FROM '.PREFIX_TABLE.'categories';
142  $query.= ' WHERE id = '.$id;
143  $query.= ';';
144  mysql_query( $query );
145}
146       
147// The function delete_image deletes the image identified by the $id
148// It also deletes (in the database) :
149//    - all the comments related to the image
150//    - all the links between categories and this image
151//    - all the favorites associated to the image
152function delete_image( $id )
153{
154  global $count_deleted;
155               
156  // destruction of the comments on the image
157  $query = 'DELETE FROM '.PREFIX_TABLE.'comments';
158  $query.= ' WHERE image_id = '.$id;
159  $query.= ';';
160  mysql_query( $query );
161
162  // destruction of the links between images and this category
163  $query = 'DELETE FROM '.PREFIX_TABLE.'image_category';
164  $query.= ' WHERE image_id = '.$id;
165  $query.= ';';
166  mysql_query( $query );
167
168  // destruction of the favorites associated with the picture
169  $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
170  $query.= ' WHERE image_id = '.$id;
171  $query.= ';';
172  mysql_query( $query );
173               
174  // destruction of the image
175  $query = 'DELETE FROM '.PREFIX_TABLE.'images';
176  $query.= ' WHERE id = '.$id;
177  $query.= ';';
178  mysql_query( $query );
179  $count_deleted++;
180}
181       
182// The delete_user function delete a user identified by the $user_id
183// It also deletes :
184//     - all the access linked to this user
185//     - all the links to any group
186//     - all the favorites linked to this user
187//     - all sessions linked to this user
188function delete_user( $user_id )
189{
190  // destruction of the access linked to the user
191  $query = 'DELETE FROM '.PREFIX_TABLE.'user_access';
192  $query.= ' WHERE user_id = '.$user_id;
193  $query.= ';';
194  mysql_query( $query );
195
196  // destruction of the group links for this user
197  $query = 'DELETE FROM '.PREFIX_TABLE.'user_group';
198  $query.= ' WHERE user_id = '.$user_id;
199  $query.= ';';
200  mysql_query( $query );
201
202  // destruction of the favorites associated with the user
203  $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
204  $query.= ' WHERE user_id = '.$user_id;
205  $query.= ';';
206  mysql_query( $query );
207
208  // destruction of the sessions linked with the user
209  $query = 'DELETE FROM '.PREFIX_TABLE.'sessions';
210  $query.= ' WHERE user_id = '.$user_id;
211  $query.= ';';
212  mysql_query( $query );
213               
214  // destruction of the user
215  $query = 'DELETE FROM '.PREFIX_TABLE.'users';
216  $query.= ' WHERE id = '.$user_id;
217  $query.= ';';
218  mysql_query( $query );
219}
220
221// delete_group deletes a group identified by its $group_id.
222// It also deletes :
223//     - all the access linked to this group
224//     - all the links between this group and any user
225function delete_group( $group_id )
226{
227  // destruction of the access linked to the group
228  $query = 'DELETE FROM '.PREFIX_TABLE.'group_access';
229  $query.= ' WHERE group_id = '.$group_id;
230  $query.= ';';
231  mysql_query( $query );
232
233  // destruction of the group links for this group
234  $query = 'DELETE FROM '.PREFIX_TABLE.'user_group';
235  $query.= ' WHERE group_id = '.$group_id;
236  $query.= ';';
237  mysql_query( $query );
238
239  // destruction of the group
240  $query = 'DELETE FROM '.PREFIX_TABLE.'groups';
241  $query.= ' WHERE id = '.$group_id;
242  $query.= ';';
243  mysql_query( $query );
244}
245
246// The check_favorites function deletes all the favorites of a user if he is
247// not allowed to see them (the category or an upper category is restricted
248// or invisible)
249function check_favorites( $user_id )
250{
251  $query = 'SELECT status';
252  $query.= ' FROM '.PREFIX_TABLE.'users';
253  $query.= ' WHERE id = '.$user_id;
254  $query.= ';';
255  $row = mysql_fetch_array( mysql_query( $query ) );
256  $status = $row['status'];
257  // retrieving all the restricted categories for this user
258  $restricted_cat = get_all_restrictions( $user_id, $status );
259  // retrieving all the favorites for this user and comparing their
260  // categories to the restricted categories
261  $query = 'SELECT image_id';
262  $query.= ' FROM '.PREFIX_TABLE.'favorites';
263  $query.= ' WHERE user_id = '.$user_id;
264  $query.= ';';
265  $result = mysql_query ( $query );
266  while ( $row = mysql_fetch_array( $result ) )
267  {
268    // for each picture, we have to check all the categories it belongs
269    // to. Indeed if a picture belongs to category_1 and category_2 and that
270    // category_2 is not restricted to the user, he can have the picture as
271    // favorite.
272    $query = 'SELECT DISTINCT(category_id) as category_id';
273    $query.= ' FROM '.PREFIX_TABLE.'image_category';
274    $query.= ' WHERE image_id = '.$row['image_id'];
275    $query.= ';';
276    $picture_result = mysql_query( $query );
277    $picture_cat = array();
278    while ( $picture_row = mysql_fetch_array( $picture_result ) )
279    {
280      array_push( $picture_cat, $picture_row['category_id'] );
281    }
282    if ( count( array_diff( $picture_cat, $restricted_cat ) ) == 0 )
283    {
284      $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
285      $query.= ' WHERE image_id = '.$row['image_id'];
286      $query.= ' AND user_id = '.$user_id;
287      $query.= ';';
288      mysql_query( $query );
289    }
290  }
291}
292
293// update_category updates calculated informations about a category :
294// date_last and nb_images. It also verifies that the representative picture
295// is really linked to the category.
296function update_category( $id = 'all' )
297{
298  if ( $id == 'all' )
299  {
300    $query = 'SELECT id';
301    $query.= ' FROM '.PREFIX_TABLE.'categories';
302    $query.= ';';
303    $result = mysql_query( $query );
304    while ( $row = mysql_fetch_array( $result ) )
305    {
306      // recursive call
307      update_category( $row['id'] );
308    }
309  }
310  else if ( is_numeric( $id ) )
311  {
312    // updating the number of pictures
313    $query = 'SELECT COUNT(*) as nb_images';
314    $query.= ' FROM '.PREFIX_TABLE.'image_category';
315    $query.= ' WHERE category_id = '.$id;
316    $query.= ';';
317    $row = mysql_fetch_array( mysql_query( $query ) );
318    $query = 'UPDATE '.PREFIX_TABLE.'categories';
319    $query.= ' SET nb_images = '.$row['nb_images'];
320    $query.= ' WHERE id = '.$id;
321    $query.= ';';
322    mysql_query( $query );
323    // updating the date_last
324    $query = 'SELECT date_available';
325    $query.= ' FROM '.PREFIX_TABLE.'images';
326    $query.= ' LEFT JOIN '.PREFIX_TABLE.'image_category ON id = image_id';
327    $query.= ' WHERE category_id = '.$id;
328    $query.= ' ORDER BY date_available DESC';
329    $query.= ' LIMIT 0,1';
330    $query.= ';';
331    $row = mysql_fetch_array( mysql_query( $query ) );
332    $query = 'UPDATE '.PREFIX_TABLE.'categories';
333    $query.= " SET date_last = '".$row['date_available']."'";
334    $query.= ' WHERE id = '.$id;
335    $query.= ';';
336    mysql_query( $query );
337    // updating the representative_picture_id : if the representative
338    // picture of the category is not any more linked to the category, we
339    // have to set representative_picture_id to NULL
340    $query = 'SELECT representative_picture_id';
341    $query.= ' FROM '.PREFIX_TABLE.'categories';
342    $query.= ' WHERE id = '.$id;
343    $row = mysql_fetch_array( mysql_query( $query ) );
344    // if the category has no representative picture (ie
345    // representative_picture_id == NULL) we don't update anything
346    if ( $row['representative_picture_id'] != '' )
347    {
348      $query = 'SELECT image_id';
349      $query.= ' FROM '.PREFIX_TABLE.'image_category';
350      $query.= ' WHERE category_id = '.$id;
351      $query.= ' AND image_id = '.$row['representative_picture_id'];
352      $query.= ';';
353      $result = mysql_query( $query );
354      if ( mysql_num_rows( $result ) == 0 )
355      {
356        $query = 'UPDATE '.PREFIX_TABLE.'categories';
357        $query.= ' SET representative_picture_id = NULL';
358        $query.= ' WHERE id = '.$id;
359        $query.= ';';
360        mysql_query( $query );
361      }
362    }
363  }
364}
365
366function check_date_format( $date )
367{
368  // date arrives at this format : DD/MM/YYYY
369  list($day,$month,$year) = explode( '/', $date );
370  return checkdate ( $month, $day, $year );
371}
372
373function date_convert( $date )
374{
375  // date arrives at this format : DD/MM/YYYY
376  // It must be transformed in YYYY-MM-DD
377  list($day,$month,$year) = explode( '/', $date );
378  return $year.'-'.$month.'-'.$day;
379}
380
381function date_convert_back( $date )
382{
383  // date arrives at this format : YYYY-MM-DD
384  // It must be transformed in DD/MM/YYYY
385  if ( $date != '' )
386  {
387    list($year,$month,$day) = explode( '-', $date );
388    return $day.'/'.$month.'/'.$year;
389  }
390  else
391  {
392    return '';
393  }
394}
395
396// get_keywords returns an array with relevant keywords found in the string
397// given in argument. Keywords must be separated by comma in this string.
398// keywords must :
399//   - be longer or equal to 3 characters
400//   - not contain ', " or blank characters
401//   - unique in the string ("test,test" -> "test")
402function get_keywords( $keywords_string )
403{
404  $keywords = array();
405
406  $candidates = explode( ',', $keywords_string );
407  foreach ( $candidates as $candidate ) {
408    if ( strlen($candidate) >= 3 and !preg_match( '/(\'|"|\s)/', $candidate ) )
409      array_push( $keywords, $candidate );
410  }
411
412  return array_unique( $keywords );
413}
414
415function display_categories( $categories, $indent,
416                             $selected = -1, $forbidden = -1 )
417{
418  global $vtp,$sub;
419
420  foreach ( $categories as $category ) {
421    if ( $category['id'] != $forbidden )
422    {
423      $vtp->addSession( $sub, 'associate_cat' );
424      $vtp->setVar( $sub, 'associate_cat.value',   $category['id'] );
425      $content = $indent.'- '.$category['name'];
426      $vtp->setVar( $sub, 'associate_cat.content', $content );
427      if ( $category['id'] == $selected )
428        $vtp->setVar( $sub, 'associate_cat.selected', ' selected="selected"' );
429      $vtp->closeSession( $sub, 'associate_cat' );
430      display_categories( $category['subcats'], $indent.str_repeat('&nbsp;',3),
431                          $selected, $forbidden );
432    }
433  }
434}
435?>
Note: See TracBrowser for help on using the repository browser.