1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2013 Piwigo Team http://piwigo.org | |
---|
6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
8 | // +-----------------------------------------------------------------------+ |
---|
9 | // | This program is free software; you can redistribute it and/or modify | |
---|
10 | // | it under the terms of the GNU General Public License as published by | |
---|
11 | // | the Free Software Foundation | |
---|
12 | // | | |
---|
13 | // | This program is distributed in the hope that it will be useful, but | |
---|
14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
16 | // | General Public License for more details. | |
---|
17 | // | | |
---|
18 | // | You should have received a copy of the GNU General Public License | |
---|
19 | // | along with this program; if not, write to the Free Software | |
---|
20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
21 | // | USA. | |
---|
22 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | include(PHPWG_ROOT_PATH.'admin/include/functions_metadata.php'); |
---|
25 | |
---|
26 | // The function delete_site deletes a site and call the function |
---|
27 | // delete_categories for each primary category of the site |
---|
28 | function delete_site( $id ) |
---|
29 | { |
---|
30 | // destruction of the categories of the site |
---|
31 | $query = ' |
---|
32 | SELECT id |
---|
33 | FROM '.CATEGORIES_TABLE.' |
---|
34 | WHERE site_id = '.$id.' |
---|
35 | ;'; |
---|
36 | $category_ids = array_from_query($query, 'id'); |
---|
37 | delete_categories($category_ids); |
---|
38 | |
---|
39 | // destruction of the site |
---|
40 | $query = ' |
---|
41 | DELETE FROM '.SITES_TABLE.' |
---|
42 | WHERE id = '.$id.' |
---|
43 | ;'; |
---|
44 | pwg_query($query); |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | // The function delete_categories deletes the categories identified by the |
---|
49 | // (numeric) key of the array $ids. It also deletes (in the database) : |
---|
50 | // - all the elements physically linked to the category (delete_elements, see further) |
---|
51 | // - all the links between elements and this category |
---|
52 | // - all the restrictions linked to the category |
---|
53 | // The function works recursively. |
---|
54 | // |
---|
55 | // the $photo_deletion_mode is for photos virtually linked to the categorty |
---|
56 | // * no_delete : delete no photo, may create orphans |
---|
57 | // * delete_orphans : delete photos that are no longer linked to any category |
---|
58 | // * force_delete : delete photos even if they are linked to another category |
---|
59 | function delete_categories($ids, $photo_deletion_mode='no_delete') |
---|
60 | { |
---|
61 | if (count($ids) == 0) |
---|
62 | { |
---|
63 | return; |
---|
64 | } |
---|
65 | |
---|
66 | // add sub-category ids to the given ids : if a category is deleted, all |
---|
67 | // sub-categories must be so |
---|
68 | $ids = get_subcat_ids($ids); |
---|
69 | |
---|
70 | // destruction of all photos physically linked to the category |
---|
71 | $query = ' |
---|
72 | SELECT id |
---|
73 | FROM '.IMAGES_TABLE.' |
---|
74 | WHERE storage_category_id IN ( |
---|
75 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
76 | ;'; |
---|
77 | $element_ids = array_from_query($query, 'id'); |
---|
78 | delete_elements($element_ids); |
---|
79 | |
---|
80 | // now, should we delete photos that are virtually linked to the category? |
---|
81 | if ('delete_orphans' == $photo_deletion_mode or 'force_delete' == $photo_deletion_mode) |
---|
82 | { |
---|
83 | $query = ' |
---|
84 | SELECT |
---|
85 | DISTINCT(image_id) |
---|
86 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
87 | WHERE category_id IN ('.implode(',', $ids).') |
---|
88 | ;'; |
---|
89 | $image_ids_linked = array_from_query($query, 'image_id'); |
---|
90 | |
---|
91 | if (count($image_ids_linked) > 0) |
---|
92 | { |
---|
93 | if ('delete_orphans' == $photo_deletion_mode) |
---|
94 | { |
---|
95 | $query = ' |
---|
96 | SELECT |
---|
97 | DISTINCT(image_id) |
---|
98 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
99 | WHERE image_id IN ('.implode(',', $image_ids_linked).') |
---|
100 | AND category_id NOT IN ('.implode(',', $ids).') |
---|
101 | ;'; |
---|
102 | $image_ids_not_orphans = array_from_query($query, 'image_id'); |
---|
103 | $image_ids_to_delete = array_diff($image_ids_linked, $image_ids_not_orphans); |
---|
104 | } |
---|
105 | |
---|
106 | if ('force_delete' == $photo_deletion_mode) |
---|
107 | { |
---|
108 | $image_ids_to_delete = $image_ids_linked; |
---|
109 | } |
---|
110 | |
---|
111 | delete_elements($image_ids_to_delete, true); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | // destruction of the links between images and this category |
---|
116 | $query = ' |
---|
117 | DELETE FROM '.IMAGE_CATEGORY_TABLE.' |
---|
118 | WHERE category_id IN ( |
---|
119 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
120 | ;'; |
---|
121 | pwg_query($query); |
---|
122 | |
---|
123 | // destruction of the access linked to the category |
---|
124 | $query = ' |
---|
125 | DELETE FROM '.USER_ACCESS_TABLE.' |
---|
126 | WHERE cat_id IN ( |
---|
127 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
128 | ;'; |
---|
129 | pwg_query($query); |
---|
130 | |
---|
131 | $query = ' |
---|
132 | DELETE FROM '.GROUP_ACCESS_TABLE.' |
---|
133 | WHERE cat_id IN ( |
---|
134 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
135 | ;'; |
---|
136 | pwg_query($query); |
---|
137 | |
---|
138 | // destruction of the category |
---|
139 | $query = ' |
---|
140 | DELETE FROM '.CATEGORIES_TABLE.' |
---|
141 | WHERE id IN ( |
---|
142 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
143 | ;'; |
---|
144 | pwg_query($query); |
---|
145 | |
---|
146 | $query=' |
---|
147 | DELETE FROM '.OLD_PERMALINKS_TABLE.' |
---|
148 | WHERE cat_id IN ('.implode(',',$ids).')'; |
---|
149 | pwg_query($query); |
---|
150 | |
---|
151 | $query=' |
---|
152 | DELETE FROM '.USER_CACHE_CATEGORIES_TABLE.' |
---|
153 | WHERE cat_id IN ('.implode(',',$ids).')'; |
---|
154 | pwg_query($query); |
---|
155 | |
---|
156 | trigger_action('delete_categories', $ids); |
---|
157 | } |
---|
158 | |
---|
159 | // Deletes all files (on disk) related to given image ids |
---|
160 | // @return image ids where files are deleted successfully |
---|
161 | function delete_element_files($ids) |
---|
162 | { |
---|
163 | global $conf; |
---|
164 | if (count($ids) == 0) |
---|
165 | { |
---|
166 | return 0; |
---|
167 | } |
---|
168 | |
---|
169 | $new_ids = array(); |
---|
170 | |
---|
171 | $query = ' |
---|
172 | SELECT |
---|
173 | id, |
---|
174 | path, |
---|
175 | representative_ext |
---|
176 | FROM '.IMAGES_TABLE.' |
---|
177 | WHERE id IN ('.implode(',', $ids).') |
---|
178 | ;'; |
---|
179 | $result = pwg_query($query); |
---|
180 | while ($row = pwg_db_fetch_assoc($result)) |
---|
181 | { |
---|
182 | if (url_is_remote($row['path'])) |
---|
183 | { |
---|
184 | continue; |
---|
185 | } |
---|
186 | |
---|
187 | $files = array(); |
---|
188 | $files[] = get_element_path($row); |
---|
189 | |
---|
190 | if (!empty($row['representative_ext'])) |
---|
191 | { |
---|
192 | $files[] = original_to_representative( $files[0], $row['representative_ext']); |
---|
193 | } |
---|
194 | |
---|
195 | $ok = true; |
---|
196 | if (!isset($conf['never_delete_originals'])) |
---|
197 | { |
---|
198 | foreach ($files as $path) |
---|
199 | { |
---|
200 | if (is_file($path) and !unlink($path)) |
---|
201 | { |
---|
202 | $ok = false; |
---|
203 | trigger_error('"'.$path.'" cannot be removed', E_USER_WARNING); |
---|
204 | break; |
---|
205 | } |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | if ($ok) |
---|
210 | { |
---|
211 | delete_element_derivatives($row); |
---|
212 | $new_ids[] = $row['id']; |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | break; |
---|
217 | } |
---|
218 | } |
---|
219 | return $new_ids; |
---|
220 | } |
---|
221 | |
---|
222 | // The function delete_elements deletes the elements identified by the |
---|
223 | // (numeric) values of the array $ids. It also deletes (in the database) : |
---|
224 | // - all the comments related to elements |
---|
225 | // - all the links between categories and elements |
---|
226 | // - all the favorites associated to elements |
---|
227 | // @return number of deleted elements |
---|
228 | function delete_elements($ids, $physical_deletion=false) |
---|
229 | { |
---|
230 | if (count($ids) == 0) |
---|
231 | { |
---|
232 | return 0; |
---|
233 | } |
---|
234 | trigger_action('begin_delete_elements', $ids); |
---|
235 | |
---|
236 | if ($physical_deletion) |
---|
237 | { |
---|
238 | $ids = delete_element_files($ids); |
---|
239 | if (count($ids)==0) |
---|
240 | { |
---|
241 | return 0; |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | // destruction of the comments on the image |
---|
246 | $query = ' |
---|
247 | DELETE FROM '.COMMENTS_TABLE.' |
---|
248 | WHERE image_id IN ( |
---|
249 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
250 | ;'; |
---|
251 | pwg_query($query); |
---|
252 | |
---|
253 | // destruction of the links between images and this category |
---|
254 | $query = ' |
---|
255 | DELETE FROM '.IMAGE_CATEGORY_TABLE.' |
---|
256 | WHERE image_id IN ( |
---|
257 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
258 | ;'; |
---|
259 | pwg_query($query); |
---|
260 | |
---|
261 | // destruction of the links between images and tags |
---|
262 | $query = ' |
---|
263 | DELETE FROM '.IMAGE_TAG_TABLE.' |
---|
264 | WHERE image_id IN ( |
---|
265 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
266 | ;'; |
---|
267 | pwg_query($query); |
---|
268 | |
---|
269 | // destruction of the favorites associated with the picture |
---|
270 | $query = ' |
---|
271 | DELETE FROM '.FAVORITES_TABLE.' |
---|
272 | WHERE image_id IN ( |
---|
273 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
274 | ;'; |
---|
275 | pwg_query($query); |
---|
276 | |
---|
277 | // destruction of the rates associated to this element |
---|
278 | $query = ' |
---|
279 | DELETE FROM '.RATE_TABLE.' |
---|
280 | WHERE element_id IN ( |
---|
281 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
282 | ;'; |
---|
283 | pwg_query($query); |
---|
284 | |
---|
285 | // destruction of the rates associated to this element |
---|
286 | $query = ' |
---|
287 | DELETE FROM '.CADDIE_TABLE.' |
---|
288 | WHERE element_id IN ( |
---|
289 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
290 | ;'; |
---|
291 | pwg_query($query); |
---|
292 | |
---|
293 | // destruction of the image |
---|
294 | $query = ' |
---|
295 | DELETE FROM '.IMAGES_TABLE.' |
---|
296 | WHERE id IN ( |
---|
297 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
298 | ;'; |
---|
299 | pwg_query($query); |
---|
300 | |
---|
301 | // are the photo used as category representant? |
---|
302 | $query = ' |
---|
303 | SELECT |
---|
304 | id |
---|
305 | FROM '.CATEGORIES_TABLE.' |
---|
306 | WHERE representative_picture_id IN ( |
---|
307 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
308 | ;'; |
---|
309 | $category_ids = array_from_query($query, 'id'); |
---|
310 | if (count($category_ids) > 0) |
---|
311 | { |
---|
312 | update_category($category_ids); |
---|
313 | } |
---|
314 | |
---|
315 | trigger_action('delete_elements', $ids); |
---|
316 | return count($ids); |
---|
317 | } |
---|
318 | |
---|
319 | // The delete_user function delete a user identified by the $user_id |
---|
320 | // It also deletes : |
---|
321 | // - all the access linked to this user |
---|
322 | // - all the links to any group |
---|
323 | // - all the favorites linked to this user |
---|
324 | // - calculated permissions linked to the user |
---|
325 | // - all datas about notifications for the user |
---|
326 | function delete_user($user_id) |
---|
327 | { |
---|
328 | global $conf; |
---|
329 | $tables = array( |
---|
330 | // destruction of the access linked to the user |
---|
331 | USER_ACCESS_TABLE, |
---|
332 | // destruction of data notification by mail for this user |
---|
333 | USER_MAIL_NOTIFICATION_TABLE, |
---|
334 | // destruction of data RSS notification for this user |
---|
335 | USER_FEED_TABLE, |
---|
336 | // deletion of calculated permissions linked to the user |
---|
337 | USER_CACHE_TABLE, |
---|
338 | // deletion of computed cache data linked to the user |
---|
339 | USER_CACHE_CATEGORIES_TABLE, |
---|
340 | // destruction of the group links for this user |
---|
341 | USER_GROUP_TABLE, |
---|
342 | // destruction of the favorites associated with the user |
---|
343 | FAVORITES_TABLE, |
---|
344 | // destruction of the caddie associated with the user |
---|
345 | CADDIE_TABLE, |
---|
346 | // deletion of piwigo specific informations |
---|
347 | USER_INFOS_TABLE, |
---|
348 | ); |
---|
349 | |
---|
350 | foreach ($tables as $table) |
---|
351 | { |
---|
352 | $query = ' |
---|
353 | DELETE FROM '.$table.' |
---|
354 | WHERE user_id = '.$user_id.' |
---|
355 | ;'; |
---|
356 | pwg_query($query); |
---|
357 | } |
---|
358 | |
---|
359 | // destruction of the user |
---|
360 | $query = ' |
---|
361 | DELETE FROM '.SESSIONS_TABLE.' |
---|
362 | WHERE data LIKE \'pwg_uid|i:'.(int)$user_id.';%\' |
---|
363 | ;'; |
---|
364 | pwg_query($query); |
---|
365 | |
---|
366 | // destruction of the user |
---|
367 | $query = ' |
---|
368 | DELETE FROM '.USERS_TABLE.' |
---|
369 | WHERE '.$conf['user_fields']['id'].' = '.$user_id.' |
---|
370 | ;'; |
---|
371 | pwg_query($query); |
---|
372 | |
---|
373 | trigger_action('delete_user', $user_id); |
---|
374 | } |
---|
375 | |
---|
376 | /** |
---|
377 | * Deletes all tags linked to no photo |
---|
378 | */ |
---|
379 | function delete_orphan_tags() |
---|
380 | { |
---|
381 | $orphan_tags = get_orphan_tags(); |
---|
382 | |
---|
383 | if (count($orphan_tags) > 0) |
---|
384 | { |
---|
385 | $orphan_tag_ids = array(); |
---|
386 | foreach ($orphan_tags as $tag) |
---|
387 | { |
---|
388 | $orphan_tag_ids[] = $tag['id']; |
---|
389 | } |
---|
390 | |
---|
391 | $query = ' |
---|
392 | DELETE |
---|
393 | FROM '.TAGS_TABLE.' |
---|
394 | WHERE id IN ('.implode(',', $orphan_tag_ids).') |
---|
395 | ;'; |
---|
396 | pwg_query($query); |
---|
397 | } |
---|
398 | } |
---|
399 | |
---|
400 | /** |
---|
401 | * Get all tags (id + name) linked to no photo |
---|
402 | */ |
---|
403 | function get_orphan_tags() |
---|
404 | { |
---|
405 | $query = ' |
---|
406 | SELECT |
---|
407 | id, |
---|
408 | name |
---|
409 | FROM '.TAGS_TABLE.' |
---|
410 | LEFT JOIN '.IMAGE_TAG_TABLE.' ON id = tag_id |
---|
411 | WHERE tag_id IS NULL |
---|
412 | ;'; |
---|
413 | return array_from_query($query); |
---|
414 | } |
---|
415 | |
---|
416 | /** |
---|
417 | * Verifies that the representative picture really exists in the db and |
---|
418 | * picks up a random represantive if possible and based on config. |
---|
419 | * |
---|
420 | * @param mixed category id |
---|
421 | * @returns void |
---|
422 | */ |
---|
423 | function update_category($ids = 'all') |
---|
424 | { |
---|
425 | global $conf; |
---|
426 | |
---|
427 | if ($ids=='all') |
---|
428 | { |
---|
429 | $where_cats = '1=1'; |
---|
430 | } |
---|
431 | elseif ( !is_array($ids) ) |
---|
432 | { |
---|
433 | $where_cats = '%s='.$ids; |
---|
434 | } |
---|
435 | else |
---|
436 | { |
---|
437 | if (count($ids) == 0) |
---|
438 | { |
---|
439 | return false; |
---|
440 | } |
---|
441 | $where_cats = '%s IN('.wordwrap(implode(', ', $ids), 120, "\n").')'; |
---|
442 | } |
---|
443 | |
---|
444 | // find all categories where the setted representative is not possible : |
---|
445 | // the picture does not exist |
---|
446 | $query = ' |
---|
447 | SELECT DISTINCT c.id |
---|
448 | FROM '.CATEGORIES_TABLE.' AS c LEFT JOIN '.IMAGES_TABLE.' AS i |
---|
449 | ON c.representative_picture_id = i.id |
---|
450 | WHERE representative_picture_id IS NOT NULL |
---|
451 | AND '.sprintf($where_cats, 'c.id').' |
---|
452 | AND i.id IS NULL |
---|
453 | ;'; |
---|
454 | $wrong_representant = array_from_query($query, 'id'); |
---|
455 | |
---|
456 | if (count($wrong_representant) > 0) |
---|
457 | { |
---|
458 | $query = ' |
---|
459 | UPDATE '.CATEGORIES_TABLE.' |
---|
460 | SET representative_picture_id = NULL |
---|
461 | WHERE id IN ('.wordwrap(implode(', ', $wrong_representant), 120, "\n").') |
---|
462 | ;'; |
---|
463 | pwg_query($query); |
---|
464 | } |
---|
465 | |
---|
466 | if (!$conf['allow_random_representative']) |
---|
467 | { |
---|
468 | // If the random representant is not allowed, we need to find |
---|
469 | // categories with elements and with no representant. Those categories |
---|
470 | // must be added to the list of categories to set to a random |
---|
471 | // representant. |
---|
472 | $query = ' |
---|
473 | SELECT DISTINCT id |
---|
474 | FROM '.CATEGORIES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' |
---|
475 | ON id = category_id |
---|
476 | WHERE representative_picture_id IS NULL |
---|
477 | AND '.sprintf($where_cats, 'category_id').' |
---|
478 | ;'; |
---|
479 | $to_rand = array_from_query($query, 'id'); |
---|
480 | if (count($to_rand) > 0) |
---|
481 | { |
---|
482 | set_random_representant($to_rand); |
---|
483 | } |
---|
484 | } |
---|
485 | } |
---|
486 | |
---|
487 | /** |
---|
488 | * check and repair images integrity |
---|
489 | * |
---|
490 | * TODO see delete_elements function to check all linked tables |
---|
491 | */ |
---|
492 | function images_integrity() |
---|
493 | { |
---|
494 | $query = ' |
---|
495 | SELECT |
---|
496 | image_id |
---|
497 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
498 | LEFT JOIN '.IMAGES_TABLE.' ON id = image_id |
---|
499 | WHERE id IS NULL |
---|
500 | ;'; |
---|
501 | $result = pwg_query($query); |
---|
502 | $orphan_image_ids = array_from_query($query, 'image_id'); |
---|
503 | |
---|
504 | if (count($orphan_image_ids) > 0) |
---|
505 | { |
---|
506 | $query = ' |
---|
507 | DELETE |
---|
508 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
509 | WHERE image_id IN ('.implode(',', $orphan_image_ids).') |
---|
510 | ;'; |
---|
511 | pwg_query($query); |
---|
512 | } |
---|
513 | } |
---|
514 | |
---|
515 | /** |
---|
516 | * returns an array containing sub-directories which can be a category, |
---|
517 | * recursive by default |
---|
518 | * |
---|
519 | * directories nammed "thumbnail", "pwg_high" or "pwg_representative" are |
---|
520 | * omitted |
---|
521 | * |
---|
522 | * @param string $basedir |
---|
523 | * @return array |
---|
524 | */ |
---|
525 | function get_fs_directories($path, $recursive = true) |
---|
526 | { |
---|
527 | $dirs = array(); |
---|
528 | |
---|
529 | if (is_dir($path)) |
---|
530 | { |
---|
531 | if ($contents = opendir($path)) |
---|
532 | { |
---|
533 | while (($node = readdir($contents)) !== false) |
---|
534 | { |
---|
535 | if ($node != '.' |
---|
536 | and $node != '..' |
---|
537 | and $node != '.svn' |
---|
538 | and $node != 'thumbnail' |
---|
539 | and $node != 'pwg_high' |
---|
540 | and $node != 'pwg_representative' |
---|
541 | and is_dir($path.'/'.$node)) |
---|
542 | { |
---|
543 | array_push($dirs, $path.'/'.$node); |
---|
544 | if ($recursive) |
---|
545 | { |
---|
546 | $dirs = array_merge($dirs, get_fs_directories($path.'/'.$node)); |
---|
547 | } |
---|
548 | } |
---|
549 | } |
---|
550 | closedir($contents); |
---|
551 | } |
---|
552 | } |
---|
553 | |
---|
554 | return $dirs; |
---|
555 | } |
---|
556 | |
---|
557 | /** |
---|
558 | * order categories (update categories.rank and global_rank database fields) |
---|
559 | * so that rank field are consecutive integers starting at 1 for each child |
---|
560 | * @return void |
---|
561 | */ |
---|
562 | function update_global_rank() |
---|
563 | { |
---|
564 | $query = ' |
---|
565 | SELECT id, id_uppercat, uppercats, rank, global_rank |
---|
566 | FROM '.CATEGORIES_TABLE.' |
---|
567 | ORDER BY id_uppercat,rank,name'; |
---|
568 | |
---|
569 | $cat_map = array(); |
---|
570 | |
---|
571 | $current_rank = 0; |
---|
572 | $current_uppercat = ''; |
---|
573 | |
---|
574 | $result = pwg_query($query); |
---|
575 | while ($row = pwg_db_fetch_assoc($result)) |
---|
576 | { |
---|
577 | if ($row['id_uppercat'] != $current_uppercat) |
---|
578 | { |
---|
579 | $current_rank = 0; |
---|
580 | $current_uppercat = $row['id_uppercat']; |
---|
581 | } |
---|
582 | ++$current_rank; |
---|
583 | $cat = |
---|
584 | array( |
---|
585 | 'rank' => $current_rank, |
---|
586 | 'rank_changed' =>$current_rank!=$row['rank'], |
---|
587 | 'global_rank' => $row['global_rank'], |
---|
588 | 'uppercats' => $row['uppercats'], |
---|
589 | ); |
---|
590 | $cat_map[ $row['id'] ] = $cat; |
---|
591 | } |
---|
592 | |
---|
593 | $datas = array(); |
---|
594 | |
---|
595 | foreach( $cat_map as $id=>$cat ) |
---|
596 | { |
---|
597 | $new_global_rank = preg_replace( |
---|
598 | '/(\d+)/e', |
---|
599 | "\$cat_map['$1']['rank']", |
---|
600 | str_replace(',', '.', $cat['uppercats'] ) |
---|
601 | ); |
---|
602 | if ( $cat['rank_changed'] |
---|
603 | or $new_global_rank!=$cat['global_rank'] |
---|
604 | ) |
---|
605 | { |
---|
606 | $datas[] = array( |
---|
607 | 'id' => $id, |
---|
608 | 'rank' => $cat['rank'], |
---|
609 | 'global_rank' => $new_global_rank, |
---|
610 | ); |
---|
611 | } |
---|
612 | } |
---|
613 | |
---|
614 | mass_updates( |
---|
615 | CATEGORIES_TABLE, |
---|
616 | array( |
---|
617 | 'primary' => array('id'), |
---|
618 | 'update' => array('rank', 'global_rank') |
---|
619 | ), |
---|
620 | $datas |
---|
621 | ); |
---|
622 | return count($datas); |
---|
623 | } |
---|
624 | |
---|
625 | /** |
---|
626 | * change the visible property on a set of categories |
---|
627 | * |
---|
628 | * @param array categories |
---|
629 | * @param string value |
---|
630 | * @return void |
---|
631 | */ |
---|
632 | function set_cat_visible($categories, $value) |
---|
633 | { |
---|
634 | if (!in_array($value, array('true', 'false'))) |
---|
635 | { |
---|
636 | trigger_error("set_cat_visible invalid param $value", E_USER_WARNING); |
---|
637 | return false; |
---|
638 | } |
---|
639 | |
---|
640 | // unlocking a category => all its parent categories become unlocked |
---|
641 | if ($value == 'true') |
---|
642 | { |
---|
643 | $uppercats = get_uppercat_ids($categories); |
---|
644 | $query = ' |
---|
645 | UPDATE '.CATEGORIES_TABLE.' |
---|
646 | SET visible = \'true\' |
---|
647 | WHERE id IN ('.implode(',', $uppercats).')'; |
---|
648 | pwg_query($query); |
---|
649 | } |
---|
650 | // locking a category => all its child categories become locked |
---|
651 | if ($value == 'false') |
---|
652 | { |
---|
653 | $subcats = get_subcat_ids($categories); |
---|
654 | $query = ' |
---|
655 | UPDATE '.CATEGORIES_TABLE.' |
---|
656 | SET visible = \'false\' |
---|
657 | WHERE id IN ('.implode(',', $subcats).')'; |
---|
658 | pwg_query($query); |
---|
659 | } |
---|
660 | } |
---|
661 | |
---|
662 | /** |
---|
663 | * change the status property on a set of categories : private or public |
---|
664 | * |
---|
665 | * @param array categories |
---|
666 | * @param string value |
---|
667 | * @return void |
---|
668 | */ |
---|
669 | function set_cat_status($categories, $value) |
---|
670 | { |
---|
671 | if (!in_array($value, array('public', 'private'))) |
---|
672 | { |
---|
673 | trigger_error("set_cat_status invalid param $value", E_USER_WARNING); |
---|
674 | return false; |
---|
675 | } |
---|
676 | |
---|
677 | // make public a category => all its parent categories become public |
---|
678 | if ($value == 'public') |
---|
679 | { |
---|
680 | $uppercats = get_uppercat_ids($categories); |
---|
681 | $query = ' |
---|
682 | UPDATE '.CATEGORIES_TABLE.' |
---|
683 | SET status = \'public\' |
---|
684 | WHERE id IN ('.implode(',', $uppercats).') |
---|
685 | ;'; |
---|
686 | pwg_query($query); |
---|
687 | } |
---|
688 | // make a category private => all its child categories become private |
---|
689 | if ($value == 'private') |
---|
690 | { |
---|
691 | $subcats = get_subcat_ids($categories); |
---|
692 | $query = ' |
---|
693 | UPDATE '.CATEGORIES_TABLE.' |
---|
694 | SET status = \'private\' |
---|
695 | WHERE id IN ('.implode(',', $subcats).')'; |
---|
696 | pwg_query($query); |
---|
697 | } |
---|
698 | } |
---|
699 | |
---|
700 | /** |
---|
701 | * returns all uppercats category ids of the given category ids |
---|
702 | * |
---|
703 | * @param array cat_ids |
---|
704 | * @return array |
---|
705 | */ |
---|
706 | function get_uppercat_ids($cat_ids) |
---|
707 | { |
---|
708 | if (!is_array($cat_ids) or count($cat_ids) < 1) |
---|
709 | { |
---|
710 | return array(); |
---|
711 | } |
---|
712 | |
---|
713 | $uppercats = array(); |
---|
714 | |
---|
715 | $query = ' |
---|
716 | SELECT uppercats |
---|
717 | FROM '.CATEGORIES_TABLE.' |
---|
718 | WHERE id IN ('.implode(',', $cat_ids).') |
---|
719 | ;'; |
---|
720 | $result = pwg_query($query); |
---|
721 | while ($row = pwg_db_fetch_assoc($result)) |
---|
722 | { |
---|
723 | $uppercats = array_merge($uppercats, |
---|
724 | explode(',', $row['uppercats'])); |
---|
725 | } |
---|
726 | $uppercats = array_unique($uppercats); |
---|
727 | |
---|
728 | return $uppercats; |
---|
729 | } |
---|
730 | |
---|
731 | /** |
---|
732 | * set a new random representant to the categories |
---|
733 | * |
---|
734 | * @param array categories |
---|
735 | */ |
---|
736 | function set_random_representant($categories) |
---|
737 | { |
---|
738 | $datas = array(); |
---|
739 | foreach ($categories as $category_id) |
---|
740 | { |
---|
741 | $query = ' |
---|
742 | SELECT image_id |
---|
743 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
744 | WHERE category_id = '.$category_id.' |
---|
745 | ORDER BY '.DB_RANDOM_FUNCTION.'() |
---|
746 | LIMIT 1 |
---|
747 | ;'; |
---|
748 | list($representative) = pwg_db_fetch_row(pwg_query($query)); |
---|
749 | |
---|
750 | array_push( |
---|
751 | $datas, |
---|
752 | array( |
---|
753 | 'id' => $category_id, |
---|
754 | 'representative_picture_id' => $representative, |
---|
755 | ) |
---|
756 | ); |
---|
757 | } |
---|
758 | |
---|
759 | mass_updates( |
---|
760 | CATEGORIES_TABLE, |
---|
761 | array( |
---|
762 | 'primary' => array('id'), |
---|
763 | 'update' => array('representative_picture_id') |
---|
764 | ), |
---|
765 | $datas |
---|
766 | ); |
---|
767 | } |
---|
768 | |
---|
769 | /** |
---|
770 | * returns the fulldir for each given category id |
---|
771 | * |
---|
772 | * @param array cat_ids |
---|
773 | * @return array |
---|
774 | */ |
---|
775 | function get_fulldirs($cat_ids) |
---|
776 | { |
---|
777 | if (count($cat_ids) == 0) |
---|
778 | { |
---|
779 | return array(); |
---|
780 | } |
---|
781 | |
---|
782 | // caching directories of existing categories |
---|
783 | $query = ' |
---|
784 | SELECT id, dir |
---|
785 | FROM '.CATEGORIES_TABLE.' |
---|
786 | WHERE dir IS NOT NULL |
---|
787 | ;'; |
---|
788 | $cat_dirs = simple_hash_from_query($query, 'id', 'dir'); |
---|
789 | |
---|
790 | // caching galleries_url |
---|
791 | $query = ' |
---|
792 | SELECT id, galleries_url |
---|
793 | FROM '.SITES_TABLE.' |
---|
794 | ;'; |
---|
795 | $galleries_url = simple_hash_from_query($query, 'id', 'galleries_url'); |
---|
796 | |
---|
797 | // categories : id, site_id, uppercats |
---|
798 | $query = ' |
---|
799 | SELECT id, uppercats, site_id |
---|
800 | FROM '.CATEGORIES_TABLE.' |
---|
801 | WHERE dir IS NOT NULL |
---|
802 | AND id IN ( |
---|
803 | '.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
804 | ;'; |
---|
805 | $categories = array_from_query($query); |
---|
806 | |
---|
807 | // filling $cat_fulldirs |
---|
808 | $cat_fulldirs = array(); |
---|
809 | foreach ($categories as $category) |
---|
810 | { |
---|
811 | $uppercats = str_replace(',', '/', $category['uppercats']); |
---|
812 | $cat_fulldirs[$category['id']] = $galleries_url[$category['site_id']]; |
---|
813 | $cat_fulldirs[$category['id']].= preg_replace('/(\d+)/e', |
---|
814 | "\$cat_dirs['$1']", |
---|
815 | $uppercats); |
---|
816 | } |
---|
817 | |
---|
818 | return $cat_fulldirs; |
---|
819 | } |
---|
820 | |
---|
821 | /** |
---|
822 | * returns an array with all file system files according to |
---|
823 | * $conf['file_ext'] |
---|
824 | * |
---|
825 | * @param string $path |
---|
826 | * @param bool recursive |
---|
827 | * @return array |
---|
828 | */ |
---|
829 | function get_fs($path, $recursive = true) |
---|
830 | { |
---|
831 | global $conf; |
---|
832 | |
---|
833 | // because isset is faster than in_array... |
---|
834 | if (!isset($conf['flip_picture_ext'])) |
---|
835 | { |
---|
836 | $conf['flip_picture_ext'] = array_flip($conf['picture_ext']); |
---|
837 | } |
---|
838 | if (!isset($conf['flip_file_ext'])) |
---|
839 | { |
---|
840 | $conf['flip_file_ext'] = array_flip($conf['file_ext']); |
---|
841 | } |
---|
842 | |
---|
843 | $fs['elements'] = array(); |
---|
844 | $fs['thumbnails'] = array(); |
---|
845 | $fs['representatives'] = array(); |
---|
846 | $subdirs = array(); |
---|
847 | |
---|
848 | if (is_dir($path)) |
---|
849 | { |
---|
850 | if ($contents = opendir($path)) |
---|
851 | { |
---|
852 | while (($node = readdir($contents)) !== false) |
---|
853 | { |
---|
854 | if ($node == '.' or $node == '..') continue; |
---|
855 | |
---|
856 | if (is_file($path.'/'.$node)) |
---|
857 | { |
---|
858 | $extension = get_extension($node); |
---|
859 | |
---|
860 | // if (in_array($extension, $conf['picture_ext'])) |
---|
861 | if (isset($conf['flip_picture_ext'][$extension])) |
---|
862 | { |
---|
863 | if (basename($path) == 'thumbnail') |
---|
864 | { |
---|
865 | array_push($fs['thumbnails'], $path.'/'.$node); |
---|
866 | } |
---|
867 | else if (basename($path) == 'pwg_representative') |
---|
868 | { |
---|
869 | array_push($fs['representatives'], $path.'/'.$node); |
---|
870 | } |
---|
871 | else |
---|
872 | { |
---|
873 | array_push($fs['elements'], $path.'/'.$node); |
---|
874 | } |
---|
875 | } |
---|
876 | // else if (in_array($extension, $conf['file_ext'])) |
---|
877 | else if (isset($conf['flip_file_ext'][$extension])) |
---|
878 | { |
---|
879 | array_push($fs['elements'], $path.'/'.$node); |
---|
880 | } |
---|
881 | } |
---|
882 | else if (is_dir($path.'/'.$node) and $node != 'pwg_high' and $recursive) |
---|
883 | { |
---|
884 | array_push($subdirs, $node); |
---|
885 | } |
---|
886 | } |
---|
887 | } |
---|
888 | closedir($contents); |
---|
889 | |
---|
890 | foreach ($subdirs as $subdir) |
---|
891 | { |
---|
892 | $tmp_fs = get_fs($path.'/'.$subdir); |
---|
893 | |
---|
894 | $fs['elements'] = array_merge($fs['elements'], |
---|
895 | $tmp_fs['elements']); |
---|
896 | |
---|
897 | $fs['thumbnails'] = array_merge($fs['thumbnails'], |
---|
898 | $tmp_fs['thumbnails']); |
---|
899 | |
---|
900 | $fs['representatives'] = array_merge($fs['representatives'], |
---|
901 | $tmp_fs['representatives']); |
---|
902 | } |
---|
903 | } |
---|
904 | return $fs; |
---|
905 | } |
---|
906 | |
---|
907 | /** |
---|
908 | * synchronize base users list and related users list |
---|
909 | * |
---|
910 | * compares and synchronizes base users table (USERS_TABLE) with its child |
---|
911 | * tables (USER_INFOS_TABLE, USER_ACCESS, USER_CACHE, USER_GROUP) : each |
---|
912 | * base user must be present in child tables, users in child tables not |
---|
913 | * present in base table must be deleted. |
---|
914 | * |
---|
915 | * @return void |
---|
916 | */ |
---|
917 | function sync_users() |
---|
918 | { |
---|
919 | global $conf; |
---|
920 | |
---|
921 | $query = ' |
---|
922 | SELECT '.$conf['user_fields']['id'].' AS id |
---|
923 | FROM '.USERS_TABLE.' |
---|
924 | ;'; |
---|
925 | $base_users = array_from_query($query, 'id'); |
---|
926 | |
---|
927 | $query = ' |
---|
928 | SELECT user_id |
---|
929 | FROM '.USER_INFOS_TABLE.' |
---|
930 | ;'; |
---|
931 | $infos_users = array_from_query($query, 'user_id'); |
---|
932 | |
---|
933 | // users present in $base_users and not in $infos_users must be added |
---|
934 | $to_create = array_diff($base_users, $infos_users); |
---|
935 | |
---|
936 | if (count($to_create) > 0) |
---|
937 | { |
---|
938 | create_user_infos($to_create); |
---|
939 | } |
---|
940 | |
---|
941 | // users present in user related tables must be present in the base user |
---|
942 | // table |
---|
943 | $tables = array( |
---|
944 | USER_MAIL_NOTIFICATION_TABLE, |
---|
945 | USER_FEED_TABLE, |
---|
946 | USER_INFOS_TABLE, |
---|
947 | USER_ACCESS_TABLE, |
---|
948 | USER_CACHE_TABLE, |
---|
949 | USER_CACHE_CATEGORIES_TABLE, |
---|
950 | USER_GROUP_TABLE |
---|
951 | ); |
---|
952 | |
---|
953 | foreach ($tables as $table) |
---|
954 | { |
---|
955 | $query = ' |
---|
956 | SELECT DISTINCT user_id |
---|
957 | FROM '.$table.' |
---|
958 | ;'; |
---|
959 | $to_delete = array_diff( |
---|
960 | array_from_query($query, 'user_id'), |
---|
961 | $base_users |
---|
962 | ); |
---|
963 | |
---|
964 | if (count($to_delete) > 0) |
---|
965 | { |
---|
966 | $query = ' |
---|
967 | DELETE |
---|
968 | FROM '.$table.' |
---|
969 | WHERE user_id in ('.implode(',', $to_delete).') |
---|
970 | ;'; |
---|
971 | pwg_query($query); |
---|
972 | } |
---|
973 | } |
---|
974 | } |
---|
975 | |
---|
976 | /** |
---|
977 | * updates categories.uppercats field based on categories.id + |
---|
978 | * categories.id_uppercat |
---|
979 | * |
---|
980 | * @return void |
---|
981 | */ |
---|
982 | function update_uppercats() |
---|
983 | { |
---|
984 | $query = ' |
---|
985 | SELECT id, id_uppercat, uppercats |
---|
986 | FROM '.CATEGORIES_TABLE.' |
---|
987 | ;'; |
---|
988 | $cat_map = hash_from_query($query, 'id'); |
---|
989 | |
---|
990 | $datas = array(); |
---|
991 | foreach ($cat_map as $id => $cat) |
---|
992 | { |
---|
993 | $upper_list = array(); |
---|
994 | |
---|
995 | $uppercat = $id; |
---|
996 | while ($uppercat) |
---|
997 | { |
---|
998 | array_push($upper_list, $uppercat); |
---|
999 | $uppercat = $cat_map[$uppercat]['id_uppercat']; |
---|
1000 | } |
---|
1001 | |
---|
1002 | $new_uppercats = implode(',', array_reverse($upper_list)); |
---|
1003 | if ($new_uppercats != $cat['uppercats']) |
---|
1004 | { |
---|
1005 | array_push( |
---|
1006 | $datas, |
---|
1007 | array( |
---|
1008 | 'id' => $id, |
---|
1009 | 'uppercats' => $new_uppercats |
---|
1010 | ) |
---|
1011 | ); |
---|
1012 | } |
---|
1013 | } |
---|
1014 | $fields = array('primary' => array('id'), 'update' => array('uppercats')); |
---|
1015 | mass_updates(CATEGORIES_TABLE, $fields, $datas); |
---|
1016 | } |
---|
1017 | |
---|
1018 | /** |
---|
1019 | * update images.path field |
---|
1020 | * |
---|
1021 | * @return void |
---|
1022 | */ |
---|
1023 | function update_path() |
---|
1024 | { |
---|
1025 | $query = ' |
---|
1026 | SELECT DISTINCT(storage_category_id) |
---|
1027 | FROM '.IMAGES_TABLE.' |
---|
1028 | WHERE storage_category_id IS NOT NULL |
---|
1029 | ;'; |
---|
1030 | $cat_ids = array_from_query($query, 'storage_category_id'); |
---|
1031 | $fulldirs = get_fulldirs($cat_ids); |
---|
1032 | |
---|
1033 | foreach ($cat_ids as $cat_id) |
---|
1034 | { |
---|
1035 | $query = ' |
---|
1036 | UPDATE '.IMAGES_TABLE.' |
---|
1037 | SET path = '.pwg_db_concat(array("'".$fulldirs[$cat_id]."/'",'file')).' |
---|
1038 | WHERE storage_category_id = '.$cat_id.' |
---|
1039 | ;'; |
---|
1040 | pwg_query($query); |
---|
1041 | } |
---|
1042 | } |
---|
1043 | |
---|
1044 | /** |
---|
1045 | * change the parent category of the given categories. The categories are |
---|
1046 | * supposed virtual. |
---|
1047 | * |
---|
1048 | * @param array category identifiers |
---|
1049 | * @param int parent category identifier |
---|
1050 | * @return void |
---|
1051 | */ |
---|
1052 | function move_categories($category_ids, $new_parent = -1) |
---|
1053 | { |
---|
1054 | global $page; |
---|
1055 | |
---|
1056 | if (count($category_ids) == 0) |
---|
1057 | { |
---|
1058 | return; |
---|
1059 | } |
---|
1060 | |
---|
1061 | $new_parent = $new_parent < 1 ? 'NULL' : $new_parent; |
---|
1062 | |
---|
1063 | $categories = array(); |
---|
1064 | |
---|
1065 | $query = ' |
---|
1066 | SELECT id, id_uppercat, status, uppercats |
---|
1067 | FROM '.CATEGORIES_TABLE.' |
---|
1068 | WHERE id IN ('.implode(',', $category_ids).') |
---|
1069 | ;'; |
---|
1070 | $result = pwg_query($query); |
---|
1071 | while ($row = pwg_db_fetch_assoc($result)) |
---|
1072 | { |
---|
1073 | $categories[$row['id']] = |
---|
1074 | array( |
---|
1075 | 'parent' => empty($row['id_uppercat']) ? 'NULL' : $row['id_uppercat'], |
---|
1076 | 'status' => $row['status'], |
---|
1077 | 'uppercats' => $row['uppercats'] |
---|
1078 | ); |
---|
1079 | } |
---|
1080 | |
---|
1081 | // is the movement possible? The movement is impossible if you try to move |
---|
1082 | // a category in a sub-category or itself |
---|
1083 | if ('NULL' != $new_parent) |
---|
1084 | { |
---|
1085 | $query = ' |
---|
1086 | SELECT uppercats |
---|
1087 | FROM '.CATEGORIES_TABLE.' |
---|
1088 | WHERE id = '.$new_parent.' |
---|
1089 | ;'; |
---|
1090 | list($new_parent_uppercats) = pwg_db_fetch_row(pwg_query($query)); |
---|
1091 | |
---|
1092 | foreach ($categories as $category) |
---|
1093 | { |
---|
1094 | // technically, you can't move a category with uppercats 12,125,13,14 |
---|
1095 | // into a new parent category with uppercats 12,125,13,14,24 |
---|
1096 | if (preg_match('/^'.$category['uppercats'].'(,|$)/', $new_parent_uppercats)) |
---|
1097 | { |
---|
1098 | array_push( |
---|
1099 | $page['errors'], |
---|
1100 | l10n('You cannot move an album in its own sub album') |
---|
1101 | ); |
---|
1102 | return; |
---|
1103 | } |
---|
1104 | } |
---|
1105 | } |
---|
1106 | |
---|
1107 | $tables = |
---|
1108 | array( |
---|
1109 | USER_ACCESS_TABLE => 'user_id', |
---|
1110 | GROUP_ACCESS_TABLE => 'group_id' |
---|
1111 | ); |
---|
1112 | |
---|
1113 | $query = ' |
---|
1114 | UPDATE '.CATEGORIES_TABLE.' |
---|
1115 | SET id_uppercat = '.$new_parent.' |
---|
1116 | WHERE id IN ('.implode(',', $category_ids).') |
---|
1117 | ;'; |
---|
1118 | pwg_query($query); |
---|
1119 | |
---|
1120 | update_uppercats(); |
---|
1121 | update_global_rank(); |
---|
1122 | |
---|
1123 | // status and related permissions management |
---|
1124 | if ('NULL' == $new_parent) |
---|
1125 | { |
---|
1126 | $parent_status = 'public'; |
---|
1127 | } |
---|
1128 | else |
---|
1129 | { |
---|
1130 | $query = ' |
---|
1131 | SELECT status |
---|
1132 | FROM '.CATEGORIES_TABLE.' |
---|
1133 | WHERE id = '.$new_parent.' |
---|
1134 | ;'; |
---|
1135 | list($parent_status) = pwg_db_fetch_row(pwg_query($query)); |
---|
1136 | } |
---|
1137 | |
---|
1138 | if ('private' == $parent_status) |
---|
1139 | { |
---|
1140 | foreach ($categories as $cat_id => $category) |
---|
1141 | { |
---|
1142 | if ('public' == $category['status']) |
---|
1143 | { |
---|
1144 | set_cat_status(array($cat_id), 'private'); |
---|
1145 | } |
---|
1146 | |
---|
1147 | $subcats = get_subcat_ids(array($cat_id)); |
---|
1148 | |
---|
1149 | foreach ($tables as $table => $field) |
---|
1150 | { |
---|
1151 | $query = ' |
---|
1152 | SELECT '.$field.' |
---|
1153 | FROM '.$table.' |
---|
1154 | WHERE cat_id = '.$cat_id.' |
---|
1155 | ;'; |
---|
1156 | $category_access = array_from_query($query, $field); |
---|
1157 | |
---|
1158 | $query = ' |
---|
1159 | SELECT '.$field.' |
---|
1160 | FROM '.$table.' |
---|
1161 | WHERE cat_id = '.$new_parent.' |
---|
1162 | ;'; |
---|
1163 | $parent_access = array_from_query($query, $field); |
---|
1164 | |
---|
1165 | $to_delete = array_diff($category_access, $parent_access); |
---|
1166 | |
---|
1167 | if (count($to_delete) > 0) |
---|
1168 | { |
---|
1169 | $query = ' |
---|
1170 | DELETE FROM '.$table.' |
---|
1171 | WHERE '.$field.' IN ('.implode(',', $to_delete).') |
---|
1172 | AND cat_id IN ('.implode(',', $subcats).') |
---|
1173 | ;'; |
---|
1174 | pwg_query($query); |
---|
1175 | } |
---|
1176 | } |
---|
1177 | } |
---|
1178 | } |
---|
1179 | |
---|
1180 | array_push( |
---|
1181 | $page['infos'], |
---|
1182 | l10n_dec( |
---|
1183 | '%d album moved', '%d albums moved', |
---|
1184 | count($categories) |
---|
1185 | ) |
---|
1186 | ); |
---|
1187 | } |
---|
1188 | |
---|
1189 | /** |
---|
1190 | * create a virtual category |
---|
1191 | * |
---|
1192 | * @param string category name |
---|
1193 | * @param int parent category id |
---|
1194 | * @return array with ('info' and 'id') or ('error') key |
---|
1195 | */ |
---|
1196 | function create_virtual_category($category_name, $parent_id=null, $options=array()) |
---|
1197 | { |
---|
1198 | global $conf, $user; |
---|
1199 | |
---|
1200 | // is the given category name only containing blank spaces ? |
---|
1201 | if (preg_match('/^\s*$/', $category_name)) |
---|
1202 | { |
---|
1203 | return array('error' => l10n('The name of an album must not be empty')); |
---|
1204 | } |
---|
1205 | |
---|
1206 | $insert = array( |
---|
1207 | 'name' => $category_name, |
---|
1208 | 'rank' => 0, |
---|
1209 | 'global_rank' => 0, |
---|
1210 | ); |
---|
1211 | |
---|
1212 | // is the album commentable? |
---|
1213 | if (isset($options['commentable']) and is_bool($options['commentable'])) |
---|
1214 | { |
---|
1215 | $insert['commentable'] = $options['commentable']; |
---|
1216 | } |
---|
1217 | else |
---|
1218 | { |
---|
1219 | $insert['commentable'] = $conf['newcat_default_commentable']; |
---|
1220 | } |
---|
1221 | $insert['commentable'] = boolean_to_string($insert['commentable']); |
---|
1222 | |
---|
1223 | // is the album temporarily locked? (only visible by administrators, |
---|
1224 | // whatever permissions) (may be overwritten if parent album is not |
---|
1225 | // visible) |
---|
1226 | if (isset($options['visible']) and is_bool($options['visible'])) |
---|
1227 | { |
---|
1228 | $insert['visible'] = $options['visible']; |
---|
1229 | } |
---|
1230 | else |
---|
1231 | { |
---|
1232 | $insert['visible'] = $conf['newcat_default_visible']; |
---|
1233 | } |
---|
1234 | $insert['visible'] = boolean_to_string($insert['visible']); |
---|
1235 | |
---|
1236 | // is the album private? (may be overwritten if parent album is private) |
---|
1237 | if (isset($options['status']) and 'private' == $options['status']) |
---|
1238 | { |
---|
1239 | $insert['status'] = 'private'; |
---|
1240 | } |
---|
1241 | else |
---|
1242 | { |
---|
1243 | $insert['status'] = $conf['newcat_default_status']; |
---|
1244 | } |
---|
1245 | |
---|
1246 | // any description for this album? |
---|
1247 | if (isset($options['comment'])) |
---|
1248 | { |
---|
1249 | $insert['comment'] = $conf['allow_html_descriptions'] ? $options['comment'] : strip_tags($options['comment']); |
---|
1250 | } |
---|
1251 | |
---|
1252 | if (!empty($parent_id) and is_numeric($parent_id)) |
---|
1253 | { |
---|
1254 | $query = ' |
---|
1255 | SELECT id, uppercats, global_rank, visible, status |
---|
1256 | FROM '.CATEGORIES_TABLE.' |
---|
1257 | WHERE id = '.$parent_id.' |
---|
1258 | ;'; |
---|
1259 | $parent = pwg_db_fetch_assoc(pwg_query($query)); |
---|
1260 | |
---|
1261 | $insert['id_uppercat'] = $parent['id']; |
---|
1262 | $insert['global_rank'] = $parent['global_rank'].'.'.$insert['rank']; |
---|
1263 | |
---|
1264 | // at creation, must a category be visible or not ? Warning : if the |
---|
1265 | // parent category is invisible, the category is automatically create |
---|
1266 | // invisible. (invisible = locked) |
---|
1267 | if ('false' == $parent['visible']) |
---|
1268 | { |
---|
1269 | $insert['visible'] = 'false'; |
---|
1270 | } |
---|
1271 | |
---|
1272 | // at creation, must a category be public or private ? Warning : if the |
---|
1273 | // parent category is private, the category is automatically create |
---|
1274 | // private. |
---|
1275 | if ('private' == $parent['status']) |
---|
1276 | { |
---|
1277 | $insert['status'] = 'private'; |
---|
1278 | } |
---|
1279 | |
---|
1280 | $uppercats_prefix = $parent['uppercats'].','; |
---|
1281 | } |
---|
1282 | else |
---|
1283 | { |
---|
1284 | $uppercats_prefix = ''; |
---|
1285 | } |
---|
1286 | |
---|
1287 | // we have then to add the virtual category |
---|
1288 | single_insert(CATEGORIES_TABLE, $insert); |
---|
1289 | $inserted_id = pwg_db_insert_id(CATEGORIES_TABLE); |
---|
1290 | |
---|
1291 | single_update( |
---|
1292 | CATEGORIES_TABLE, |
---|
1293 | array('uppercats' => $uppercats_prefix.$inserted_id), |
---|
1294 | array('id' => $inserted_id) |
---|
1295 | ); |
---|
1296 | |
---|
1297 | update_global_rank(); |
---|
1298 | |
---|
1299 | if ('private' == $insert['status']) |
---|
1300 | { |
---|
1301 | add_permission_on_category($inserted_id, array_unique(array_merge(get_admins(), array($user['id'])))); |
---|
1302 | } |
---|
1303 | |
---|
1304 | return array( |
---|
1305 | 'info' => l10n('Virtual album added'), |
---|
1306 | 'id' => $inserted_id, |
---|
1307 | ); |
---|
1308 | } |
---|
1309 | |
---|
1310 | /** |
---|
1311 | * Set tags to an image. Warning: given tags are all tags associated to the |
---|
1312 | * image, not additionnal tags. |
---|
1313 | * |
---|
1314 | * @param array tag ids |
---|
1315 | * @param int image id |
---|
1316 | * @return void |
---|
1317 | */ |
---|
1318 | function set_tags($tags, $image_id) |
---|
1319 | { |
---|
1320 | set_tags_of( array($image_id=>$tags) ); |
---|
1321 | } |
---|
1322 | |
---|
1323 | /** |
---|
1324 | * Add new tags to a set of images. |
---|
1325 | * |
---|
1326 | * @param array tag ids |
---|
1327 | * @param array image ids |
---|
1328 | * @return void |
---|
1329 | */ |
---|
1330 | function add_tags($tags, $images) |
---|
1331 | { |
---|
1332 | if (count($tags) == 0 or count($images) == 0) |
---|
1333 | { |
---|
1334 | return; |
---|
1335 | } |
---|
1336 | |
---|
1337 | // we can't insert twice the same {image_id,tag_id} so we must first |
---|
1338 | // delete lines we'll insert later |
---|
1339 | $query = ' |
---|
1340 | DELETE |
---|
1341 | FROM '.IMAGE_TAG_TABLE.' |
---|
1342 | WHERE image_id IN ('.implode(',', $images).') |
---|
1343 | AND tag_id IN ('.implode(',', $tags).') |
---|
1344 | ;'; |
---|
1345 | pwg_query($query); |
---|
1346 | |
---|
1347 | $inserts = array(); |
---|
1348 | foreach ($images as $image_id) |
---|
1349 | { |
---|
1350 | foreach ( array_unique($tags) as $tag_id) |
---|
1351 | { |
---|
1352 | $inserts[] = array( |
---|
1353 | 'image_id' => $image_id, |
---|
1354 | 'tag_id' => $tag_id, |
---|
1355 | ); |
---|
1356 | } |
---|
1357 | } |
---|
1358 | mass_inserts( |
---|
1359 | IMAGE_TAG_TABLE, |
---|
1360 | array_keys($inserts[0]), |
---|
1361 | $inserts |
---|
1362 | ); |
---|
1363 | } |
---|
1364 | |
---|
1365 | /** |
---|
1366 | * |
---|
1367 | */ |
---|
1368 | function delete_tags($tag_ids) |
---|
1369 | { |
---|
1370 | if (is_numeric($tag_ids)) |
---|
1371 | { |
---|
1372 | $tag_ids = array($tag_ids); |
---|
1373 | } |
---|
1374 | |
---|
1375 | if (!is_array($tag_ids)) |
---|
1376 | { |
---|
1377 | return false; |
---|
1378 | } |
---|
1379 | |
---|
1380 | $query = ' |
---|
1381 | DELETE |
---|
1382 | FROM '.IMAGE_TAG_TABLE.' |
---|
1383 | WHERE tag_id IN ('.implode(',', $tag_ids).') |
---|
1384 | ;'; |
---|
1385 | pwg_query($query); |
---|
1386 | |
---|
1387 | $query = ' |
---|
1388 | DELETE |
---|
1389 | FROM '.TAGS_TABLE.' |
---|
1390 | WHERE id IN ('.implode(',', $tag_ids).') |
---|
1391 | ;'; |
---|
1392 | pwg_query($query); |
---|
1393 | } |
---|
1394 | |
---|
1395 | function tag_id_from_tag_name($tag_name) |
---|
1396 | { |
---|
1397 | global $page; |
---|
1398 | |
---|
1399 | $tag_name = trim($tag_name); |
---|
1400 | if (isset($page['tag_id_from_tag_name_cache'][$tag_name])) |
---|
1401 | { |
---|
1402 | return $page['tag_id_from_tag_name_cache'][$tag_name]; |
---|
1403 | } |
---|
1404 | |
---|
1405 | // search existing by exact name |
---|
1406 | $query = ' |
---|
1407 | SELECT id |
---|
1408 | FROM '.TAGS_TABLE.' |
---|
1409 | WHERE name = \''.$tag_name.'\' |
---|
1410 | ;'; |
---|
1411 | if (count($existing_tags = array_from_query($query, 'id')) == 0) |
---|
1412 | { |
---|
1413 | // search existing by case insensitive name |
---|
1414 | $query = ' |
---|
1415 | SELECT id |
---|
1416 | FROM '.TAGS_TABLE.' |
---|
1417 | WHERE CONVERT(name, CHAR) = \''.$tag_name.'\' |
---|
1418 | ;'; |
---|
1419 | if (count($existing_tags = array_from_query($query, 'id')) == 0) |
---|
1420 | { |
---|
1421 | $url_name = trigger_event('render_tag_url', $tag_name); |
---|
1422 | // search existing by url name |
---|
1423 | $query = ' |
---|
1424 | SELECT id |
---|
1425 | FROM '.TAGS_TABLE.' |
---|
1426 | WHERE url_name = \''.$url_name.'\' |
---|
1427 | ;'; |
---|
1428 | if (count($existing_tags = array_from_query($query, 'id')) == 0) |
---|
1429 | { |
---|
1430 | mass_inserts( |
---|
1431 | TAGS_TABLE, |
---|
1432 | array('name', 'url_name'), |
---|
1433 | array( |
---|
1434 | array( |
---|
1435 | 'name' => $tag_name, |
---|
1436 | 'url_name' => $url_name, |
---|
1437 | ) |
---|
1438 | ) |
---|
1439 | ); |
---|
1440 | $page['tag_id_from_tag_name_cache'][$tag_name] = pwg_db_insert_id(TAGS_TABLE); |
---|
1441 | return $page['tag_id_from_tag_name_cache'][$tag_name]; |
---|
1442 | } |
---|
1443 | } |
---|
1444 | } |
---|
1445 | |
---|
1446 | $page['tag_id_from_tag_name_cache'][$tag_name] = $existing_tags[0]; |
---|
1447 | return $page['tag_id_from_tag_name_cache'][$tag_name]; |
---|
1448 | } |
---|
1449 | |
---|
1450 | function set_tags_of($tags_of) |
---|
1451 | { |
---|
1452 | if (count($tags_of) > 0) |
---|
1453 | { |
---|
1454 | $query = ' |
---|
1455 | DELETE |
---|
1456 | FROM '.IMAGE_TAG_TABLE.' |
---|
1457 | WHERE image_id IN ('.implode(',', array_keys($tags_of)).') |
---|
1458 | ;'; |
---|
1459 | pwg_query($query); |
---|
1460 | |
---|
1461 | $inserts = array(); |
---|
1462 | |
---|
1463 | foreach ($tags_of as $image_id => $tag_ids) |
---|
1464 | { |
---|
1465 | foreach (array_unique($tag_ids) as $tag_id) |
---|
1466 | { |
---|
1467 | $inserts[] = array( |
---|
1468 | 'image_id' => $image_id, |
---|
1469 | 'tag_id' => $tag_id, |
---|
1470 | ); |
---|
1471 | } |
---|
1472 | } |
---|
1473 | |
---|
1474 | if (count($inserts)) |
---|
1475 | { |
---|
1476 | mass_inserts( |
---|
1477 | IMAGE_TAG_TABLE, |
---|
1478 | array_keys($inserts[0]), |
---|
1479 | $inserts |
---|
1480 | ); |
---|
1481 | } |
---|
1482 | } |
---|
1483 | } |
---|
1484 | |
---|
1485 | /** |
---|
1486 | * Associate a list of images to a list of categories. |
---|
1487 | * |
---|
1488 | * The function will not duplicate links and will preserve ranks |
---|
1489 | * |
---|
1490 | * @param array images |
---|
1491 | * @param array categories |
---|
1492 | * @return void |
---|
1493 | */ |
---|
1494 | function associate_images_to_categories($images, $categories) |
---|
1495 | { |
---|
1496 | if (count($images) == 0 |
---|
1497 | or count($categories) == 0) |
---|
1498 | { |
---|
1499 | return false; |
---|
1500 | } |
---|
1501 | |
---|
1502 | // get existing associations |
---|
1503 | $query = ' |
---|
1504 | SELECT |
---|
1505 | image_id, |
---|
1506 | category_id |
---|
1507 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1508 | WHERE image_id IN ('.implode(',', $images).') |
---|
1509 | AND category_id IN ('.implode(',', $categories).') |
---|
1510 | ;'; |
---|
1511 | $result = pwg_query($query); |
---|
1512 | |
---|
1513 | $existing = array(); |
---|
1514 | while ($row = pwg_db_fetch_assoc($result)) |
---|
1515 | { |
---|
1516 | $existing[ $row['category_id'] ][] = $row['image_id']; |
---|
1517 | } |
---|
1518 | |
---|
1519 | // get max rank of each categories |
---|
1520 | $query = ' |
---|
1521 | SELECT |
---|
1522 | category_id, |
---|
1523 | MAX(rank) AS max_rank |
---|
1524 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1525 | WHERE rank IS NOT NULL |
---|
1526 | AND category_id IN ('.implode(',', $categories).') |
---|
1527 | GROUP BY category_id |
---|
1528 | ;'; |
---|
1529 | |
---|
1530 | $current_rank_of = simple_hash_from_query( |
---|
1531 | $query, |
---|
1532 | 'category_id', |
---|
1533 | 'max_rank' |
---|
1534 | ); |
---|
1535 | |
---|
1536 | // associate only not already associated images |
---|
1537 | $inserts = array(); |
---|
1538 | foreach ($categories as $category_id) |
---|
1539 | { |
---|
1540 | if (!isset($current_rank_of[$category_id])) |
---|
1541 | { |
---|
1542 | $current_rank_of[$category_id] = 0; |
---|
1543 | } |
---|
1544 | if (!isset($existing[$category_id])) |
---|
1545 | { |
---|
1546 | $existing[$category_id] = array(); |
---|
1547 | } |
---|
1548 | |
---|
1549 | foreach ($images as $image_id) |
---|
1550 | { |
---|
1551 | if (!in_array($image_id, $existing[$category_id])) |
---|
1552 | { |
---|
1553 | $rank = ++$current_rank_of[$category_id]; |
---|
1554 | |
---|
1555 | array_push( |
---|
1556 | $inserts, |
---|
1557 | array( |
---|
1558 | 'image_id' => $image_id, |
---|
1559 | 'category_id' => $category_id, |
---|
1560 | 'rank' => $rank, |
---|
1561 | ) |
---|
1562 | ); |
---|
1563 | } |
---|
1564 | } |
---|
1565 | } |
---|
1566 | |
---|
1567 | if (count($inserts)) |
---|
1568 | { |
---|
1569 | mass_inserts( |
---|
1570 | IMAGE_CATEGORY_TABLE, |
---|
1571 | array_keys($inserts[0]), |
---|
1572 | $inserts |
---|
1573 | ); |
---|
1574 | |
---|
1575 | update_category($categories); |
---|
1576 | } |
---|
1577 | } |
---|
1578 | |
---|
1579 | /** |
---|
1580 | * Dissociate images from all old categories except their storage category and |
---|
1581 | * associate to new categories. |
---|
1582 | * |
---|
1583 | * This function will preserve ranks |
---|
1584 | * |
---|
1585 | * @param array images |
---|
1586 | * @param array categories |
---|
1587 | * @return void |
---|
1588 | */ |
---|
1589 | function move_images_to_categories($images, $categories) |
---|
1590 | { |
---|
1591 | if (count($images) == 0) |
---|
1592 | { |
---|
1593 | return false; |
---|
1594 | } |
---|
1595 | |
---|
1596 | // let's first break links with all old albums but their "storage album" |
---|
1597 | $query = ' |
---|
1598 | DELETE '.IMAGE_CATEGORY_TABLE.'.* |
---|
1599 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1600 | JOIN '.IMAGES_TABLE.' ON image_id=id |
---|
1601 | WHERE id IN ('.implode(',', $images).') |
---|
1602 | '; |
---|
1603 | |
---|
1604 | if (is_array($categories) and count($categories) > 0) |
---|
1605 | { |
---|
1606 | $query.= ' |
---|
1607 | AND category_id NOT IN ('.implode(',', $categories).') |
---|
1608 | '; |
---|
1609 | } |
---|
1610 | |
---|
1611 | $query.= ' |
---|
1612 | AND (storage_category_id IS NULL OR storage_category_id != category_id) |
---|
1613 | ;'; |
---|
1614 | pwg_query($query); |
---|
1615 | |
---|
1616 | if (is_array($categories) and count($categories) > 0) |
---|
1617 | { |
---|
1618 | associate_images_to_categories($images, $categories); |
---|
1619 | } |
---|
1620 | } |
---|
1621 | |
---|
1622 | /** |
---|
1623 | * Associate images associated to a list of source categories to a list of |
---|
1624 | * destination categories. |
---|
1625 | * |
---|
1626 | * @param array sources |
---|
1627 | * @param array destinations |
---|
1628 | * @return void |
---|
1629 | */ |
---|
1630 | function associate_categories_to_categories($sources, $destinations) |
---|
1631 | { |
---|
1632 | if (count($sources) == 0) |
---|
1633 | { |
---|
1634 | return false; |
---|
1635 | } |
---|
1636 | |
---|
1637 | $query = ' |
---|
1638 | SELECT image_id |
---|
1639 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1640 | WHERE category_id IN ('.implode(',', $sources).') |
---|
1641 | ;'; |
---|
1642 | $images = array_from_query($query, 'image_id'); |
---|
1643 | |
---|
1644 | associate_images_to_categories($images, $destinations); |
---|
1645 | } |
---|
1646 | |
---|
1647 | /** |
---|
1648 | * Refer main Piwigo URLs (currently PHPWG_DOMAIN domain) |
---|
1649 | * |
---|
1650 | * @param void |
---|
1651 | * @return array like $conf['links'] |
---|
1652 | */ |
---|
1653 | function pwg_URL() |
---|
1654 | { |
---|
1655 | $urls = array( |
---|
1656 | 'HOME' => PHPWG_URL, |
---|
1657 | 'WIKI' => PHPWG_URL.'/doc', |
---|
1658 | 'DEMO' => PHPWG_URL.'/demo', |
---|
1659 | 'FORUM' => PHPWG_URL.'/forum', |
---|
1660 | 'BUGS' => PHPWG_URL.'/bugs', |
---|
1661 | 'EXTENSIONS' => PHPWG_URL.'/ext', |
---|
1662 | ); |
---|
1663 | return $urls; |
---|
1664 | } |
---|
1665 | |
---|
1666 | /** |
---|
1667 | * Invalidates cahed data (permissions and category counts) for all users. |
---|
1668 | */ |
---|
1669 | function invalidate_user_cache($full = true) |
---|
1670 | { |
---|
1671 | if ($full) |
---|
1672 | { |
---|
1673 | $query = ' |
---|
1674 | TRUNCATE TABLE '.USER_CACHE_CATEGORIES_TABLE.';'; |
---|
1675 | pwg_query($query); |
---|
1676 | $query = ' |
---|
1677 | TRUNCATE TABLE '.USER_CACHE_TABLE.';'; |
---|
1678 | pwg_query($query); |
---|
1679 | } |
---|
1680 | else |
---|
1681 | { |
---|
1682 | $query = ' |
---|
1683 | UPDATE '.USER_CACHE_TABLE.' |
---|
1684 | SET need_update = \'true\';'; |
---|
1685 | pwg_query($query); |
---|
1686 | } |
---|
1687 | trigger_action('invalidate_user_cache', $full); |
---|
1688 | } |
---|
1689 | |
---|
1690 | /** |
---|
1691 | * adds the caracter set to a create table sql query. |
---|
1692 | * all CREATE TABLE queries must call this function |
---|
1693 | * @param string query - the sql query |
---|
1694 | */ |
---|
1695 | function create_table_add_character_set($query) |
---|
1696 | { |
---|
1697 | defined('DB_CHARSET') or fatal_error('create_table_add_character_set DB_CHARSET undefined'); |
---|
1698 | if ('DB_CHARSET'!='') |
---|
1699 | { |
---|
1700 | if ( version_compare(pwg_get_db_version(), '4.1.0', '<') ) |
---|
1701 | { |
---|
1702 | return $query; |
---|
1703 | } |
---|
1704 | $charset_collate = " DEFAULT CHARACTER SET ".DB_CHARSET; |
---|
1705 | if (DB_COLLATE!='') |
---|
1706 | { |
---|
1707 | $charset_collate .= " COLLATE ".DB_COLLATE; |
---|
1708 | } |
---|
1709 | if ( is_array($query) ) |
---|
1710 | { |
---|
1711 | foreach( $query as $id=>$q) |
---|
1712 | { |
---|
1713 | $q=trim($q); |
---|
1714 | $q=trim($q, ';'); |
---|
1715 | if (preg_match('/^CREATE\s+TABLE/i',$q)) |
---|
1716 | { |
---|
1717 | $q.=$charset_collate; |
---|
1718 | } |
---|
1719 | $q .= ';'; |
---|
1720 | $query[$id] = $q; |
---|
1721 | } |
---|
1722 | } |
---|
1723 | else |
---|
1724 | { |
---|
1725 | $query=trim($query); |
---|
1726 | $query=trim($query, ';'); |
---|
1727 | if (preg_match('/^CREATE\s+TABLE/i',$query)) |
---|
1728 | { |
---|
1729 | $query.=$charset_collate; |
---|
1730 | } |
---|
1731 | $query .= ';'; |
---|
1732 | } |
---|
1733 | } |
---|
1734 | return $query; |
---|
1735 | } |
---|
1736 | |
---|
1737 | /** |
---|
1738 | * Returns array use on template with html_options method |
---|
1739 | * @param Min and Max access to use |
---|
1740 | * @return array of user access level |
---|
1741 | */ |
---|
1742 | function get_user_access_level_html_options($MinLevelAccess = ACCESS_FREE, $MaxLevelAccess = ACCESS_CLOSED) |
---|
1743 | { |
---|
1744 | $tpl_options = array(); |
---|
1745 | for ($level = $MinLevelAccess; $level <= $MaxLevelAccess; $level++) |
---|
1746 | { |
---|
1747 | $tpl_options[$level] = l10n(sprintf('ACCESS_%d', $level)); |
---|
1748 | } |
---|
1749 | return $tpl_options; |
---|
1750 | } |
---|
1751 | |
---|
1752 | /** |
---|
1753 | * returns a list of templates currently available in template-extension |
---|
1754 | * Each .tpl file is extracted from template-extension. |
---|
1755 | * @return array |
---|
1756 | */ |
---|
1757 | function get_extents($start='') |
---|
1758 | { |
---|
1759 | if ($start == '') { $start = './template-extension'; } |
---|
1760 | $dir = opendir($start); |
---|
1761 | $extents = array(); |
---|
1762 | |
---|
1763 | while (($file = readdir($dir)) !== false) |
---|
1764 | { |
---|
1765 | if ( $file == '.' or $file == '..' or $file == '.svn') continue; |
---|
1766 | $path = $start . '/' . $file; |
---|
1767 | if (is_dir($path)) |
---|
1768 | { |
---|
1769 | $extents = array_merge($extents, get_extents($path)); |
---|
1770 | } |
---|
1771 | elseif ( !is_link($path) and file_exists($path) |
---|
1772 | and get_extension($path) == 'tpl' ) |
---|
1773 | { |
---|
1774 | $extents[] = substr($path, 21); |
---|
1775 | } |
---|
1776 | } |
---|
1777 | return $extents; |
---|
1778 | } |
---|
1779 | |
---|
1780 | function create_tag($tag_name) |
---|
1781 | { |
---|
1782 | // does the tag already exists? |
---|
1783 | $query = ' |
---|
1784 | SELECT id |
---|
1785 | FROM '.TAGS_TABLE.' |
---|
1786 | WHERE name = \''.$tag_name.'\' |
---|
1787 | ;'; |
---|
1788 | $existing_tags = array_from_query($query, 'id'); |
---|
1789 | |
---|
1790 | if (count($existing_tags) == 0) |
---|
1791 | { |
---|
1792 | mass_inserts( |
---|
1793 | TAGS_TABLE, |
---|
1794 | array('name', 'url_name'), |
---|
1795 | array( |
---|
1796 | array( |
---|
1797 | 'name' => $tag_name, |
---|
1798 | 'url_name' => trigger_event('render_tag_url', $tag_name), |
---|
1799 | ) |
---|
1800 | ) |
---|
1801 | ); |
---|
1802 | |
---|
1803 | $inserted_id = pwg_db_insert_id(TAGS_TABLE); |
---|
1804 | |
---|
1805 | return array( |
---|
1806 | 'info' => sprintf( |
---|
1807 | l10n('Tag "%s" was added'), |
---|
1808 | stripslashes($tag_name) |
---|
1809 | ), |
---|
1810 | 'id' => $inserted_id, |
---|
1811 | ); |
---|
1812 | } |
---|
1813 | else |
---|
1814 | { |
---|
1815 | return array( |
---|
1816 | 'error' => sprintf( |
---|
1817 | l10n('Tag "%s" already exists'), |
---|
1818 | stripslashes($tag_name) |
---|
1819 | ) |
---|
1820 | ); |
---|
1821 | } |
---|
1822 | } |
---|
1823 | |
---|
1824 | /** |
---|
1825 | * Is the category accessible to the (Admin) user ? |
---|
1826 | * |
---|
1827 | * Note : if the user is not authorized to see this category, category jump |
---|
1828 | * will be replaced by admin cat_modify page |
---|
1829 | * |
---|
1830 | * @param int category id to verify |
---|
1831 | * @return bool |
---|
1832 | */ |
---|
1833 | function cat_admin_access($category_id) |
---|
1834 | { |
---|
1835 | global $user; |
---|
1836 | |
---|
1837 | // $filter['visible_categories'] and $filter['visible_images'] |
---|
1838 | // are not used because it's not necessary (filter <> restriction) |
---|
1839 | if (in_array($category_id, explode(',', $user['forbidden_categories']))) |
---|
1840 | { |
---|
1841 | return false; |
---|
1842 | } |
---|
1843 | return true; |
---|
1844 | } |
---|
1845 | |
---|
1846 | /** |
---|
1847 | * Retrieve data from external URL |
---|
1848 | * |
---|
1849 | * @param string $src: URL |
---|
1850 | * @param global $dest: can be a file ressource or string |
---|
1851 | * @return bool |
---|
1852 | */ |
---|
1853 | function fetchRemote($src, &$dest, $get_data=array(), $post_data=array(), $user_agent='Piwigo', $step=0) |
---|
1854 | { |
---|
1855 | // Try to retrieve data from local file? |
---|
1856 | if (!url_is_remote($src)) |
---|
1857 | { |
---|
1858 | $content = @file_get_contents($src); |
---|
1859 | if ($content !== false) |
---|
1860 | { |
---|
1861 | is_resource($dest) ? @fwrite($dest, $content) : $dest = $content; |
---|
1862 | return true; |
---|
1863 | } |
---|
1864 | else |
---|
1865 | { |
---|
1866 | return false; |
---|
1867 | } |
---|
1868 | } |
---|
1869 | |
---|
1870 | // After 3 redirections, return false |
---|
1871 | if ($step > 3) return false; |
---|
1872 | |
---|
1873 | // Initialization |
---|
1874 | $method = empty($post_data) ? 'GET' : 'POST'; |
---|
1875 | $request = empty($post_data) ? '' : http_build_query($post_data, '', '&'); |
---|
1876 | if (!empty($get_data)) |
---|
1877 | { |
---|
1878 | $src .= strpos($src, '?') === false ? '?' : '&'; |
---|
1879 | $src .= http_build_query($get_data, '', '&'); |
---|
1880 | } |
---|
1881 | |
---|
1882 | // Initialize $dest |
---|
1883 | is_resource($dest) or $dest = ''; |
---|
1884 | |
---|
1885 | // Try curl to read remote file |
---|
1886 | if (function_exists('curl_init')) |
---|
1887 | { |
---|
1888 | $ch = @curl_init(); |
---|
1889 | @curl_setopt($ch, CURLOPT_URL, $src); |
---|
1890 | @curl_setopt($ch, CURLOPT_HEADER, 1); |
---|
1891 | @curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); |
---|
1892 | @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
---|
1893 | if ($method == 'POST') |
---|
1894 | { |
---|
1895 | @curl_setopt($ch, CURLOPT_POST, 1); |
---|
1896 | @curl_setopt($ch, CURLOPT_POSTFIELDS, $request); |
---|
1897 | } |
---|
1898 | $content = @curl_exec($ch); |
---|
1899 | $header_length = @curl_getinfo($ch, CURLINFO_HEADER_SIZE); |
---|
1900 | $status = @curl_getinfo($ch, CURLINFO_HTTP_CODE); |
---|
1901 | @curl_close($ch); |
---|
1902 | if ($content !== false and $status >= 200 and $status < 400) |
---|
1903 | { |
---|
1904 | if (preg_match('/Location:\s+?(.+)/', substr($content, 0, $header_length), $m)) |
---|
1905 | { |
---|
1906 | return fetchRemote($m[1], $dest, array(), array(), $user_agent, $step+1); |
---|
1907 | } |
---|
1908 | $content = substr($content, $header_length); |
---|
1909 | is_resource($dest) ? @fwrite($dest, $content) : $dest = $content; |
---|
1910 | return true; |
---|
1911 | } |
---|
1912 | } |
---|
1913 | |
---|
1914 | // Try file_get_contents to read remote file |
---|
1915 | if (ini_get('allow_url_fopen')) |
---|
1916 | { |
---|
1917 | $opts = array( |
---|
1918 | 'http' => array( |
---|
1919 | 'method' => $method, |
---|
1920 | 'user_agent' => $user_agent, |
---|
1921 | ) |
---|
1922 | ); |
---|
1923 | if ($method == 'POST') |
---|
1924 | { |
---|
1925 | $opts['http']['content'] = $request; |
---|
1926 | } |
---|
1927 | $context = @stream_context_create($opts); |
---|
1928 | $content = @file_get_contents($src, false, $context); |
---|
1929 | if ($content !== false) |
---|
1930 | { |
---|
1931 | is_resource($dest) ? @fwrite($dest, $content) : $dest = $content; |
---|
1932 | return true; |
---|
1933 | } |
---|
1934 | } |
---|
1935 | |
---|
1936 | // Try fsockopen to read remote file |
---|
1937 | $src = parse_url($src); |
---|
1938 | $host = $src['host']; |
---|
1939 | $path = isset($src['path']) ? $src['path'] : '/'; |
---|
1940 | $path .= isset($src['query']) ? '?'.$src['query'] : ''; |
---|
1941 | |
---|
1942 | if (($s = @fsockopen($host,80,$errno,$errstr,5)) === false) |
---|
1943 | { |
---|
1944 | return false; |
---|
1945 | } |
---|
1946 | |
---|
1947 | $http_request = $method." ".$path." HTTP/1.0\r\n"; |
---|
1948 | $http_request .= "Host: ".$host."\r\n"; |
---|
1949 | if ($method == 'POST') |
---|
1950 | { |
---|
1951 | $http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n"; |
---|
1952 | $http_request .= "Content-Length: ".strlen($request)."\r\n"; |
---|
1953 | } |
---|
1954 | $http_request .= "User-Agent: ".$user_agent."\r\n"; |
---|
1955 | $http_request .= "Accept: */*\r\n"; |
---|
1956 | $http_request .= "\r\n"; |
---|
1957 | $http_request .= $request; |
---|
1958 | |
---|
1959 | fwrite($s, $http_request); |
---|
1960 | |
---|
1961 | $i = 0; |
---|
1962 | $in_content = false; |
---|
1963 | while (!feof($s)) |
---|
1964 | { |
---|
1965 | $line = fgets($s); |
---|
1966 | |
---|
1967 | if (rtrim($line,"\r\n") == '' && !$in_content) |
---|
1968 | { |
---|
1969 | $in_content = true; |
---|
1970 | $i++; |
---|
1971 | continue; |
---|
1972 | } |
---|
1973 | if ($i == 0) |
---|
1974 | { |
---|
1975 | if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/',rtrim($line,"\r\n"), $m)) |
---|
1976 | { |
---|
1977 | fclose($s); |
---|
1978 | return false; |
---|
1979 | } |
---|
1980 | $status = (integer) $m[2]; |
---|
1981 | if ($status < 200 || $status >= 400) |
---|
1982 | { |
---|
1983 | fclose($s); |
---|
1984 | return false; |
---|
1985 | } |
---|
1986 | } |
---|
1987 | if (!$in_content) |
---|
1988 | { |
---|
1989 | if (preg_match('/Location:\s+?(.+)$/',rtrim($line,"\r\n"),$m)) |
---|
1990 | { |
---|
1991 | fclose($s); |
---|
1992 | return fetchRemote(trim($m[1]),$dest,array(),array(),$user_agent,$step+1); |
---|
1993 | } |
---|
1994 | $i++; |
---|
1995 | continue; |
---|
1996 | } |
---|
1997 | is_resource($dest) ? @fwrite($dest, $line) : $dest .= $line; |
---|
1998 | $i++; |
---|
1999 | } |
---|
2000 | fclose($s); |
---|
2001 | return true; |
---|
2002 | } |
---|
2003 | |
---|
2004 | |
---|
2005 | /** |
---|
2006 | * returns the groupname corresponding to the given group identifier if |
---|
2007 | * exists |
---|
2008 | * |
---|
2009 | * @param int group_id |
---|
2010 | * @return mixed |
---|
2011 | */ |
---|
2012 | function get_groupname($group_id) |
---|
2013 | { |
---|
2014 | $query = ' |
---|
2015 | SELECT name |
---|
2016 | FROM '.GROUPS_TABLE.' |
---|
2017 | WHERE id = '.intval($group_id).' |
---|
2018 | ;'; |
---|
2019 | $result = pwg_query($query); |
---|
2020 | if (pwg_db_num_rows($result) > 0) |
---|
2021 | { |
---|
2022 | list($groupname) = pwg_db_fetch_row($result); |
---|
2023 | } |
---|
2024 | else |
---|
2025 | { |
---|
2026 | return false; |
---|
2027 | } |
---|
2028 | |
---|
2029 | return $groupname; |
---|
2030 | } |
---|
2031 | |
---|
2032 | /** |
---|
2033 | * returns the username corresponding to the given user identifier if exists |
---|
2034 | * |
---|
2035 | * @param int user_id |
---|
2036 | * @return mixed |
---|
2037 | */ |
---|
2038 | function get_username($user_id) |
---|
2039 | { |
---|
2040 | global $conf; |
---|
2041 | |
---|
2042 | $query = ' |
---|
2043 | SELECT '.$conf['user_fields']['username'].' |
---|
2044 | FROM '.USERS_TABLE.' |
---|
2045 | WHERE '.$conf['user_fields']['id'].' = '.intval($user_id).' |
---|
2046 | ;'; |
---|
2047 | $result = pwg_query($query); |
---|
2048 | if (pwg_db_num_rows($result) > 0) |
---|
2049 | { |
---|
2050 | list($username) = pwg_db_fetch_row($result); |
---|
2051 | } |
---|
2052 | else |
---|
2053 | { |
---|
2054 | return false; |
---|
2055 | } |
---|
2056 | |
---|
2057 | return stripslashes($username); |
---|
2058 | } |
---|
2059 | |
---|
2060 | function get_newsletter_subscribe_base_url($language) { |
---|
2061 | return PHPWG_URL.'/announcement/subscribe/'; |
---|
2062 | } |
---|
2063 | |
---|
2064 | /** |
---|
2065 | * Accordion menus need to know which section to open by default when |
---|
2066 | * loading the page |
---|
2067 | */ |
---|
2068 | function get_active_menu($menu_page) |
---|
2069 | { |
---|
2070 | global $page; |
---|
2071 | |
---|
2072 | if (isset($page['active_menu'])) |
---|
2073 | { |
---|
2074 | return $page['active_menu']; |
---|
2075 | } |
---|
2076 | |
---|
2077 | switch ($menu_page) |
---|
2078 | { |
---|
2079 | case 'photo': |
---|
2080 | case 'photos_add': |
---|
2081 | case 'rating': |
---|
2082 | case 'tags': |
---|
2083 | case 'batch_manager': |
---|
2084 | return 0; |
---|
2085 | |
---|
2086 | case 'album': |
---|
2087 | case 'cat_list': |
---|
2088 | case 'cat_move': |
---|
2089 | case 'cat_options': |
---|
2090 | case 'permalinks': |
---|
2091 | return 1; |
---|
2092 | |
---|
2093 | case 'user_list': |
---|
2094 | case 'user_perm': |
---|
2095 | case 'group_list': |
---|
2096 | case 'group_perm': |
---|
2097 | case 'notification_by_mail': |
---|
2098 | return 2; |
---|
2099 | |
---|
2100 | case 'plugins': |
---|
2101 | case 'plugin': |
---|
2102 | return 3; |
---|
2103 | |
---|
2104 | case 'site_manager': |
---|
2105 | case 'site_update': |
---|
2106 | case 'stats': |
---|
2107 | case 'history': |
---|
2108 | case 'maintenance': |
---|
2109 | case 'comments': |
---|
2110 | case 'updates': |
---|
2111 | return 4; |
---|
2112 | |
---|
2113 | case 'configuration': |
---|
2114 | case 'derivatives': |
---|
2115 | case 'extend_for_templates': |
---|
2116 | case 'menubar': |
---|
2117 | case 'themes': |
---|
2118 | case 'theme': |
---|
2119 | case 'languages': |
---|
2120 | return 5; |
---|
2121 | } |
---|
2122 | return 0; |
---|
2123 | } |
---|
2124 | |
---|
2125 | /* |
---|
2126 | * get tags list from SQL query (ids are surrounded by ~~, for get_tag_ids()) |
---|
2127 | * @param string: query |
---|
2128 | * @param boolean: only_user_language, if true, only local name is returned for multilingual tags |
---|
2129 | */ |
---|
2130 | function get_taglist($query, $only_user_language=true) |
---|
2131 | { |
---|
2132 | $result = pwg_query($query); |
---|
2133 | |
---|
2134 | $taglist = array(); |
---|
2135 | $altlist = array(); |
---|
2136 | while ($row = pwg_db_fetch_assoc($result)) |
---|
2137 | { |
---|
2138 | $raw_name = $row['name']; |
---|
2139 | $name = trigger_event('render_tag_name', $raw_name); |
---|
2140 | |
---|
2141 | $taglist[] = array( |
---|
2142 | 'name' => $name, |
---|
2143 | 'id' => '~~'.$row['id'].'~~', |
---|
2144 | ); |
---|
2145 | |
---|
2146 | if (!$only_user_language) |
---|
2147 | { |
---|
2148 | $alt_names = trigger_event('get_tag_alt_names', array(), $raw_name); |
---|
2149 | |
---|
2150 | foreach( array_diff( array_unique($alt_names), array($name) ) as $alt) |
---|
2151 | { |
---|
2152 | $altlist[] = array( |
---|
2153 | 'name' => $alt, |
---|
2154 | 'id' => '~~'.$row['id'].'~~', |
---|
2155 | ); |
---|
2156 | } |
---|
2157 | } |
---|
2158 | } |
---|
2159 | |
---|
2160 | usort($taglist, 'tag_alpha_compare'); |
---|
2161 | if (count($altlist)) |
---|
2162 | { |
---|
2163 | usort($altlist, 'tag_alpha_compare'); |
---|
2164 | $taglist = array_merge($taglist, $altlist); |
---|
2165 | } |
---|
2166 | |
---|
2167 | return $taglist; |
---|
2168 | } |
---|
2169 | |
---|
2170 | /* |
---|
2171 | * get tags ids from a list of raw tags (existing tags or new tags) |
---|
2172 | * @param mixed: raw_tags, array or comma separated string |
---|
2173 | * @param boolean: allow_create |
---|
2174 | */ |
---|
2175 | function get_tag_ids($raw_tags, $allow_create=true) |
---|
2176 | { |
---|
2177 | // In $raw_tags we receive something like array('~~6~~', '~~59~~', 'New |
---|
2178 | // tag', 'Another new tag') The ~~34~~ means that it is an existing |
---|
2179 | // tag. I've added the surrounding ~~ to permit creation of tags like "10" |
---|
2180 | // or "1234" (numeric characters only) |
---|
2181 | |
---|
2182 | $tag_ids = array(); |
---|
2183 | if (!is_array($raw_tags)) |
---|
2184 | { |
---|
2185 | $raw_tags = explode(',',$raw_tags); |
---|
2186 | } |
---|
2187 | |
---|
2188 | foreach ($raw_tags as $raw_tag) |
---|
2189 | { |
---|
2190 | if (preg_match('/^~~(\d+)~~$/', $raw_tag, $matches)) |
---|
2191 | { |
---|
2192 | $tag_ids[] = $matches[1]; |
---|
2193 | } |
---|
2194 | elseif ($allow_create) |
---|
2195 | { |
---|
2196 | // we have to create a new tag |
---|
2197 | $tag_ids[] = tag_id_from_tag_name($raw_tag); |
---|
2198 | } |
---|
2199 | } |
---|
2200 | |
---|
2201 | return $tag_ids; |
---|
2202 | } |
---|
2203 | |
---|
2204 | /** returns the argument_ids array with new sequenced keys based on related |
---|
2205 | * names. Sequence is not case sensitive. |
---|
2206 | * Warning: By definition, this function breaks original keys |
---|
2207 | */ |
---|
2208 | function order_by_name($element_ids,$name) |
---|
2209 | { |
---|
2210 | $ordered_element_ids = array(); |
---|
2211 | foreach ($element_ids as $k_id => $element_id) |
---|
2212 | { |
---|
2213 | $key = strtolower($name[$element_id]) .'-'. $name[$element_id] .'-'. $k_id; |
---|
2214 | $ordered_element_ids[$key] = $element_id; |
---|
2215 | } |
---|
2216 | ksort($ordered_element_ids); |
---|
2217 | return $ordered_element_ids; |
---|
2218 | } |
---|
2219 | |
---|
2220 | function add_permission_on_category($category_ids, $user_ids) |
---|
2221 | { |
---|
2222 | // array-ify categories and users |
---|
2223 | if (!is_array($category_ids)) |
---|
2224 | { |
---|
2225 | $category_ids = array($category_ids); |
---|
2226 | } |
---|
2227 | |
---|
2228 | if (!is_array($user_ids)) |
---|
2229 | { |
---|
2230 | $user_ids = array($user_ids); |
---|
2231 | } |
---|
2232 | |
---|
2233 | // check for emptiness |
---|
2234 | if (count($category_ids) == 0 or count($user_ids) == 0) |
---|
2235 | { |
---|
2236 | return; |
---|
2237 | } |
---|
2238 | |
---|
2239 | // make sure categories are private and select uppercats or subcats |
---|
2240 | $cat_ids = (isset($_POST['apply_on_sub'])) ? implode(',', get_subcat_ids($category_ids)).",".implode(',', get_uppercat_ids($category_ids)) : implode(',', get_uppercat_ids($category_ids)); |
---|
2241 | $query = ' |
---|
2242 | SELECT |
---|
2243 | id |
---|
2244 | FROM '.CATEGORIES_TABLE.' |
---|
2245 | WHERE id IN ('.$cat_ids.') |
---|
2246 | AND status = \'private\' |
---|
2247 | ;'; |
---|
2248 | $private_cats = array_from_query($query, 'id'); |
---|
2249 | |
---|
2250 | if (count($private_cats) == 0) |
---|
2251 | { |
---|
2252 | return; |
---|
2253 | } |
---|
2254 | |
---|
2255 | // We must not reinsert already existing lines in user_access table |
---|
2256 | $granteds = array(); |
---|
2257 | foreach ($private_cats as $cat_id) |
---|
2258 | { |
---|
2259 | $granteds[$cat_id] = array(); |
---|
2260 | } |
---|
2261 | |
---|
2262 | $query = ' |
---|
2263 | SELECT |
---|
2264 | user_id, |
---|
2265 | cat_id |
---|
2266 | FROM '.USER_ACCESS_TABLE.' |
---|
2267 | WHERE cat_id IN ('.implode(',', $private_cats).') |
---|
2268 | AND user_id IN ('.implode(',', $user_ids).') |
---|
2269 | ;'; |
---|
2270 | $result = pwg_query($query); |
---|
2271 | while ($row = pwg_db_fetch_assoc($result)) |
---|
2272 | { |
---|
2273 | array_push($granteds[$row['cat_id']], $row['user_id']); |
---|
2274 | } |
---|
2275 | |
---|
2276 | $inserts = array(); |
---|
2277 | |
---|
2278 | foreach ($private_cats as $cat_id) |
---|
2279 | { |
---|
2280 | $grant_to_users = array_diff($user_ids, $granteds[$cat_id]); |
---|
2281 | |
---|
2282 | foreach ($grant_to_users as $user_id) |
---|
2283 | { |
---|
2284 | array_push( |
---|
2285 | $inserts, |
---|
2286 | array( |
---|
2287 | 'user_id' => $user_id, |
---|
2288 | 'cat_id' => $cat_id |
---|
2289 | ) |
---|
2290 | ); |
---|
2291 | } |
---|
2292 | } |
---|
2293 | |
---|
2294 | if (count($inserts) > 0) |
---|
2295 | { |
---|
2296 | mass_inserts(USER_ACCESS_TABLE, array_keys($inserts[0]), $inserts); |
---|
2297 | } |
---|
2298 | } |
---|
2299 | |
---|
2300 | |
---|
2301 | function get_admins($include_webmaster=true) |
---|
2302 | { |
---|
2303 | $status_list = array('admin'); |
---|
2304 | |
---|
2305 | if ($include_webmaster) |
---|
2306 | { |
---|
2307 | $status_list[] = 'webmaster'; |
---|
2308 | } |
---|
2309 | |
---|
2310 | $query = ' |
---|
2311 | SELECT |
---|
2312 | user_id |
---|
2313 | FROM '.USER_INFOS_TABLE.' |
---|
2314 | WHERE status in (\''.implode("','", $status_list).'\') |
---|
2315 | ;'; |
---|
2316 | |
---|
2317 | return array_from_query($query, 'user_id'); |
---|
2318 | } |
---|
2319 | |
---|
2320 | /** delete all derivative files for one or several types */ |
---|
2321 | function clear_derivative_cache($types='all') |
---|
2322 | { |
---|
2323 | if ($types === 'all') |
---|
2324 | { |
---|
2325 | $types = ImageStdParams::get_all_types(); |
---|
2326 | $types[] = IMG_CUSTOM; |
---|
2327 | } |
---|
2328 | elseif (!is_array($types)) |
---|
2329 | { |
---|
2330 | $types = array($types); |
---|
2331 | } |
---|
2332 | |
---|
2333 | for ($i=0; $i<count($types); $i++) |
---|
2334 | { |
---|
2335 | $type = $types[$i]; |
---|
2336 | if ($type == IMG_CUSTOM) |
---|
2337 | { |
---|
2338 | $type = derivative_to_url($type).'[a-zA-Z0-9]+'; |
---|
2339 | } |
---|
2340 | elseif (in_array($type, ImageStdParams::get_all_types())) |
---|
2341 | { |
---|
2342 | $type = derivative_to_url($type); |
---|
2343 | } |
---|
2344 | else |
---|
2345 | {//assume a custom type |
---|
2346 | $type = derivative_to_url(IMG_CUSTOM).'_'.$type; |
---|
2347 | } |
---|
2348 | $types[$i] = $type; |
---|
2349 | } |
---|
2350 | |
---|
2351 | $pattern='#.*-'; |
---|
2352 | if (count($types)>1) |
---|
2353 | { |
---|
2354 | $pattern .= '(' . implode('|',$types) . ')'; |
---|
2355 | } |
---|
2356 | else |
---|
2357 | { |
---|
2358 | $pattern .= $types[0]; |
---|
2359 | } |
---|
2360 | $pattern.='\.[a-zA-Z0-9]{3,4}$#'; |
---|
2361 | |
---|
2362 | if ($contents = @opendir(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR)) |
---|
2363 | { |
---|
2364 | while (($node = readdir($contents)) !== false) |
---|
2365 | { |
---|
2366 | if ($node != '.' |
---|
2367 | and $node != '..' |
---|
2368 | and is_dir(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$node)) |
---|
2369 | { |
---|
2370 | clear_derivative_cache_rec(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$node, $pattern); |
---|
2371 | } |
---|
2372 | } |
---|
2373 | closedir($contents); |
---|
2374 | } |
---|
2375 | } |
---|
2376 | |
---|
2377 | function clear_derivative_cache_rec($path, $pattern) |
---|
2378 | { |
---|
2379 | $rmdir = true; |
---|
2380 | $rm_index = false; |
---|
2381 | |
---|
2382 | if ($contents = opendir($path)) |
---|
2383 | { |
---|
2384 | while (($node = readdir($contents)) !== false) |
---|
2385 | { |
---|
2386 | if ($node == '.' or $node == '..') |
---|
2387 | continue; |
---|
2388 | if (is_dir($path.'/'.$node)) |
---|
2389 | { |
---|
2390 | $rmdir &= clear_derivative_cache_rec($path.'/'.$node, $pattern); |
---|
2391 | } |
---|
2392 | else |
---|
2393 | { |
---|
2394 | if (preg_match($pattern, $node)) |
---|
2395 | { |
---|
2396 | unlink($path.'/'.$node); |
---|
2397 | } |
---|
2398 | elseif ($node=='index.htm') |
---|
2399 | { |
---|
2400 | $rm_index = true; |
---|
2401 | } |
---|
2402 | else |
---|
2403 | { |
---|
2404 | $rmdir = false; |
---|
2405 | } |
---|
2406 | } |
---|
2407 | } |
---|
2408 | closedir($contents); |
---|
2409 | |
---|
2410 | if ($rmdir) |
---|
2411 | { |
---|
2412 | if ($rm_index) |
---|
2413 | { |
---|
2414 | unlink($path.'/index.htm'); |
---|
2415 | } |
---|
2416 | clearstatcache(); |
---|
2417 | @rmdir($path); |
---|
2418 | } |
---|
2419 | return $rmdir; |
---|
2420 | } |
---|
2421 | } |
---|
2422 | |
---|
2423 | function delete_element_derivatives($infos, $type='all') |
---|
2424 | { |
---|
2425 | $path = $infos['path']; |
---|
2426 | if (!empty($infos['representative_ext'])) |
---|
2427 | { |
---|
2428 | $path = original_to_representative( $path, $infos['representative_ext']); |
---|
2429 | } |
---|
2430 | if (substr_compare($path, '../', 0, 3)==0) |
---|
2431 | { |
---|
2432 | $path = substr($path, 3); |
---|
2433 | } |
---|
2434 | $dot = strrpos($path, '.'); |
---|
2435 | if ($type=='all') |
---|
2436 | { |
---|
2437 | $pattern = '-*'; |
---|
2438 | } |
---|
2439 | else |
---|
2440 | { |
---|
2441 | $pattern = '-'.derivative_to_url($type).'*'; |
---|
2442 | } |
---|
2443 | $path = substr_replace($path, $pattern, $dot, 0); |
---|
2444 | if ( ($glob=glob(PHPWG_ROOT_PATH.PWG_DERIVATIVE_DIR.$path)) !== false) |
---|
2445 | { |
---|
2446 | foreach( $glob as $file) |
---|
2447 | { |
---|
2448 | @unlink($file); |
---|
2449 | } |
---|
2450 | } |
---|
2451 | } |
---|
2452 | |
---|
2453 | /** |
---|
2454 | * returns an array contening sub-directories, excluding ".svn" |
---|
2455 | * |
---|
2456 | * @param string $dir |
---|
2457 | * @return array |
---|
2458 | */ |
---|
2459 | function get_dirs($directory) |
---|
2460 | { |
---|
2461 | $sub_dirs = array(); |
---|
2462 | if ($opendir = opendir($directory)) |
---|
2463 | { |
---|
2464 | while ($file = readdir($opendir)) |
---|
2465 | { |
---|
2466 | if ($file != '.' |
---|
2467 | and $file != '..' |
---|
2468 | and is_dir($directory.'/'.$file) |
---|
2469 | and $file != '.svn') |
---|
2470 | { |
---|
2471 | array_push($sub_dirs, $file); |
---|
2472 | } |
---|
2473 | } |
---|
2474 | closedir($opendir); |
---|
2475 | } |
---|
2476 | return $sub_dirs; |
---|
2477 | } |
---|
2478 | ?> |
---|