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