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