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