1 | <?php |
---|
2 | /*************************************************************************** |
---|
3 | * update.php * |
---|
4 | * ------------------ * |
---|
5 | * application : PhpWebGallery 1.3 <http://phpwebgallery.net> * |
---|
6 | * author : Pierrick LE GALL <pierrick@z0rglub.com> * |
---|
7 | * * |
---|
8 | * $Id: update.php 377 2004-02-27 17:37:10Z z0rglub $ |
---|
9 | * * |
---|
10 | ***************************************************************************/ |
---|
11 | |
---|
12 | /*************************************************************************** |
---|
13 | * * |
---|
14 | * This program is free software; you can redistribute it and/or modify * |
---|
15 | * it under the terms of the GNU General Public License as published by * |
---|
16 | * the Free Software Foundation; * |
---|
17 | * * |
---|
18 | ***************************************************************************/ |
---|
19 | |
---|
20 | include_once( './include/isadmin.inc.php' ); |
---|
21 | //------------------------------------------------------------------- functions |
---|
22 | function insert_local_category( $id_uppercat ) |
---|
23 | { |
---|
24 | global $conf, $page, $user, $lang; |
---|
25 | |
---|
26 | $uppercats = ''; |
---|
27 | $output = ''; |
---|
28 | // 0. retrieving informations on the category to display |
---|
29 | $cat_directory = '../galleries'; |
---|
30 | |
---|
31 | if ( is_numeric( $id_uppercat ) ) |
---|
32 | { |
---|
33 | $query = 'SELECT name,uppercats,dir'; |
---|
34 | $query.= ' FROM '.PREFIX_TABLE.'categories'; |
---|
35 | $query.= ' WHERE id = '.$id_uppercat; |
---|
36 | $query.= ';'; |
---|
37 | $row = mysql_fetch_array( mysql_query( $query ) ); |
---|
38 | $uppercats = $row['uppercats']; |
---|
39 | $name = $row['name']; |
---|
40 | $dir = $row['dir']; |
---|
41 | |
---|
42 | $upper_array = explode( ',', $uppercats ); |
---|
43 | |
---|
44 | $local_dir = ''; |
---|
45 | |
---|
46 | $database_dirs = array(); |
---|
47 | $query = 'SELECT id,dir'; |
---|
48 | $query.= ' FROM '.PREFIX_TABLE.'categories'; |
---|
49 | $query.= ' WHERE id IN ('.$uppercats.')'; |
---|
50 | $query.= ';'; |
---|
51 | $result = mysql_query( $query ); |
---|
52 | while( $row = mysql_fetch_array( $result ) ) |
---|
53 | { |
---|
54 | $database_dirs[$row['id']] = $row['dir']; |
---|
55 | } |
---|
56 | foreach ( $upper_array as $id ) { |
---|
57 | $local_dir.= $database_dirs[$id].'/'; |
---|
58 | } |
---|
59 | |
---|
60 | $cat_directory.= '/'.$local_dir; |
---|
61 | |
---|
62 | // 1. display the category name to update |
---|
63 | $src = '../template/'.$user['template'].'/admin/images/puce.gif'; |
---|
64 | $output = '<img src="'.$src.'" alt=">" />'; |
---|
65 | $output.= '<span style="font-weight:bold;">'.$name.'</span>'; |
---|
66 | $output.= ' [ '.$dir.' ]'; |
---|
67 | $output.= '<div class="retrait">'; |
---|
68 | |
---|
69 | // 2. we search pictures of the category only if the update is for all |
---|
70 | // or a cat_id is specified |
---|
71 | if ( isset( $page['cat'] ) or $_GET['update'] == 'all' ) |
---|
72 | { |
---|
73 | $output.= insert_local_image( $cat_directory, $id_uppercat ); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | $sub_dirs = get_category_directories( $cat_directory ); |
---|
78 | |
---|
79 | $sub_category_dirs = array(); |
---|
80 | $query = 'SELECT id,dir'; |
---|
81 | $query.= ' FROM '.PREFIX_TABLE.'categories'; |
---|
82 | $query.= ' WHERE site_id = 1'; |
---|
83 | if (!is_numeric($id_uppercat)) $query.= ' AND id_uppercat IS NULL'; |
---|
84 | else $query.= ' AND id_uppercat = '.$id_uppercat; |
---|
85 | $query.= ' AND dir IS NOT NULL'; // virtual categories not taken |
---|
86 | $query.= ';'; |
---|
87 | $result = mysql_query( $query ); |
---|
88 | while ( $row = mysql_fetch_array( $result ) ) |
---|
89 | { |
---|
90 | $sub_category_dirs[$row['id']] = $row['dir']; |
---|
91 | } |
---|
92 | |
---|
93 | // 3. we have to remove the categories of the database not present anymore |
---|
94 | foreach ( $sub_category_dirs as $id => $dir ) { |
---|
95 | if ( !in_array( $dir, $sub_dirs ) ) delete_category( $id ); |
---|
96 | } |
---|
97 | |
---|
98 | // array of new categories to insert |
---|
99 | $inserts = array(); |
---|
100 | |
---|
101 | foreach ( $sub_dirs as $sub_dir ) { |
---|
102 | // 5. Is the category already existing ? we create a subcat if not |
---|
103 | // existing |
---|
104 | $category_id = array_search( $sub_dir, $sub_category_dirs ); |
---|
105 | if ( !is_numeric( $category_id ) ) |
---|
106 | { |
---|
107 | if ( preg_match( '/^[a-zA-Z0-9-_.]+$/', $sub_dir ) ) |
---|
108 | { |
---|
109 | $name = str_replace( '_', ' ', $sub_dir ); |
---|
110 | |
---|
111 | $value = "('".$sub_dir."','".$name."',1"; |
---|
112 | if ( !is_numeric( $id_uppercat ) ) $value.= ',NULL'; |
---|
113 | else $value.= ','.$id_uppercat; |
---|
114 | $value.= ",'undef'"; |
---|
115 | $value.= ')'; |
---|
116 | array_push( $inserts, $value ); |
---|
117 | } |
---|
118 | else |
---|
119 | { |
---|
120 | $output.= '<span style="color:red;">"'.$sub_dir.'" : '; |
---|
121 | $output.= $lang['update_wrong_dirname'].'</span><br />'; |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | // we have to create the category |
---|
127 | if ( count( $inserts ) > 0 ) |
---|
128 | { |
---|
129 | $query = 'INSERT INTO '.PREFIX_TABLE.'categories'; |
---|
130 | $query.= ' (dir,name,site_id,id_uppercat,uppercats) VALUES '; |
---|
131 | $query.= implode( ',', $inserts ); |
---|
132 | $query.= ';'; |
---|
133 | mysql_query( $query ); |
---|
134 | // updating uppercats field |
---|
135 | $query = 'UPDATE '.PREFIX_TABLE.'categories'; |
---|
136 | $query.= ' SET uppercats = '; |
---|
137 | if ( $uppercats != '' ) $query.= "CONCAT('".$uppercats."',',',id)"; |
---|
138 | else $query.= 'id'; |
---|
139 | $query.= ' WHERE id_uppercat '; |
---|
140 | if (!is_numeric($id_uppercat)) $query.= 'IS NULL'; |
---|
141 | else $query.= '= '.$id_uppercat; |
---|
142 | $query.= ';'; |
---|
143 | mysql_query( $query ); |
---|
144 | } |
---|
145 | |
---|
146 | // Recursive call on the sub-categories (not virtual ones) |
---|
147 | $query = 'SELECT id'; |
---|
148 | $query.= ' FROM '.PREFIX_TABLE.'categories'; |
---|
149 | $query.= ' WHERE site_id = 1'; |
---|
150 | if (!is_numeric($id_uppercat)) $query.= ' AND id_uppercat IS NULL'; |
---|
151 | else $query.= ' AND id_uppercat = '.$id_uppercat; |
---|
152 | $query.= ' AND dir IS NOT NULL'; // virtual categories not taken |
---|
153 | $query.= ';'; |
---|
154 | $result = mysql_query( $query ); |
---|
155 | while ( $row = mysql_fetch_array( $result ) ) |
---|
156 | { |
---|
157 | $output.= insert_local_category( $row['id'] ); |
---|
158 | } |
---|
159 | |
---|
160 | if ( is_numeric( $id_uppercat ) ) |
---|
161 | { |
---|
162 | $output.= '</div>'; |
---|
163 | } |
---|
164 | return $output; |
---|
165 | } |
---|
166 | |
---|
167 | function insert_local_image( $dir, $category_id ) |
---|
168 | { |
---|
169 | global $lang,$conf,$count_new; |
---|
170 | |
---|
171 | $output = ''; |
---|
172 | |
---|
173 | // fs means filesystem : $fs_pictures contains pictures in the filesystem |
---|
174 | // found in $dir, $fs_thumbnails contains thumbnails... |
---|
175 | $fs_pictures = get_picture_files( $dir ); |
---|
176 | $fs_thumbnails = get_thumb_files( $dir.'thumbnail' ); |
---|
177 | |
---|
178 | // we have to delete all the images from the database that : |
---|
179 | // - are not in the directory anymore |
---|
180 | // - don't have the associated thumbnail available anymore |
---|
181 | $query = 'SELECT id,file,tn_ext'; |
---|
182 | $query.= ' FROM '.PREFIX_TABLE.'images'; |
---|
183 | $query.= ' WHERE storage_category_id = '.$category_id; |
---|
184 | $query.= ';'; |
---|
185 | $result = mysql_query( $query ); |
---|
186 | while ( $row = mysql_fetch_array( $result ) ) |
---|
187 | { |
---|
188 | $pic_to_delete = false; |
---|
189 | if ( !in_array( $row['file'], $fs_pictures ) ) |
---|
190 | { |
---|
191 | $output.= $row['file']; |
---|
192 | $output.= ' <span style="font-weight:bold;">'; |
---|
193 | $output.= $lang['update_disappeared'].'</span><br />'; |
---|
194 | $pic_to_delete = true; |
---|
195 | } |
---|
196 | |
---|
197 | $thumbnail = $conf['prefix_thumbnail']; |
---|
198 | $thumbnail.= get_filename_wo_extension( $row['file'] ); |
---|
199 | $thumbnail.= '.'.$row['tn_ext']; |
---|
200 | if ( !in_array( $thumbnail, $fs_thumbnails ) ) |
---|
201 | { |
---|
202 | $output.= $row['file']; |
---|
203 | $output.= ' : <span style="font-weight:bold;">'; |
---|
204 | $output.= $lang['update_disappeared_tn'].'</span><br />'; |
---|
205 | $pic_to_delete = true; |
---|
206 | } |
---|
207 | |
---|
208 | if ( $pic_to_delete ) delete_image( $row['id'] ); |
---|
209 | } |
---|
210 | |
---|
211 | $registered_pictures = array(); |
---|
212 | $query = 'SELECT file'; |
---|
213 | $query.= ' FROM '.PREFIX_TABLE.'images'; |
---|
214 | $query.= ' WHERE storage_category_id = '.$category_id; |
---|
215 | $query.= ';'; |
---|
216 | $result = mysql_query( $query ); |
---|
217 | while ( $row = mysql_fetch_array( $result ) ) |
---|
218 | { |
---|
219 | array_push( $registered_pictures, $row['file'] ); |
---|
220 | } |
---|
221 | |
---|
222 | // validated pictures are picture uploaded by users, validated by an admin |
---|
223 | // and not registered (visible) yet |
---|
224 | $validated_pictures = array(); |
---|
225 | $unvalidated_pictures = array(); |
---|
226 | |
---|
227 | $query = 'SELECT file,infos,validated'; |
---|
228 | $query.= ' FROM '.PREFIX_TABLE.'waiting'; |
---|
229 | $query.= ' WHERE storage_category_id = '.$category_id; |
---|
230 | $query.= ';'; |
---|
231 | $result = mysql_query( $query ); |
---|
232 | while ( $row = mysql_fetch_array( $result ) ) |
---|
233 | { |
---|
234 | if ( $row['validated'] == 'true' ) |
---|
235 | $validated_pictures[$row['file']] = $row['infos']; |
---|
236 | else |
---|
237 | array_push( $unvalidated_pictures, $row['file'] ); |
---|
238 | } |
---|
239 | |
---|
240 | // we only search among the picture present in the filesystem and not |
---|
241 | // present in the database yet. If we know that this picture is known as |
---|
242 | // an uploaded one but not validated, it's not tested neither |
---|
243 | $unregistered_pictures = array_diff( $fs_pictures |
---|
244 | ,$registered_pictures |
---|
245 | ,$unvalidated_pictures ); |
---|
246 | |
---|
247 | $inserts = array(); |
---|
248 | |
---|
249 | foreach ( $unregistered_pictures as $unregistered_picture ) { |
---|
250 | if ( preg_match( '/^[a-zA-Z0-9-_.]+$/', $unregistered_picture ) ) |
---|
251 | { |
---|
252 | $file_wo_ext = get_filename_wo_extension( $unregistered_picture ); |
---|
253 | $tn_ext = ''; |
---|
254 | foreach ( $conf['picture_ext'] as $ext ) { |
---|
255 | $test = $conf['prefix_thumbnail'].$file_wo_ext.'.'.$ext; |
---|
256 | if ( !in_array( $test, $fs_thumbnails ) ) continue; |
---|
257 | else { $tn_ext = $ext; break; } |
---|
258 | } |
---|
259 | // if we found a thumnbnail corresponding to our picture... |
---|
260 | if ( $tn_ext != '' ) |
---|
261 | { |
---|
262 | $image_size = @getimagesize( $dir.$unregistered_picture ); |
---|
263 | // (file, storage_category_id, date_available, tn_ext, filesize, |
---|
264 | // width, height, name, author, comment, date_creation)' |
---|
265 | $value = '('; |
---|
266 | $value.= "'".$unregistered_picture."'"; |
---|
267 | $value.= ','.$category_id; |
---|
268 | $value.= ",'".date( 'Y-m-d' )."'"; |
---|
269 | $value.= ",'".$tn_ext."'"; |
---|
270 | $value.= ','.floor( filesize( $dir.$unregistered_picture) / 1024 ); |
---|
271 | $value.= ','.$image_size[0]; |
---|
272 | $value.= ','.$image_size[1]; |
---|
273 | if ( isset( $validated_pictures[$unregistered_picture] ) ) |
---|
274 | { |
---|
275 | // retrieving infos from the XML description from waiting table |
---|
276 | $infos = nl2br( $validated_pictures[$unregistered_picture] ); |
---|
277 | |
---|
278 | $unixtime = getAttribute( $infos, 'date_creation' ); |
---|
279 | if ($unixtime != '') $date_creation ="'".date('Y-m-d',$unixtime)."'"; |
---|
280 | else $date_creation = 'NULL'; |
---|
281 | |
---|
282 | $value.= ",'".getAttribute( $infos, 'name' )."'"; |
---|
283 | $value.= ",'".getAttribute( $infos, 'author' )."'"; |
---|
284 | $value.= ",'".getAttribute( $infos, 'comment')."'"; |
---|
285 | $value.= ','.$date_creation; |
---|
286 | |
---|
287 | // deleting the waiting element |
---|
288 | $query = 'DELETE FROM '.PREFIX_TABLE.'waiting'; |
---|
289 | $query.= " WHERE file = '".$unregistered_picture."'"; |
---|
290 | $query.= ' AND storage_category_id = '.$category_id; |
---|
291 | $query.= ';'; |
---|
292 | mysql_query( $query ); |
---|
293 | } |
---|
294 | else |
---|
295 | { |
---|
296 | $value.= ",'','','',NULL"; |
---|
297 | } |
---|
298 | $value.= ')'; |
---|
299 | |
---|
300 | $count_new++; |
---|
301 | $output.= $unregistered_picture; |
---|
302 | $output.= ' <span style="font-weight:bold;">'; |
---|
303 | $output.= $lang['update_research_added'].'</span>'; |
---|
304 | $output.= ' ('.$lang['update_research_tn_ext'].' '.$tn_ext.')'; |
---|
305 | $output.= '<br />'; |
---|
306 | array_push( $inserts, $value ); |
---|
307 | } |
---|
308 | else |
---|
309 | { |
---|
310 | $output.= '<span style="color:red;">'; |
---|
311 | $output.= $lang['update_missing_tn'].' : '.$unregistered_picture; |
---|
312 | $output.= ' (<span style="font-weight:bold;">'; |
---|
313 | $output.= $conf['prefix_thumbnail']; |
---|
314 | $output.= get_filename_wo_extension( $unregistered_picture ); |
---|
315 | $output.= '.XXX</span>'; |
---|
316 | $output.= ', XXX = '; |
---|
317 | $output.= implode( ', ', $conf['picture_ext'] ); |
---|
318 | $output.= ')</span><br />'; |
---|
319 | } |
---|
320 | } |
---|
321 | else |
---|
322 | { |
---|
323 | $output.= '<span style="color:red;">"'.$unregistered_picture.'" : '; |
---|
324 | $output.= $lang['update_wrong_dirname'].'</span><br />'; |
---|
325 | } |
---|
326 | } |
---|
327 | |
---|
328 | if ( count( $inserts ) > 0 ) |
---|
329 | { |
---|
330 | // inserts all found pictures |
---|
331 | $query = 'INSERT INTO '.PREFIX_TABLE.'images'; |
---|
332 | $query.= ' (file,storage_category_id,date_available,tn_ext'; |
---|
333 | $query.= ',filesize,width,height'; |
---|
334 | $query.= ',name,author,comment,date_creation)'; |
---|
335 | $query.= ' VALUES '; |
---|
336 | $query.= implode( ',', $inserts ); |
---|
337 | $query.= ';'; |
---|
338 | mysql_query( $query ); |
---|
339 | |
---|
340 | // what are the ids of the pictures in the $category_id ? |
---|
341 | $ids = array(); |
---|
342 | |
---|
343 | $query = 'SELECT id'; |
---|
344 | $query.= ' FROM '.PREFIX_TABLE.'images'; |
---|
345 | $query.= ' WHERE storage_category_id = '.$category_id; |
---|
346 | $query.= ';'; |
---|
347 | $result = mysql_query( $query ); |
---|
348 | while ( $row = mysql_fetch_array( $result ) ) |
---|
349 | { |
---|
350 | array_push( $ids, $row['id'] ); |
---|
351 | } |
---|
352 | |
---|
353 | // recreation of the links between this storage category pictures and |
---|
354 | // its storage category |
---|
355 | $query = 'DELETE FROM '.PREFIX_TABLE.'image_category'; |
---|
356 | $query.= ' WHERE category_id = '.$category_id; |
---|
357 | $query.= ' AND image_id IN ('.implode( ',', $ids ).')'; |
---|
358 | $query.= ';'; |
---|
359 | mysql_query( $query ); |
---|
360 | |
---|
361 | $query = 'INSERT INTO '.PREFIX_TABLE.'image_category'; |
---|
362 | $query.= '(category_id,image_id) VALUES '; |
---|
363 | foreach ( $ids as $num => $image_id ) { |
---|
364 | if ( $num > 0 ) $query.= ','; |
---|
365 | $query.= '('.$category_id.','.$image_id.')'; |
---|
366 | } |
---|
367 | $query.= ';'; |
---|
368 | mysql_query( $query ); |
---|
369 | } |
---|
370 | return $output; |
---|
371 | } |
---|
372 | |
---|
373 | // remote_images verifies if a file named "listing.xml" is present is the |
---|
374 | // admin directory. If it is the case, creation of a remote picture storage |
---|
375 | // site if it doesn't already exists. Then, the function calls |
---|
376 | // insert_remote_category for this remote site on the root category. |
---|
377 | function remote_images() |
---|
378 | { |
---|
379 | global $conf, $lang, $vtp, $sub; |
---|
380 | |
---|
381 | // 1. is there a file listing.xml ? |
---|
382 | if ( !( $xml_content = getXmlCode( 'listing.xml' ) ) ) |
---|
383 | { |
---|
384 | return false; |
---|
385 | } |
---|
386 | $url = getContent( getChild( $xml_content, 'url' ) ); |
---|
387 | $vtp->setVar( $sub, 'remote_update.url', $url ); |
---|
388 | |
---|
389 | // 2. is the site already existing ? |
---|
390 | $query = 'SELECT id'; |
---|
391 | $query.= ' FROM '.PREFIX_TABLE.'sites'; |
---|
392 | $query.= " WHERE galleries_url = '".$url."'"; |
---|
393 | $query.= ';'; |
---|
394 | $result = mysql_query( $query ); |
---|
395 | if ( mysql_num_rows($result ) == 0 ) |
---|
396 | { |
---|
397 | // we have to register this site in the database |
---|
398 | $query = 'INSERT INTO '.PREFIX_TABLE.'sites'; |
---|
399 | $query.= " (galleries_url) VALUES ('".$url."')"; |
---|
400 | $query.= ';'; |
---|
401 | mysql_query( $query ); |
---|
402 | $site_id = mysql_insert_id(); |
---|
403 | } |
---|
404 | else |
---|
405 | { |
---|
406 | // we get the already registered id |
---|
407 | $row = mysql_fetch_array( $result ); |
---|
408 | $site_id = $row['id']; |
---|
409 | } |
---|
410 | |
---|
411 | // 3. available dirs in the file |
---|
412 | $categories = insert_remote_category( $xml_content, $site_id, 'NULL', 0 ); |
---|
413 | $vtp->setVar( $sub, 'remote_update.categories', $categories ); |
---|
414 | } |
---|
415 | |
---|
416 | // insert_remote_category searchs the "dir" node of the xml_dir given and |
---|
417 | // insert the contained categories if the are not in the database yet. The |
---|
418 | // function also deletes the categories that are in the database and not in |
---|
419 | // the xml_file. |
---|
420 | function insert_remote_category( $xml_content, $site_id, $id_uppercat, $level ) |
---|
421 | { |
---|
422 | global $conf, $page, $user, $lang; |
---|
423 | |
---|
424 | $uppercats = ''; |
---|
425 | $output = ''; |
---|
426 | // 0. retrieving informations on the category to display |
---|
427 | $cat_directory = '../galleries'; |
---|
428 | |
---|
429 | if ( is_numeric( $id_uppercat ) ) |
---|
430 | { |
---|
431 | $query = 'SELECT name,uppercats,dir'; |
---|
432 | $query.= ' FROM '.PREFIX_TABLE.'categories'; |
---|
433 | $query.= ' WHERE id = '.$id_uppercat; |
---|
434 | $query.= ';'; |
---|
435 | $row = mysql_fetch_array( mysql_query( $query ) ); |
---|
436 | $uppercats = $row['uppercats']; |
---|
437 | $name = $row['name']; |
---|
438 | |
---|
439 | // 1. display the category name to update |
---|
440 | $src = '../template/'.$user['template'].'/admin/images/puce.gif'; |
---|
441 | $output = '<img src="'.$src.'" alt=">" />'; |
---|
442 | $output.= '<span style="font-weight:bold;">'.$name.'</span>'; |
---|
443 | $output.= ' [ '.$row['dir'].' ]'; |
---|
444 | $output.= '<div class="retrait">'; |
---|
445 | |
---|
446 | // 2. we search pictures of the category only if the update is for all |
---|
447 | // or a cat_id is specified |
---|
448 | $output.= insert_remote_image( $xml_content, $id_uppercat ); |
---|
449 | } |
---|
450 | |
---|
451 | // $xml_dirs contains dir names contained in the xml file for this |
---|
452 | // id_uppercat |
---|
453 | $xml_dirs = array(); |
---|
454 | $temp_dirs = getChildren( $xml_content, 'dir'.$level ); |
---|
455 | foreach ( $temp_dirs as $temp_dir ) { |
---|
456 | array_push( $xml_dirs, getAttribute( $temp_dir, 'name' ) ); |
---|
457 | } |
---|
458 | |
---|
459 | // $database_dirs contains dir names contained in the database for this |
---|
460 | // id_uppercat and site_id |
---|
461 | $database_dirs = array(); |
---|
462 | $query = 'SELECT id,dir'; |
---|
463 | $query.= ' FROM '.PREFIX_TABLE.'categories'; |
---|
464 | $query.= ' WHERE site_id = '.$site_id; |
---|
465 | if (!is_numeric($id_uppercat)) $query.= ' AND id_uppercat IS NULL'; |
---|
466 | else $query.= ' AND id_uppercat = '.$id_uppercat; |
---|
467 | $query.= ' AND dir IS NOT NULL'; // virtual categories not taken |
---|
468 | $query.= ';'; |
---|
469 | $result = mysql_query( $query ); |
---|
470 | while ( $row = mysql_fetch_array( $result ) ) |
---|
471 | { |
---|
472 | $database_dirs[$row['id']] = $row['dir']; |
---|
473 | } |
---|
474 | |
---|
475 | // 3. we have to remove the categories of the database not present anymore |
---|
476 | foreach ( $database_dirs as $id => $dir ) { |
---|
477 | if ( !in_array( $dir, $xml_dirs ) ) delete_category( $id ); |
---|
478 | } |
---|
479 | |
---|
480 | // array of new categories to insert |
---|
481 | $inserts = array(); |
---|
482 | |
---|
483 | foreach ( $xml_dirs as $xml_dir ) { |
---|
484 | // 5. Is the category already existing ? we create a subcat if not |
---|
485 | // existing |
---|
486 | $category_id = array_search( $xml_dir, $database_dirs ); |
---|
487 | if ( !is_numeric( $category_id ) ) |
---|
488 | { |
---|
489 | $name = str_replace( '_', ' ', $xml_dir ); |
---|
490 | |
---|
491 | $value = "('".$xml_dir."','".$name."',".$site_id; |
---|
492 | if ( !is_numeric( $id_uppercat ) ) $value.= ',NULL'; |
---|
493 | else $value.= ','.$id_uppercat; |
---|
494 | $value.= ",'undef'"; |
---|
495 | $value.= ')'; |
---|
496 | array_push( $inserts, $value ); |
---|
497 | } |
---|
498 | } |
---|
499 | |
---|
500 | // we have to create the category |
---|
501 | if ( count( $inserts ) > 0 ) |
---|
502 | { |
---|
503 | $query = 'INSERT INTO '.PREFIX_TABLE.'categories'; |
---|
504 | $query.= ' (dir,name,site_id,id_uppercat,uppercats) VALUES '; |
---|
505 | $query.= implode( ',', $inserts ); |
---|
506 | $query.= ';'; |
---|
507 | mysql_query( $query ); |
---|
508 | // updating uppercats field |
---|
509 | $query = 'UPDATE '.PREFIX_TABLE.'categories'; |
---|
510 | $query.= ' SET uppercats = '; |
---|
511 | if ( $uppercats != '' ) $query.= "CONCAT('".$uppercats."',',',id)"; |
---|
512 | else $query.= 'id'; |
---|
513 | $query.= ' WHERE id_uppercat '; |
---|
514 | if (!is_numeric($id_uppercat)) $query.= 'IS NULL'; |
---|
515 | else $query.= '= '.$id_uppercat; |
---|
516 | $query.= ';'; |
---|
517 | mysql_query( $query ); |
---|
518 | } |
---|
519 | |
---|
520 | // Recursive call on the sub-categories (not virtual ones) |
---|
521 | $query = 'SELECT id,dir'; |
---|
522 | $query.= ' FROM '.PREFIX_TABLE.'categories'; |
---|
523 | $query.= ' WHERE site_id = '.$site_id; |
---|
524 | if (!is_numeric($id_uppercat)) $query.= ' AND id_uppercat IS NULL'; |
---|
525 | else $query.= ' AND id_uppercat = '.$id_uppercat; |
---|
526 | $query.= ' AND dir IS NOT NULL'; // virtual categories not taken |
---|
527 | $query.= ';'; |
---|
528 | $result = mysql_query( $query ); |
---|
529 | while ( $row = mysql_fetch_array( $result ) ) |
---|
530 | { |
---|
531 | $database_dirs[$row['dir']] = $row['id']; |
---|
532 | } |
---|
533 | foreach ( $temp_dirs as $temp_dir ) { |
---|
534 | $dir = getAttribute( $temp_dir, 'name' ); |
---|
535 | $id_uppercat = $database_dirs[$dir]; |
---|
536 | $output.= insert_remote_category( $temp_dir, $site_id, |
---|
537 | $id_uppercat,$level+1 ); |
---|
538 | } |
---|
539 | |
---|
540 | if ( is_numeric( $id_uppercat ) ) $output.= '</div>'; |
---|
541 | |
---|
542 | return $output; |
---|
543 | } |
---|
544 | |
---|
545 | // insert_remote_image searchs the "root" node of the xml_dir given and |
---|
546 | // insert the contained pictures if the are not in the database yet. |
---|
547 | function insert_remote_image( $xml_dir, $category_id ) |
---|
548 | { |
---|
549 | global $count_new,$lang; |
---|
550 | |
---|
551 | $output = ''; |
---|
552 | $root = getChild( $xml_dir, 'root' ); |
---|
553 | |
---|
554 | $fs_pictures = array(); |
---|
555 | $xml_pictures = getChildren( $root, 'picture' ); |
---|
556 | foreach ( $xml_pictures as $xml_picture ) { |
---|
557 | array_push( $fs_pictures, getAttribute( $xml_picture, 'file' ) ); |
---|
558 | } |
---|
559 | |
---|
560 | // we have to delete all the images from the database that are not in the |
---|
561 | // directory anymore (not in the XML anymore) |
---|
562 | $query = 'SELECT id,file'; |
---|
563 | $query.= ' FROM '.PREFIX_TABLE.'images'; |
---|
564 | $query.= ' WHERE storage_category_id = '.$category_id; |
---|
565 | $query.= ';'; |
---|
566 | $result = mysql_query( $query ); |
---|
567 | while ( $row = mysql_fetch_array( $result ) ) |
---|
568 | { |
---|
569 | if ( !in_array( $row['file'], $fs_pictures ) ) |
---|
570 | { |
---|
571 | $output.= $row['file']; |
---|
572 | $output.= ' <span style="font-weight:bold;">'; |
---|
573 | $output.= $lang['update_disappeared'].'</span><br />'; |
---|
574 | delete_image( $row['id'] ); |
---|
575 | } |
---|
576 | } |
---|
577 | |
---|
578 | $database_pictures = array(); |
---|
579 | $query = 'SELECT file'; |
---|
580 | $query.= ' FROM '.PREFIX_TABLE.'images'; |
---|
581 | $query.= ' WHERE storage_category_id = '.$category_id; |
---|
582 | $query.= ';'; |
---|
583 | $result = mysql_query( $query ); |
---|
584 | while ( $row = mysql_fetch_array( $result ) ) |
---|
585 | { |
---|
586 | array_push( $database_pictures, $row['file'] ); |
---|
587 | } |
---|
588 | |
---|
589 | $inserts = array(); |
---|
590 | $xml_pictures = getChildren( $root, 'picture' ); |
---|
591 | foreach ( $xml_pictures as $xml_picture ) { |
---|
592 | // <picture file="albatros.jpg" tn_ext="png" filesize="35" width="640" |
---|
593 | // height="480" /> |
---|
594 | $file = getAttribute( $xml_picture, 'file' ); |
---|
595 | |
---|
596 | // is the picture already existing in the database ? |
---|
597 | if ( !in_array( $file, $database_pictures ) ) |
---|
598 | { |
---|
599 | $tn_ext = getAttribute( $xml_picture, 'tn_ext' ); |
---|
600 | // (file, storage_category_id, date_available, tn_ext, filesize, |
---|
601 | // width, height) |
---|
602 | $value = '('; |
---|
603 | $value.= "'".$file."'"; |
---|
604 | $value.= ','.$category_id; |
---|
605 | $value.= ",'".date( 'Y-m-d' )."'"; |
---|
606 | $value.= ",'".$tn_ext."'"; |
---|
607 | $value.= ','.getAttribute( $xml_picture, 'filesize' ); |
---|
608 | $value.= ','.getAttribute( $xml_picture, 'width' ); |
---|
609 | $value.= ','.getAttribute( $xml_picture, 'height' ); |
---|
610 | $value.= ')'; |
---|
611 | |
---|
612 | $count_new++; |
---|
613 | $output.= $file; |
---|
614 | $output.= ' <span style="font-weight:bold;">'; |
---|
615 | $output.= $lang['update_research_added'].'</span>'; |
---|
616 | $output.= ' ('.$lang['update_research_tn_ext'].' '.$tn_ext.')'; |
---|
617 | $output.= '<br />'; |
---|
618 | array_push( $inserts, $value ); |
---|
619 | } |
---|
620 | } |
---|
621 | |
---|
622 | if ( count( $inserts ) > 0 ) |
---|
623 | { |
---|
624 | // inserts all found pictures |
---|
625 | $query = 'INSERT INTO '.PREFIX_TABLE.'images'; |
---|
626 | $query.= ' (file,storage_category_id,date_available,tn_ext'; |
---|
627 | $query.= ',filesize,width,height)'; |
---|
628 | $query.= ' VALUES '; |
---|
629 | $query.= implode( ',', $inserts ); |
---|
630 | $query.= ';'; |
---|
631 | mysql_query( $query ); |
---|
632 | |
---|
633 | // what are the ids of the pictures in the $category_id ? |
---|
634 | $ids = array(); |
---|
635 | |
---|
636 | $query = 'SELECT id'; |
---|
637 | $query.= ' FROM '.PREFIX_TABLE.'images'; |
---|
638 | $query.= ' WHERE storage_category_id = '.$category_id; |
---|
639 | $query.= ';'; |
---|
640 | $result = mysql_query( $query ); |
---|
641 | while ( $row = mysql_fetch_array( $result ) ) |
---|
642 | { |
---|
643 | array_push( $ids, $row['id'] ); |
---|
644 | } |
---|
645 | |
---|
646 | // recreation of the links between this storage category pictures and |
---|
647 | // its storage category |
---|
648 | $query = 'DELETE FROM '.PREFIX_TABLE.'image_category'; |
---|
649 | $query.= ' WHERE category_id = '.$category_id; |
---|
650 | $query.= ' AND image_id IN ('.implode( ',', $ids ).')'; |
---|
651 | $query.= ';'; |
---|
652 | mysql_query( $query ); |
---|
653 | |
---|
654 | $query = 'INSERT INTO '.PREFIX_TABLE.'image_category'; |
---|
655 | $query.= '(category_id,image_id) VALUES '; |
---|
656 | foreach ( $ids as $num => $image_id ) { |
---|
657 | if ( $num > 0 ) $query.= ','; |
---|
658 | $query.= '('.$category_id.','.$image_id.')'; |
---|
659 | } |
---|
660 | $query.= ';'; |
---|
661 | mysql_query( $query ); |
---|
662 | } |
---|
663 | |
---|
664 | return $output; |
---|
665 | } |
---|
666 | //----------------------------------------------------- template initialization |
---|
667 | $sub = $vtp->Open( '../template/'.$user['template'].'/admin/update.vtp' ); |
---|
668 | $tpl = array( 'update_default_title', 'update_only_cat', 'update_all', |
---|
669 | 'update_research_conclusion', 'update_deletion_conclusion', |
---|
670 | 'remote_site', 'update_part_research' ); |
---|
671 | templatize_array( $tpl, 'lang', $sub ); |
---|
672 | $vtp->setGlobalVar( $sub, 'user_template', $user['template'] ); |
---|
673 | //-------------------------------------------- introduction : choices of update |
---|
674 | // Display choice if "update" var is not specified |
---|
675 | if (!isset( $_GET['update'] )) |
---|
676 | { |
---|
677 | $vtp->addSession( $sub, 'introduction' ); |
---|
678 | // only update the categories, not the pictures. |
---|
679 | $url = add_session_id( './admin.php?page=update&update=cats' ); |
---|
680 | $vtp->setVar( $sub, 'introduction.only_cat:url', $url ); |
---|
681 | // update the entire tree folder |
---|
682 | $url = add_session_id( './admin.php?page=update&update=all' ); |
---|
683 | $vtp->setVar( $sub, 'introduction.all:url', $url ); |
---|
684 | $vtp->closeSession( $sub, 'introduction' ); |
---|
685 | } |
---|
686 | //------------------------------------------------- local update : ../galleries |
---|
687 | else |
---|
688 | { |
---|
689 | check_cat_id( $_GET['update'] ); |
---|
690 | $start = get_moment(); |
---|
691 | $count_new = 0; |
---|
692 | $count_deleted = 0; |
---|
693 | $vtp->addSession( $sub, 'local_update' ); |
---|
694 | if ( isset( $page['cat'] ) ) |
---|
695 | { |
---|
696 | $categories = insert_local_category( $page['cat'] ); |
---|
697 | } |
---|
698 | else |
---|
699 | { |
---|
700 | $categories = insert_local_category( 'NULL' ); |
---|
701 | } |
---|
702 | $end = get_moment(); |
---|
703 | // echo get_elapsed_time( $start, $end ).' for update <br />'; |
---|
704 | $vtp->setVar( $sub, 'local_update.categories', $categories ); |
---|
705 | $vtp->setVar( $sub, 'local_update.count_new', $count_new ); |
---|
706 | $vtp->setVar( $sub, 'local_update.count_deleted', $count_deleted ); |
---|
707 | $vtp->closeSession( $sub, 'local_update' ); |
---|
708 | } |
---|
709 | //------------------------------------------------- remote update : listing.xml |
---|
710 | if ( @is_file( './listing.xml' ) ) |
---|
711 | { |
---|
712 | $count_new = 0; |
---|
713 | $count_deleted = 0; |
---|
714 | $vtp->addSession( $sub, 'remote_update' ); |
---|
715 | |
---|
716 | $start = get_moment(); |
---|
717 | remote_images(); |
---|
718 | $end = get_moment(); |
---|
719 | // echo get_elapsed_time( $start, $end ).' for remote_images<br />'; |
---|
720 | |
---|
721 | $vtp->setVar( $sub, 'remote_update.count_new', $count_new ); |
---|
722 | $vtp->setVar( $sub, 'remote_update.count_deleted', $count_deleted ); |
---|
723 | |
---|
724 | $vtp->closeSession( $sub, 'remote_update' ); |
---|
725 | } |
---|
726 | //---------------------------------------- update informations about categories |
---|
727 | if ( isset( $_GET['update'] ) |
---|
728 | or isset( $page['cat'] ) |
---|
729 | or @is_file( './listing.xml' ) ) |
---|
730 | { |
---|
731 | $start = get_moment(); |
---|
732 | update_category( 'all' ); |
---|
733 | $end = get_moment(); |
---|
734 | // echo get_elapsed_time( $start, $end ).' for update_category( all )<br />'; |
---|
735 | |
---|
736 | $start = get_moment(); |
---|
737 | synchronize_all_users(); |
---|
738 | $end = get_moment(); |
---|
739 | // echo get_elapsed_time( $start, $end ).' for synchronize_all_users<br />'; |
---|
740 | } |
---|
741 | //----------------------------------------------------------- sending html code |
---|
742 | $vtp->Parse( $handle , 'sub', $sub ); |
---|
743 | ?> |
---|