1 | <?php |
---|
2 | /*************************************************************************** |
---|
3 | * functions.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 | $tab_ext_create_TN = array ( 'jpg', 'png', 'JPG', 'PNG' ); |
---|
19 | |
---|
20 | // is_image returns true if the given $filename (including the path) is a |
---|
21 | // picture according to its format and its extension. |
---|
22 | // As GD library can only generate pictures from jpeg and png files, if you |
---|
23 | // ask if the filename is an image for thumbnail creation (second parameter |
---|
24 | // set to true), the only authorized formats are jpeg and png. |
---|
25 | function is_image( $filename, $create_thumbnail = false ) |
---|
26 | { |
---|
27 | global $conf, $tab_ext_create_TN; |
---|
28 | |
---|
29 | if ( is_file( $filename ) ) |
---|
30 | { |
---|
31 | $size = getimagesize( $filename ); |
---|
32 | // $size[2] == 1 means GIF |
---|
33 | // $size[2] == 2 means JPG |
---|
34 | // $size[2] == 3 means PNG |
---|
35 | if ( !$create_thumbnail ) |
---|
36 | { |
---|
37 | if ( in_array( get_extension( $filename ), $conf['picture_ext'] ) |
---|
38 | and ( $size[2] == 1 or $size[2] == 2 or $size[2] == 3 ) ) |
---|
39 | { |
---|
40 | return true; |
---|
41 | } |
---|
42 | } |
---|
43 | else |
---|
44 | { |
---|
45 | if ( in_array( get_extension( $filename ), $tab_ext_create_TN ) |
---|
46 | and ( $size[2] == 2 or $size[2] == 3 ) ) |
---|
47 | { |
---|
48 | return true; |
---|
49 | } |
---|
50 | } |
---|
51 | } |
---|
52 | return false; |
---|
53 | } |
---|
54 | |
---|
55 | function TN_exists( $dir, $file ) |
---|
56 | { |
---|
57 | global $conf; |
---|
58 | |
---|
59 | $filename = get_filename_wo_extension( $file ); |
---|
60 | foreach ( $conf['picture_ext'] as $ext ) { |
---|
61 | $test = $dir.'/thumbnail/'.$conf['prefix_thumbnail'].$filename.'.'.$ext; |
---|
62 | if ( is_file ( $test ) ) |
---|
63 | { |
---|
64 | return $ext; |
---|
65 | } |
---|
66 | } |
---|
67 | return false; |
---|
68 | } |
---|
69 | |
---|
70 | // The function delete_site deletes a site |
---|
71 | // and call the function delete_category for each primary category of the site |
---|
72 | function delete_site( $id ) |
---|
73 | { |
---|
74 | // destruction of the categories of the site |
---|
75 | $query = 'SELECT id'; |
---|
76 | $query.= ' FROM '.PREFIX_TABLE.'categories'; |
---|
77 | $query.= ' WHERE site_id = '.$id; |
---|
78 | $query.= ';'; |
---|
79 | $result = mysql_query( $query ); |
---|
80 | while ( $row = mysql_fetch_array( $result ) ) |
---|
81 | { |
---|
82 | delete_category( $row['id'] ); |
---|
83 | } |
---|
84 | |
---|
85 | // destruction of the site |
---|
86 | $query = 'DELETE FROM '.PREFIX_TABLE.'sites'; |
---|
87 | $query.= ' WHERE id = '.$id; |
---|
88 | $query.= ';'; |
---|
89 | mysql_query( $query ); |
---|
90 | } |
---|
91 | |
---|
92 | // The function delete_category deletes the category identified by the $id |
---|
93 | // It also deletes (in the database) : |
---|
94 | // - all the images of the images (thanks to delete_image, see further) |
---|
95 | // - all the restrictions linked to the category |
---|
96 | // The function works recursively. |
---|
97 | function delete_category( $id ) |
---|
98 | { |
---|
99 | // destruction of all the related images |
---|
100 | $query = 'SELECT id'; |
---|
101 | $query.= ' FROM '.PREFIX_TABLE.'images'; |
---|
102 | $query.= ' WHERE cat_id = '.$id; |
---|
103 | $query.= ';'; |
---|
104 | $result = mysql_query( $query ); |
---|
105 | while ( $row = mysql_fetch_array( $result ) ) |
---|
106 | { |
---|
107 | delete_image( $row['id'] ); |
---|
108 | } |
---|
109 | |
---|
110 | // destruction of the access linked to the category |
---|
111 | $query = 'DELETE FROM '.PREFIX_TABLE.'user_access'; |
---|
112 | $query.= ' WHERE cat_id = '.$id; |
---|
113 | $query.= ';'; |
---|
114 | mysql_query( $query ); |
---|
115 | $query = 'DELETE FROM '.PREFIX_TABLE.'group_access'; |
---|
116 | $query.= ' WHERE cat_id = '.$id; |
---|
117 | $query.= ';'; |
---|
118 | mysql_query( $query ); |
---|
119 | |
---|
120 | // destruction of the sub-categories |
---|
121 | $query = 'SELECT id'; |
---|
122 | $query.= ' FROM '.PREFIX_TABLE.'categories'; |
---|
123 | $query.= ' WHERE id_uppercat = '.$id; |
---|
124 | $query.= ';'; |
---|
125 | $result = mysql_query( $query ); |
---|
126 | while( $row = mysql_fetch_array( $result ) ) |
---|
127 | { |
---|
128 | delete_category( $row['id'] ); |
---|
129 | } |
---|
130 | |
---|
131 | // destruction of the category |
---|
132 | $query = 'DELETE FROM '.PREFIX_TABLE.'categories'; |
---|
133 | $query.= ' WHERE id = '.$id; |
---|
134 | $query.= ';'; |
---|
135 | mysql_query( $query ); |
---|
136 | } |
---|
137 | |
---|
138 | // The function delete_image deletes the image identified by the $id |
---|
139 | // It also deletes (in the database) : |
---|
140 | // - all the comments related to the image |
---|
141 | // - all the favorites associated to the image |
---|
142 | function delete_image( $id ) |
---|
143 | { |
---|
144 | global $count_deleted; |
---|
145 | |
---|
146 | // destruction of the comments on the image |
---|
147 | $query = 'DELETE FROM '.PREFIX_TABLE.'comments'; |
---|
148 | $query.= ' WHERE image_id = '.$id; |
---|
149 | $query.= ';'; |
---|
150 | mysql_query( $query ); |
---|
151 | |
---|
152 | // destruction of the favorites associated with the picture |
---|
153 | $query = 'DELETE FROM '.PREFIX_TABLE.'favorites'; |
---|
154 | $query.= ' WHERE image_id = '.$id; |
---|
155 | $query.= ';'; |
---|
156 | mysql_query( $query ); |
---|
157 | |
---|
158 | // destruction of the image |
---|
159 | $query = 'DELETE FROM '.PREFIX_TABLE.'images'; |
---|
160 | $query.= ' WHERE id = '.$id; |
---|
161 | $query.= ';'; |
---|
162 | mysql_query( $query ); |
---|
163 | $count_deleted++; |
---|
164 | } |
---|
165 | |
---|
166 | // The delete_user function delete a user identified by the $user_id |
---|
167 | // It also deletes : |
---|
168 | // - all the access linked to this user |
---|
169 | // - all the links to any group |
---|
170 | // - all the favorites linked to this user |
---|
171 | // - all sessions linked to this user |
---|
172 | function delete_user( $user_id ) |
---|
173 | { |
---|
174 | // destruction of the access linked to the user |
---|
175 | $query = 'DELETE FROM '.PREFIX_TABLE.'user_access'; |
---|
176 | $query.= ' WHERE user_id = '.$user_id; |
---|
177 | $query.= ';'; |
---|
178 | mysql_query( $query ); |
---|
179 | |
---|
180 | // destruction of the group links for this user |
---|
181 | $query = 'DELETE FROM '.PREFIX_TABLE.'user_group'; |
---|
182 | $query.= ' WHERE user_id = '.$user_id; |
---|
183 | $query.= ';'; |
---|
184 | mysql_query( $query ); |
---|
185 | |
---|
186 | // destruction of the favorites associated with the user |
---|
187 | $query = 'DELETE FROM '.PREFIX_TABLE.'favorites'; |
---|
188 | $query.= ' WHERE user_id = '.$user_id; |
---|
189 | $query.= ';'; |
---|
190 | mysql_query( $query ); |
---|
191 | |
---|
192 | // destruction of the sessions linked with the user |
---|
193 | $query = 'DELETE FROM '.PREFIX_TABLE.'sessions'; |
---|
194 | $query.= ' WHERE user_id = '.$user_id; |
---|
195 | $query.= ';'; |
---|
196 | mysql_query( $query ); |
---|
197 | |
---|
198 | // destruction of the user |
---|
199 | $query = 'DELETE FROM '.PREFIX_TABLE.'users'; |
---|
200 | $query.= ' WHERE id = '.$user_id; |
---|
201 | $query.= ';'; |
---|
202 | mysql_query( $query ); |
---|
203 | } |
---|
204 | |
---|
205 | // delete_group deletes a group identified by its $group_id. |
---|
206 | // It also deletes : |
---|
207 | // - all the access linked to this group |
---|
208 | // - all the links between this group and any user |
---|
209 | function delete_group( $group_id ) |
---|
210 | { |
---|
211 | // destruction of the access linked to the group |
---|
212 | $query = 'DELETE FROM '.PREFIX_TABLE.'group_access'; |
---|
213 | $query.= ' WHERE group_id = '.$group_id; |
---|
214 | $query.= ';'; |
---|
215 | mysql_query( $query ); |
---|
216 | |
---|
217 | // destruction of the group links for this group |
---|
218 | $query = 'DELETE FROM '.PREFIX_TABLE.'user_group'; |
---|
219 | $query.= ' WHERE group_id = '.$group_id; |
---|
220 | $query.= ';'; |
---|
221 | mysql_query( $query ); |
---|
222 | |
---|
223 | // destruction of the group |
---|
224 | $query = 'DELETE FROM '.PREFIX_TABLE.'groups'; |
---|
225 | $query.= ' WHERE id = '.$group_id; |
---|
226 | $query.= ';'; |
---|
227 | mysql_query( $query ); |
---|
228 | } |
---|
229 | |
---|
230 | // The check_favorites function deletes all the favorites of a user if he is |
---|
231 | // not allowed to see them (the category or an upper category is restricted |
---|
232 | // or invisible) |
---|
233 | function check_favorites( $user_id ) |
---|
234 | { |
---|
235 | $query = 'SELECT status'; |
---|
236 | $query.= ' FROM '.PREFIX_TABLE.'users'; |
---|
237 | $query.= ' WHERE id = '.$user_id; |
---|
238 | $query.= ';'; |
---|
239 | $row = mysql_fetch_array( mysql_query( $query ) ); |
---|
240 | $status = $row['status']; |
---|
241 | // retrieving all the restricted categories for this user |
---|
242 | $restricted_cat = get_all_restrictions( $user_id, $status ); |
---|
243 | // retrieving all the favorites for this user and comparing their |
---|
244 | // categories to the restricted categories |
---|
245 | $query = 'SELECT image_id, cat_id'; |
---|
246 | $query.= ' FROM '.PREFIX_TABLE.'favorites, '.PREFIX_TABLE.'images'; |
---|
247 | $query.= ' WHERE user_id = '.$user_id; |
---|
248 | $query.= ' AND id = image_id'; |
---|
249 | $query.= ';'; |
---|
250 | $result = mysql_query ( $query ); |
---|
251 | while ( $row = mysql_fetch_array( $result ) ) |
---|
252 | { |
---|
253 | if ( in_array( $row['cat_id'], $restricted_cat ) ) |
---|
254 | { |
---|
255 | $query = 'DELETE FROM '.PREFIX_TABLE.'favorites'; |
---|
256 | $query.= ' WHERE image_id = '.$row['image_id']; |
---|
257 | $query.= ' AND user_id = '.$user_id; |
---|
258 | $query.= ';'; |
---|
259 | mysql_query( $query ); |
---|
260 | } |
---|
261 | } |
---|
262 | } |
---|
263 | ?> |
---|