1 | <?php |
---|
2 | /*************************************************************************** |
---|
3 | * functions_group.inc.php * |
---|
4 | * -------------------- * |
---|
5 | * application : PhpWebGallery 1.3 * |
---|
6 | * author : Pierrick LE GALL <pierrick@z0rglub.com> * |
---|
7 | * * |
---|
8 | *************************************************************************** |
---|
9 | |
---|
10 | *************************************************************************** |
---|
11 | * * |
---|
12 | * This program is free software; you can redistribute it and/or modify * |
---|
13 | * it under the terms of the GNU General Public License as published by * |
---|
14 | * the Free Software Foundation; * |
---|
15 | * * |
---|
16 | ***************************************************************************/ |
---|
17 | |
---|
18 | // get_group_restrictions returns an array containing all unaccessible |
---|
19 | // category ids. |
---|
20 | function get_group_restrictions( $group_id ) |
---|
21 | { |
---|
22 | // 1. retrieving ids of private categories |
---|
23 | $query = 'SELECT id'; |
---|
24 | $query.= ' FROM '.PREFIX_TABLE.'categories'; |
---|
25 | $query.= " WHERE status = 'private'"; |
---|
26 | $query.= ';'; |
---|
27 | $result = mysql_query( $query ); |
---|
28 | $privates = array(); |
---|
29 | while ( $row = mysql_fetch_array( $result ) ) |
---|
30 | { |
---|
31 | array_push( $privates, $row['id'] ); |
---|
32 | } |
---|
33 | // 2. retrieving all authorized categories for the group |
---|
34 | $authorized = array(); |
---|
35 | $query = 'SELECT cat_id'; |
---|
36 | $query.= ' FROM '.PREFIX_TABLE.'group_access'; |
---|
37 | $query.= ' WHERE group_id = '.$group_id; |
---|
38 | $query.= ';'; |
---|
39 | $result = mysql_query( $query ); |
---|
40 | while ( $row = mysql_fetch_array( $result ) ) |
---|
41 | { |
---|
42 | array_push( $authorized, $row['cat_id'] ); |
---|
43 | } |
---|
44 | |
---|
45 | $forbidden = array(); |
---|
46 | foreach ( $privates as $private ) { |
---|
47 | if ( !in_array( $private, $authorized ) ) |
---|
48 | { |
---|
49 | array_push( $forbidden, $private ); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | return $forbidden; |
---|
54 | } |
---|
55 | |
---|
56 | // get_all_group_restrictions returns an array with ALL unaccessible |
---|
57 | // category ids, including sub-categories |
---|
58 | function get_all_group_restrictions( $group_id ) |
---|
59 | { |
---|
60 | $restricted_cats = get_group_restrictions( $group_id ); |
---|
61 | foreach ( $restricted_cats as $restricted_cat ) { |
---|
62 | $sub_restricted_cats = get_subcats_id( $restricted_cat ); |
---|
63 | foreach ( $sub_restricted_cats as $sub_restricted_cat ) { |
---|
64 | array_push( $restricted_cats, $sub_restricted_cat ); |
---|
65 | } |
---|
66 | } |
---|
67 | return $restricted_cats; |
---|
68 | } |
---|
69 | |
---|
70 | // The function is_group_allowed returns : |
---|
71 | // - 0 : if the category is allowed with this $restrictions array |
---|
72 | // - 1 : if this category is not allowed |
---|
73 | // - 2 : if an uppercat category is not allowed |
---|
74 | function is_group_allowed( $category_id, $restrictions ) |
---|
75 | { |
---|
76 | $lowest_category_id = $category_id; |
---|
77 | |
---|
78 | $is_root = false; |
---|
79 | while ( !$is_root and !in_array( $category_id, $restrictions ) ) |
---|
80 | { |
---|
81 | $query = 'SELECT id_uppercat'; |
---|
82 | $query.= ' FROM '.PREFIX_TABLE.'categories'; |
---|
83 | $query.= ' WHERE id = '.$category_id; |
---|
84 | $query.= ';'; |
---|
85 | $row = mysql_fetch_array( mysql_query( $query ) ); |
---|
86 | if ( $row['id_uppercat'] == '' ) |
---|
87 | { |
---|
88 | $is_root = true; |
---|
89 | } |
---|
90 | $category_id = $row['id_uppercat']; |
---|
91 | } |
---|
92 | |
---|
93 | if ( in_array( $lowest_category_id, $restrictions ) ) |
---|
94 | { |
---|
95 | return 1; |
---|
96 | } |
---|
97 | if ( in_array( $category_id, $restrictions ) ) |
---|
98 | { |
---|
99 | return 2; |
---|
100 | } |
---|
101 | // this group is allowed to go in this category |
---|
102 | return 0; |
---|
103 | } |
---|
104 | ?> |
---|