1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
5 | // | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2005-01-08 11:23:52 +0000 (Sat, 08 Jan 2005) $ |
---|
10 | // | last modifier : $Author: plg $ |
---|
11 | // | revision : $Revision: 680 $ |
---|
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 | include(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. |
---|
37 | function is_image( $filename, $create_thumbnail = false ) |
---|
38 | { |
---|
39 | global $conf, $tab_ext_create_TN; |
---|
40 | |
---|
41 | if (is_file($filename) |
---|
42 | and in_array(get_extension($filename), $conf['picture_ext'])) |
---|
43 | { |
---|
44 | $size = getimagesize( $filename ); |
---|
45 | // $size[2] == 1 means GIF |
---|
46 | // $size[2] == 2 means JPG |
---|
47 | // $size[2] == 3 means PNG |
---|
48 | if ( !$create_thumbnail ) |
---|
49 | { |
---|
50 | if ( in_array( get_extension( $filename ), $conf['picture_ext'] ) |
---|
51 | and ( $size[2] == 1 or $size[2] == 2 or $size[2] == 3 ) ) |
---|
52 | { |
---|
53 | return true; |
---|
54 | } |
---|
55 | } |
---|
56 | else |
---|
57 | { |
---|
58 | if ( in_array( get_extension( $filename ), $tab_ext_create_TN ) |
---|
59 | and ( $size[2] == 2 or $size[2] == 3 ) ) |
---|
60 | { |
---|
61 | return true; |
---|
62 | } |
---|
63 | } |
---|
64 | } |
---|
65 | return false; |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * returns an array with all picture files according to $conf['file_ext'] |
---|
70 | * |
---|
71 | * @param string $dir |
---|
72 | * @return array |
---|
73 | */ |
---|
74 | function get_pwg_files($dir) |
---|
75 | { |
---|
76 | global $conf; |
---|
77 | |
---|
78 | $pictures = array(); |
---|
79 | if ($opendir = opendir($dir)) |
---|
80 | { |
---|
81 | while ($file = readdir($opendir)) |
---|
82 | { |
---|
83 | if (in_array(get_extension($file), $conf['file_ext'])) |
---|
84 | { |
---|
85 | array_push($pictures, $file); |
---|
86 | } |
---|
87 | } |
---|
88 | } |
---|
89 | return $pictures; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * returns an array with all thumbnails according to $conf['picture_ext'] |
---|
94 | * and $conf['prefix_thumbnail'] |
---|
95 | * |
---|
96 | * @param string $dir |
---|
97 | * @return array |
---|
98 | */ |
---|
99 | function get_thumb_files($dir) |
---|
100 | { |
---|
101 | global $conf; |
---|
102 | |
---|
103 | $prefix_length = strlen($conf['prefix_thumbnail']); |
---|
104 | |
---|
105 | $thumbnails = array(); |
---|
106 | if ($opendir = @opendir($dir.'/thumbnail')) |
---|
107 | { |
---|
108 | while ($file = readdir($opendir)) |
---|
109 | { |
---|
110 | if (in_array(get_extension($file), $conf['picture_ext']) |
---|
111 | and substr($file, 0, $prefix_length) == $conf['prefix_thumbnail']) |
---|
112 | { |
---|
113 | array_push($thumbnails, $file); |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | return $thumbnails; |
---|
118 | } |
---|
119 | |
---|
120 | /** |
---|
121 | * returns an array with representative picture files of a directory |
---|
122 | * according to $conf['picture_ext'] |
---|
123 | * |
---|
124 | * @param string $dir |
---|
125 | * @return array |
---|
126 | */ |
---|
127 | function get_representative_files($dir) |
---|
128 | { |
---|
129 | global $conf; |
---|
130 | |
---|
131 | $pictures = array(); |
---|
132 | if ($opendir = @opendir($dir.'/pwg_representative')) |
---|
133 | { |
---|
134 | while ($file = readdir($opendir)) |
---|
135 | { |
---|
136 | if (in_array(get_extension($file), $conf['picture_ext'])) |
---|
137 | { |
---|
138 | array_push($pictures, $file); |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | return $pictures; |
---|
143 | } |
---|
144 | |
---|
145 | function TN_exists( $dir, $file ) |
---|
146 | { |
---|
147 | global $conf; |
---|
148 | |
---|
149 | $filename = get_filename_wo_extension( $file ); |
---|
150 | foreach ( $conf['picture_ext'] as $ext ) { |
---|
151 | $test = $dir.'/thumbnail/'.$conf['prefix_thumbnail'].$filename.'.'.$ext; |
---|
152 | if ( is_file ( $test ) ) |
---|
153 | { |
---|
154 | return $ext; |
---|
155 | } |
---|
156 | } |
---|
157 | return false; |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | // The function delete_site deletes a site and call the function |
---|
162 | // delete_categories for each primary category of the site |
---|
163 | function delete_site( $id ) |
---|
164 | { |
---|
165 | // destruction of the categories of the site |
---|
166 | $query = ' |
---|
167 | SELECT id |
---|
168 | FROM '.CATEGORIES_TABLE.' |
---|
169 | WHERE site_id = '.$id.' |
---|
170 | ;'; |
---|
171 | $result = pwg_query($query); |
---|
172 | $category_ids = array(); |
---|
173 | while ($row = mysql_fetch_array($result)) |
---|
174 | { |
---|
175 | array_push($category_ids, $row['id']); |
---|
176 | } |
---|
177 | delete_categories($category_ids); |
---|
178 | |
---|
179 | // destruction of the site |
---|
180 | $query = ' |
---|
181 | DELETE FROM '.SITES_TABLE.' |
---|
182 | WHERE id = '.$id.' |
---|
183 | ;'; |
---|
184 | pwg_query($query); |
---|
185 | } |
---|
186 | |
---|
187 | |
---|
188 | // The function delete_categories deletes the categories identified by the |
---|
189 | // (numeric) key of the array $ids. It also deletes (in the database) : |
---|
190 | // - all the elements of the category (delete_elements, see further) |
---|
191 | // - all the links between elements and this category |
---|
192 | // - all the restrictions linked to the category |
---|
193 | // The function works recursively. |
---|
194 | function delete_categories($ids) |
---|
195 | { |
---|
196 | global $counts; |
---|
197 | |
---|
198 | if (count($ids) == 0) |
---|
199 | { |
---|
200 | return; |
---|
201 | } |
---|
202 | |
---|
203 | // add sub-category ids to the given ids : if a category is deleted, all |
---|
204 | // sub-categories must be so |
---|
205 | $ids = get_subcat_ids($ids); |
---|
206 | |
---|
207 | // destruction of all the related elements |
---|
208 | $query = ' |
---|
209 | SELECT id |
---|
210 | FROM '.IMAGES_TABLE.' |
---|
211 | WHERE storage_category_id IN ( |
---|
212 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
213 | ;'; |
---|
214 | $result = pwg_query($query); |
---|
215 | $element_ids = array(); |
---|
216 | while ($row = mysql_fetch_array($result)) |
---|
217 | { |
---|
218 | array_push($element_ids, $row['id']); |
---|
219 | } |
---|
220 | delete_elements($element_ids); |
---|
221 | |
---|
222 | // destruction of the links between images and this category |
---|
223 | $query = ' |
---|
224 | DELETE FROM '.IMAGE_CATEGORY_TABLE.' |
---|
225 | WHERE category_id IN ( |
---|
226 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
227 | ;'; |
---|
228 | pwg_query($query); |
---|
229 | |
---|
230 | // destruction of the access linked to the category |
---|
231 | $query = ' |
---|
232 | DELETE FROM '.USER_ACCESS_TABLE.' |
---|
233 | WHERE cat_id IN ( |
---|
234 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
235 | ;'; |
---|
236 | pwg_query($query); |
---|
237 | $query = ' |
---|
238 | DELETE FROM '.GROUP_ACCESS_TABLE.' |
---|
239 | WHERE cat_id IN ( |
---|
240 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
241 | ;'; |
---|
242 | pwg_query($query); |
---|
243 | |
---|
244 | // destruction of the category |
---|
245 | $query = ' |
---|
246 | DELETE FROM '.CATEGORIES_TABLE.' |
---|
247 | WHERE id IN ( |
---|
248 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
249 | ;'; |
---|
250 | pwg_query($query); |
---|
251 | |
---|
252 | if (isset($counts['del_categories'])) |
---|
253 | { |
---|
254 | $counts['del_categories']+= count($ids); |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | // The function delete_elements deletes the elements identified by the |
---|
259 | // (numeric) values of the array $ids. It also deletes (in the database) : |
---|
260 | // - all the comments related to elements |
---|
261 | // - all the links between categories and elements |
---|
262 | // - all the favorites associated to elements |
---|
263 | function delete_elements($ids) |
---|
264 | { |
---|
265 | global $counts; |
---|
266 | |
---|
267 | if (count($ids) == 0) |
---|
268 | { |
---|
269 | return; |
---|
270 | } |
---|
271 | |
---|
272 | // destruction of the comments on the image |
---|
273 | $query = ' |
---|
274 | DELETE FROM '.COMMENTS_TABLE.' |
---|
275 | WHERE image_id IN ( |
---|
276 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
277 | ;'; |
---|
278 | pwg_query($query); |
---|
279 | |
---|
280 | // destruction of the links between images and this category |
---|
281 | $query = ' |
---|
282 | DELETE FROM '.IMAGE_CATEGORY_TABLE.' |
---|
283 | WHERE image_id IN ( |
---|
284 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
285 | ;'; |
---|
286 | pwg_query($query); |
---|
287 | |
---|
288 | // destruction of the favorites associated with the picture |
---|
289 | $query = ' |
---|
290 | DELETE FROM '.FAVORITES_TABLE.' |
---|
291 | WHERE image_id IN ( |
---|
292 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
293 | ;'; |
---|
294 | pwg_query($query); |
---|
295 | |
---|
296 | // destruction of the rates associated to this element |
---|
297 | $query = ' |
---|
298 | DELETE FROM '.RATE_TABLE.' |
---|
299 | WHERE element_id IN ( |
---|
300 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
301 | ;'; |
---|
302 | pwg_query($query); |
---|
303 | |
---|
304 | // destruction of the image |
---|
305 | $query = ' |
---|
306 | DELETE FROM '.IMAGES_TABLE.' |
---|
307 | WHERE id IN ( |
---|
308 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
309 | ;'; |
---|
310 | pwg_query($query); |
---|
311 | |
---|
312 | if (isset($counts['del_elements'])) |
---|
313 | { |
---|
314 | $counts['del_elements']+= count($ids); |
---|
315 | } |
---|
316 | } |
---|
317 | |
---|
318 | // The delete_user function delete a user identified by the $user_id |
---|
319 | // It also deletes : |
---|
320 | // - all the access linked to this user |
---|
321 | // - all the links to any group |
---|
322 | // - all the favorites linked to this user |
---|
323 | // - all sessions linked to this user |
---|
324 | // - calculated permissions linked to the user |
---|
325 | function delete_user($user_id) |
---|
326 | { |
---|
327 | // destruction of the access linked to the user |
---|
328 | $query = ' |
---|
329 | DELETE FROM '.USER_ACCESS_TABLE.' |
---|
330 | WHERE user_id = '.$user_id.' |
---|
331 | ;'; |
---|
332 | pwg_query($query); |
---|
333 | |
---|
334 | // destruction of the group links for this user |
---|
335 | $query = ' |
---|
336 | DELETE FROM '.USER_GROUP_TABLE.' |
---|
337 | WHERE user_id = '.$user_id.' |
---|
338 | ;'; |
---|
339 | pwg_query($query); |
---|
340 | |
---|
341 | // destruction of the favorites associated with the user |
---|
342 | $query = ' |
---|
343 | DELETE FROM '.FAVORITES_TABLE.' |
---|
344 | WHERE user_id = '.$user_id.' |
---|
345 | ;'; |
---|
346 | pwg_query($query); |
---|
347 | |
---|
348 | // destruction of the sessions linked with the user |
---|
349 | $query = ' |
---|
350 | DELETE FROM '.SESSIONS_TABLE.' |
---|
351 | WHERE user_id = '.$user_id.' |
---|
352 | ;'; |
---|
353 | pwg_query($query); |
---|
354 | |
---|
355 | // deletion of calculated permissions linked to the user |
---|
356 | $query = ' |
---|
357 | DELETE FROM '.USER_FORBIDDEN_TABLE.' |
---|
358 | WHERE user_id = '.$user_id.' |
---|
359 | ;'; |
---|
360 | pwg_query($query); |
---|
361 | |
---|
362 | // destruction of the user |
---|
363 | $query = ' |
---|
364 | DELETE FROM '.USERS_TABLE.' |
---|
365 | WHERE id = '.$user_id.' |
---|
366 | ;'; |
---|
367 | pwg_query($query); |
---|
368 | } |
---|
369 | |
---|
370 | /** |
---|
371 | * updates calculated informations about a set of categories : date_last and |
---|
372 | * nb_images. It also verifies that the representative picture is really |
---|
373 | * linked to the category. Optionnaly recursive. |
---|
374 | * |
---|
375 | * @param mixed category id |
---|
376 | * @param boolean recursive |
---|
377 | * @returns void |
---|
378 | */ |
---|
379 | function update_category($ids = 'all', $recursive = false) |
---|
380 | { |
---|
381 | // retrieving all categories to update |
---|
382 | $cat_ids = array(); |
---|
383 | |
---|
384 | $query = ' |
---|
385 | SELECT id |
---|
386 | FROM '.CATEGORIES_TABLE; |
---|
387 | if (is_array($ids)) |
---|
388 | { |
---|
389 | if ($recursive) |
---|
390 | { |
---|
391 | foreach ($ids as $num => $id) |
---|
392 | { |
---|
393 | if ($num == 0) |
---|
394 | { |
---|
395 | $query.= ' |
---|
396 | WHERE '; |
---|
397 | } |
---|
398 | else |
---|
399 | { |
---|
400 | $query.= ' |
---|
401 | OR '; |
---|
402 | } |
---|
403 | $query.= 'uppercats REGEXP \'(^|,)'.$id.'(,|$)\''; |
---|
404 | } |
---|
405 | } |
---|
406 | else |
---|
407 | { |
---|
408 | $query.= ' |
---|
409 | WHERE id IN ('.wordwrap(implode(', ', $ids), 80, "\n").')'; |
---|
410 | } |
---|
411 | } |
---|
412 | $query.= ' |
---|
413 | ;'; |
---|
414 | $result = pwg_query( $query ); |
---|
415 | while ( $row = mysql_fetch_array( $result ) ) |
---|
416 | { |
---|
417 | array_push($cat_ids, $row['id']); |
---|
418 | } |
---|
419 | $cat_ids = array_unique($cat_ids); |
---|
420 | |
---|
421 | if (count($cat_ids) == 0) |
---|
422 | { |
---|
423 | return false; |
---|
424 | } |
---|
425 | |
---|
426 | // calculate informations about categories retrieved |
---|
427 | $query = ' |
---|
428 | SELECT category_id, |
---|
429 | COUNT(image_id) AS nb_images, |
---|
430 | MAX(date_available) AS date_last |
---|
431 | FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id |
---|
432 | WHERE category_id IN ('.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
433 | GROUP BY category_id |
---|
434 | ;'; |
---|
435 | $result = pwg_query($query); |
---|
436 | $datas = array(); |
---|
437 | $query_ids = array(); |
---|
438 | while ( $row = mysql_fetch_array( $result ) ) |
---|
439 | { |
---|
440 | array_push($query_ids, $row['category_id']); |
---|
441 | array_push($datas, array('id' => $row['category_id'], |
---|
442 | 'date_last' => $row['date_last'], |
---|
443 | 'nb_images' => $row['nb_images'])); |
---|
444 | } |
---|
445 | // if all links between a category and elements have disappeared, no line |
---|
446 | // is returned but the update must be done ! |
---|
447 | foreach (array_diff($cat_ids, $query_ids) as $id) |
---|
448 | { |
---|
449 | array_push($datas, array('id' => $id, 'nb_images' => 0)); |
---|
450 | } |
---|
451 | |
---|
452 | $fields = array('primary' => array('id'), |
---|
453 | 'update' => array('date_last', 'nb_images')); |
---|
454 | mass_updates(CATEGORIES_TABLE, $fields, $datas); |
---|
455 | |
---|
456 | $query = ' |
---|
457 | UPDATE '.CATEGORIES_TABLE.' |
---|
458 | SET representative_picture_id = NULL |
---|
459 | WHERE nb_images = 0 |
---|
460 | ;'; |
---|
461 | pwg_query($query); |
---|
462 | |
---|
463 | if (count($cat_ids) > 0) |
---|
464 | { |
---|
465 | $categories = array(); |
---|
466 | // find all categories where the setted representative is not possible |
---|
467 | $query = ' |
---|
468 | SELECT id |
---|
469 | FROM '.CATEGORIES_TABLE.' LEFT JOIN '.IMAGE_CATEGORY_TABLE.' |
---|
470 | ON id = category_id AND representative_picture_id = image_id |
---|
471 | WHERE representative_picture_id IS NOT NULL |
---|
472 | AND id IN ('.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
473 | AND category_id IS NULL |
---|
474 | ;'; |
---|
475 | $result = pwg_query($query); |
---|
476 | while ($row = mysql_fetch_array($result)) |
---|
477 | { |
---|
478 | array_push($categories, $row['id']); |
---|
479 | } |
---|
480 | // find categories with elements and with no representant |
---|
481 | $query = ' |
---|
482 | SELECT id |
---|
483 | FROM '.CATEGORIES_TABLE.' |
---|
484 | WHERE representative_picture_id IS NULL |
---|
485 | AND nb_images != 0 |
---|
486 | ;'; |
---|
487 | $result = pwg_query($query); |
---|
488 | while ($row = mysql_fetch_array($result)) |
---|
489 | { |
---|
490 | array_push($categories, $row['id']); |
---|
491 | } |
---|
492 | |
---|
493 | $categories = array_unique($categories); |
---|
494 | set_random_representant($categories); |
---|
495 | } |
---|
496 | } |
---|
497 | |
---|
498 | function check_date_format( $date ) |
---|
499 | { |
---|
500 | // date arrives at this format : DD/MM/YYYY |
---|
501 | @list($day,$month,$year) = explode( '/', $date ); |
---|
502 | return @checkdate( $month, $day, $year ); |
---|
503 | } |
---|
504 | |
---|
505 | function date_convert( $date ) |
---|
506 | { |
---|
507 | // date arrives at this format : DD/MM/YYYY |
---|
508 | // It must be transformed in YYYY-MM-DD |
---|
509 | list($day,$month,$year) = explode( '/', $date ); |
---|
510 | return $year.'-'.$month.'-'.$day; |
---|
511 | } |
---|
512 | |
---|
513 | function date_convert_back( $date ) |
---|
514 | { |
---|
515 | // date arrives at this format : YYYY-MM-DD |
---|
516 | // It must be transformed in DD/MM/YYYY |
---|
517 | if ( $date != '' ) |
---|
518 | { |
---|
519 | list($year,$month,$day) = explode( '-', $date ); |
---|
520 | return $day.'/'.$month.'/'.$year; |
---|
521 | } |
---|
522 | else |
---|
523 | { |
---|
524 | return ''; |
---|
525 | } |
---|
526 | } |
---|
527 | |
---|
528 | // get_keywords returns an array with relevant keywords found in the string |
---|
529 | // given in argument. Keywords must be separated by comma in this string. |
---|
530 | // keywords must : |
---|
531 | // - be longer or equal to 3 characters |
---|
532 | // - not contain ', " or blank characters |
---|
533 | // - unique in the string ("test,test" -> "test") |
---|
534 | function get_keywords( $keywords_string ) |
---|
535 | { |
---|
536 | $keywords = array(); |
---|
537 | |
---|
538 | $candidates = explode( ',', $keywords_string ); |
---|
539 | foreach ( $candidates as $candidate ) { |
---|
540 | if ( strlen($candidate) >= 3 and !preg_match( '/(\'|"|\s)/', $candidate ) ) |
---|
541 | array_push( $keywords, $candidate ); |
---|
542 | } |
---|
543 | |
---|
544 | return array_unique( $keywords ); |
---|
545 | } |
---|
546 | |
---|
547 | /** |
---|
548 | * returns an array with the ids of the restricted categories for the user |
---|
549 | * |
---|
550 | * Returns an array with the ids of the restricted categories for the |
---|
551 | * user. If the $check_invisible parameter is set to true, invisible |
---|
552 | * categorie are added to the restricted one in the array. |
---|
553 | * |
---|
554 | * @param int $user_id |
---|
555 | * @param string $user_status |
---|
556 | * @param bool $check_invisible |
---|
557 | * @param bool $use_groups |
---|
558 | * @return array |
---|
559 | */ |
---|
560 | function get_user_restrictions( $user_id, $user_status, |
---|
561 | $check_invisible, $use_groups = true ) |
---|
562 | { |
---|
563 | // 1. retrieving ids of private categories |
---|
564 | $query = 'SELECT id FROM '.CATEGORIES_TABLE; |
---|
565 | $query.= " WHERE status = 'private'"; |
---|
566 | $query.= ';'; |
---|
567 | $result = pwg_query( $query ); |
---|
568 | $privates = array(); |
---|
569 | while ( $row = mysql_fetch_array( $result ) ) |
---|
570 | { |
---|
571 | array_push( $privates, $row['id'] ); |
---|
572 | } |
---|
573 | // 2. retrieving all authorized categories for the user |
---|
574 | $authorized = array(); |
---|
575 | // 2.1. retrieving authorized categories thanks to personnal user |
---|
576 | // authorization |
---|
577 | $query = 'SELECT cat_id FROM '.USER_ACCESS_TABLE; |
---|
578 | $query.= ' WHERE user_id = '.$user_id; |
---|
579 | $query.= ';'; |
---|
580 | $result = pwg_query( $query ); |
---|
581 | while ( $row = mysql_fetch_array( $result ) ) |
---|
582 | { |
---|
583 | array_push( $authorized, $row['cat_id'] ); |
---|
584 | } |
---|
585 | // 2.2. retrieving authorized categories thanks to group authorization to |
---|
586 | // which the user is a member |
---|
587 | if ( $use_groups ) |
---|
588 | { |
---|
589 | $query = 'SELECT ga.cat_id'; |
---|
590 | $query.= ' FROM '.USER_GROUP_TABLE.' as ug'; |
---|
591 | $query.= ', '.GROUP_ACCESS_TABLE.' as ga'; |
---|
592 | $query.= ' WHERE ug.group_id = ga.group_id'; |
---|
593 | $query.= ' AND ug.user_id = '.$user_id; |
---|
594 | $query.= ';'; |
---|
595 | $result = pwg_query( $query ); |
---|
596 | while ( $row = mysql_fetch_array( $result ) ) |
---|
597 | { |
---|
598 | array_push( $authorized, $row['cat_id'] ); |
---|
599 | } |
---|
600 | $authorized = array_unique( $authorized ); |
---|
601 | } |
---|
602 | |
---|
603 | $forbidden = array(); |
---|
604 | foreach ( $privates as $private ) { |
---|
605 | if ( !in_array( $private, $authorized ) ) |
---|
606 | { |
---|
607 | array_push( $forbidden, $private ); |
---|
608 | } |
---|
609 | } |
---|
610 | |
---|
611 | if ( $check_invisible ) |
---|
612 | { |
---|
613 | // 3. adding to the restricted categories, the invisible ones |
---|
614 | if ( $user_status != 'admin' ) |
---|
615 | { |
---|
616 | $query = 'SELECT id FROM '.CATEGORIES_TABLE; |
---|
617 | $query.= " WHERE visible = 'false';"; |
---|
618 | $result = pwg_query( $query ); |
---|
619 | while ( $row = mysql_fetch_array( $result ) ) |
---|
620 | { |
---|
621 | array_push( $forbidden, $row['id'] ); |
---|
622 | } |
---|
623 | } |
---|
624 | } |
---|
625 | return array_unique( $forbidden ); |
---|
626 | } |
---|
627 | |
---|
628 | /** |
---|
629 | * updates the calculated data users.forbidden_categories, it includes |
---|
630 | * sub-categories of the direct forbidden categories |
---|
631 | * |
---|
632 | * @param nt $user_id |
---|
633 | * @return array |
---|
634 | */ |
---|
635 | function update_user_restrictions( $user_id ) |
---|
636 | { |
---|
637 | $restrictions = get_user_all_restrictions( $user_id ); |
---|
638 | |
---|
639 | // update the users.forbidden_categories in database |
---|
640 | $query = 'UPDATE '.USERS_TABLE; |
---|
641 | $query.= ' SET forbidden_categories = '; |
---|
642 | if ( count( $restrictions ) > 0 ) |
---|
643 | $query.= "'".implode( ',', $restrictions )."'"; |
---|
644 | else |
---|
645 | $query.= 'NULL'; |
---|
646 | $query .= ' WHERE id = '.$user_id; |
---|
647 | $query.= ';'; |
---|
648 | pwg_query( $query ); |
---|
649 | |
---|
650 | return $restrictions; |
---|
651 | } |
---|
652 | |
---|
653 | /** |
---|
654 | * returns all the restricted categories ids including sub-categories |
---|
655 | * |
---|
656 | * @param int $user_id |
---|
657 | * @return array |
---|
658 | */ |
---|
659 | function get_user_all_restrictions( $user_id ) |
---|
660 | { |
---|
661 | global $page; |
---|
662 | |
---|
663 | $query = 'SELECT status'; |
---|
664 | $query.= ' FROM '.USERS_TABLE; |
---|
665 | $query.= ' WHERE id = '.$user_id; |
---|
666 | $query.= ';'; |
---|
667 | $row = mysql_fetch_array( pwg_query( $query ) ); |
---|
668 | |
---|
669 | $base_restrictions=get_user_restrictions($user_id,$row['status'],true,true); |
---|
670 | |
---|
671 | $restrictions = $base_restrictions; |
---|
672 | foreach ( $base_restrictions as $category_id ) { |
---|
673 | echo $category_id.' is forbidden to user '.$user_id.'<br />'; |
---|
674 | $restrictions = |
---|
675 | array_merge( $restrictions, |
---|
676 | $page['plain_structure'][$category_id]['all_subcats_ids'] ); |
---|
677 | } |
---|
678 | |
---|
679 | return array_unique( $restrictions ); |
---|
680 | } |
---|
681 | |
---|
682 | // The function is_user_allowed returns : |
---|
683 | // - 0 : if the category is allowed with this $restrictions array |
---|
684 | // - 1 : if this category is not allowed |
---|
685 | // - 2 : if an uppercat category is not allowed |
---|
686 | // Note : the restrictions array must represent ONLY direct forbidden |
---|
687 | // categories, not all forbidden categories |
---|
688 | function is_user_allowed( $category_id, $restrictions ) |
---|
689 | { |
---|
690 | if ( in_array( $category_id, $restrictions ) ) return 1; |
---|
691 | |
---|
692 | $query = 'SELECT uppercats'; |
---|
693 | $query.= ' FROM '.CATEGORIES_TABLE; |
---|
694 | $query.= ' WHERE id = '.$category_id; |
---|
695 | $query.= ';'; |
---|
696 | $row = mysql_fetch_array( pwg_query( $query ) ); |
---|
697 | $uppercats = explode( ',', $row['uppercats'] ); |
---|
698 | foreach ( $uppercats as $category_id ) { |
---|
699 | if ( in_array( $category_id, $restrictions ) ) return 2; |
---|
700 | } |
---|
701 | |
---|
702 | // no restriction found : the user is allowed to access this category |
---|
703 | return 0; |
---|
704 | } |
---|
705 | |
---|
706 | /** |
---|
707 | * returns an array containing sub-directories which can be a category |
---|
708 | * |
---|
709 | * directories nammed "thumbnail", "pwg_high" or "pwg_representative" are |
---|
710 | * omitted |
---|
711 | * |
---|
712 | * @param string $basedir |
---|
713 | * @return array |
---|
714 | */ |
---|
715 | function get_category_directories( $basedir ) |
---|
716 | { |
---|
717 | $sub_dirs = array(); |
---|
718 | |
---|
719 | if ( $opendir = opendir( $basedir ) ) |
---|
720 | { |
---|
721 | while ( $file = readdir( $opendir ) ) |
---|
722 | { |
---|
723 | if ($file != '.' |
---|
724 | and $file != '..' |
---|
725 | and $file != 'thumbnail' |
---|
726 | and $file != 'pwg_high' |
---|
727 | and $file != 'pwg_representative' |
---|
728 | and is_dir($basedir.'/'.$file)) |
---|
729 | { |
---|
730 | array_push( $sub_dirs, $file ); |
---|
731 | } |
---|
732 | } |
---|
733 | } |
---|
734 | return $sub_dirs; |
---|
735 | } |
---|
736 | |
---|
737 | /** |
---|
738 | * returns an array containing sub-directories which can be a category, |
---|
739 | * recursive by default |
---|
740 | * |
---|
741 | * directories nammed "thumbnail", "pwg_high" or "pwg_representative" are |
---|
742 | * omitted |
---|
743 | * |
---|
744 | * @param string $basedir |
---|
745 | * @return array |
---|
746 | */ |
---|
747 | function get_fs_directories($path, $recursive = true) |
---|
748 | { |
---|
749 | $dirs = array(); |
---|
750 | |
---|
751 | if (is_dir($path)) |
---|
752 | { |
---|
753 | if ($contents = opendir($path)) |
---|
754 | { |
---|
755 | while (($node = readdir($contents)) !== false) |
---|
756 | { |
---|
757 | if (is_dir($path.'/'.$node) |
---|
758 | and $node != '.' |
---|
759 | and $node != '..' |
---|
760 | and $node != 'thumbnail' |
---|
761 | and $node != 'pwg_high' |
---|
762 | and $node != 'pwg_representative') |
---|
763 | { |
---|
764 | array_push($dirs, $path.'/'.$node); |
---|
765 | if ($recursive) |
---|
766 | { |
---|
767 | $dirs = array_merge($dirs, get_fs_directories($path.'/'.$node)); |
---|
768 | } |
---|
769 | } |
---|
770 | } |
---|
771 | } |
---|
772 | } |
---|
773 | |
---|
774 | return $dirs; |
---|
775 | } |
---|
776 | |
---|
777 | /** |
---|
778 | * inserts multiple lines in a table |
---|
779 | * |
---|
780 | * @param string table_name |
---|
781 | * @param array dbfields |
---|
782 | * @param array inserts |
---|
783 | * @return void |
---|
784 | */ |
---|
785 | function mass_inserts($table_name, $dbfields, $datas) |
---|
786 | { |
---|
787 | // inserts all found categories |
---|
788 | $query = ' |
---|
789 | INSERT INTO '.$table_name.' |
---|
790 | ('.implode(',', $dbfields).') |
---|
791 | VALUES'; |
---|
792 | foreach ($datas as $insert_id => $insert) |
---|
793 | { |
---|
794 | $query.= ' |
---|
795 | '; |
---|
796 | if ($insert_id > 0) |
---|
797 | { |
---|
798 | $query.= ','; |
---|
799 | } |
---|
800 | $query.= '('; |
---|
801 | foreach ($dbfields as $field_id => $dbfield) |
---|
802 | { |
---|
803 | if ($field_id > 0) |
---|
804 | { |
---|
805 | $query.= ','; |
---|
806 | } |
---|
807 | |
---|
808 | if (!isset($insert[$dbfield]) or $insert[$dbfield] == '') |
---|
809 | { |
---|
810 | $query.= 'NULL'; |
---|
811 | } |
---|
812 | else |
---|
813 | { |
---|
814 | $query.= "'".$insert[$dbfield]."'"; |
---|
815 | } |
---|
816 | } |
---|
817 | $query.=')'; |
---|
818 | } |
---|
819 | $query.= ' |
---|
820 | ;'; |
---|
821 | pwg_query($query); |
---|
822 | } |
---|
823 | |
---|
824 | /** |
---|
825 | * updates multiple lines in a table |
---|
826 | * |
---|
827 | * @param string table_name |
---|
828 | * @param array dbfields |
---|
829 | * @param array datas |
---|
830 | * @return void |
---|
831 | */ |
---|
832 | function mass_updates($tablename, $dbfields, $datas) |
---|
833 | { |
---|
834 | // depending on the MySQL version, we use the multi table update or N |
---|
835 | // update queries |
---|
836 | $query = 'SELECT VERSION() AS version;'; |
---|
837 | $row = mysql_fetch_array(pwg_query($query)); |
---|
838 | if (count($datas) < 10 or version_compare($row['version'],'4.0.4') < 0) |
---|
839 | { |
---|
840 | // MySQL is prior to version 4.0.4, multi table update feature is not |
---|
841 | // available |
---|
842 | foreach ($datas as $data) |
---|
843 | { |
---|
844 | $query = ' |
---|
845 | UPDATE '.$tablename.' |
---|
846 | SET '; |
---|
847 | foreach ($dbfields['update'] as $num => $key) |
---|
848 | { |
---|
849 | if ($num >= 1) |
---|
850 | { |
---|
851 | $query.= ",\n "; |
---|
852 | } |
---|
853 | $query.= $key.' = '; |
---|
854 | if (isset($data[$key])) |
---|
855 | { |
---|
856 | $query.= '\''.$data[$key].'\''; |
---|
857 | } |
---|
858 | else |
---|
859 | { |
---|
860 | $query.= 'NULL'; |
---|
861 | } |
---|
862 | } |
---|
863 | $query.= ' |
---|
864 | WHERE '; |
---|
865 | foreach ($dbfields['primary'] as $num => $key) |
---|
866 | { |
---|
867 | if ($num > 1) |
---|
868 | { |
---|
869 | $query.= ' AND '; |
---|
870 | } |
---|
871 | $query.= $key.' = \''.$data[$key].'\''; |
---|
872 | } |
---|
873 | $query.= ' |
---|
874 | ;'; |
---|
875 | pwg_query($query); |
---|
876 | } |
---|
877 | } |
---|
878 | else |
---|
879 | { |
---|
880 | // creation of the temporary table |
---|
881 | $query = ' |
---|
882 | DESCRIBE '.$tablename.' |
---|
883 | ;'; |
---|
884 | $result = pwg_query($query); |
---|
885 | $columns = array(); |
---|
886 | $all_fields = array_merge($dbfields['primary'], $dbfields['update']); |
---|
887 | while ($row = mysql_fetch_array($result)) |
---|
888 | { |
---|
889 | if (in_array($row['Field'], $all_fields)) |
---|
890 | { |
---|
891 | $column = $row['Field']; |
---|
892 | $column.= ' '.$row['Type']; |
---|
893 | if (!isset($row['Null']) or $row['Null'] == '') |
---|
894 | { |
---|
895 | $column.= ' NOT NULL'; |
---|
896 | } |
---|
897 | if (isset($row['Default'])) |
---|
898 | { |
---|
899 | $column.= " default '".$row['Default']."'"; |
---|
900 | } |
---|
901 | array_push($columns, $column); |
---|
902 | } |
---|
903 | } |
---|
904 | |
---|
905 | $temporary_tablename = $tablename.'_'.micro_seconds(); |
---|
906 | |
---|
907 | $query = ' |
---|
908 | CREATE TABLE '.$temporary_tablename.' |
---|
909 | ( |
---|
910 | '.implode(",\n", $columns).', |
---|
911 | PRIMARY KEY (id) |
---|
912 | ) |
---|
913 | ;'; |
---|
914 | pwg_query($query); |
---|
915 | mass_inserts($temporary_tablename, $all_fields, $datas); |
---|
916 | // update of images table by joining with temporary table |
---|
917 | $query = ' |
---|
918 | UPDATE '.$tablename.' AS t1, '.$temporary_tablename.' AS t2 |
---|
919 | SET '.implode("\n , ", |
---|
920 | array_map( |
---|
921 | create_function('$s', 'return "t1.$s = t2.$s";') |
---|
922 | , $dbfields['update'])).' |
---|
923 | WHERE '.implode("\n AND ", |
---|
924 | array_map( |
---|
925 | create_function('$s', 'return "t1.$s = t2.$s";') |
---|
926 | , $dbfields['primary'])).' |
---|
927 | ;'; |
---|
928 | pwg_query($query); |
---|
929 | $query = ' |
---|
930 | DROP TABLE '.$temporary_tablename.' |
---|
931 | ;'; |
---|
932 | pwg_query($query); |
---|
933 | } |
---|
934 | } |
---|
935 | |
---|
936 | /** |
---|
937 | * updates the global_rank of categories under the given id_uppercat |
---|
938 | * |
---|
939 | * @param int id_uppercat |
---|
940 | * @return void |
---|
941 | */ |
---|
942 | function update_global_rank($id_uppercat = 'all') |
---|
943 | { |
---|
944 | $query = ' |
---|
945 | SELECT id,rank |
---|
946 | FROM '.CATEGORIES_TABLE.' |
---|
947 | ;'; |
---|
948 | $result = pwg_query($query); |
---|
949 | $ranks_array = array(); |
---|
950 | while ($row = mysql_fetch_array($result)) |
---|
951 | { |
---|
952 | $ranks_array[$row['id']] = $row['rank']; |
---|
953 | } |
---|
954 | |
---|
955 | // which categories to update ? |
---|
956 | $uppercats_array = array(); |
---|
957 | |
---|
958 | $query = ' |
---|
959 | SELECT id,uppercats |
---|
960 | FROM '.CATEGORIES_TABLE; |
---|
961 | if (is_numeric($id_uppercat)) |
---|
962 | { |
---|
963 | $query.= ' |
---|
964 | WHERE uppercats REGEXP \'(^|,)'.$id_uppercat.'(,|$)\' |
---|
965 | AND id != '.$id_uppercat.' |
---|
966 | '; |
---|
967 | } |
---|
968 | $query.= ' |
---|
969 | ;'; |
---|
970 | $result = pwg_query($query); |
---|
971 | while ($row = mysql_fetch_array($result)) |
---|
972 | { |
---|
973 | $uppercats_array[$row['id']] = $row['uppercats']; |
---|
974 | } |
---|
975 | |
---|
976 | $datas = array(); |
---|
977 | foreach ($uppercats_array as $id => $uppercats) |
---|
978 | { |
---|
979 | $data = array(); |
---|
980 | $data['id'] = $id; |
---|
981 | $global_rank = preg_replace('/(\d+)/e', |
---|
982 | "\$ranks_array['$1']", |
---|
983 | str_replace(',', '.', $uppercats)); |
---|
984 | $data['global_rank'] = $global_rank; |
---|
985 | array_push($datas, $data); |
---|
986 | } |
---|
987 | |
---|
988 | $fields = array('primary' => array('id'), 'update' => array('global_rank')); |
---|
989 | mass_updates(CATEGORIES_TABLE, $fields, $datas); |
---|
990 | } |
---|
991 | |
---|
992 | /** |
---|
993 | * change the visible property on a set of categories |
---|
994 | * |
---|
995 | * @param array categories |
---|
996 | * @param string value |
---|
997 | * @return void |
---|
998 | */ |
---|
999 | function set_cat_visible($categories, $value) |
---|
1000 | { |
---|
1001 | if (!in_array($value, array('true', 'false'))) |
---|
1002 | { |
---|
1003 | return false; |
---|
1004 | } |
---|
1005 | |
---|
1006 | // unlocking a category => all its parent categories become unlocked |
---|
1007 | if ($value == 'true') |
---|
1008 | { |
---|
1009 | $uppercats = get_uppercat_ids($categories); |
---|
1010 | $query = ' |
---|
1011 | UPDATE '.CATEGORIES_TABLE.' |
---|
1012 | SET visible = \'true\' |
---|
1013 | WHERE id IN ('.implode(',', $uppercats).') |
---|
1014 | ;'; |
---|
1015 | pwg_query($query); |
---|
1016 | } |
---|
1017 | // locking a category => all its child categories become locked |
---|
1018 | if ($value == 'false') |
---|
1019 | { |
---|
1020 | $subcats = get_subcat_ids($categories); |
---|
1021 | $query = ' |
---|
1022 | UPDATE '.CATEGORIES_TABLE.' |
---|
1023 | SET visible = \'false\' |
---|
1024 | WHERE id IN ('.implode(',', $subcats).') |
---|
1025 | ;'; |
---|
1026 | pwg_query($query); |
---|
1027 | } |
---|
1028 | } |
---|
1029 | |
---|
1030 | /** |
---|
1031 | * change the status property on a set of categories : private or public |
---|
1032 | * |
---|
1033 | * @param array categories |
---|
1034 | * @param string value |
---|
1035 | * @return void |
---|
1036 | */ |
---|
1037 | function set_cat_status($categories, $value) |
---|
1038 | { |
---|
1039 | if (!in_array($value, array('public', 'private'))) |
---|
1040 | { |
---|
1041 | return false; |
---|
1042 | } |
---|
1043 | |
---|
1044 | // make public a category => all its parent categories become public |
---|
1045 | if ($value == 'public') |
---|
1046 | { |
---|
1047 | $uppercats = get_uppercat_ids($categories); |
---|
1048 | $query = ' |
---|
1049 | UPDATE '.CATEGORIES_TABLE.' |
---|
1050 | SET status = \'public\' |
---|
1051 | WHERE id IN ('.implode(',', $uppercats).') |
---|
1052 | ;'; |
---|
1053 | pwg_query($query); |
---|
1054 | } |
---|
1055 | // make a category private => all its child categories become private |
---|
1056 | if ($value == 'private') |
---|
1057 | { |
---|
1058 | $subcats = get_subcat_ids($categories); |
---|
1059 | $query = ' |
---|
1060 | UPDATE '.CATEGORIES_TABLE.' |
---|
1061 | SET status = \'private\' |
---|
1062 | WHERE id IN ('.implode(',', $subcats).') |
---|
1063 | ;'; |
---|
1064 | pwg_query($query); |
---|
1065 | } |
---|
1066 | } |
---|
1067 | |
---|
1068 | /** |
---|
1069 | * returns all uppercats category ids of the given category ids |
---|
1070 | * |
---|
1071 | * @param array cat_ids |
---|
1072 | * @return array |
---|
1073 | */ |
---|
1074 | function get_uppercat_ids($cat_ids) |
---|
1075 | { |
---|
1076 | if (!is_array($cat_ids) or count($cat_ids) < 1) |
---|
1077 | { |
---|
1078 | return array(); |
---|
1079 | } |
---|
1080 | |
---|
1081 | $uppercats = array(); |
---|
1082 | |
---|
1083 | $query = ' |
---|
1084 | SELECT uppercats |
---|
1085 | FROM '.CATEGORIES_TABLE.' |
---|
1086 | WHERE id IN ('.implode(',', $cat_ids).') |
---|
1087 | ;'; |
---|
1088 | $result = pwg_query($query); |
---|
1089 | while ($row = mysql_fetch_array($result)) |
---|
1090 | { |
---|
1091 | $uppercats = array_merge($uppercats, |
---|
1092 | explode(',', $row['uppercats'])); |
---|
1093 | } |
---|
1094 | $uppercats = array_unique($uppercats); |
---|
1095 | |
---|
1096 | return $uppercats; |
---|
1097 | } |
---|
1098 | |
---|
1099 | /** |
---|
1100 | * set a new random representant to the categories |
---|
1101 | * |
---|
1102 | * @param array categories |
---|
1103 | */ |
---|
1104 | function set_random_representant($categories) |
---|
1105 | { |
---|
1106 | $datas = array(); |
---|
1107 | foreach ($categories as $category_id) |
---|
1108 | { |
---|
1109 | $query = ' |
---|
1110 | SELECT image_id |
---|
1111 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1112 | WHERE category_id = '.$category_id.' |
---|
1113 | ORDER BY RAND() |
---|
1114 | LIMIT 0,1 |
---|
1115 | ;'; |
---|
1116 | list($representative) = mysql_fetch_array(pwg_query($query)); |
---|
1117 | $data = array('id' => $category_id, |
---|
1118 | 'representative_picture_id' => $representative); |
---|
1119 | array_push($datas, $data); |
---|
1120 | } |
---|
1121 | |
---|
1122 | $fields = array('primary' => array('id'), |
---|
1123 | 'update' => array('representative_picture_id')); |
---|
1124 | mass_updates(CATEGORIES_TABLE, $fields, $datas); |
---|
1125 | } |
---|
1126 | |
---|
1127 | /** |
---|
1128 | * order categories (update categories.rank and global_rank database fields) |
---|
1129 | * |
---|
1130 | * the purpose of this function is to give a rank for all categories |
---|
1131 | * (insides its sub-category), even the newer that have none at te |
---|
1132 | * beginning. For this, ordering function selects all categories ordered by |
---|
1133 | * rank ASC then name ASC for each uppercat. |
---|
1134 | * |
---|
1135 | * @returns void |
---|
1136 | */ |
---|
1137 | function ordering() |
---|
1138 | { |
---|
1139 | $current_rank = 0; |
---|
1140 | $current_uppercat = ''; |
---|
1141 | |
---|
1142 | $query = ' |
---|
1143 | SELECT id, if(id_uppercat is null,\'\',id_uppercat) AS id_uppercat |
---|
1144 | FROM '.CATEGORIES_TABLE.' |
---|
1145 | ORDER BY id_uppercat,rank,name |
---|
1146 | ;'; |
---|
1147 | $result = pwg_query($query); |
---|
1148 | $datas = array(); |
---|
1149 | while ($row = mysql_fetch_array($result)) |
---|
1150 | { |
---|
1151 | if ($row['id_uppercat'] != $current_uppercat) |
---|
1152 | { |
---|
1153 | $current_rank = 0; |
---|
1154 | $current_uppercat = $row['id_uppercat']; |
---|
1155 | } |
---|
1156 | $data = array('id' => $row['id'], 'rank' => ++$current_rank); |
---|
1157 | array_push($datas, $data); |
---|
1158 | } |
---|
1159 | |
---|
1160 | $fields = array('primary' => array('id'), 'update' => array('rank')); |
---|
1161 | mass_updates(CATEGORIES_TABLE, $fields, $datas); |
---|
1162 | } |
---|
1163 | |
---|
1164 | /** |
---|
1165 | * returns the fulldir for each given category id |
---|
1166 | * |
---|
1167 | * @param array cat_ids |
---|
1168 | * @return array |
---|
1169 | */ |
---|
1170 | function get_fulldirs($cat_ids) |
---|
1171 | { |
---|
1172 | if (count($cat_ids) == 0) |
---|
1173 | { |
---|
1174 | return array(); |
---|
1175 | } |
---|
1176 | |
---|
1177 | // caching directories of existing categories |
---|
1178 | $query = ' |
---|
1179 | SELECT id, dir |
---|
1180 | FROM '.CATEGORIES_TABLE.' |
---|
1181 | WHERE dir IS NOT NULL |
---|
1182 | ;'; |
---|
1183 | $result = pwg_query($query); |
---|
1184 | $cat_dirs = array(); |
---|
1185 | while ($row = mysql_fetch_array($result)) |
---|
1186 | { |
---|
1187 | $cat_dirs[$row['id']] = $row['dir']; |
---|
1188 | } |
---|
1189 | |
---|
1190 | // caching galleries_url |
---|
1191 | $query = ' |
---|
1192 | SELECT id, galleries_url |
---|
1193 | FROM '.SITES_TABLE.' |
---|
1194 | ;'; |
---|
1195 | $result = pwg_query($query); |
---|
1196 | $galleries_url = array(); |
---|
1197 | while ($row = mysql_fetch_array($result)) |
---|
1198 | { |
---|
1199 | $galleries_url[$row['id']] = $row['galleries_url']; |
---|
1200 | } |
---|
1201 | |
---|
1202 | // categories : id, site_id, uppercats |
---|
1203 | $categories = array(); |
---|
1204 | |
---|
1205 | $query = ' |
---|
1206 | SELECT id, uppercats, site_id |
---|
1207 | FROM '.CATEGORIES_TABLE.' |
---|
1208 | WHERE id IN ( |
---|
1209 | '.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
1210 | ;'; |
---|
1211 | $result = pwg_query($query); |
---|
1212 | while ($row = mysql_fetch_array($result)) |
---|
1213 | { |
---|
1214 | array_push($categories, $row); |
---|
1215 | } |
---|
1216 | |
---|
1217 | // filling $cat_fulldirs |
---|
1218 | $cat_fulldirs = array(); |
---|
1219 | foreach ($categories as $category) |
---|
1220 | { |
---|
1221 | $uppercats = str_replace(',', '/', $category['uppercats']); |
---|
1222 | $cat_fulldirs[$category['id']] = $galleries_url[$category['site_id']]; |
---|
1223 | $cat_fulldirs[$category['id']].= preg_replace('/(\d+)/e', |
---|
1224 | "\$cat_dirs['$1']", |
---|
1225 | $uppercats); |
---|
1226 | } |
---|
1227 | |
---|
1228 | return $cat_fulldirs; |
---|
1229 | } |
---|
1230 | |
---|
1231 | /** |
---|
1232 | * returns an array with all file system files according to |
---|
1233 | * $conf['file_ext'] |
---|
1234 | * |
---|
1235 | * @param string $path |
---|
1236 | * @param bool recursive |
---|
1237 | * @return array |
---|
1238 | */ |
---|
1239 | function get_fs($path, $recursive = true) |
---|
1240 | { |
---|
1241 | global $conf; |
---|
1242 | |
---|
1243 | // because isset is faster than in_array... |
---|
1244 | if (!isset($conf['flip_picture_ext'])) |
---|
1245 | { |
---|
1246 | $conf['flip_picture_ext'] = array_flip($conf['picture_ext']); |
---|
1247 | } |
---|
1248 | if (!isset($conf['flip_file_ext'])) |
---|
1249 | { |
---|
1250 | $conf['flip_file_ext'] = array_flip($conf['file_ext']); |
---|
1251 | } |
---|
1252 | |
---|
1253 | $fs['elements'] = array(); |
---|
1254 | $fs['thumbnails'] = array(); |
---|
1255 | $fs['representatives'] = array(); |
---|
1256 | $subdirs = array(); |
---|
1257 | |
---|
1258 | if (is_dir($path)) |
---|
1259 | { |
---|
1260 | if ($contents = opendir($path)) |
---|
1261 | { |
---|
1262 | while (($node = readdir($contents)) !== false) |
---|
1263 | { |
---|
1264 | if (is_file($path.'/'.$node)) |
---|
1265 | { |
---|
1266 | $extension = get_extension($node); |
---|
1267 | |
---|
1268 | // if (in_array($extension, $conf['picture_ext'])) |
---|
1269 | if (isset($conf['flip_picture_ext'][$extension])) |
---|
1270 | { |
---|
1271 | if (basename($path) == 'thumbnail') |
---|
1272 | { |
---|
1273 | array_push($fs['thumbnails'], $path.'/'.$node); |
---|
1274 | } |
---|
1275 | else if (basename($path) == 'pwg_representative') |
---|
1276 | { |
---|
1277 | array_push($fs['representatives'], $path.'/'.$node); |
---|
1278 | } |
---|
1279 | else |
---|
1280 | { |
---|
1281 | array_push($fs['elements'], $path.'/'.$node); |
---|
1282 | } |
---|
1283 | } |
---|
1284 | // else if (in_array($extension, $conf['file_ext'])) |
---|
1285 | else if (isset($conf['flip_file_ext'][$extension])) |
---|
1286 | { |
---|
1287 | array_push($fs['elements'], $path.'/'.$node); |
---|
1288 | } |
---|
1289 | } |
---|
1290 | else if (is_dir($path.'/'.$node) |
---|
1291 | and $node != '.' |
---|
1292 | and $node != '..' |
---|
1293 | and $node != 'pwg_high' |
---|
1294 | and $recursive) |
---|
1295 | { |
---|
1296 | array_push($subdirs, $node); |
---|
1297 | } |
---|
1298 | } |
---|
1299 | } |
---|
1300 | closedir($contents); |
---|
1301 | |
---|
1302 | foreach ($subdirs as $subdir) |
---|
1303 | { |
---|
1304 | $tmp_fs = get_fs($path.'/'.$subdir); |
---|
1305 | |
---|
1306 | $fs['elements'] = array_merge($fs['elements'], |
---|
1307 | $tmp_fs['elements']); |
---|
1308 | |
---|
1309 | $fs['thumbnails'] = array_merge($fs['thumbnails'], |
---|
1310 | $tmp_fs['thumbnails']); |
---|
1311 | |
---|
1312 | $fs['representatives'] = array_merge($fs['representatives'], |
---|
1313 | $tmp_fs['representatives']); |
---|
1314 | } |
---|
1315 | } |
---|
1316 | return $fs; |
---|
1317 | } |
---|
1318 | |
---|
1319 | /** |
---|
1320 | * stupidly returns the current microsecond since Unix epoch |
---|
1321 | */ |
---|
1322 | function micro_seconds() |
---|
1323 | { |
---|
1324 | $t1 = explode(' ', microtime()); |
---|
1325 | $t2 = explode('.', $t1[0]); |
---|
1326 | $t2 = $t1[1].substr($t2[1], 0, 6); |
---|
1327 | return $t2; |
---|
1328 | } |
---|
1329 | ?> |
---|