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

Last change on this file since 498 was 498, checked in by z0rglub, 20 years ago

simplier display of update result :

  • number of new categories
  • number of new elements
  • number of deleted categories
  • number of deleted elements
  • only errors are fully listed
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// |                             functions.php                             |
4// +-----------------------------------------------------------------------+
5// | application   : PhpWebGallery <http://phpwebgallery.net>              |
6// | branch        : BSF (Best So Far)                                     |
7// +-----------------------------------------------------------------------+
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-08-29 11:21:21 +0000 (Sun, 29 Aug 2004) $
10// | last modifier : $Author: z0rglub $
11// | revision      : $Revision: 498 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28include(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php');
29
30$tab_ext_create_TN = array ( 'jpg', 'png', 'JPG', 'PNG' );
31
32// is_image returns true if the given $filename (including the path) is a
33// picture according to its format and its extension.
34// As GD library can only generate pictures from jpeg and png files, if you
35// ask if the filename is an image for thumbnail creation (second parameter
36// set to true), the only authorized formats are jpeg and png.
37function is_image( $filename, $create_thumbnail = false )
38{
39  global $conf, $tab_ext_create_TN;
40
41  if ( is_file( $filename ) )
42  {
43    $size = getimagesize( $filename );
44    // $size[2] == 1 means GIF
45    // $size[2] == 2 means JPG
46    // $size[2] == 3 means PNG
47    if ( !$create_thumbnail )
48    {
49      if ( in_array( get_extension( $filename ), $conf['picture_ext'] )
50           and ( $size[2] == 1 or $size[2] == 2 or $size[2] == 3 ) )
51      {
52        return true;
53      }
54    }
55    else
56    {
57      if ( in_array( get_extension( $filename ), $tab_ext_create_TN )
58           and ( $size[2] == 2 or $size[2] == 3 ) )
59      {
60        return true;
61      }
62    }
63  }
64  return false;
65}
66
67/**
68 * returns an array with all picture files according to $conf['file_ext']
69 *
70 * @param string $dir
71 * @return array
72 */
73function get_pwg_files($dir)
74{
75  global $conf;
76
77  $pictures = array();
78  if ($opendir = opendir($dir))
79  {
80    while ($file = readdir($opendir))
81    {
82      if (in_array(get_extension($file), $conf['file_ext']))
83      {
84        array_push($pictures, $file);
85      }
86    }
87  }
88  return $pictures;
89}
90
91/**
92 * returns an array with all thumbnails according to $conf['picture_ext']
93 * and $conf['prefix_thumbnail']
94 *
95 * @param string $dir
96 * @return array
97 */
98function get_thumb_files($dir)
99{
100  global $conf;
101
102  $prefix_length = strlen($conf['prefix_thumbnail']);
103 
104  $thumbnails = array();
105  if ($opendir = @opendir($dir.'/thumbnail'))
106  {
107    while ($file = readdir($opendir))
108    {
109      if (in_array(get_extension($file), $conf['picture_ext'])
110          and substr($file, 0, $prefix_length) == $conf['prefix_thumbnail'])
111      {
112        array_push($thumbnails, $file);
113      }
114    }
115  }
116  return $thumbnails;
117}
118
119/**
120 * returns an array with representative picture files of a directory
121 * according to $conf['picture_ext']
122 *
123 * @param string $dir
124 * @return array
125 */
126function get_representative_files($dir)
127{
128  global $conf;
129
130  $pictures = array();
131  if ($opendir = @opendir($dir.'/representative'))
132  {
133    while ($file = readdir($opendir))
134    {
135      if (in_array(get_extension($file), $conf['picture_ext']))
136      {
137        array_push($pictures, $file);
138      }
139    }
140  }
141  return $pictures;
142}
143
144function TN_exists( $dir, $file )
145{
146  global $conf;
147
148  $filename = get_filename_wo_extension( $file );
149  foreach ( $conf['picture_ext'] as $ext ) {
150    $test = $dir.'/thumbnail/'.$conf['prefix_thumbnail'].$filename.'.'.$ext;
151    if ( is_file ( $test ) )
152    {
153      return $ext;
154    }
155  }
156  return false;
157}
158       
159
160// The function delete_site deletes a site and call the function
161// delete_categories for each primary category of the site
162function delete_site( $id )
163{
164  // destruction of the categories of the site
165  $query = '
166SELECT id
167  FROM '.CATEGORIES_TABLE.'
168  WHERE site_id = '.$id.'
169;';
170  $result = mysql_query($query);
171  $category_ids = array();
172  while ($row = mysql_fetch_array($result))
173  {
174    array_push($category_ids, $row['id']);
175  }
176  delete_categories($category_ids);
177               
178  // destruction of the site
179  $query = '
180DELETE FROM '.SITES_TABLE.'
181  WHERE id = '.$id.'
182;';
183  mysql_query($query);
184}
185       
186
187// The function delete_categories deletes the categories identified by the
188// (numeric) key of the array $ids. It also deletes (in the database) :
189//    - all the elements of the category (delete_elements, see further)
190//    - all the links between elements and this category
191//    - all the restrictions linked to the category
192// The function works recursively.
193function delete_categories($ids)
194{
195  global $counts;
196 
197  // destruction of all the related elements
198  $query = '
199SELECT id
200  FROM '.IMAGES_TABLE.'
201  WHERE storage_category_id IN ('.implode(',', $ids).')
202;';
203  $result = mysql_query($query);
204  $element_ids = array();
205  while ($row = mysql_fetch_array($result))
206  {
207    array_push($element_ids, $row['id']);
208  }
209  if (count($element_ids) > 0)
210  {
211    delete_elements($element_ids);
212  }
213
214  // destruction of the links between images and this category
215  $query = '
216DELETE FROM '.IMAGE_CATEGORY_TABLE.'
217  WHERE category_id IN ('.implode(',', $ids).')
218;';
219  mysql_query($query);
220
221  // destruction of the access linked to the category
222  $query = '
223DELETE FROM '.USER_ACCESS_TABLE.'
224  WHERE cat_id IN ('.implode(',', $ids).')
225;';
226  mysql_query($query);
227  $query = '
228DELETE FROM '.GROUP_ACCESS_TABLE.'
229  WHERE cat_id IN ('.implode(',', $ids).')
230;';
231  mysql_query($query);
232
233  // destruction of the sub-categories
234  $query = '
235SELECT id
236  FROM '.CATEGORIES_TABLE.'
237  WHERE id_uppercat IN ('.implode(',', $ids).')
238;';
239  $result = mysql_query($query);
240  $subcat_ids = array();
241  while($row = mysql_fetch_array($result))
242  {
243    array_push($subcat_ids, $row['id']);
244  }
245  if (count($subcat_ids) > 0)
246  {
247    delete_categories($subcat_ids);
248  }
249
250  // destruction of the category
251  $query = '
252DELETE FROM '.CATEGORIES_TABLE.'
253  WHERE id IN ('.implode(',', $ids).')
254;';
255  mysql_query($query);
256
257  if (isset($counts['del_categories']))
258  {
259    $counts['del_categories']+= count($ids);
260  }
261}
262
263// The function delete_elements deletes the elements identified by the
264// (numeric) values of the array $ids. It also deletes (in the database) :
265//    - all the comments related to elements
266//    - all the links between categories and elements
267//    - all the favorites associated to elements
268function delete_elements($ids)
269{
270  global $counts;
271 
272  // destruction of the comments on the image
273  $query = '
274DELETE FROM '.COMMENTS_TABLE.'
275  WHERE image_id IN (
276'.wordwrap(implode(', ', $ids), 80, "\n").')
277;';
278  mysql_query($query);
279
280  // destruction of the links between images and this category
281  $query = '
282DELETE FROM '.IMAGE_CATEGORY_TABLE.'
283  WHERE image_id IN (
284'.wordwrap(implode(', ', $ids), 80, "\n").')
285;';
286  mysql_query($query);
287
288  // destruction of the favorites associated with the picture
289  $query = '
290DELETE FROM '.FAVORITES_TABLE.'
291  WHERE image_id IN (
292'.wordwrap(implode(', ', $ids), 80, "\n").')
293;';
294  mysql_query($query);
295               
296  // destruction of the image
297  $query = '
298DELETE FROM '.IMAGES_TABLE.'
299  WHERE id IN (
300'.wordwrap(implode(', ', $ids), 80, "\n").')
301;';
302  mysql_query($query);
303
304  if (isset($counts['del_elements']))
305  {
306    $counts['del_elements']+= count($ids);
307  }
308}
309
310// The delete_user function delete a user identified by the $user_id
311// It also deletes :
312//     - all the access linked to this user
313//     - all the links to any group
314//     - all the favorites linked to this user
315//     - all sessions linked to this user
316//     - all categories informations linked to this user
317function delete_user( $user_id )
318{
319  // destruction of the access linked to the user
320  $query = 'DELETE FROM '.PREFIX_TABLE.'user_access';
321  $query.= ' WHERE user_id = '.$user_id;
322  $query.= ';';
323  mysql_query( $query );
324
325  // destruction of the group links for this user
326  $query = 'DELETE FROM '.PREFIX_TABLE.'user_group';
327  $query.= ' WHERE user_id = '.$user_id;
328  $query.= ';';
329  mysql_query( $query );
330
331  // destruction of the favorites associated with the user
332  $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
333  $query.= ' WHERE user_id = '.$user_id;
334  $query.= ';';
335  mysql_query( $query );
336
337  // destruction of the sessions linked with the user
338  $query = 'DELETE FROM '.PREFIX_TABLE.'sessions';
339  $query.= ' WHERE user_id = '.$user_id;
340  $query.= ';';
341  mysql_query( $query );
342
343  // destruction of the user
344  $query = 'DELETE FROM '.USERS_TABLE;
345  $query.= ' WHERE id = '.$user_id;
346  $query.= ';';
347  mysql_query( $query );
348}
349
350// delete_group deletes a group identified by its $group_id.
351// It also deletes :
352//     - all the access linked to this group
353//     - all the links between this group and any user
354function delete_group( $group_id )
355{
356  // destruction of the access linked to the group
357  $query = 'DELETE FROM '.PREFIX_TABLE.'group_access';
358  $query.= ' WHERE group_id = '.$group_id;
359  $query.= ';';
360  mysql_query( $query );
361
362  // synchronize all users linked to the group
363  synchronize_group( $group_id );
364
365  // destruction of the users links for this group
366  $query = 'DELETE FROM '.PREFIX_TABLE.'user_group';
367  $query.= ' WHERE group_id = '.$group_id;
368  $query.= ';';
369  mysql_query( $query );
370
371  // destruction of the group
372  $query = 'DELETE FROM '.PREFIX_TABLE.'groups';
373  $query.= ' WHERE id = '.$group_id;
374  $query.= ';';
375  mysql_query( $query );
376}
377
378// The check_favorites function deletes all the favorites of a user if he is
379// not allowed to see them (the category or an upper category is restricted
380// or invisible)
381function check_favorites( $user_id )
382{
383  $query = 'SELECT status,forbidden_categories';
384  $query.= ' FROM '.USERS_TABLE;
385  $query.= ' WHERE id = '.$user_id;
386  $query.= ';';
387  $row = mysql_fetch_array( mysql_query( $query ) );
388  $status = $row['status'];
389  // retrieving all the restricted categories for this user
390  if ( isset( $row['forbidden_categories'] ) )
391    $restricted_cat = explode( ',', $row['forbidden_categories'] );
392  else
393    $restricted_cat = array();
394  // retrieving all the favorites for this user and comparing their
395  // categories to the restricted categories
396  $query = 'SELECT image_id';
397  $query.= ' FROM '.PREFIX_TABLE.'favorites';
398  $query.= ' WHERE user_id = '.$user_id;
399  $query.= ';';
400  $result = mysql_query ( $query );
401  while ( $row = mysql_fetch_array( $result ) )
402  {
403    // for each picture, we have to check all the categories it belongs
404    // to. Indeed if a picture belongs to category_1 and category_2 and that
405    // category_2 is not restricted to the user, he can have the picture as
406    // favorite.
407    $query = 'SELECT DISTINCT(category_id) as category_id';
408    $query.= ' FROM '.PREFIX_TABLE.'image_category';
409    $query.= ' WHERE image_id = '.$row['image_id'];
410    $query.= ';';
411    $picture_result = mysql_query( $query );
412    $picture_cat = array();
413    while ( $picture_row = mysql_fetch_array( $picture_result ) )
414    {
415      array_push( $picture_cat, $picture_row['category_id'] );
416    }
417    if ( count( array_diff( $picture_cat, $restricted_cat ) ) == 0 )
418    {
419      $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
420      $query.= ' WHERE image_id = '.$row['image_id'];
421      $query.= ' AND user_id = '.$user_id;
422      $query.= ';';
423      mysql_query( $query );
424    }
425  }
426}
427
428/**
429 * updates calculated informations about a category : date_last and
430 * nb_images. It also verifies that the representative picture is really
431 * linked to the category. Recursive.
432 *
433 * @param mixed category id
434 * @returns void
435 */
436function update_category($id = 'all')
437{
438  $cat_ids = array();
439 
440  $query = '
441SELECT category_id, COUNT(image_id) AS count, max(date_available) AS date_last
442  FROM '.IMAGES_TABLE.'
443    INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id';
444  if (is_numeric($id))
445  {
446    $query.= '
447  WHERE uppercats REGEXP \'(^|,)'.$id.'(,|$)\'';
448  }
449  $query.= '
450  GROUP BY category_id
451;';
452  $result = mysql_query( $query );
453  while ( $row = mysql_fetch_array( $result ) )
454  {
455    array_push($cat_ids, $row['category_id']);
456    $query = '
457UPDATE '.CATEGORIES_TABLE.'
458  SET date_last = \''.$row['date_last'].'\'
459    , nb_images = '.$row['count'].'
460  WHERE id = '.$row['category_id'].'
461;';
462    mysql_query($query);
463  }
464
465  if (count($cat_ids) > 0)
466  {
467    $query = '
468SELECT id, representative_picture_id
469  FROM '.CATEGORIES_TABLE.'
470  WHERE representative_picture_id IS NOT NULL
471    AND id IN ('.implode(',', $cat_ids).')
472;';
473    $result = mysql_query( $query );
474    while ( $row = mysql_fetch_array( $result ) )
475    {
476      $query = '
477SELECT image_id
478  FROM '.IMAGE_CATEGORY_TABLE.'
479  WHERE category_id = '.$row['id'].'
480    AND image_id = '.$row['representative_picture_id'].'
481;';
482      $result = mysql_query( $query );
483      if (mysql_num_rows($result) == 0)
484      {
485        $query = '
486UPDATE '.CATEGORIES_TABLE.'
487  SET representative_picture_id = NULL
488  WHERE id = '.$row['id'].'
489;';
490        mysql_query( $query );
491      }
492    }
493  }
494}
495
496function check_date_format( $date )
497{
498  // date arrives at this format : DD/MM/YYYY
499  @list($day,$month,$year) = explode( '/', $date );
500  return @checkdate( $month, $day, $year );
501}
502
503function date_convert( $date )
504{
505  // date arrives at this format : DD/MM/YYYY
506  // It must be transformed in YYYY-MM-DD
507  list($day,$month,$year) = explode( '/', $date );
508  return $year.'-'.$month.'-'.$day;
509}
510
511function date_convert_back( $date )
512{
513  // date arrives at this format : YYYY-MM-DD
514  // It must be transformed in DD/MM/YYYY
515  if ( $date != '' )
516  {
517    list($year,$month,$day) = explode( '-', $date );
518    return $day.'/'.$month.'/'.$year;
519  }
520  else
521  {
522    return '';
523  }
524}
525
526// get_keywords returns an array with relevant keywords found in the string
527// given in argument. Keywords must be separated by comma in this string.
528// keywords must :
529//   - be longer or equal to 3 characters
530//   - not contain ', " or blank characters
531//   - unique in the string ("test,test" -> "test")
532function get_keywords( $keywords_string )
533{
534  $keywords = array();
535
536  $candidates = explode( ',', $keywords_string );
537  foreach ( $candidates as $candidate ) {
538    if ( strlen($candidate) >= 3 and !preg_match( '/(\'|"|\s)/', $candidate ) )
539      array_push( $keywords, $candidate );
540  }
541
542  return array_unique( $keywords );
543}
544
545function display_categories( $categories, $indent,
546                             $selected = -1, $forbidden = -1 )
547{
548  global $vtp,$sub;
549
550  foreach ( $categories as $category ) {
551    if ( $category['id'] != $forbidden )
552    {
553      $vtp->addSession( $sub, 'associate_cat' );
554      $vtp->setVar( $sub, 'associate_cat.value',   $category['id'] );
555      $content = $indent.'- '.$category['name'];
556      $vtp->setVar( $sub, 'associate_cat.content', $content );
557      if ( $category['id'] == $selected )
558        $vtp->setVar( $sub, 'associate_cat.selected', ' selected="selected"' );
559      $vtp->closeSession( $sub, 'associate_cat' );
560      display_categories( $category['subcats'], $indent.str_repeat('&nbsp;',3),
561                          $selected, $forbidden );
562    }
563  }
564}
565
566/**
567 * returns an array with the ids of the restricted categories for the user
568 *
569 * Returns an array with the ids of the restricted categories for the
570 * user. If the $check_invisible parameter is set to true, invisible
571 * categorie are added to the restricted one in the array.
572 *
573 * @param int $user_id
574 * @param string $user_status
575 * @param bool $check_invisible
576 * @param bool $use_groups
577 * @return array
578 */
579function get_user_restrictions( $user_id, $user_status,
580                                $check_invisible, $use_groups = true )
581{
582  // 1. retrieving ids of private categories
583  $query = 'SELECT id FROM '.CATEGORIES_TABLE;
584  $query.= " WHERE status = 'private'";
585  $query.= ';';
586  $result = mysql_query( $query );
587  $privates = array();
588  while ( $row = mysql_fetch_array( $result ) )
589  {
590    array_push( $privates, $row['id'] );
591  }
592  // 2. retrieving all authorized categories for the user
593  $authorized = array();
594  // 2.1. retrieving authorized categories thanks to personnal user
595  //      authorization
596  $query = 'SELECT cat_id FROM '.USER_ACCESS_TABLE;
597  $query.= ' WHERE user_id = '.$user_id;
598  $query.= ';';
599  $result = mysql_query( $query );
600  while ( $row = mysql_fetch_array( $result ) )
601  {
602    array_push( $authorized, $row['cat_id'] );
603  }
604  // 2.2. retrieving authorized categories thanks to group authorization to
605  //      which the user is a member
606  if ( $use_groups )
607  {
608    $query = 'SELECT ga.cat_id';
609    $query.= ' FROM '.USER_GROUP_TABLE.' as ug';
610    $query.= ', '.GROUP_ACCESS_TABLE.' as ga';
611    $query.= ' WHERE ug.group_id = ga.group_id';
612    $query.= ' AND ug.user_id = '.$user_id;
613    $query.= ';';
614    $result = mysql_query( $query );
615    while ( $row = mysql_fetch_array( $result ) )
616    {
617      array_push( $authorized, $row['cat_id'] );
618    }
619    $authorized = array_unique( $authorized );
620  }
621
622  $forbidden = array();
623  foreach ( $privates as $private ) {
624    if ( !in_array( $private, $authorized ) )
625    {
626      array_push( $forbidden, $private );
627    }
628  }
629
630  if ( $check_invisible )
631  {
632    // 3. adding to the restricted categories, the invisible ones
633    if ( $user_status != 'admin' )
634    {
635      $query = 'SELECT id FROM '.CATEGORIES_TABLE;
636      $query.= " WHERE visible = 'false';";
637      $result = mysql_query( $query );
638      while ( $row = mysql_fetch_array( $result ) )
639      {
640        array_push( $forbidden, $row['id'] );
641      }
642    }
643  }
644  return array_unique( $forbidden );
645}
646
647/**
648 * updates the calculated data users.forbidden_categories, it includes
649 * sub-categories of the direct forbidden categories
650 *
651 * @param nt $user_id
652 * @return array
653 */
654function update_user_restrictions( $user_id )
655{
656  $restrictions = get_user_all_restrictions( $user_id );
657
658  // update the users.forbidden_categories in database
659  $query = 'UPDATE '.USERS_TABLE;
660  $query.= ' SET forbidden_categories = ';
661  if ( count( $restrictions ) > 0 )
662    $query.= "'".implode( ',', $restrictions )."'";
663  else
664    $query.= 'NULL';
665  $query .= ' WHERE id = '.$user_id;
666  $query.= ';';
667  mysql_query( $query );
668
669  return $restrictions;
670}
671
672/**
673 * returns all the restricted categories ids including sub-categories
674 *
675 * @param int $user_id
676 * @return array
677 */
678function get_user_all_restrictions( $user_id )
679{
680  global $page;
681 
682  $query = 'SELECT status';
683  $query.= ' FROM '.USERS_TABLE;
684  $query.= ' WHERE id = '.$user_id;
685  $query.= ';';
686  $row = mysql_fetch_array( mysql_query( $query ) );
687 
688  $base_restrictions=get_user_restrictions($user_id,$row['status'],true,true);
689
690  $restrictions = $base_restrictions;
691  foreach ( $base_restrictions as $category_id ) {
692    echo $category_id.' is forbidden to user '.$user_id.'<br />';
693    $restrictions =
694      array_merge( $restrictions,
695                   $page['plain_structure'][$category_id]['all_subcats_ids'] );
696  }
697
698  return array_unique( $restrictions );
699}
700
701// The function is_user_allowed returns :
702//      - 0 : if the category is allowed with this $restrictions array
703//      - 1 : if this category is not allowed
704//      - 2 : if an uppercat category is not allowed
705// Note : the restrictions array must represent ONLY direct forbidden
706// categories, not all forbidden categories
707function is_user_allowed( $category_id, $restrictions )
708{
709  if ( in_array( $category_id, $restrictions ) ) return 1;
710
711  $query = 'SELECT uppercats';
712  $query.= ' FROM '.CATEGORIES_TABLE;
713  $query.= ' WHERE id = '.$category_id;
714  $query.= ';';
715  $row = mysql_fetch_array( mysql_query( $query ) );
716  $uppercats = explode( ',', $row['uppercats'] );
717  foreach ( $uppercats as $category_id ) {
718    if ( in_array( $category_id, $restrictions ) ) return 2;
719  }
720
721  // no restriction found : the user is allowed to access this category
722  return 0;
723}
724
725/**
726 * returns an array containing sub-directories which can be a category
727 *
728 * directories nammed "thumbnail" are omitted
729 *
730 * @param string $basedir
731 * @return array
732 */
733function get_category_directories( $basedir )
734{
735  $sub_dirs = array();
736
737  if ( $opendir = opendir( $basedir ) )
738  {
739    while ( $file = readdir( $opendir ) )
740    {
741      if ( $file != '.' and $file != '..'
742           and is_dir( $basedir.'/'.$file )
743           and $file != 'thumbnail' )
744      {
745        array_push( $sub_dirs, $file );
746      }
747    }
748  }
749  return $sub_dirs;
750}
751
752// my_error returns (or send to standard output) the message concerning the
753// error occured for the last mysql query.
754function my_error($header, $echo = true)
755{
756  $error = $header.'<span style="font-weight:bold;">N°= '.mysql_errno();
757  $error.= ' -->> '.mysql_error()."</span><br /><br />\n";
758  if ($echo)
759  {
760    echo $error;
761  }
762  else
763  {
764    return $error;
765  }
766}
767?>
Note: See TracBrowser for help on using the repository browser.