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

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

table user_category dropped

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