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-2006 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2006-07-09 09:08:19 +0000 (Sun, 09 Jul 2006) $ |
---|
10 | // | last modifier : $Author: rub $ |
---|
11 | // | revision : $Revision: 1453 $ |
---|
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 | /** |
---|
31 | * returns an array with all picture files according to $conf['file_ext'] |
---|
32 | * |
---|
33 | * @param string $dir |
---|
34 | * @return array |
---|
35 | */ |
---|
36 | function get_pwg_files($dir) |
---|
37 | { |
---|
38 | global $conf; |
---|
39 | |
---|
40 | $pictures = array(); |
---|
41 | if ($opendir = opendir($dir)) |
---|
42 | { |
---|
43 | while ($file = readdir($opendir)) |
---|
44 | { |
---|
45 | if (in_array(get_extension($file), $conf['file_ext'])) |
---|
46 | { |
---|
47 | array_push($pictures, $file); |
---|
48 | } |
---|
49 | } |
---|
50 | } |
---|
51 | return $pictures; |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * returns an array with all thumbnails according to $conf['picture_ext'] |
---|
56 | * and $conf['prefix_thumbnail'] |
---|
57 | * |
---|
58 | * @param string $dir |
---|
59 | * @return array |
---|
60 | */ |
---|
61 | function get_thumb_files($dir) |
---|
62 | { |
---|
63 | global $conf; |
---|
64 | |
---|
65 | $prefix_length = strlen($conf['prefix_thumbnail']); |
---|
66 | |
---|
67 | $thumbnails = array(); |
---|
68 | if ($opendir = @opendir($dir.'/thumbnail')) |
---|
69 | { |
---|
70 | while ($file = readdir($opendir)) |
---|
71 | { |
---|
72 | if (in_array(get_extension($file), $conf['picture_ext']) |
---|
73 | and substr($file, 0, $prefix_length) == $conf['prefix_thumbnail']) |
---|
74 | { |
---|
75 | array_push($thumbnails, $file); |
---|
76 | } |
---|
77 | } |
---|
78 | } |
---|
79 | return $thumbnails; |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * returns an array with representative picture files of a directory |
---|
84 | * according to $conf['picture_ext'] |
---|
85 | * |
---|
86 | * @param string $dir |
---|
87 | * @return array |
---|
88 | */ |
---|
89 | function get_representative_files($dir) |
---|
90 | { |
---|
91 | global $conf; |
---|
92 | |
---|
93 | $pictures = array(); |
---|
94 | if ($opendir = @opendir($dir.'/pwg_representative')) |
---|
95 | { |
---|
96 | while ($file = readdir($opendir)) |
---|
97 | { |
---|
98 | if (in_array(get_extension($file), $conf['picture_ext'])) |
---|
99 | { |
---|
100 | array_push($pictures, $file); |
---|
101 | } |
---|
102 | } |
---|
103 | } |
---|
104 | return $pictures; |
---|
105 | } |
---|
106 | |
---|
107 | // The function delete_site deletes a site and call the function |
---|
108 | // delete_categories for each primary category of the site |
---|
109 | function delete_site( $id ) |
---|
110 | { |
---|
111 | // destruction of the categories of the site |
---|
112 | $query = ' |
---|
113 | SELECT id |
---|
114 | FROM '.CATEGORIES_TABLE.' |
---|
115 | WHERE site_id = '.$id.' |
---|
116 | ;'; |
---|
117 | $result = pwg_query($query); |
---|
118 | $category_ids = array(); |
---|
119 | while ($row = mysql_fetch_array($result)) |
---|
120 | { |
---|
121 | array_push($category_ids, $row['id']); |
---|
122 | } |
---|
123 | delete_categories($category_ids); |
---|
124 | |
---|
125 | // destruction of the site |
---|
126 | $query = ' |
---|
127 | DELETE FROM '.SITES_TABLE.' |
---|
128 | WHERE id = '.$id.' |
---|
129 | ;'; |
---|
130 | pwg_query($query); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | // The function delete_categories deletes the categories identified by the |
---|
135 | // (numeric) key of the array $ids. It also deletes (in the database) : |
---|
136 | // - all the elements of the category (delete_elements, see further) |
---|
137 | // - all the links between elements and this category |
---|
138 | // - all the restrictions linked to the category |
---|
139 | // The function works recursively. |
---|
140 | function delete_categories($ids) |
---|
141 | { |
---|
142 | global $counts; |
---|
143 | |
---|
144 | if (count($ids) == 0) |
---|
145 | { |
---|
146 | return; |
---|
147 | } |
---|
148 | |
---|
149 | // add sub-category ids to the given ids : if a category is deleted, all |
---|
150 | // sub-categories must be so |
---|
151 | $ids = get_subcat_ids($ids); |
---|
152 | |
---|
153 | // destruction of all the related elements |
---|
154 | $query = ' |
---|
155 | SELECT id |
---|
156 | FROM '.IMAGES_TABLE.' |
---|
157 | WHERE storage_category_id IN ( |
---|
158 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
159 | ;'; |
---|
160 | $result = pwg_query($query); |
---|
161 | $element_ids = array(); |
---|
162 | while ($row = mysql_fetch_array($result)) |
---|
163 | { |
---|
164 | array_push($element_ids, $row['id']); |
---|
165 | } |
---|
166 | delete_elements($element_ids); |
---|
167 | |
---|
168 | // destruction of the links between images and this category |
---|
169 | $query = ' |
---|
170 | DELETE FROM '.IMAGE_CATEGORY_TABLE.' |
---|
171 | WHERE category_id IN ( |
---|
172 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
173 | ;'; |
---|
174 | pwg_query($query); |
---|
175 | |
---|
176 | // destruction of the access linked to the category |
---|
177 | $query = ' |
---|
178 | DELETE FROM '.USER_ACCESS_TABLE.' |
---|
179 | WHERE cat_id IN ( |
---|
180 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
181 | ;'; |
---|
182 | pwg_query($query); |
---|
183 | |
---|
184 | $query = ' |
---|
185 | DELETE FROM '.GROUP_ACCESS_TABLE.' |
---|
186 | WHERE cat_id IN ( |
---|
187 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
188 | ;'; |
---|
189 | pwg_query($query); |
---|
190 | |
---|
191 | // destruction of the category |
---|
192 | $query = ' |
---|
193 | DELETE FROM '.CATEGORIES_TABLE.' |
---|
194 | WHERE id IN ( |
---|
195 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
196 | ;'; |
---|
197 | pwg_query($query); |
---|
198 | |
---|
199 | if (isset($counts['del_categories'])) |
---|
200 | { |
---|
201 | $counts['del_categories']+= count($ids); |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | // The function delete_elements deletes the elements identified by the |
---|
206 | // (numeric) values of the array $ids. It also deletes (in the database) : |
---|
207 | // - all the comments related to elements |
---|
208 | // - all the links between categories and elements |
---|
209 | // - all the favorites associated to elements |
---|
210 | function delete_elements($ids) |
---|
211 | { |
---|
212 | global $counts; |
---|
213 | |
---|
214 | if (count($ids) == 0) |
---|
215 | { |
---|
216 | return; |
---|
217 | } |
---|
218 | |
---|
219 | // destruction of the comments on the image |
---|
220 | $query = ' |
---|
221 | DELETE FROM '.COMMENTS_TABLE.' |
---|
222 | WHERE image_id IN ( |
---|
223 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
224 | ;'; |
---|
225 | pwg_query($query); |
---|
226 | |
---|
227 | // destruction of the links between images and this category |
---|
228 | $query = ' |
---|
229 | DELETE FROM '.IMAGE_CATEGORY_TABLE.' |
---|
230 | WHERE image_id IN ( |
---|
231 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
232 | ;'; |
---|
233 | pwg_query($query); |
---|
234 | |
---|
235 | // destruction of the links between images and tags |
---|
236 | $query = ' |
---|
237 | DELETE FROM '.IMAGE_TAG_TABLE.' |
---|
238 | WHERE image_id IN ( |
---|
239 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
240 | ;'; |
---|
241 | pwg_query($query); |
---|
242 | |
---|
243 | // destruction of the favorites associated with the picture |
---|
244 | $query = ' |
---|
245 | DELETE FROM '.FAVORITES_TABLE.' |
---|
246 | WHERE image_id IN ( |
---|
247 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
248 | ;'; |
---|
249 | pwg_query($query); |
---|
250 | |
---|
251 | // destruction of the rates associated to this element |
---|
252 | $query = ' |
---|
253 | DELETE FROM '.RATE_TABLE.' |
---|
254 | WHERE element_id IN ( |
---|
255 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
256 | ;'; |
---|
257 | pwg_query($query); |
---|
258 | |
---|
259 | // destruction of the rates associated to this element |
---|
260 | $query = ' |
---|
261 | DELETE FROM '.CADDIE_TABLE.' |
---|
262 | WHERE element_id IN ( |
---|
263 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
264 | ;'; |
---|
265 | pwg_query($query); |
---|
266 | |
---|
267 | // destruction of the image |
---|
268 | $query = ' |
---|
269 | DELETE FROM '.IMAGES_TABLE.' |
---|
270 | WHERE id IN ( |
---|
271 | '.wordwrap(implode(', ', $ids), 80, "\n").') |
---|
272 | ;'; |
---|
273 | pwg_query($query); |
---|
274 | |
---|
275 | if (isset($counts['del_elements'])) |
---|
276 | { |
---|
277 | $counts['del_elements']+= count($ids); |
---|
278 | } |
---|
279 | } |
---|
280 | |
---|
281 | // The delete_user function delete a user identified by the $user_id |
---|
282 | // It also deletes : |
---|
283 | // - all the access linked to this user |
---|
284 | // - all the links to any group |
---|
285 | // - all the favorites linked to this user |
---|
286 | // - calculated permissions linked to the user |
---|
287 | // - all datas about notifications for the user |
---|
288 | function delete_user($user_id) |
---|
289 | { |
---|
290 | global $conf; |
---|
291 | |
---|
292 | // destruction of the access linked to the user |
---|
293 | $query = ' |
---|
294 | DELETE FROM '.USER_ACCESS_TABLE.' |
---|
295 | WHERE user_id = '.$user_id.' |
---|
296 | ;'; |
---|
297 | pwg_query($query); |
---|
298 | |
---|
299 | // destruction of data notification by mail for this user |
---|
300 | $query = ' |
---|
301 | DELETE FROM '.USER_MAIL_NOTIFICATION_TABLE.' |
---|
302 | WHERE user_id = '.$user_id.' |
---|
303 | ;'; |
---|
304 | pwg_query($query); |
---|
305 | |
---|
306 | // destruction of data RSS notification for this user |
---|
307 | $query = ' |
---|
308 | DELETE FROM '.USER_FEED_TABLE.' |
---|
309 | WHERE user_id = '.$user_id.' |
---|
310 | ;'; |
---|
311 | pwg_query($query); |
---|
312 | |
---|
313 | // destruction of the group links for this user |
---|
314 | $query = ' |
---|
315 | DELETE FROM '.USER_GROUP_TABLE.' |
---|
316 | WHERE user_id = '.$user_id.' |
---|
317 | ;'; |
---|
318 | pwg_query($query); |
---|
319 | |
---|
320 | // destruction of the favorites associated with the user |
---|
321 | $query = ' |
---|
322 | DELETE FROM '.FAVORITES_TABLE.' |
---|
323 | WHERE user_id = '.$user_id.' |
---|
324 | ;'; |
---|
325 | pwg_query($query); |
---|
326 | |
---|
327 | // deletion of calculated permissions linked to the user |
---|
328 | $query = ' |
---|
329 | DELETE FROM '.USER_CACHE_TABLE.' |
---|
330 | WHERE user_id = '.$user_id.' |
---|
331 | ;'; |
---|
332 | pwg_query($query); |
---|
333 | |
---|
334 | // deletion of phpwebgallery specific informations |
---|
335 | $query = ' |
---|
336 | DELETE FROM '.USER_INFOS_TABLE.' |
---|
337 | WHERE user_id = '.$user_id.' |
---|
338 | ;'; |
---|
339 | pwg_query($query); |
---|
340 | |
---|
341 | // destruction of the user |
---|
342 | $query = ' |
---|
343 | DELETE FROM '.USERS_TABLE.' |
---|
344 | WHERE '.$conf['user_fields']['id'].' = '.$user_id.' |
---|
345 | ;'; |
---|
346 | pwg_query($query); |
---|
347 | } |
---|
348 | |
---|
349 | /** |
---|
350 | * updates calculated informations about a set of categories : date_last and |
---|
351 | * nb_images. It also verifies that the representative picture is really |
---|
352 | * linked to the category. Optionnaly recursive. |
---|
353 | * |
---|
354 | * @param mixed category id |
---|
355 | * @param boolean recursive |
---|
356 | * @returns void |
---|
357 | */ |
---|
358 | function update_category($ids = 'all', $recursive = false) |
---|
359 | { |
---|
360 | global $conf; |
---|
361 | |
---|
362 | // retrieving all categories to update |
---|
363 | $cat_ids = array(); |
---|
364 | |
---|
365 | $query = ' |
---|
366 | SELECT id |
---|
367 | FROM '.CATEGORIES_TABLE; |
---|
368 | if (is_array($ids)) |
---|
369 | { |
---|
370 | if ($recursive) |
---|
371 | { |
---|
372 | foreach ($ids as $num => $id) |
---|
373 | { |
---|
374 | if ($num == 0) |
---|
375 | { |
---|
376 | $query.= ' |
---|
377 | WHERE '; |
---|
378 | } |
---|
379 | else |
---|
380 | { |
---|
381 | $query.= ' |
---|
382 | OR '; |
---|
383 | } |
---|
384 | $query.= 'uppercats REGEXP \'(^|,)'.$id.'(,|$)\''; |
---|
385 | } |
---|
386 | } |
---|
387 | else |
---|
388 | { |
---|
389 | $query.= ' |
---|
390 | WHERE id IN ('.wordwrap(implode(', ', $ids), 80, "\n").')'; |
---|
391 | } |
---|
392 | } |
---|
393 | $query.= ' |
---|
394 | ;'; |
---|
395 | $cat_ids = array_unique(array_from_query($query, 'id')); |
---|
396 | |
---|
397 | if (count($cat_ids) == 0) |
---|
398 | { |
---|
399 | return false; |
---|
400 | } |
---|
401 | |
---|
402 | // calculate informations about categories retrieved |
---|
403 | $query = ' |
---|
404 | SELECT category_id, |
---|
405 | COUNT(image_id) AS nb_images, |
---|
406 | MAX(date_available) AS date_last |
---|
407 | FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id = image_id |
---|
408 | WHERE category_id IN ('.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
409 | GROUP BY category_id |
---|
410 | ;'; |
---|
411 | $result = pwg_query($query); |
---|
412 | $datas = array(); |
---|
413 | $query_ids = array(); |
---|
414 | while ( $row = mysql_fetch_array( $result ) ) |
---|
415 | { |
---|
416 | array_push($query_ids, $row['category_id']); |
---|
417 | |
---|
418 | array_push( |
---|
419 | $datas, |
---|
420 | array( |
---|
421 | 'id' => $row['category_id'], |
---|
422 | 'date_last' => $row['date_last'], |
---|
423 | 'nb_images' => $row['nb_images'] |
---|
424 | ) |
---|
425 | ); |
---|
426 | } |
---|
427 | // if all links between a category and elements have disappeared, no line |
---|
428 | // is returned but the update must be done ! |
---|
429 | foreach (array_diff($cat_ids, $query_ids) as $id) |
---|
430 | { |
---|
431 | array_push($datas, array('id' => $id, 'nb_images' => 0)); |
---|
432 | } |
---|
433 | |
---|
434 | $fields = array('primary' => array('id'), |
---|
435 | 'update' => array('date_last', 'nb_images')); |
---|
436 | mass_updates(CATEGORIES_TABLE, $fields, $datas); |
---|
437 | |
---|
438 | // representative pictures |
---|
439 | if (count($cat_ids) > 0) |
---|
440 | { |
---|
441 | // find all categories where the setted representative is not possible : |
---|
442 | // the picture does not exist |
---|
443 | $query = ' |
---|
444 | SELECT c.id |
---|
445 | FROM '.CATEGORIES_TABLE.' AS c LEFT JOIN '.IMAGES_TABLE.' AS i |
---|
446 | ON c.representative_picture_id = i.id |
---|
447 | WHERE representative_picture_id IS NOT NULL |
---|
448 | AND c.id IN ('.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
449 | AND i.id IS NULL |
---|
450 | ;'; |
---|
451 | $wrong_representant = array_from_query($query, 'id'); |
---|
452 | |
---|
453 | if ($conf['allow_random_representative']) |
---|
454 | { |
---|
455 | if (count($wrong_representant) > 0) |
---|
456 | { |
---|
457 | $query = ' |
---|
458 | UPDATE '.CATEGORIES_TABLE.' |
---|
459 | SET representative_picture_id = NULL |
---|
460 | WHERE id IN ('.wordwrap(implode(', ', $wrong_representant), 80, "\n").') |
---|
461 | ;'; |
---|
462 | pwg_query($query); |
---|
463 | } |
---|
464 | } |
---|
465 | else |
---|
466 | { |
---|
467 | $to_null = array(); |
---|
468 | $to_rand = array(); |
---|
469 | |
---|
470 | if (count($wrong_representant) > 0) |
---|
471 | { |
---|
472 | // among the categories with an unknown representant, we dissociate |
---|
473 | // categories containing pictures and categories containing no |
---|
474 | // pictures. Indeed, the representant must set to NULL if no picture |
---|
475 | // in the category and set to a random picture otherwise. |
---|
476 | $query = ' |
---|
477 | SELECT id |
---|
478 | FROM '.CATEGORIES_TABLE.' |
---|
479 | WHERE id IN ('.wordwrap(implode(', ', $wrong_representant), 80, "\n").') |
---|
480 | AND nb_images = 0 |
---|
481 | ;'; |
---|
482 | $to_null = array_from_query($query, 'id'); |
---|
483 | $to_rand = array_diff($wrong_representant, $to_null); |
---|
484 | } |
---|
485 | |
---|
486 | if (count($to_null) > 0) |
---|
487 | { |
---|
488 | $query = ' |
---|
489 | UPDATE '.CATEGORIES_TABLE.' |
---|
490 | SET representative_picture_id = NULL |
---|
491 | WHERE id IN ('.wordwrap(implode(', ', $to_null), 80, "\n").') |
---|
492 | ;'; |
---|
493 | pwg_query($query); |
---|
494 | } |
---|
495 | |
---|
496 | // If the random representant is not allowed, we need to find |
---|
497 | // categories with elements and with no representant. Those categories |
---|
498 | // must be added to the list of categories to set to a random |
---|
499 | // representant. |
---|
500 | $query = ' |
---|
501 | SELECT id |
---|
502 | FROM '.CATEGORIES_TABLE.' |
---|
503 | WHERE representative_picture_id IS NULL |
---|
504 | AND nb_images != 0 |
---|
505 | AND id IN ('.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
506 | ;'; |
---|
507 | $to_rand = |
---|
508 | array_unique( |
---|
509 | array_merge( |
---|
510 | $to_rand, |
---|
511 | array_from_query($query, 'id') |
---|
512 | ) |
---|
513 | ); |
---|
514 | |
---|
515 | if (count($to_rand) > 0) |
---|
516 | { |
---|
517 | set_random_representant($to_rand); |
---|
518 | } |
---|
519 | } |
---|
520 | } |
---|
521 | } |
---|
522 | |
---|
523 | function date_convert_back( $date ) |
---|
524 | { |
---|
525 | // date arrives at this format : YYYY-MM-DD |
---|
526 | // It must be transformed in DD/MM/YYYY |
---|
527 | if ( $date != '' ) |
---|
528 | { |
---|
529 | list($year,$month,$day) = explode( '-', $date ); |
---|
530 | return $day.'/'.$month.'/'.$year; |
---|
531 | } |
---|
532 | else |
---|
533 | { |
---|
534 | return ''; |
---|
535 | } |
---|
536 | } |
---|
537 | |
---|
538 | /** |
---|
539 | * returns an array containing sub-directories which can be a category, |
---|
540 | * recursive by default |
---|
541 | * |
---|
542 | * directories nammed "thumbnail", "pwg_high" or "pwg_representative" are |
---|
543 | * omitted |
---|
544 | * |
---|
545 | * @param string $basedir |
---|
546 | * @return array |
---|
547 | */ |
---|
548 | function get_fs_directories($path, $recursive = true) |
---|
549 | { |
---|
550 | $dirs = array(); |
---|
551 | |
---|
552 | if (is_dir($path)) |
---|
553 | { |
---|
554 | if ($contents = opendir($path)) |
---|
555 | { |
---|
556 | while (($node = readdir($contents)) !== false) |
---|
557 | { |
---|
558 | if (is_dir($path.'/'.$node) |
---|
559 | and $node != '.' |
---|
560 | and $node != '..' |
---|
561 | and $node != '.svn' |
---|
562 | and $node != 'thumbnail' |
---|
563 | and $node != 'pwg_high' |
---|
564 | and $node != 'pwg_representative') |
---|
565 | { |
---|
566 | array_push($dirs, $path.'/'.$node); |
---|
567 | if ($recursive) |
---|
568 | { |
---|
569 | $dirs = array_merge($dirs, get_fs_directories($path.'/'.$node)); |
---|
570 | } |
---|
571 | } |
---|
572 | } |
---|
573 | } |
---|
574 | } |
---|
575 | |
---|
576 | return $dirs; |
---|
577 | } |
---|
578 | |
---|
579 | /** |
---|
580 | * inserts multiple lines in a table |
---|
581 | * |
---|
582 | * @param string table_name |
---|
583 | * @param array dbfields |
---|
584 | * @param array inserts |
---|
585 | * @return void |
---|
586 | */ |
---|
587 | function mass_inserts($table_name, $dbfields, $datas) |
---|
588 | { |
---|
589 | // inserts all found categories |
---|
590 | $query = ' |
---|
591 | INSERT INTO '.$table_name.' |
---|
592 | ('.implode(',', $dbfields).') |
---|
593 | VALUES'; |
---|
594 | foreach ($datas as $insert_id => $insert) |
---|
595 | { |
---|
596 | $query.= ' |
---|
597 | '; |
---|
598 | if ($insert_id > 0) |
---|
599 | { |
---|
600 | $query.= ','; |
---|
601 | } |
---|
602 | $query.= '('; |
---|
603 | foreach ($dbfields as $field_id => $dbfield) |
---|
604 | { |
---|
605 | if ($field_id > 0) |
---|
606 | { |
---|
607 | $query.= ','; |
---|
608 | } |
---|
609 | |
---|
610 | if (!isset($insert[$dbfield]) or $insert[$dbfield] === '') |
---|
611 | { |
---|
612 | $query.= 'NULL'; |
---|
613 | } |
---|
614 | else |
---|
615 | { |
---|
616 | $query.= "'".$insert[$dbfield]."'"; |
---|
617 | } |
---|
618 | } |
---|
619 | $query.=')'; |
---|
620 | } |
---|
621 | $query.= ' |
---|
622 | ;'; |
---|
623 | pwg_query($query); |
---|
624 | } |
---|
625 | |
---|
626 | /** |
---|
627 | * updates multiple lines in a table |
---|
628 | * |
---|
629 | * @param string table_name |
---|
630 | * @param array dbfields |
---|
631 | * @param array datas |
---|
632 | * @return void |
---|
633 | */ |
---|
634 | function mass_updates($tablename, $dbfields, $datas) |
---|
635 | { |
---|
636 | // depending on the MySQL version, we use the multi table update or N |
---|
637 | // update queries |
---|
638 | $query = 'SELECT VERSION() AS version;'; |
---|
639 | list($mysql_version) = mysql_fetch_array(pwg_query($query)); |
---|
640 | if (count($datas) < 10 or version_compare($mysql_version, '4.0.4') < 0) |
---|
641 | { |
---|
642 | // MySQL is prior to version 4.0.4, multi table update feature is not |
---|
643 | // available |
---|
644 | foreach ($datas as $data) |
---|
645 | { |
---|
646 | $query = ' |
---|
647 | UPDATE '.$tablename.' |
---|
648 | SET '; |
---|
649 | $is_first = true; |
---|
650 | foreach ($dbfields['update'] as $num => $key) |
---|
651 | { |
---|
652 | if (!$is_first) |
---|
653 | { |
---|
654 | $query.= ",\n "; |
---|
655 | } |
---|
656 | $query.= $key.' = '; |
---|
657 | if (isset($data[$key]) and $data[$key] != '') |
---|
658 | { |
---|
659 | $query.= '\''.$data[$key].'\''; |
---|
660 | } |
---|
661 | else |
---|
662 | { |
---|
663 | $query.= 'NULL'; |
---|
664 | } |
---|
665 | $is_first = false; |
---|
666 | } |
---|
667 | $query.= ' |
---|
668 | WHERE '; |
---|
669 | foreach ($dbfields['primary'] as $num => $key) |
---|
670 | { |
---|
671 | if ($num > 1) |
---|
672 | { |
---|
673 | $query.= ' AND '; |
---|
674 | } |
---|
675 | $query.= $key.' = \''.$data[$key].'\''; |
---|
676 | } |
---|
677 | $query.= ' |
---|
678 | ;'; |
---|
679 | pwg_query($query); |
---|
680 | } |
---|
681 | } |
---|
682 | else |
---|
683 | { |
---|
684 | // creation of the temporary table |
---|
685 | $query = ' |
---|
686 | SHOW FULL COLUMNS FROM '.$tablename.' |
---|
687 | ;'; |
---|
688 | $result = pwg_query($query); |
---|
689 | $columns = array(); |
---|
690 | $all_fields = array_merge($dbfields['primary'], $dbfields['update']); |
---|
691 | while ($row = mysql_fetch_array($result)) |
---|
692 | { |
---|
693 | if (in_array($row['Field'], $all_fields)) |
---|
694 | { |
---|
695 | $column = $row['Field']; |
---|
696 | $column.= ' '.$row['Type']; |
---|
697 | if (!isset($row['Null']) or $row['Null'] == '') |
---|
698 | { |
---|
699 | $column.= ' NOT NULL'; |
---|
700 | } |
---|
701 | if (isset($row['Default'])) |
---|
702 | { |
---|
703 | $column.= " default '".$row['Default']."'"; |
---|
704 | } |
---|
705 | if (isset($row['Collation']) and $row['Collation'] != 'NULL') |
---|
706 | { |
---|
707 | $column.= " collate '".$row['Collation']."'"; |
---|
708 | } |
---|
709 | array_push($columns, $column); |
---|
710 | } |
---|
711 | } |
---|
712 | |
---|
713 | $temporary_tablename = $tablename.'_'.micro_seconds(); |
---|
714 | |
---|
715 | $query = ' |
---|
716 | CREATE TABLE '.$temporary_tablename.' |
---|
717 | ( |
---|
718 | '.implode(",\n", $columns).', |
---|
719 | PRIMARY KEY ('.implode(',', $dbfields['primary']).') |
---|
720 | ) |
---|
721 | ;'; |
---|
722 | pwg_query($query); |
---|
723 | mass_inserts($temporary_tablename, $all_fields, $datas); |
---|
724 | // update of images table by joining with temporary table |
---|
725 | $query = ' |
---|
726 | UPDATE '.$tablename.' AS t1, '.$temporary_tablename.' AS t2 |
---|
727 | SET '. |
---|
728 | implode( |
---|
729 | "\n , ", |
---|
730 | array_map( |
---|
731 | create_function('$s', 'return "t1.$s = t2.$s";'), |
---|
732 | $dbfields['update'] |
---|
733 | ) |
---|
734 | ).' |
---|
735 | WHERE '. |
---|
736 | implode( |
---|
737 | "\n AND ", |
---|
738 | array_map( |
---|
739 | create_function('$s', 'return "t1.$s = t2.$s";'), |
---|
740 | $dbfields['primary'] |
---|
741 | ) |
---|
742 | ).' |
---|
743 | ;'; |
---|
744 | pwg_query($query); |
---|
745 | $query = ' |
---|
746 | DROP TABLE '.$temporary_tablename.' |
---|
747 | ;'; |
---|
748 | pwg_query($query); |
---|
749 | } |
---|
750 | } |
---|
751 | |
---|
752 | /** |
---|
753 | * updates the global_rank of categories under the given id_uppercat |
---|
754 | * |
---|
755 | * @param int id_uppercat |
---|
756 | * @return void |
---|
757 | */ |
---|
758 | function update_global_rank($id_uppercat = 'all') |
---|
759 | { |
---|
760 | $query = ' |
---|
761 | SELECT id,rank |
---|
762 | FROM '.CATEGORIES_TABLE.' |
---|
763 | ;'; |
---|
764 | $result = pwg_query($query); |
---|
765 | $ranks_array = array(); |
---|
766 | while ($row = mysql_fetch_array($result)) |
---|
767 | { |
---|
768 | $ranks_array[$row['id']] = $row['rank']; |
---|
769 | } |
---|
770 | |
---|
771 | // which categories to update ? |
---|
772 | $uppercats_array = array(); |
---|
773 | |
---|
774 | $query = ' |
---|
775 | SELECT id,uppercats |
---|
776 | FROM '.CATEGORIES_TABLE; |
---|
777 | if (is_numeric($id_uppercat)) |
---|
778 | { |
---|
779 | $query.= ' |
---|
780 | WHERE uppercats REGEXP \'(^|,)'.$id_uppercat.'(,|$)\' |
---|
781 | AND id != '.$id_uppercat.' |
---|
782 | '; |
---|
783 | } |
---|
784 | $query.= ' |
---|
785 | ;'; |
---|
786 | $result = pwg_query($query); |
---|
787 | while ($row = mysql_fetch_array($result)) |
---|
788 | { |
---|
789 | $uppercats_array[$row['id']] = $row['uppercats']; |
---|
790 | } |
---|
791 | |
---|
792 | $datas = array(); |
---|
793 | foreach ($uppercats_array as $id => $uppercats) |
---|
794 | { |
---|
795 | array_push( |
---|
796 | $datas, |
---|
797 | array( |
---|
798 | 'id' => $id, |
---|
799 | 'global_rank' => preg_replace( |
---|
800 | '/(\d+)/e', |
---|
801 | "\$ranks_array['$1']", |
---|
802 | str_replace(',', '.', $uppercats) |
---|
803 | ), |
---|
804 | ) |
---|
805 | ); |
---|
806 | } |
---|
807 | |
---|
808 | mass_updates( |
---|
809 | CATEGORIES_TABLE, |
---|
810 | array( |
---|
811 | 'primary' => array('id'), |
---|
812 | 'update' => array('global_rank') |
---|
813 | ), |
---|
814 | $datas |
---|
815 | ); |
---|
816 | } |
---|
817 | |
---|
818 | /** |
---|
819 | * change the visible property on a set of categories |
---|
820 | * |
---|
821 | * @param array categories |
---|
822 | * @param string value |
---|
823 | * @return void |
---|
824 | */ |
---|
825 | function set_cat_visible($categories, $value) |
---|
826 | { |
---|
827 | if (!in_array($value, array('true', 'false'))) |
---|
828 | { |
---|
829 | return false; |
---|
830 | } |
---|
831 | |
---|
832 | // unlocking a category => all its parent categories become unlocked |
---|
833 | if ($value == 'true') |
---|
834 | { |
---|
835 | $uppercats = get_uppercat_ids($categories); |
---|
836 | $query = ' |
---|
837 | UPDATE '.CATEGORIES_TABLE.' |
---|
838 | SET visible = \'true\' |
---|
839 | WHERE id IN ('.implode(',', $uppercats).') |
---|
840 | ;'; |
---|
841 | pwg_query($query); |
---|
842 | } |
---|
843 | // locking a category => all its child categories become locked |
---|
844 | if ($value == 'false') |
---|
845 | { |
---|
846 | $subcats = get_subcat_ids($categories); |
---|
847 | $query = ' |
---|
848 | UPDATE '.CATEGORIES_TABLE.' |
---|
849 | SET visible = \'false\' |
---|
850 | WHERE id IN ('.implode(',', $subcats).') |
---|
851 | ;'; |
---|
852 | pwg_query($query); |
---|
853 | } |
---|
854 | } |
---|
855 | |
---|
856 | /** |
---|
857 | * change the status property on a set of categories : private or public |
---|
858 | * |
---|
859 | * @param array categories |
---|
860 | * @param string value |
---|
861 | * @return void |
---|
862 | */ |
---|
863 | function set_cat_status($categories, $value) |
---|
864 | { |
---|
865 | if (!in_array($value, array('public', 'private'))) |
---|
866 | { |
---|
867 | return false; |
---|
868 | } |
---|
869 | |
---|
870 | // make public a category => all its parent categories become public |
---|
871 | if ($value == 'public') |
---|
872 | { |
---|
873 | $uppercats = get_uppercat_ids($categories); |
---|
874 | $query = ' |
---|
875 | UPDATE '.CATEGORIES_TABLE.' |
---|
876 | SET status = \'public\' |
---|
877 | WHERE id IN ('.implode(',', $uppercats).') |
---|
878 | ;'; |
---|
879 | pwg_query($query); |
---|
880 | } |
---|
881 | // make a category private => all its child categories become private |
---|
882 | if ($value == 'private') |
---|
883 | { |
---|
884 | $subcats = get_subcat_ids($categories); |
---|
885 | $query = ' |
---|
886 | UPDATE '.CATEGORIES_TABLE.' |
---|
887 | SET status = \'private\' |
---|
888 | WHERE id IN ('.implode(',', $subcats).') |
---|
889 | ;'; |
---|
890 | pwg_query($query); |
---|
891 | } |
---|
892 | } |
---|
893 | |
---|
894 | /** |
---|
895 | * returns all uppercats category ids of the given category ids |
---|
896 | * |
---|
897 | * @param array cat_ids |
---|
898 | * @return array |
---|
899 | */ |
---|
900 | function get_uppercat_ids($cat_ids) |
---|
901 | { |
---|
902 | if (!is_array($cat_ids) or count($cat_ids) < 1) |
---|
903 | { |
---|
904 | return array(); |
---|
905 | } |
---|
906 | |
---|
907 | $uppercats = array(); |
---|
908 | |
---|
909 | $query = ' |
---|
910 | SELECT uppercats |
---|
911 | FROM '.CATEGORIES_TABLE.' |
---|
912 | WHERE id IN ('.implode(',', $cat_ids).') |
---|
913 | ;'; |
---|
914 | $result = pwg_query($query); |
---|
915 | while ($row = mysql_fetch_array($result)) |
---|
916 | { |
---|
917 | $uppercats = array_merge($uppercats, |
---|
918 | explode(',', $row['uppercats'])); |
---|
919 | } |
---|
920 | $uppercats = array_unique($uppercats); |
---|
921 | |
---|
922 | return $uppercats; |
---|
923 | } |
---|
924 | |
---|
925 | /** |
---|
926 | * set a new random representant to the categories |
---|
927 | * |
---|
928 | * @param array categories |
---|
929 | */ |
---|
930 | function set_random_representant($categories) |
---|
931 | { |
---|
932 | $datas = array(); |
---|
933 | foreach ($categories as $category_id) |
---|
934 | { |
---|
935 | $query = ' |
---|
936 | SELECT image_id |
---|
937 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
938 | WHERE category_id = '.$category_id.' |
---|
939 | ORDER BY RAND() |
---|
940 | LIMIT 0,1 |
---|
941 | ;'; |
---|
942 | list($representative) = mysql_fetch_array(pwg_query($query)); |
---|
943 | |
---|
944 | array_push( |
---|
945 | $datas, |
---|
946 | array( |
---|
947 | 'id' => $category_id, |
---|
948 | 'representative_picture_id' => $representative, |
---|
949 | ) |
---|
950 | ); |
---|
951 | } |
---|
952 | |
---|
953 | mass_updates( |
---|
954 | CATEGORIES_TABLE, |
---|
955 | array( |
---|
956 | 'primary' => array('id'), |
---|
957 | 'update' => array('representative_picture_id') |
---|
958 | ), |
---|
959 | $datas |
---|
960 | ); |
---|
961 | } |
---|
962 | |
---|
963 | /** |
---|
964 | * order categories (update categories.rank and global_rank database fields) |
---|
965 | * |
---|
966 | * the purpose of this function is to give a rank for all categories |
---|
967 | * (insides its sub-category), even the newer that have none at te |
---|
968 | * beginning. For this, ordering function selects all categories ordered by |
---|
969 | * rank ASC then name ASC for each uppercat. |
---|
970 | * |
---|
971 | * @returns void |
---|
972 | */ |
---|
973 | function ordering() |
---|
974 | { |
---|
975 | $current_rank = 0; |
---|
976 | $current_uppercat = ''; |
---|
977 | |
---|
978 | $query = ' |
---|
979 | SELECT id, if(id_uppercat is null,\'\',id_uppercat) AS id_uppercat |
---|
980 | FROM '.CATEGORIES_TABLE.' |
---|
981 | ORDER BY id_uppercat,rank,name |
---|
982 | ;'; |
---|
983 | $result = pwg_query($query); |
---|
984 | $datas = array(); |
---|
985 | while ($row = mysql_fetch_array($result)) |
---|
986 | { |
---|
987 | if ($row['id_uppercat'] != $current_uppercat) |
---|
988 | { |
---|
989 | $current_rank = 0; |
---|
990 | $current_uppercat = $row['id_uppercat']; |
---|
991 | } |
---|
992 | $data = array('id' => $row['id'], 'rank' => ++$current_rank); |
---|
993 | array_push($datas, $data); |
---|
994 | } |
---|
995 | |
---|
996 | $fields = array('primary' => array('id'), 'update' => array('rank')); |
---|
997 | mass_updates(CATEGORIES_TABLE, $fields, $datas); |
---|
998 | } |
---|
999 | |
---|
1000 | /** |
---|
1001 | * returns the fulldir for each given category id |
---|
1002 | * |
---|
1003 | * @param array cat_ids |
---|
1004 | * @return array |
---|
1005 | */ |
---|
1006 | function get_fulldirs($cat_ids) |
---|
1007 | { |
---|
1008 | if (count($cat_ids) == 0) |
---|
1009 | { |
---|
1010 | return array(); |
---|
1011 | } |
---|
1012 | |
---|
1013 | // caching directories of existing categories |
---|
1014 | $query = ' |
---|
1015 | SELECT id, dir |
---|
1016 | FROM '.CATEGORIES_TABLE.' |
---|
1017 | WHERE dir IS NOT NULL |
---|
1018 | ;'; |
---|
1019 | $result = pwg_query($query); |
---|
1020 | $cat_dirs = array(); |
---|
1021 | while ($row = mysql_fetch_array($result)) |
---|
1022 | { |
---|
1023 | $cat_dirs[$row['id']] = $row['dir']; |
---|
1024 | } |
---|
1025 | |
---|
1026 | // caching galleries_url |
---|
1027 | $query = ' |
---|
1028 | SELECT id, galleries_url |
---|
1029 | FROM '.SITES_TABLE.' |
---|
1030 | ;'; |
---|
1031 | $result = pwg_query($query); |
---|
1032 | $galleries_url = array(); |
---|
1033 | while ($row = mysql_fetch_array($result)) |
---|
1034 | { |
---|
1035 | $galleries_url[$row['id']] = $row['galleries_url']; |
---|
1036 | } |
---|
1037 | |
---|
1038 | // categories : id, site_id, uppercats |
---|
1039 | $categories = array(); |
---|
1040 | |
---|
1041 | $query = ' |
---|
1042 | SELECT id, uppercats, site_id |
---|
1043 | FROM '.CATEGORIES_TABLE.' |
---|
1044 | WHERE id IN ( |
---|
1045 | '.wordwrap(implode(', ', $cat_ids), 80, "\n").') |
---|
1046 | ;'; |
---|
1047 | $result = pwg_query($query); |
---|
1048 | while ($row = mysql_fetch_array($result)) |
---|
1049 | { |
---|
1050 | array_push($categories, $row); |
---|
1051 | } |
---|
1052 | |
---|
1053 | // filling $cat_fulldirs |
---|
1054 | $cat_fulldirs = array(); |
---|
1055 | foreach ($categories as $category) |
---|
1056 | { |
---|
1057 | $uppercats = str_replace(',', '/', $category['uppercats']); |
---|
1058 | $cat_fulldirs[$category['id']] = $galleries_url[$category['site_id']]; |
---|
1059 | $cat_fulldirs[$category['id']].= preg_replace('/(\d+)/e', |
---|
1060 | "\$cat_dirs['$1']", |
---|
1061 | $uppercats); |
---|
1062 | } |
---|
1063 | |
---|
1064 | return $cat_fulldirs; |
---|
1065 | } |
---|
1066 | |
---|
1067 | /** |
---|
1068 | * returns an array with all file system files according to |
---|
1069 | * $conf['file_ext'] |
---|
1070 | * |
---|
1071 | * @param string $path |
---|
1072 | * @param bool recursive |
---|
1073 | * @return array |
---|
1074 | */ |
---|
1075 | function get_fs($path, $recursive = true) |
---|
1076 | { |
---|
1077 | global $conf; |
---|
1078 | |
---|
1079 | // because isset is faster than in_array... |
---|
1080 | if (!isset($conf['flip_picture_ext'])) |
---|
1081 | { |
---|
1082 | $conf['flip_picture_ext'] = array_flip($conf['picture_ext']); |
---|
1083 | } |
---|
1084 | if (!isset($conf['flip_file_ext'])) |
---|
1085 | { |
---|
1086 | $conf['flip_file_ext'] = array_flip($conf['file_ext']); |
---|
1087 | } |
---|
1088 | |
---|
1089 | $fs['elements'] = array(); |
---|
1090 | $fs['thumbnails'] = array(); |
---|
1091 | $fs['representatives'] = array(); |
---|
1092 | $subdirs = array(); |
---|
1093 | |
---|
1094 | if (is_dir($path)) |
---|
1095 | { |
---|
1096 | if ($contents = opendir($path)) |
---|
1097 | { |
---|
1098 | while (($node = readdir($contents)) !== false) |
---|
1099 | { |
---|
1100 | if (is_file($path.'/'.$node)) |
---|
1101 | { |
---|
1102 | $extension = get_extension($node); |
---|
1103 | |
---|
1104 | // if (in_array($extension, $conf['picture_ext'])) |
---|
1105 | if (isset($conf['flip_picture_ext'][$extension])) |
---|
1106 | { |
---|
1107 | if (basename($path) == 'thumbnail') |
---|
1108 | { |
---|
1109 | array_push($fs['thumbnails'], $path.'/'.$node); |
---|
1110 | } |
---|
1111 | else if (basename($path) == 'pwg_representative') |
---|
1112 | { |
---|
1113 | array_push($fs['representatives'], $path.'/'.$node); |
---|
1114 | } |
---|
1115 | else |
---|
1116 | { |
---|
1117 | array_push($fs['elements'], $path.'/'.$node); |
---|
1118 | } |
---|
1119 | } |
---|
1120 | // else if (in_array($extension, $conf['file_ext'])) |
---|
1121 | else if (isset($conf['flip_file_ext'][$extension])) |
---|
1122 | { |
---|
1123 | array_push($fs['elements'], $path.'/'.$node); |
---|
1124 | } |
---|
1125 | } |
---|
1126 | else if (is_dir($path.'/'.$node) |
---|
1127 | and $node != '.' |
---|
1128 | and $node != '..' |
---|
1129 | and $node != 'pwg_high' |
---|
1130 | and $recursive) |
---|
1131 | { |
---|
1132 | array_push($subdirs, $node); |
---|
1133 | } |
---|
1134 | } |
---|
1135 | } |
---|
1136 | closedir($contents); |
---|
1137 | |
---|
1138 | foreach ($subdirs as $subdir) |
---|
1139 | { |
---|
1140 | $tmp_fs = get_fs($path.'/'.$subdir); |
---|
1141 | |
---|
1142 | $fs['elements'] = array_merge($fs['elements'], |
---|
1143 | $tmp_fs['elements']); |
---|
1144 | |
---|
1145 | $fs['thumbnails'] = array_merge($fs['thumbnails'], |
---|
1146 | $tmp_fs['thumbnails']); |
---|
1147 | |
---|
1148 | $fs['representatives'] = array_merge($fs['representatives'], |
---|
1149 | $tmp_fs['representatives']); |
---|
1150 | } |
---|
1151 | } |
---|
1152 | return $fs; |
---|
1153 | } |
---|
1154 | |
---|
1155 | /** |
---|
1156 | * stupidly returns the current microsecond since Unix epoch |
---|
1157 | */ |
---|
1158 | function micro_seconds() |
---|
1159 | { |
---|
1160 | $t1 = explode(' ', microtime()); |
---|
1161 | $t2 = explode('.', $t1[0]); |
---|
1162 | $t2 = $t1[1].substr($t2[1], 0, 6); |
---|
1163 | return $t2; |
---|
1164 | } |
---|
1165 | |
---|
1166 | /** |
---|
1167 | * synchronize base users list and related users list |
---|
1168 | * |
---|
1169 | * compares and synchronizes base users table (USERS_TABLE) with its child |
---|
1170 | * tables (USER_INFOS_TABLE, USER_ACCESS, USER_CACHE, USER_GROUP) : each |
---|
1171 | * base user must be present in child tables, users in child tables not |
---|
1172 | * present in base table must be deleted. |
---|
1173 | * |
---|
1174 | * @return void |
---|
1175 | */ |
---|
1176 | function sync_users() |
---|
1177 | { |
---|
1178 | global $conf; |
---|
1179 | |
---|
1180 | $query = ' |
---|
1181 | SELECT '.$conf['user_fields']['id'].' AS id |
---|
1182 | FROM '.USERS_TABLE.' |
---|
1183 | ;'; |
---|
1184 | $base_users = array_from_query($query, 'id'); |
---|
1185 | |
---|
1186 | $query = ' |
---|
1187 | SELECT user_id |
---|
1188 | FROM '.USER_INFOS_TABLE.' |
---|
1189 | ;'; |
---|
1190 | $infos_users = array_from_query($query, 'user_id'); |
---|
1191 | |
---|
1192 | // users present in $base_users and not in $infos_users must be added |
---|
1193 | $to_create = array_diff($base_users, $infos_users); |
---|
1194 | |
---|
1195 | if (count($to_create) > 0) |
---|
1196 | { |
---|
1197 | $inserts = array(); |
---|
1198 | |
---|
1199 | list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();')); |
---|
1200 | |
---|
1201 | foreach ($to_create as $user_id) |
---|
1202 | { |
---|
1203 | $insert = array(); |
---|
1204 | $insert['user_id'] = $user_id; |
---|
1205 | $insert['status'] = 'normal'; |
---|
1206 | $insert['template'] = $conf['default_template']; |
---|
1207 | $insert['nb_image_line'] = $conf['nb_image_line']; |
---|
1208 | $insert['nb_line_page'] = $conf['nb_line_page']; |
---|
1209 | $insert['language'] = $conf['default_language']; |
---|
1210 | $insert['recent_period'] = $conf['recent_period']; |
---|
1211 | $insert['expand'] = boolean_to_string($conf['auto_expand']); |
---|
1212 | $insert['show_nb_comments'] = |
---|
1213 | boolean_to_string($conf['show_nb_comments']); |
---|
1214 | $insert['maxwidth'] = $conf['default_maxwidth']; |
---|
1215 | $insert['maxheight'] = $conf['default_maxheight']; |
---|
1216 | $insert['registration_date'] = $dbnow; |
---|
1217 | |
---|
1218 | array_push($inserts, $insert); |
---|
1219 | } |
---|
1220 | |
---|
1221 | mass_inserts(USER_INFOS_TABLE, |
---|
1222 | array_keys($inserts[0]), |
---|
1223 | $inserts); |
---|
1224 | } |
---|
1225 | |
---|
1226 | // users present in user related tables must be present in the base user |
---|
1227 | // table |
---|
1228 | $tables = array( |
---|
1229 | USER_MAIL_NOTIFICATION_TABLE, |
---|
1230 | USER_FEED_TABLE, |
---|
1231 | USER_INFOS_TABLE, |
---|
1232 | USER_ACCESS_TABLE, |
---|
1233 | USER_CACHE_TABLE, |
---|
1234 | USER_GROUP_TABLE |
---|
1235 | ); |
---|
1236 | |
---|
1237 | foreach ($tables as $table) |
---|
1238 | { |
---|
1239 | $query = ' |
---|
1240 | SELECT user_id |
---|
1241 | FROM '.$table.' |
---|
1242 | ;'; |
---|
1243 | $to_delete = array_diff( |
---|
1244 | array_from_query($query, 'user_id'), |
---|
1245 | $base_users |
---|
1246 | ); |
---|
1247 | |
---|
1248 | if (count($to_delete) > 0) |
---|
1249 | { |
---|
1250 | $query = ' |
---|
1251 | DELETE |
---|
1252 | FROM '.$table.' |
---|
1253 | WHERE user_id in ('.implode(',', $to_delete).') |
---|
1254 | ;'; |
---|
1255 | pwg_query($query); |
---|
1256 | } |
---|
1257 | } |
---|
1258 | } |
---|
1259 | |
---|
1260 | /** |
---|
1261 | * updates categories.uppercats field based on categories.id + |
---|
1262 | * categories.id_uppercat |
---|
1263 | * |
---|
1264 | * @return void |
---|
1265 | */ |
---|
1266 | function update_uppercats() |
---|
1267 | { |
---|
1268 | $uppercat_ids = array(); |
---|
1269 | |
---|
1270 | $query = ' |
---|
1271 | SELECT id, id_uppercat |
---|
1272 | FROM '.CATEGORIES_TABLE.' |
---|
1273 | ;'; |
---|
1274 | $result = pwg_query($query); |
---|
1275 | while ($row = mysql_fetch_array($result)) |
---|
1276 | { |
---|
1277 | $uppercat_ids[$row['id']] = |
---|
1278 | !empty($row['id_uppercat']) ? $row['id_uppercat'] : 'NULL'; |
---|
1279 | } |
---|
1280 | |
---|
1281 | // uppercats array associates a category id to the list of uppercats id. |
---|
1282 | $uppercats = array(); |
---|
1283 | |
---|
1284 | foreach (array_keys($uppercat_ids) as $id) |
---|
1285 | { |
---|
1286 | $uppercats[$id] = array(); |
---|
1287 | |
---|
1288 | $uppercat = $id; |
---|
1289 | |
---|
1290 | while ($uppercat != 'NULL') |
---|
1291 | { |
---|
1292 | array_push($uppercats[$id], $uppercat); |
---|
1293 | $uppercat = $uppercat_ids[$uppercat]; |
---|
1294 | } |
---|
1295 | } |
---|
1296 | |
---|
1297 | $datas = array(); |
---|
1298 | |
---|
1299 | foreach ($uppercats as $id => $list) |
---|
1300 | { |
---|
1301 | array_push( |
---|
1302 | $datas, |
---|
1303 | array( |
---|
1304 | 'id' => $id, |
---|
1305 | 'uppercats' => implode(',', array_reverse($list)) |
---|
1306 | ) |
---|
1307 | ); |
---|
1308 | } |
---|
1309 | |
---|
1310 | $fields = array('primary' => array('id'), 'update' => array('uppercats')); |
---|
1311 | mass_updates(CATEGORIES_TABLE, $fields, $datas); |
---|
1312 | } |
---|
1313 | |
---|
1314 | /** |
---|
1315 | * update images.path field |
---|
1316 | * |
---|
1317 | * @return void |
---|
1318 | */ |
---|
1319 | function update_path() |
---|
1320 | { |
---|
1321 | $query = ' |
---|
1322 | SELECT DISTINCT(storage_category_id) |
---|
1323 | FROM '.IMAGES_TABLE.' |
---|
1324 | ;'; |
---|
1325 | $cat_ids = array_from_query($query, 'storage_category_id'); |
---|
1326 | $fulldirs = get_fulldirs($cat_ids); |
---|
1327 | |
---|
1328 | foreach ($cat_ids as $cat_id) |
---|
1329 | { |
---|
1330 | $query = ' |
---|
1331 | UPDATE '.IMAGES_TABLE.' |
---|
1332 | SET path = CONCAT(\''.$fulldirs[$cat_id].'\',\'/\',file) |
---|
1333 | WHERE storage_category_id = '.$cat_id.' |
---|
1334 | ;'; |
---|
1335 | pwg_query($query); |
---|
1336 | } |
---|
1337 | } |
---|
1338 | |
---|
1339 | /** |
---|
1340 | * update images.average_rate field |
---|
1341 | * param int $element_id optional, otherwise applies to all |
---|
1342 | * @return void |
---|
1343 | */ |
---|
1344 | function update_average_rate( $element_id=-1 ) |
---|
1345 | { |
---|
1346 | $query = ' |
---|
1347 | SELECT element_id, |
---|
1348 | ROUND(AVG(rate),2) AS average_rate |
---|
1349 | FROM '.RATE_TABLE; |
---|
1350 | if ( $element_id != -1 ) |
---|
1351 | { |
---|
1352 | $query .= ' WHERE element_id=' . $element_id; |
---|
1353 | } |
---|
1354 | $query .= ' GROUP BY element_id;'; |
---|
1355 | |
---|
1356 | $result = pwg_query($query); |
---|
1357 | |
---|
1358 | $datas = array(); |
---|
1359 | |
---|
1360 | while ($row = mysql_fetch_array($result)) |
---|
1361 | { |
---|
1362 | array_push( |
---|
1363 | $datas, |
---|
1364 | array( |
---|
1365 | 'id' => $row['element_id'], |
---|
1366 | 'average_rate' => $row['average_rate'] |
---|
1367 | ) |
---|
1368 | ); |
---|
1369 | } |
---|
1370 | |
---|
1371 | mass_updates( |
---|
1372 | IMAGES_TABLE, |
---|
1373 | array( |
---|
1374 | 'primary' => array('id'), |
---|
1375 | 'update' => array('average_rate') |
---|
1376 | ), |
---|
1377 | $datas |
---|
1378 | ); |
---|
1379 | |
---|
1380 | $query=' |
---|
1381 | SELECT id FROM '.IMAGES_TABLE .' |
---|
1382 | LEFT JOIN '.RATE_TABLE.' ON id=element_id |
---|
1383 | WHERE element_id IS NULL AND average_rate IS NOT NULL'; |
---|
1384 | if ( $element_id != -1 ) |
---|
1385 | { |
---|
1386 | $query .= ' AND id=' . $element_id; |
---|
1387 | } |
---|
1388 | $to_update = array_from_query( $query, 'id'); |
---|
1389 | |
---|
1390 | if ( !empty($to_update) ) |
---|
1391 | { |
---|
1392 | $query=' |
---|
1393 | UPDATE '.IMAGES_TABLE .' |
---|
1394 | SET average_rate=NULL |
---|
1395 | WHERE id IN (' . implode(',',$to_update) . ')'; |
---|
1396 | pwg_query($query); |
---|
1397 | } |
---|
1398 | } |
---|
1399 | |
---|
1400 | /** |
---|
1401 | * change the parent category of the given categories. The categories are |
---|
1402 | * supposed virtual. |
---|
1403 | * |
---|
1404 | * @param array category identifiers |
---|
1405 | * @param int parent category identifier |
---|
1406 | * @return void |
---|
1407 | */ |
---|
1408 | function move_categories($category_ids, $new_parent = -1) |
---|
1409 | { |
---|
1410 | global $page; |
---|
1411 | |
---|
1412 | if (count($category_ids) == 0) |
---|
1413 | { |
---|
1414 | return; |
---|
1415 | } |
---|
1416 | |
---|
1417 | $new_parent = $new_parent < 1 ? 'NULL' : $new_parent; |
---|
1418 | |
---|
1419 | $categories = array(); |
---|
1420 | |
---|
1421 | $query = ' |
---|
1422 | SELECT id, id_uppercat, status, uppercats |
---|
1423 | FROM '.CATEGORIES_TABLE.' |
---|
1424 | WHERE id IN ('.implode(',', $category_ids).') |
---|
1425 | ;'; |
---|
1426 | $result = pwg_query($query); |
---|
1427 | while ($row = mysql_fetch_array($result)) |
---|
1428 | { |
---|
1429 | $categories[$row['id']] = |
---|
1430 | array( |
---|
1431 | 'parent' => empty($row['id_uppercat']) ? 'NULL' : $row['id_uppercat'], |
---|
1432 | 'status' => $row['status'], |
---|
1433 | 'uppercats' => $row['uppercats'] |
---|
1434 | ); |
---|
1435 | } |
---|
1436 | |
---|
1437 | // is the movement possible? The movement is impossible if you try to move |
---|
1438 | // a category in a sub-category or itself |
---|
1439 | if ('NULL' != $new_parent) |
---|
1440 | { |
---|
1441 | $query = ' |
---|
1442 | SELECT uppercats |
---|
1443 | FROM '.CATEGORIES_TABLE.' |
---|
1444 | WHERE id = '.$new_parent.' |
---|
1445 | ;'; |
---|
1446 | list($new_parent_uppercats) = mysql_fetch_row(pwg_query($query)); |
---|
1447 | |
---|
1448 | foreach ($categories as $category) |
---|
1449 | { |
---|
1450 | // technically, you can't move a category with uppercats 12,125,13,14 |
---|
1451 | // into a new parent category with uppercats 12,125,13,14,24 |
---|
1452 | if (preg_match('/^'.$category['uppercats'].'/', $new_parent_uppercats)) |
---|
1453 | { |
---|
1454 | array_push( |
---|
1455 | $page['errors'], |
---|
1456 | l10n('You cannot move a category in its own sub category') |
---|
1457 | ); |
---|
1458 | return; |
---|
1459 | } |
---|
1460 | } |
---|
1461 | } |
---|
1462 | |
---|
1463 | $tables = |
---|
1464 | array( |
---|
1465 | USER_ACCESS_TABLE => 'user_id', |
---|
1466 | GROUP_ACCESS_TABLE => 'group_id' |
---|
1467 | ); |
---|
1468 | |
---|
1469 | $query = ' |
---|
1470 | UPDATE '.CATEGORIES_TABLE.' |
---|
1471 | SET id_uppercat = '.$new_parent.' |
---|
1472 | WHERE id IN ('.implode(',', $category_ids).') |
---|
1473 | ;'; |
---|
1474 | pwg_query($query); |
---|
1475 | |
---|
1476 | update_uppercats(); |
---|
1477 | ordering(); |
---|
1478 | update_global_rank(); |
---|
1479 | |
---|
1480 | // status and related permissions management |
---|
1481 | if ('NULL' == $new_parent) |
---|
1482 | { |
---|
1483 | $parent_status = 'public'; |
---|
1484 | } |
---|
1485 | else |
---|
1486 | { |
---|
1487 | $query = ' |
---|
1488 | SELECT status |
---|
1489 | FROM '.CATEGORIES_TABLE.' |
---|
1490 | WHERE id = '.$new_parent.' |
---|
1491 | ;'; |
---|
1492 | list($parent_status) = mysql_fetch_row(pwg_query($query)); |
---|
1493 | } |
---|
1494 | |
---|
1495 | if ('private' == $parent_status) |
---|
1496 | { |
---|
1497 | foreach ($categories as $cat_id => $category) |
---|
1498 | { |
---|
1499 | switch ($category['status']) |
---|
1500 | { |
---|
1501 | case 'public' : |
---|
1502 | { |
---|
1503 | set_cat_status(array($cat_id), 'private'); |
---|
1504 | break; |
---|
1505 | } |
---|
1506 | case 'private' : |
---|
1507 | { |
---|
1508 | $subcats = get_subcat_ids(array($cat_id)); |
---|
1509 | |
---|
1510 | foreach ($tables as $table => $field) |
---|
1511 | { |
---|
1512 | $query = ' |
---|
1513 | SELECT '.$field.' |
---|
1514 | FROM '.$table.' |
---|
1515 | WHERE cat_id = '.$cat_id.' |
---|
1516 | ;'; |
---|
1517 | $category_access = array_from_query($query, $field); |
---|
1518 | |
---|
1519 | $query = ' |
---|
1520 | SELECT '.$field.' |
---|
1521 | FROM '.$table.' |
---|
1522 | WHERE cat_id = '.$new_parent.' |
---|
1523 | ;'; |
---|
1524 | $parent_access = array_from_query($query, $field); |
---|
1525 | |
---|
1526 | $to_delete = array_diff($parent_access, $category_access); |
---|
1527 | |
---|
1528 | if (count($to_delete) > 0) |
---|
1529 | { |
---|
1530 | $query = ' |
---|
1531 | DELETE FROM '.$table.' |
---|
1532 | WHERE '.$field.' IN ('.implode(',', $to_delete).') |
---|
1533 | AND cat_id IN ('.implode(',', $subcats).') |
---|
1534 | ;'; |
---|
1535 | pwg_query($query); |
---|
1536 | } |
---|
1537 | } |
---|
1538 | break; |
---|
1539 | } |
---|
1540 | } |
---|
1541 | } |
---|
1542 | } |
---|
1543 | |
---|
1544 | array_push( |
---|
1545 | $page['infos'], |
---|
1546 | sprintf( |
---|
1547 | l10n('%d categories moved'), |
---|
1548 | count($categories) |
---|
1549 | ) |
---|
1550 | ); |
---|
1551 | } |
---|
1552 | |
---|
1553 | /** |
---|
1554 | * create a virtual category |
---|
1555 | * |
---|
1556 | * @param string category name |
---|
1557 | * @param int parent category id |
---|
1558 | * @return array with ('info' and 'id') or ('error') key |
---|
1559 | */ |
---|
1560 | function create_virtual_category($category_name, $parent_id=null) |
---|
1561 | { |
---|
1562 | global $conf; |
---|
1563 | |
---|
1564 | // is the given category name only containing blank spaces ? |
---|
1565 | if (preg_match('/^\s*$/', $category_name)) |
---|
1566 | { |
---|
1567 | return array('error' => l10n('cat_error_name')); |
---|
1568 | } |
---|
1569 | |
---|
1570 | $parent_id = !empty($parent_id) ? $parent_id : 'NULL'; |
---|
1571 | |
---|
1572 | $query = ' |
---|
1573 | SELECT MAX(rank) |
---|
1574 | FROM '.CATEGORIES_TABLE.' |
---|
1575 | WHERE id_uppercat '.(is_numeric($parent_id) ? '= '.$parent_id : 'IS NULL').' |
---|
1576 | ;'; |
---|
1577 | list($current_rank) = mysql_fetch_array(pwg_query($query)); |
---|
1578 | |
---|
1579 | $insert = array( |
---|
1580 | 'name' => $category_name, |
---|
1581 | 'rank' => ++$current_rank, |
---|
1582 | 'commentable' => boolean_to_string($conf['newcat_default_commentable']), |
---|
1583 | 'uploadable' => 'false', |
---|
1584 | ); |
---|
1585 | |
---|
1586 | if ($parent_id != 'NULL') |
---|
1587 | { |
---|
1588 | $query = ' |
---|
1589 | SELECT id, uppercats, global_rank, visible, status |
---|
1590 | FROM '.CATEGORIES_TABLE.' |
---|
1591 | WHERE id = '.$parent_id.' |
---|
1592 | ;'; |
---|
1593 | $parent = mysql_fetch_array(pwg_query($query)); |
---|
1594 | |
---|
1595 | $insert{'id_uppercat'} = $parent{'id'}; |
---|
1596 | $insert{'global_rank'} = $parent{'global_rank'}.'.'.$insert{'rank'}; |
---|
1597 | |
---|
1598 | // at creation, must a category be visible or not ? Warning : if the |
---|
1599 | // parent category is invisible, the category is automatically create |
---|
1600 | // invisible. (invisible = locked) |
---|
1601 | if ('false' == $parent['visible']) |
---|
1602 | { |
---|
1603 | $insert{'visible'} = 'false'; |
---|
1604 | } |
---|
1605 | else |
---|
1606 | { |
---|
1607 | $insert{'visible'} = boolean_to_string($conf['newcat_default_visible']); |
---|
1608 | } |
---|
1609 | |
---|
1610 | // at creation, must a category be public or private ? Warning : if the |
---|
1611 | // parent category is private, the category is automatically create |
---|
1612 | // private. |
---|
1613 | if ('private' == $parent['status']) |
---|
1614 | { |
---|
1615 | $insert{'status'} = 'private'; |
---|
1616 | } |
---|
1617 | else |
---|
1618 | { |
---|
1619 | $insert{'status'} = $conf['newcat_default_status']; |
---|
1620 | } |
---|
1621 | } |
---|
1622 | else |
---|
1623 | { |
---|
1624 | $insert{'visible'} = boolean_to_string($conf['newcat_default_visible']); |
---|
1625 | $insert{'status'} = $conf['newcat_default_status']; |
---|
1626 | $insert{'global_rank'} = $insert{'rank'}; |
---|
1627 | } |
---|
1628 | |
---|
1629 | // we have then to add the virtual category |
---|
1630 | mass_inserts( |
---|
1631 | CATEGORIES_TABLE, |
---|
1632 | array( |
---|
1633 | 'site_id', 'name', 'id_uppercat', 'rank', 'commentable', |
---|
1634 | 'uploadable', 'visible', 'status', 'global_rank', |
---|
1635 | ), |
---|
1636 | array($insert) |
---|
1637 | ); |
---|
1638 | |
---|
1639 | $inserted_id = mysql_insert_id(); |
---|
1640 | |
---|
1641 | $query = ' |
---|
1642 | UPDATE |
---|
1643 | '.CATEGORIES_TABLE.' |
---|
1644 | SET uppercats = \''. |
---|
1645 | (isset($parent) ? $parent{'uppercats'}.',' : ''). |
---|
1646 | $inserted_id. |
---|
1647 | '\' |
---|
1648 | WHERE id = '.$inserted_id.' |
---|
1649 | ;'; |
---|
1650 | pwg_query($query); |
---|
1651 | |
---|
1652 | return array( |
---|
1653 | 'info' => l10n('cat_virtual_added'), |
---|
1654 | 'id' => $inserted_id, |
---|
1655 | ); |
---|
1656 | } |
---|
1657 | |
---|
1658 | /** |
---|
1659 | * Set tags to an image. Warning: given tags are all tags associated to the |
---|
1660 | * image, not additionnal tags. |
---|
1661 | * |
---|
1662 | * @param array tag ids |
---|
1663 | * @param int image id |
---|
1664 | * @return void |
---|
1665 | */ |
---|
1666 | function set_tags($tags, $image_id) |
---|
1667 | { |
---|
1668 | $query = ' |
---|
1669 | DELETE |
---|
1670 | FROM '.IMAGE_TAG_TABLE.' |
---|
1671 | WHERE image_id = '.$image_id.' |
---|
1672 | ;'; |
---|
1673 | pwg_query($query); |
---|
1674 | |
---|
1675 | if (count($tags) > 0) |
---|
1676 | { |
---|
1677 | $inserts = array(); |
---|
1678 | foreach ($tags as $tag_id) |
---|
1679 | { |
---|
1680 | array_push( |
---|
1681 | $inserts, |
---|
1682 | array( |
---|
1683 | 'tag_id' => $tag_id, |
---|
1684 | 'image_id' => $image_id |
---|
1685 | ) |
---|
1686 | ); |
---|
1687 | } |
---|
1688 | mass_inserts( |
---|
1689 | IMAGE_TAG_TABLE, |
---|
1690 | array_keys($inserts[0]), |
---|
1691 | $inserts |
---|
1692 | ); |
---|
1693 | } |
---|
1694 | } |
---|
1695 | |
---|
1696 | /** |
---|
1697 | * Add new tags to a set of images. |
---|
1698 | * |
---|
1699 | * @param array tag ids |
---|
1700 | * @param array image ids |
---|
1701 | * @return void |
---|
1702 | */ |
---|
1703 | function add_tags($tags, $images) |
---|
1704 | { |
---|
1705 | if (count($tags) == 0 or count($tags) == 0) |
---|
1706 | { |
---|
1707 | return; |
---|
1708 | } |
---|
1709 | |
---|
1710 | // we can't insert twice the same {image_id,tag_id} so we must first |
---|
1711 | // delete lines we'll insert later |
---|
1712 | $query = ' |
---|
1713 | DELETE |
---|
1714 | FROM '.IMAGE_TAG_TABLE.' |
---|
1715 | WHERE image_id IN ('.implode(',', $images).') |
---|
1716 | AND tag_id IN ('.implode(',', $tags).') |
---|
1717 | ;'; |
---|
1718 | pwg_query($query); |
---|
1719 | |
---|
1720 | $inserts = array(); |
---|
1721 | foreach ($images as $image_id) |
---|
1722 | { |
---|
1723 | foreach ($tags as $tag_id) |
---|
1724 | { |
---|
1725 | array_push( |
---|
1726 | $inserts, |
---|
1727 | array( |
---|
1728 | 'image_id' => $image_id, |
---|
1729 | 'tag_id' => $tag_id, |
---|
1730 | ) |
---|
1731 | ); |
---|
1732 | } |
---|
1733 | } |
---|
1734 | mass_inserts( |
---|
1735 | IMAGE_TAG_TABLE, |
---|
1736 | array_keys($inserts[0]), |
---|
1737 | $inserts |
---|
1738 | ); |
---|
1739 | } |
---|
1740 | |
---|
1741 | function tag_id_from_tag_name($tag_name) |
---|
1742 | { |
---|
1743 | global $page; |
---|
1744 | |
---|
1745 | if (isset($page['tag_id_from_tag_name_cache'][$tag_name])) |
---|
1746 | { |
---|
1747 | return $page['tag_id_from_tag_name_cache'][$tag_name]; |
---|
1748 | } |
---|
1749 | |
---|
1750 | if (function_exists('mysql_real_escape_string')) |
---|
1751 | { |
---|
1752 | $tag_name = mysql_real_escape_string($tag_name); |
---|
1753 | } |
---|
1754 | else |
---|
1755 | { |
---|
1756 | $tag_name = mysql_escape_string($tag_name); |
---|
1757 | } |
---|
1758 | |
---|
1759 | // does the tag already exists? |
---|
1760 | $query = ' |
---|
1761 | SELECT id |
---|
1762 | FROM '.TAGS_TABLE.' |
---|
1763 | WHERE name = \''.$tag_name.'\' |
---|
1764 | ;'; |
---|
1765 | $existing_tags = array_from_query($query, 'id'); |
---|
1766 | |
---|
1767 | if (count($existing_tags) == 0) |
---|
1768 | { |
---|
1769 | mass_inserts( |
---|
1770 | TAGS_TABLE, |
---|
1771 | array('name', 'url_name'), |
---|
1772 | array( |
---|
1773 | array( |
---|
1774 | 'name' => $tag_name, |
---|
1775 | 'url_name' => str2url($tag_name), |
---|
1776 | ) |
---|
1777 | ) |
---|
1778 | ); |
---|
1779 | |
---|
1780 | $page['tag_id_from_tag_name_cache'][$tag_name] = mysql_insert_id(); |
---|
1781 | } |
---|
1782 | else |
---|
1783 | { |
---|
1784 | $page['tag_id_from_tag_name_cache'][$tag_name] = $existing_tags[0]; |
---|
1785 | } |
---|
1786 | |
---|
1787 | return $page['tag_id_from_tag_name_cache'][$tag_name]; |
---|
1788 | } |
---|
1789 | |
---|
1790 | function set_tags_of($tags_of) |
---|
1791 | { |
---|
1792 | if (count($tags_of) > 0) |
---|
1793 | { |
---|
1794 | $query = ' |
---|
1795 | DELETE |
---|
1796 | FROM '.IMAGE_TAG_TABLE.' |
---|
1797 | WHERE image_id IN ('.implode(',', array_keys($tags_of)).') |
---|
1798 | ;'; |
---|
1799 | pwg_query($query); |
---|
1800 | |
---|
1801 | $inserts = array(); |
---|
1802 | |
---|
1803 | foreach ($tags_of as $image_id => $tag_ids) |
---|
1804 | { |
---|
1805 | foreach ($tag_ids as $tag_id) |
---|
1806 | { |
---|
1807 | array_push( |
---|
1808 | $inserts, |
---|
1809 | array( |
---|
1810 | 'image_id' => $image_id, |
---|
1811 | 'tag_id' => $tag_id, |
---|
1812 | ) |
---|
1813 | ); |
---|
1814 | } |
---|
1815 | } |
---|
1816 | |
---|
1817 | mass_inserts( |
---|
1818 | IMAGE_TAG_TABLE, |
---|
1819 | array_keys($inserts[0]), |
---|
1820 | $inserts |
---|
1821 | ); |
---|
1822 | } |
---|
1823 | } |
---|
1824 | |
---|
1825 | /** |
---|
1826 | * Do maintenance on all PWG tables |
---|
1827 | * |
---|
1828 | * @return nono |
---|
1829 | */ |
---|
1830 | function do_maintenance_all_tables() |
---|
1831 | { |
---|
1832 | global $prefixeTable; |
---|
1833 | |
---|
1834 | $all_tables = array(); |
---|
1835 | |
---|
1836 | // List all tables |
---|
1837 | $query = 'SHOW TABLES LIKE \''.$prefixeTable.'%\';'; |
---|
1838 | $result = pwg_query($query); |
---|
1839 | while ($row = mysql_fetch_array($result)) |
---|
1840 | { |
---|
1841 | array_push($all_tables, $row[0]); |
---|
1842 | } |
---|
1843 | |
---|
1844 | // Repair all tables |
---|
1845 | $query = 'REPAIR TABLE '.implode(', ', $all_tables).';'; |
---|
1846 | pwg_query($query); |
---|
1847 | |
---|
1848 | // Re-Order all tables |
---|
1849 | foreach ($all_tables as $table_name) |
---|
1850 | { |
---|
1851 | $all_primary_key = array(); |
---|
1852 | |
---|
1853 | $query = 'DESC '.$table_name.';'; |
---|
1854 | $result = pwg_query($query); |
---|
1855 | while ($row = mysql_fetch_array($result)) |
---|
1856 | { |
---|
1857 | if ($row['Key'] == 'PRI') |
---|
1858 | { |
---|
1859 | array_push($all_primary_key, $row['Field']); |
---|
1860 | } |
---|
1861 | } |
---|
1862 | |
---|
1863 | if (count($all_primary_key) != 0) |
---|
1864 | { |
---|
1865 | $query = 'ALTER TABLE '.$table_name.' ORDER BY '.implode(', ', $all_primary_key).';'; |
---|
1866 | pwg_query($query); |
---|
1867 | } |
---|
1868 | } |
---|
1869 | |
---|
1870 | // Optimize all tables |
---|
1871 | $query = 'OPTIMIZE TABLE '.implode(', ', $all_tables).';'; |
---|
1872 | pwg_query($query); |
---|
1873 | |
---|
1874 | } |
---|
1875 | |
---|
1876 | /** |
---|
1877 | * Associate a list of images to a list of categories. |
---|
1878 | * |
---|
1879 | * The function will not duplicate links |
---|
1880 | * |
---|
1881 | * @param array images |
---|
1882 | * @param array categories |
---|
1883 | * @return void |
---|
1884 | */ |
---|
1885 | function associate_images_to_categories($images, $categories) |
---|
1886 | { |
---|
1887 | if (count($images) == 0 |
---|
1888 | or count($categories) == 0) |
---|
1889 | { |
---|
1890 | return false; |
---|
1891 | } |
---|
1892 | |
---|
1893 | $query = ' |
---|
1894 | DELETE |
---|
1895 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1896 | WHERE image_id IN ('.implode(',', $images).') |
---|
1897 | AND category_id IN ('.implode(',', $categories).') |
---|
1898 | ;'; |
---|
1899 | pwg_query($query); |
---|
1900 | |
---|
1901 | $inserts = array(); |
---|
1902 | foreach ($categories as $category_id) |
---|
1903 | { |
---|
1904 | foreach ($images as $image_id) |
---|
1905 | { |
---|
1906 | array_push( |
---|
1907 | $inserts, |
---|
1908 | array( |
---|
1909 | 'image_id' => $image_id, |
---|
1910 | 'category_id' => $category_id, |
---|
1911 | ) |
---|
1912 | ); |
---|
1913 | } |
---|
1914 | } |
---|
1915 | |
---|
1916 | mass_inserts( |
---|
1917 | IMAGE_CATEGORY_TABLE, |
---|
1918 | array_keys($inserts[0]), |
---|
1919 | $inserts |
---|
1920 | ); |
---|
1921 | |
---|
1922 | update_category($categories); |
---|
1923 | } |
---|
1924 | |
---|
1925 | /** |
---|
1926 | * Associate images associated to a list of source categories to a list of |
---|
1927 | * destination categories. |
---|
1928 | * |
---|
1929 | * @param array sources |
---|
1930 | * @param array destinations |
---|
1931 | * @return void |
---|
1932 | */ |
---|
1933 | function associate_categories_to_categories($sources, $destinations) |
---|
1934 | { |
---|
1935 | if (count($sources) == 0) |
---|
1936 | { |
---|
1937 | return false; |
---|
1938 | } |
---|
1939 | |
---|
1940 | $query = ' |
---|
1941 | SELECT image_id |
---|
1942 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
1943 | WHERE category_id IN ('.implode(',', $sources).') |
---|
1944 | ;'; |
---|
1945 | $images = array_from_query($query, 'image_id'); |
---|
1946 | |
---|
1947 | associate_images_to_categories($images, $destinations); |
---|
1948 | } |
---|
1949 | ?> |
---|