source: branches/release-1_3/admin/include/functions.php @ 281

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

improved function update_category essentially by using INNER JOIN instead of
LEFT JOIN

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 29.2 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 281 2004-01-15 23:19:51Z 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
57/**
58 * returns an array with all picture files according to $conf['picture_ext']
59 *
60 * @param string $dir
61 * @return array
62 */
63function get_picture_files( $dir )
64{
65  global $conf;
66
67  $pictures = array();
68  if ( $opendir = opendir( $dir ) )
69  {
70    while ( $file = readdir( $opendir ) )
71    {
72      if ( in_array( get_extension( $file ), $conf['picture_ext'] ) )
73      {
74        array_push( $pictures, $file );
75      }
76    }
77  }
78  return $pictures;
79}
80
81/**
82 * returns an array with all thumbnails according to $conf['picture_ext']
83 * and $conf['prefix_thumbnail']
84 *
85 * @param string $dir
86 * @return array
87 */
88function get_thumb_files( $dir )
89{
90  global $conf;
91
92  $prefix_length = strlen( $conf['prefix_thumbnail'] );
93 
94  $thumbnails = array();
95  if ( $opendir = @opendir( $dir ) )
96  {
97    while ( $file = readdir( $opendir ) )
98    {
99      if ( in_array( get_extension( $file ), $conf['picture_ext'] )
100           and substr($file,0,$prefix_length) == $conf['prefix_thumbnail'] )
101      {
102        array_push( $thumbnails, $file );
103      }
104    }
105  }
106  return $thumbnails;
107}
108
109function TN_exists( $dir, $file )
110{
111  global $conf;
112
113  $filename = get_filename_wo_extension( $file );
114  foreach ( $conf['picture_ext'] as $ext ) {
115    $test = $dir.'/thumbnail/'.$conf['prefix_thumbnail'].$filename.'.'.$ext;
116    if ( is_file ( $test ) )
117    {
118      return $ext;
119    }
120  }
121  return false;
122}
123       
124
125// The function delete_site deletes a site
126// and call the function delete_category for each primary category of the site
127function delete_site( $id )
128{
129  // destruction of the categories of the site
130  $query = 'SELECT id';
131  $query.= ' FROM '.PREFIX_TABLE.'categories';
132  $query.= ' WHERE site_id = '.$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 site
141  $query = 'DELETE FROM '.PREFIX_TABLE.'sites';
142  $query.= ' WHERE id = '.$id;
143  $query.= ';';
144  mysql_query( $query );
145}
146       
147
148// The function delete_category deletes the category identified by the $id
149// It also deletes (in the database) :
150//    - all the images of the images (thanks to delete_image, see further)
151//    - all the links between images and this category
152//    - all the restrictions linked to the category
153// The function works recursively.
154function delete_category( $id )
155{
156  // destruction of all the related images
157  $query = 'SELECT id';
158  $query.= ' FROM '.PREFIX_TABLE.'images';
159  $query.= ' WHERE storage_category_id = '.$id;
160  $query.= ';';
161  $result = mysql_query( $query );
162  while ( $row = mysql_fetch_array( $result ) )
163  {
164    delete_image( $row['id'] );
165  }
166
167  // destruction of the links between images and this category
168  $query = 'DELETE FROM '.PREFIX_TABLE.'image_category';
169  $query.= ' WHERE category_id = '.$id;
170  $query.= ';';
171  mysql_query( $query );
172
173  // destruction of the access linked to the category
174  $query = 'DELETE FROM '.PREFIX_TABLE.'user_access';
175  $query.= ' WHERE cat_id = '.$id;
176  $query.= ';';
177  mysql_query( $query );
178  $query = 'DELETE FROM '.PREFIX_TABLE.'group_access';
179  $query.= ' WHERE cat_id = '.$id;
180  $query.= ';';
181  mysql_query( $query );
182
183  // destruction of the sub-categories
184  $query = 'SELECT id';
185  $query.= ' FROM '.PREFIX_TABLE.'categories';
186  $query.= ' WHERE id_uppercat = '.$id;
187  $query.= ';';
188  $result = mysql_query( $query );
189  while( $row = mysql_fetch_array( $result ) )
190  {
191    delete_category( $row['id'] );
192  }
193
194  // destruction of the category
195  $query = 'DELETE FROM '.PREFIX_TABLE.'categories';
196  $query.= ' WHERE id = '.$id;
197  $query.= ';';
198  mysql_query( $query );
199}
200       
201
202// The function delete_image deletes the image identified by the $id
203// It also deletes (in the database) :
204//    - all the comments related to the image
205//    - all the links between categories and this image
206//    - all the favorites associated to the image
207function delete_image( $id )
208{
209  global $count_deleted;
210               
211  // destruction of the comments on the image
212  $query = 'DELETE FROM '.PREFIX_TABLE.'comments';
213  $query.= ' WHERE image_id = '.$id;
214  $query.= ';';
215  mysql_query( $query );
216
217  // destruction of the links between images and this category
218  $query = 'DELETE FROM '.PREFIX_TABLE.'image_category';
219  $query.= ' WHERE image_id = '.$id;
220  $query.= ';';
221  mysql_query( $query );
222
223  // destruction of the favorites associated with the picture
224  $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
225  $query.= ' WHERE image_id = '.$id;
226  $query.= ';';
227  mysql_query( $query );
228               
229  // destruction of the image
230  $query = 'DELETE FROM '.PREFIX_TABLE.'images';
231  $query.= ' WHERE id = '.$id;
232  $query.= ';';
233  mysql_query( $query );
234  $count_deleted++;
235}
236
237// The delete_user function delete a user identified by the $user_id
238// It also deletes :
239//     - all the access linked to this user
240//     - all the links to any group
241//     - all the favorites linked to this user
242//     - all sessions linked to this user
243//     - all categories informations linked to this user
244function delete_user( $user_id )
245{
246  // destruction of the access linked to the user
247  $query = 'DELETE FROM '.PREFIX_TABLE.'user_access';
248  $query.= ' WHERE user_id = '.$user_id;
249  $query.= ';';
250  mysql_query( $query );
251
252  // destruction of the group links for this user
253  $query = 'DELETE FROM '.PREFIX_TABLE.'user_group';
254  $query.= ' WHERE user_id = '.$user_id;
255  $query.= ';';
256  mysql_query( $query );
257
258  // destruction of the favorites associated with the user
259  $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
260  $query.= ' WHERE user_id = '.$user_id;
261  $query.= ';';
262  mysql_query( $query );
263
264  // destruction of the sessions linked with the user
265  $query = 'DELETE FROM '.PREFIX_TABLE.'sessions';
266  $query.= ' WHERE user_id = '.$user_id;
267  $query.= ';';
268  mysql_query( $query );
269
270  // destruction of the categories informations linked with the user
271  $query = 'DELETE FROM '.PREFIX_TABLE.'user_category';
272  $query.= ' WHERE user_id = '.$user_id;
273  $query.= ';';
274  mysql_query( $query );
275
276  // destruction of the user
277  $query = 'DELETE FROM '.PREFIX_TABLE.'users';
278  $query.= ' WHERE id = '.$user_id;
279  $query.= ';';
280  mysql_query( $query );
281}
282
283// delete_group deletes a group identified by its $group_id.
284// It also deletes :
285//     - all the access linked to this group
286//     - all the links between this group and any user
287function delete_group( $group_id )
288{
289  // destruction of the access linked to the group
290  $query = 'DELETE FROM '.PREFIX_TABLE.'group_access';
291  $query.= ' WHERE group_id = '.$group_id;
292  $query.= ';';
293  mysql_query( $query );
294
295  // synchronize all users linked to the group
296  synchronize_group( $group_id );
297
298  // destruction of the users links for this group
299  $query = 'DELETE FROM '.PREFIX_TABLE.'user_group';
300  $query.= ' WHERE group_id = '.$group_id;
301  $query.= ';';
302  mysql_query( $query );
303
304  // destruction of the group
305  $query = 'DELETE FROM '.PREFIX_TABLE.'groups';
306  $query.= ' WHERE id = '.$group_id;
307  $query.= ';';
308  mysql_query( $query );
309}
310
311// The check_favorites function deletes all the favorites of a user if he is
312// not allowed to see them (the category or an upper category is restricted
313// or invisible)
314function check_favorites( $user_id )
315{
316  $query = 'SELECT status';
317  $query.= ' FROM '.PREFIX_TABLE.'users';
318  $query.= ' WHERE id = '.$user_id;
319  $query.= ';';
320  $row = mysql_fetch_array( mysql_query( $query ) );
321  $status = $row['status'];
322  // retrieving all the restricted categories for this user
323  $restricted_cat = get_all_restrictions( $user_id, $status );
324  // retrieving all the favorites for this user and comparing their
325  // categories to the restricted categories
326  $query = 'SELECT image_id';
327  $query.= ' FROM '.PREFIX_TABLE.'favorites';
328  $query.= ' WHERE user_id = '.$user_id;
329  $query.= ';';
330  $result = mysql_query ( $query );
331  while ( $row = mysql_fetch_array( $result ) )
332  {
333    // for each picture, we have to check all the categories it belongs
334    // to. Indeed if a picture belongs to category_1 and category_2 and that
335    // category_2 is not restricted to the user, he can have the picture as
336    // favorite.
337    $query = 'SELECT DISTINCT(category_id) as category_id';
338    $query.= ' FROM '.PREFIX_TABLE.'image_category';
339    $query.= ' WHERE image_id = '.$row['image_id'];
340    $query.= ';';
341    $picture_result = mysql_query( $query );
342    $picture_cat = array();
343    while ( $picture_row = mysql_fetch_array( $picture_result ) )
344    {
345      array_push( $picture_cat, $picture_row['category_id'] );
346    }
347    if ( count( array_diff( $picture_cat, $restricted_cat ) ) == 0 )
348    {
349      $query = 'DELETE FROM '.PREFIX_TABLE.'favorites';
350      $query.= ' WHERE image_id = '.$row['image_id'];
351      $query.= ' AND user_id = '.$user_id;
352      $query.= ';';
353      mysql_query( $query );
354    }
355  }
356}
357
358// update_category updates calculated informations about a category :
359// date_last and nb_images. It also verifies that the representative picture
360// is really linked to the category.
361function update_category( $id = 'all' )
362{
363  if ( $id == 'all' )
364  {
365    $query = 'SELECT id';
366    $query.= ' FROM '.PREFIX_TABLE.'categories';
367    $query.= ';';
368    $result = mysql_query( $query );
369    while ( $row = mysql_fetch_array( $result ) )
370    {
371      // recursive call
372      update_category( $row['id'] );
373    }
374  }
375  else if ( is_numeric( $id ) )
376  {
377    // updating the number of pictures
378    $query = 'SELECT COUNT(*) as nb_images';
379    $query.= ' FROM '.PREFIX_TABLE.'image_category';
380    $query.= ' WHERE category_id = '.$id;
381    $query.= ';';
382    list( $nb_images ) = mysql_fetch_array( mysql_query( $query ) );
383    // updating the date_last
384    $query = 'SELECT MAX(date_available) AS date_available';
385    $query.= ' FROM '.PREFIX_TABLE.'images';
386    $query.= ' INNER JOIN '.PREFIX_TABLE.'image_category ON id = image_id';
387    $query.= ' WHERE category_id = '.$id;
388    $query.= ';';
389    list( $date_available ) = mysql_fetch_array( mysql_query( $query ) );
390   
391    $query = 'UPDATE '.PREFIX_TABLE.'categories';
392    $query.= " SET date_last = '".$date_available."'";
393    $query.= ' SET nb_images = '.$nb_images;
394    $query.= ' WHERE id = '.$id;
395    $query.= ';';
396    mysql_query( $query );
397
398    // updating the representative_picture_id : if the representative
399    // picture of the category is not any more linked to the category, we
400    // have to set representative_picture_id to NULL
401    $query = 'SELECT representative_picture_id';
402    $query.= ' FROM '.PREFIX_TABLE.'categories';
403    $query.= ' WHERE id = '.$id;
404    $row = mysql_fetch_array( mysql_query( $query ) );
405    // if the category has no representative picture (ie
406    // representative_picture_id == NULL) we don't update anything
407    if ( $row['representative_picture_id'] != '' )
408    {
409      $query = 'SELECT image_id';
410      $query.= ' FROM '.PREFIX_TABLE.'image_category';
411      $query.= ' WHERE category_id = '.$id;
412      $query.= ' AND image_id = '.$row['representative_picture_id'];
413      $query.= ';';
414      $result = mysql_query( $query );
415      if ( mysql_num_rows( $result ) == 0 )
416      {
417        $query = 'UPDATE '.PREFIX_TABLE.'categories';
418        $query.= ' SET representative_picture_id = NULL';
419        $query.= ' WHERE id = '.$id;
420        $query.= ';';
421        mysql_query( $query );
422      }
423    }
424  }
425}
426
427function check_date_format( $date )
428{
429  // date arrives at this format : DD/MM/YYYY
430  @list($day,$month,$year) = explode( '/', $date );
431  return @checkdate( $month, $day, $year );
432}
433
434function date_convert( $date )
435{
436  // date arrives at this format : DD/MM/YYYY
437  // It must be transformed in YYYY-MM-DD
438  list($day,$month,$year) = explode( '/', $date );
439  return $year.'-'.$month.'-'.$day;
440}
441
442function date_convert_back( $date )
443{
444  // date arrives at this format : YYYY-MM-DD
445  // It must be transformed in DD/MM/YYYY
446  if ( $date != '' )
447  {
448    list($year,$month,$day) = explode( '-', $date );
449    return $day.'/'.$month.'/'.$year;
450  }
451  else
452  {
453    return '';
454  }
455}
456
457// get_keywords returns an array with relevant keywords found in the string
458// given in argument. Keywords must be separated by comma in this string.
459// keywords must :
460//   - be longer or equal to 3 characters
461//   - not contain ', " or blank characters
462//   - unique in the string ("test,test" -> "test")
463function get_keywords( $keywords_string )
464{
465  $keywords = array();
466
467  $candidates = explode( ',', $keywords_string );
468  foreach ( $candidates as $candidate ) {
469    if ( strlen($candidate) >= 3 and !preg_match( '/(\'|"|\s)/', $candidate ) )
470      array_push( $keywords, $candidate );
471  }
472
473  return array_unique( $keywords );
474}
475
476function display_categories( $categories, $indent,
477                             $selected = -1, $forbidden = -1 )
478{
479  global $vtp,$sub;
480
481  foreach ( $categories as $category ) {
482    if ( $category['id'] != $forbidden )
483    {
484      $vtp->addSession( $sub, 'associate_cat' );
485      $vtp->setVar( $sub, 'associate_cat.value',   $category['id'] );
486      $content = $indent.'- '.$category['name'];
487      $vtp->setVar( $sub, 'associate_cat.content', $content );
488      if ( $category['id'] == $selected )
489        $vtp->setVar( $sub, 'associate_cat.selected', ' selected="selected"' );
490      $vtp->closeSession( $sub, 'associate_cat' );
491      display_categories( $category['subcats'], $indent.str_repeat('&nbsp;',3),
492                          $selected, $forbidden );
493    }
494  }
495}
496
497/**
498 * Complete plain structure of the gallery
499 *
500 * Returns the plain structure (one level array) of the gallery. In the
501 * returned array, each element is an array with jeys 'id' and
502 * 'id_uppercat'. The function also fills the array $page['subcats'] which
503 * associate (category_id => array of sub-categories id).
504 *
505 * @param bool $use_name
506 * @return array
507 */
508function get_plain_structure( $use_name = false )
509{
510  global $page;
511
512  $plain_structure = array();
513
514  $query = 'SELECT id,id_uppercat';
515  if ( $use_name ) $query.= ',name';
516  $query.= ' FROM '.PREFIX_TABLE.'categories';
517  $query.= ' ORDER BY id_uppercat ASC, rank ASC';
518  $query.= ';';
519
520  $subcats = array();
521  $id_uppercat = 'NULL';
522
523  $result = mysql_query( $query );
524  while ( $row = mysql_fetch_array( $result ) )
525  {
526    $plain_structure[$row['id']]['id'] = $row['id'];
527    $plain_structure[$row['id']]['id_uppercat'] = $row['id_uppercat'];
528    if ( $use_name ) $plain_structure[$row['id']]['name'] = $row['name'];
529    // subcats list
530    if ( $row['id_uppercat'] == '' ) $row['id_uppercat'] = 'NULL';
531    if ( $row['id_uppercat'] != $id_uppercat )
532    {
533      $page['subcats'][$id_uppercat] = $subcats;
534
535      $subcats = array();
536      $id_uppercat = $row['id_uppercat'];
537    }
538    array_push( $subcats, $row['id'] );
539  }
540  mysql_free_result( $result );
541 
542  $page['subcats'][$id_uppercat] = $subcats;
543
544  return $plain_structure;
545}
546
547/**
548 * get N levels array representing structure under the given category
549 *
550 * create_structure returns the N levels array representing structure under
551 * the given gategory id. It also updates the
552 * $page['plain_structure'][id]['all_subcats_id'] and
553 * $page['plain_structure'][id]['direct_subcats_ids'] for each sub category.
554 *
555 * @param int $id_uppercat
556 * @return array
557 */
558function create_structure( $id_uppercat )
559{
560  global $page;
561
562  $structure = array();
563  $ids = get_subcats_ids( $id_uppercat );
564  foreach ( $ids as $id ) {
565    $category = $page['plain_structure'][$id];
566
567    $category['subcats'] = create_structure( $id );
568
569    $page['plain_structure'][$id]['all_subcats_ids'] =
570      get_all_subcats_ids( $id );
571
572    $page['plain_structure'][$id]['direct_subcats_ids'] =
573      get_subcats_ids( $id );
574
575    array_push( $structure, $category );
576  }
577  return $structure;
578}
579
580/**
581 * returns direct sub-categories ids
582 *
583 * Returns an array containing all the direct sub-categories ids of the
584 * given category. It uses the $page['subcats'] global array.
585 *
586 * @param int $id_uppercat
587 * @return array
588 */
589function get_subcats_ids( $id_uppercat )
590{
591  global $page;
592
593  if ( $id_uppercat == '' ) $id_uppercat = 'NULL';
594
595  if ( isset( $page['subcats'][$id_uppercat] ) )
596    return $page['subcats'][$id_uppercat];
597  else
598    return array();
599}
600
601/**
602 * returns all sub-categories ids, not only direct ones
603 *
604 * Returns an array containing all the sub-categories ids of the given
605 * category, not only direct ones. This function is recursive.
606 *
607 * @param int $category_id
608 * @return array
609 */
610function get_all_subcats_ids( $category_id )
611{
612  $ids = array();
613 
614  $subcats = get_subcats_ids( $category_id );
615  $ids = array_merge( $ids, $subcats );
616  foreach ( $subcats as $subcat ) {
617    // recursive call
618    $sub_subcats = get_all_subcats_ids( $subcat );
619    $ids = array_merge( $ids, $sub_subcats );
620  }
621  return array_unique( $ids );
622}
623
624/**
625 * prepares the query to update the table user_category
626 *
627 * Prepares the query (global variable $values) to update table
628 * user_category : for a couple (user,category) the number of sub-categories
629 * and the last date of the category (all sub-categories taken into
630 * account). It also calls function update_uppercats for each category. The
631 * function is recursive.
632 *
633 * @param array $categories
634 * @return void
635 */
636function update_user_category( $categories )
637{
638  global $page,$user_restrictions,$value_num,$values;
639
640  foreach ( $categories as $category ) {
641    // recursive call
642    update_user_category( $category['subcats'] );
643    // 1. update the table user_category
644    foreach ( $user_restrictions as $user_id => $restrictions ) {
645      // if the category is forbidden to this user, go to next user
646      if ( in_array( $category['id'], $restrictions ) ) continue;
647
648      // how many sub_categories for this user ?
649      $user_subcats = array_diff(
650        $page['plain_structure'][$category['id']]['direct_subcats_ids'],
651        $restrictions );
652      $user_nb_subcats = count( array_unique( $user_subcats ) );
653      // last date of the category
654      $user_all_subcats = array_unique( array_diff(
655        $page['plain_structure'][$category['id']]['all_subcats_ids'],
656        $restrictions ) );
657           
658      $query = 'SELECT MAX(date_last) AS last_date';
659      $query.= ' FROM '.PREFIX_TABLE.'categories';
660      $query.= ' WHERE id IN ('.$category['id'];
661      if ( count( $user_all_subcats ) > 0 )
662        $query.= ','.implode( ',', $user_all_subcats );
663      $query.= ')';
664      $query.= ';';
665      $row = mysql_fetch_array( mysql_query( $query ) );
666      $last_date = $row['last_date'];
667
668      // insert a new line in database
669      if ( $value_num++ > 0 ) $values.= ', ';
670      else                    $values.= ' ';
671      $values.= '('.$user_id.",".$category['id'].",'".$last_date."'";
672      $values.= ','.$user_nb_subcats.')';
673    }
674    update_uppercats( $category['id'] );
675  }
676}
677
678/**
679 * updates the column categories.uppercats
680 *
681 * @param int $category_id
682 * @return void
683 */
684function update_uppercats( $category_id )
685{
686  global $page;
687
688  $final_id = $category_id;
689  $uppercats = array();
690
691  array_push( $uppercats, $category_id );
692  $uppercat = $page['plain_structure'][$category_id]['id_uppercat'];
693
694  while ( $uppercat != '' )
695  {
696    array_push( $uppercats, $uppercat );
697    $category_id = $page['plain_structure'][$category_id]['id_uppercat'];
698    $uppercat = $page['plain_structure'][$category_id]['id_uppercat'];
699  }
700
701  $string_uppercats = implode( ',', array_reverse( $uppercats ) );
702  $query = 'UPDATE '.PREFIX_TABLE.'categories';
703  $query.= ' SET uppercats = '."'".$string_uppercats."'";
704  $query.= ' WHERE id = '.$final_id;
705  $query.= ';';
706  mysql_query( $query );
707}
708
709/**
710 * returns an array with the ids of the restricted categories for the user
711 *
712 * Returns an array with the ids of the restricted categories for the
713 * user. If the $check_invisible parameter is set to true, invisible
714 * categorie are added to the restricted one in the array.
715 *
716 * @param int $user_id
717 * @param string $user_status
718 * @param bool $check_invisible
719 * @param bool $use_groups
720 * @return array
721 */
722function get_user_restrictions( $user_id, $user_status,
723                                $check_invisible, $use_groups = true )
724{
725  // 1. retrieving ids of private categories
726  $query = 'SELECT id';
727  $query.= ' FROM '.PREFIX_TABLE.'categories';
728  $query.= " WHERE status = 'private'";
729  $query.= ';';
730  $result = mysql_query( $query );
731  $privates = array();
732  while ( $row = mysql_fetch_array( $result ) )
733  {
734    array_push( $privates, $row['id'] );
735  }
736  // 2. retrieving all authorized categories for the user
737  $authorized = array();
738  // 2.1. retrieving authorized categories thanks to personnal user
739  //      authorization
740  $query = 'SELECT cat_id';
741  $query.= ' FROM '.PREFIX_TABLE.'user_access';
742  $query.= ' WHERE user_id = '.$user_id;
743  $query.= ';';
744  $result = mysql_query( $query );
745  while ( $row = mysql_fetch_array( $result ) )
746  {
747    array_push( $authorized, $row['cat_id'] );
748  }
749  // 2.2. retrieving authorized categories thanks to group authorization to
750  //      which the user is a member
751  if ( $use_groups )
752  {
753    $query = 'SELECT ga.cat_id';
754    $query.= ' FROM '.PREFIX_TABLE.'user_group as ug';
755    $query.= ', '.PREFIX_TABLE.'group_access as ga';
756    $query.= ' WHERE ug.group_id = ga.group_id';
757    $query.= ' AND ug.user_id = '.$user_id;
758    $query.= ';';
759    $result = mysql_query( $query );
760    while ( $row = mysql_fetch_array( $result ) )
761    {
762      array_push( $authorized, $row['cat_id'] );
763    }
764    $authorized = array_unique( $authorized );
765  }
766
767  $forbidden = array();
768  foreach ( $privates as $private ) {
769    if ( !in_array( $private, $authorized ) )
770    {
771      array_push( $forbidden, $private );
772    }
773  }
774
775  if ( $check_invisible )
776  {
777    // 3. adding to the restricted categories, the invisible ones
778    if ( $user_status != 'admin' )
779    {
780      $query = 'SELECT id';
781      $query.= ' FROM '.PREFIX_TABLE.'categories';
782      $query.= " WHERE visible = 'false';";
783      $result = mysql_query( $query );
784      while ( $row = mysql_fetch_array( $result ) )
785      {
786        array_push( $forbidden, $row['id'] );
787      }
788    }
789  }
790  return array_unique( $forbidden );
791}
792
793/**
794 * finalizes operation for user_category table update
795 *
796 * This function is called by synchronization_*. It creates the
797 * $page['plain_structure'] and $page['structure'], get the SQL query to
798 * update user_category, clean user_category, and finally update the
799 * table. The users updates depends on the global array $user_restrictions.
800 *
801 * @return void
802 */
803function synchronize()
804{
805  global $user_restrictions,$page,$values;
806
807  if ( !isset( $page['plain_structure'] ) )
808    $page['plain_structure'] = get_plain_structure();
809  if ( !isset( $page['structure'] ) )
810    $page['structure']       = create_structure( '' );
811 
812  update_user_category( $page['structure'] );
813
814  // cleaning user_category table for users to update
815  foreach( $user_restrictions as $user_id => $restrictions ) {
816    $query = 'DELETE';
817    $query.= ' FROM '.PREFIX_TABLE.'user_category';
818    $query.= ' WHERE user_id = '.$user_id;
819    $query.= ';';
820    mysql_query( $query );
821  }
822
823  $query = 'INSERT INTO '.PREFIX_TABLE.'user_category';
824  $query.= ' (user_id,category_id,date_last,nb_sub_categories) VALUES ';
825  $query.= $values;
826  $query.= ';';
827  mysql_query( $query );
828}
829
830/**
831 * synchronizes all users calculated informations
832 *
833 * fills global array $user_restrictions with all users and related
834 * restrictions before calling synchronize.
835 *
836 * @return void
837 */
838function synchronize_all_users()
839{
840  global $user_restrictions;
841 
842  $user_restrictions = array();
843 
844  $query = 'SELECT id';
845  $query.= ' FROM '.PREFIX_TABLE.'users';
846  $query.= ';';
847  $result = mysql_query( $query );
848  while ( $row = mysql_fetch_array( $result ) )
849  {
850    $user_restrictions[$row['id']] = update_user_restrictions( $row['id'] );
851  }
852  synchronize();
853}
854
855/**
856 * synchronizes 1 user calculated informations
857 *
858 * fills global array $user_restrictions with the user id and its related
859 * restrictions before calling synchronize.
860 *
861 * @param int $user_id
862 * @return void
863 */
864function synchronize_user( $user_id )
865{
866  global $user_restrictions;
867
868  $user_restrictions = array();
869  $user_restrictions[$user_id] = update_user_restrictions( $user_id );
870  synchronize();
871}
872
873/**
874 * synchronizes all users (belonging to the group) calculated informations
875 *
876 * fills global array $user_restrictions with all users and related
877 * restrictions before calling synchronize.
878 *
879 * @return void
880 */
881function synchronize_group( $group_id )
882{
883  global $user_restrictions;
884
885  $user_restrictions = array();
886 
887  $query = 'SELECT id';
888  $query.= ' FROM '.PREFIX_TABLE.'users';
889  $query.= ', '.PREFIX_TABLE.'user_group';
890  $query.= ' WHERE group_id = '.$group_id;
891  $query.= ' AND id = user_id';
892  $query.= ';';
893  $result = mysql_query( $query );
894  while ( $row = mysql_fetch_array( $result ) )
895  {
896    $user_restrictions[$row['id']] = update_user_restrictions( $row['id'] );
897  }
898  synchronize();
899}
900
901/**
902 * updates the calculated data users.forbidden_categories, it includes
903 * sub-categories of the direct forbidden categories
904 *
905 * @param nt $user_id
906 * @return array
907 */
908function update_user_restrictions( $user_id )
909{
910  $restrictions = get_user_all_restrictions( $user_id );
911
912  // update the users.forbidden_categories in database
913  $query = 'UPDATE '.PREFIX_TABLE.'users';
914  $query.= ' SET forbidden_categories = ';
915  if ( count( $restrictions ) > 0 )
916    $query.= "'".implode( ',', $restrictions )."'";
917  else
918    $query.= 'NULL';
919  $query .= ' WHERE id = $user_id';
920  $query.= ';';
921  mysql_query( $query );
922
923  return $restrictions;
924}
925
926/**
927 * returns all the restricted categories ids including sub-categories
928 *
929 * @param int $user_id
930 * @return array
931 */
932function get_user_all_restrictions( $user_id )
933{
934  global $page;
935 
936  $query = 'SELECT status';
937  $query.= ' FROM '.PREFIX_TABLE.'users';
938  $query.= ' WHERE id = '.$user_id;
939  $query.= ';';
940  $row = mysql_fetch_array( mysql_query( $query ) );
941 
942  $base_restrictions=get_user_restrictions($user_id,$row['status'],true,true);
943
944  $restrictions = array();
945  foreach ( $base_restrictions as $category_id ) {
946    $restrictions =
947      array_merge( $restrictions,
948                   $page['plain_structure'][$category_id]['all_subcats_ids'] );
949  }
950
951  return array_unique( $restrictions );
952}
953
954// The function is_user_allowed returns :
955//      - 0 : if the category is allowed with this $restrictions array
956//      - 1 : if this category is not allowed
957//      - 2 : if an uppercat category is not allowed
958// Note : the restrictions array must represent ONLY direct forbidden
959// categories, not all forbidden categories
960function is_user_allowed( $category_id, $restrictions )
961{
962  if ( in_array( $category_id, $restrictions ) ) return 1;
963
964  $query = 'SELECT uppercats';
965  $query.= ' FROM '.PREFIX_TABLE.'categories';
966  $query.= ' WHERE id = '.$category_id;
967  $query.= ';';
968  $row = mysql_fetch_array( mysql_query( $query ) );
969  $uppercats = explode( ',', $row['uppercats'] );
970  foreach ( $uppercats as $category_id ) {
971    if ( in_array( $category_id, $restrictions ) ) return 2;
972  }
973
974  // no restriction found : the user is allowed to access this category
975  return 0;
976}
977
978/**
979 * returns an array containing sub-directories which can be a category
980 *
981 * directories nammed "thumbnail" are omitted
982 *
983 * @param string $basedir
984 * @return array
985 */
986function get_category_directories( $basedir )
987{
988  $sub_dirs = array();
989
990  if ( $opendir = opendir( $basedir ) )
991  {
992    while ( $file = readdir( $opendir ) )
993    {
994      if ( $file != '.' and $file != '..'
995           and is_dir( $basedir.'/'.$file )
996           and $file != 'thumbnail' )
997      {
998        array_push( $sub_dirs, $file );
999      }
1000    }
1001  }
1002  return $sub_dirs;
1003}
1004?>
Note: See TracBrowser for help on using the repository browser.