source: trunk/admin/update.php @ 134

Last change on this file since 134 was 130, checked in by z0rglub, 21 years ago

If we delete a picture which is logically linked to a category, the
informations about this category are not updated (number of pictures and
date of last picture) -> use of update_category( 'all' )

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 21.0 KB
Line 
1<?php
2/***************************************************************************
3 *                                update.php                               *
4 *                            ------------------                           *
5 *   application   : PhpWebGallery 1.3 <http://phpwebgallery.net>          *
6 *   author        : Pierrick LE GALL <pierrick@z0rglub.com>               *
7 *                                                                         *
8 *   $Id: update.php 130 2003-09-17 20:40:15Z z0rglub $
9 *                                                                         *
10 ***************************************************************************/
11
12/***************************************************************************
13 *                                                                         *
14 *   This program is free software; you can redistribute it and/or modify  *
15 *   it under the terms of the GNU General Public License as published by  *
16 *   the Free Software Foundation;                                         *
17 *                                                                         *
18 ***************************************************************************/
19
20include_once( './include/isadmin.inc.php' );
21//------------------------------------------------------------------- functions
22function insert_local_category( $cat_id )
23{
24  global $conf, $page, $user, $lang;
25               
26  $site_id = 1;
27               
28  // 0. retrieving informations on the category to display
29  $cat_directory = '../galleries';
30               
31  if ( is_numeric( $cat_id ) )
32  {
33    $cat_directory.= '/'.get_local_dir( $cat_id );
34    $result = get_cat_info( $cat_id );
35    // 1. display the category name to update
36    $src = '../template/'.$user['template'].'/admin/images/puce.gif';
37    $output = '<img src="'.$src.'" alt="&gt;" />';
38    $output.= '<span style="font-weight:bold;">'.$result['name'][0].'</span>';
39    $output.= ' [ '.$result['dir'].' ]';
40    $output.= '<div class="retrait">';
41
42    // 2. we search pictures of the category only if the update is for all
43    //    or a cat_id is specified
44    if ( isset( $page['cat'] ) or $_GET['update'] == 'all' )
45    {
46      $output.= insert_local_image( $cat_directory, $cat_id );
47    }
48  }
49
50  // 3. we have to remove the categories of the database not present anymore
51  $query = 'SELECT id';
52  $query.= ' FROM '.PREFIX_TABLE.'categories';
53  $query.= ' WHERE site_id = '.$site_id;
54  if ( !is_numeric( $cat_id ) )
55  {
56    $query.= ' AND id_uppercat IS NULL';
57  }
58  else
59  {
60    $query.= ' AND id_uppercat = '.$cat_id;
61  }
62  $query.= ';';
63  $result = mysql_query( $query );
64  while ( $row = mysql_fetch_array( $result ) )
65  {
66    // retrieving the directory
67    $rep = '../galleries/'.get_local_dir( $row['id'] );
68    // is the directory present ?
69    if ( !is_dir( $rep ) ) delete_category( $row['id'] );
70  }
71  // 4. retrieving the sub-directories
72  $subdirs = array();
73  $dirs = '';
74  if ( $opendir = opendir( $cat_directory ) )
75  {
76    while ( $file = readdir( $opendir ) )
77    {
78      if ( $file != '.'
79           and $file != '..'
80           and is_dir ( $cat_directory.'/'.$file )
81           and $file != 'thumbnail' )
82      {
83        if ( preg_match( '/^[a-zA-Z0-9-_.]+$/', $file ) )
84          array_push( $subdirs, $file );
85        else
86        {
87          $output.= '<span style="color:red;">"'.$file.'" : ';
88          $output.= $lang['update_wrong_dirname'].'</span><br />';
89        }
90      }
91    }
92  }
93  foreach ( $subdirs as $subdir ) {
94    // 5. Is the category already existing ? we create a subcat if not
95    //    existing
96    $category_id = '';
97    $query = 'SELECT id';
98    $query.= ' FROM '.PREFIX_TABLE.'categories';
99    $query.= ' WHERE site_id = '.$site_id;
100    $query.= " AND dir = '".$subdir."'";
101    $query.= ' AND id_uppercat';
102    if ( !is_numeric( $cat_id ) ) $query.= ' IS NULL';
103    else                          $query.= ' = '.$cat_id;
104    $query.= ';';
105    $result = mysql_query( $query );
106    if ( mysql_num_rows( $result ) == 0 )
107    {
108      $name = str_replace( '_', ' ', $subdir );
109      // we have to create the category
110      $query = 'INSERT INTO '.PREFIX_TABLE.'categories';
111      $query.= ' (dir,name,site_id,id_uppercat) VALUES';
112      $query.= " ('".$subdir."','".$name."','".$site_id."'";
113      if ( !is_numeric( $cat_id ) ) $query.= ',NULL';
114      else                          $query.= ",'".$cat_id."'";
115      $query.= ');';
116      mysql_query( $query );
117      $category_id = mysql_insert_id();
118      // regeneration of the plain_structure to integrate the new category
119      $page['plain_structure'] = get_plain_structure();
120    }
121    else
122    {
123      // we get the already registered id
124      $row = mysql_fetch_array( $result );
125      $category_id = $row['id'];
126    }
127    // 6. recursive call
128    $output.= insert_local_category( $category_id );
129  }
130               
131  if ( is_numeric( $cat_id ) )
132  {
133    $output.= '</div>';
134  }
135  return $output;
136}
137       
138function insert_local_image( $rep, $category_id )
139{
140  global $lang,$conf,$count_new;
141
142  $output = '';
143  // we have to delete all the images from the database that :
144  //     - are not in the directory anymore
145  //     - don't have the associated thumbnail available anymore
146  $query = 'SELECT id,file,tn_ext';
147  $query.= ' FROM '.PREFIX_TABLE.'images';
148  $query.= ' WHERE storage_category_id = '.$category_id;
149  $query.= ';';
150  $result = mysql_query( $query );
151  while ( $row = mysql_fetch_array( $result ) )
152  {
153    $lien_image = $rep.'/'.$row['file'];
154    $lien_thumbnail = $rep.'/thumbnail/'.$conf['prefix_thumbnail'];
155    $lien_thumbnail.= get_filename_wo_extension( $row['file'] );
156    $lien_thumbnail.= '.'.$row['tn_ext'];
157               
158    if ( !is_file ( $lien_image ) or !is_file ( $lien_thumbnail ) )
159    {
160      if ( !is_file ( $lien_image ) )
161      {
162        $output.= $row['file'];
163        $output.= ' <span style="font-weight:bold;">';
164        $output.= $lang['update_disappeared'].'</span><br />';
165      }
166      if ( !is_file ( $lien_thumbnail ) )
167      {
168        $output.= $row['file'];
169        $output.= ' : <span style="font-weight:bold;">';
170        $output.= $lang['update_disappeared_tn'].'</span><br />';
171      }
172      // suppression de la base :
173      delete_image( $row['id'] );
174    }
175  }
176               
177  // searching the new images in the directory
178  $pictures = array();         
179  $tn_ext = '';
180  if ( $opendir = opendir( $rep ) )
181  {
182    while ( $file = readdir( $opendir ) )
183    {
184      if ( is_file( $rep.'/'.$file ) and is_image( $rep.'/'.$file ) )
185      {
186        // is the picture waiting for validation by an administrator ?
187        $query = 'SELECT id,validated,infos';
188        $query.= ' FROM '.PREFIX_TABLE.'waiting';
189        $query.= ' WHERE storage_category_id = '.$category_id;
190        $query.= " AND file = '".$file."'";
191        $query.= ';';
192        $result = mysql_query( $query );
193        $waiting = mysql_fetch_array( $result );
194        if (mysql_num_rows( $result ) == 0 or $waiting['validated'] == 'true')
195        {
196          if ( $tn_ext = TN_exists( $rep, $file ) )
197          {
198            // is the picture already in the database ?
199            $query = 'SELECT id';
200            $query.= ' FROM '.PREFIX_TABLE.'images';
201            $query.= ' WHERE storage_category_id = '.$category_id;
202            $query.= " AND file = '".$file."'";
203            $query.= ';';
204            $result = mysql_query( $query );
205            if ( mysql_num_rows( $result ) == 0 )
206            {
207              $picture = array();
208              $picture['file']     = $file;
209              $picture['tn_ext']   = $tn_ext;
210              $picture['date'] = date( 'Y-m-d', filemtime ( $rep.'/'.$file ) );
211              $picture['filesize'] = floor( filesize( $rep.'/'.$file ) / 1024);
212              $image_size = @getimagesize( $rep.'/'.$file );
213              $picture['width']    = $image_size[0];
214              $picture['height']   = $image_size[1];
215              if ( $waiting['validated'] == 'true' )
216              {
217                // retrieving infos from the XML description of
218                // $waiting['infos']
219                $infos = nl2br( $waiting['infos'] );
220                $picture['author']        = getAttribute( $infos, 'author' );
221                $picture['comment']       = getAttribute( $infos, 'comment' );
222                $unixtime = getAttribute( $infos, 'date_creation' );
223                $picture['date_creation'] = '';
224                if ( $unixtime != '' )
225                {
226                  $picture['date_creation'] = date( 'Y-m-d', $unixtime );
227                }
228                $picture['name']          = getAttribute( $infos, 'name' );
229                // deleting the waiting element
230                $query = 'DELETE FROM '.PREFIX_TABLE.'waiting';
231                $query.= ' WHERE id = '.$waiting['id'];
232                $query.= ';';
233                mysql_query( $query );
234              }
235              array_push( $pictures, $picture );
236            }
237          }
238          else
239          {
240            $output.= '<span style="color:red;">';
241            $output.= $lang['update_missing_tn'].' : '.$file;
242            $output.= ' (<span style="font-weight:bold;">';
243            $output.= $conf['prefix_thumbnail'];
244            $output.= get_filename_wo_extension( $file ).'.XXX</span>';
245            $output.= ', XXX = ';
246            $output.= implode( ', ', $conf['picture_ext'] );
247            $output.= ')</span><br />';
248          }
249        }
250      }
251    }
252  }
253  // inserting the pictures found in the directory
254  foreach ( $pictures as $picture ) {
255    $query = 'INSERT INTO '.PREFIX_TABLE.'images';
256    $query.= ' (file,storage_category_id,date_available,tn_ext';
257    $query.= ',filesize,width,height';
258    $query.= ',name,author,comment,date_creation)';
259    $query.= ' VALUES ';
260    $query.= "('".$picture['file']."','".$category_id."'";
261    $query.= ",'".$picture['date']."','".$picture['tn_ext']."'";
262    $query.= ",'".$picture['filesize']."','".$picture['width']."'";
263    $query.= ",'".$picture['height']."','".$picture['name']."'";
264    $query.= ",'".$picture['author']."','".$picture['comment']."'";
265    if ( $picture['date_creation'] != '' )
266    {
267      $query.= ",'".$picture['date_creation']."'";
268    }
269    else
270    {
271      $query.= ',NULL';
272    }
273    $query.= ');';
274    mysql_query( $query );
275    $count_new++;
276    // retrieving the id of newly inserted picture
277    $query = 'SELECT id';
278    $query.= ' FROM '.PREFIX_TABLE.'images';
279    $query.= ' WHERE storage_category_id = '.$category_id;
280    $query.= " AND file = '".$picture['file']."'";
281    $query.= ';';
282    list( $image_id ) = mysql_fetch_array( mysql_query( $query ) );
283    // adding the link between this picture and its storage category
284    $query = 'INSERT INTO '.PREFIX_TABLE.'image_category';
285    $query.= ' (image_id,category_id) VALUES ';
286    $query.= ' ('.$image_id.','.$category_id.')';
287    $query.= ';';
288    mysql_query( $query );
289
290    $output.= $picture['file'];
291    $output.= ' <span style="font-weight:bold;">';
292    $output.= $lang['update_research_added'].'</span>';
293    $output.= ' ('.$lang['update_research_tn_ext'].' '.$picture['tn_ext'].')';
294    $output.= '<br />';
295  }
296  return $output;
297}
298
299// remote_images verifies if a file named "listing.xml" is present is the
300// admin directory. If it is the case, creation of a remote picture storage
301// site if it doesn't already exists. Then, the function calls
302// insert_remote_category for this remote site on the root category.
303function remote_images()
304{
305  global $conf, $lang, $vtp, $sub;
306
307  // 1. is there a file listing.xml ?
308  if ( !( $xml_content = getXmlCode( 'listing.xml' ) ) )
309  {
310    return false;
311  }
312  $url = getContent( getChild( $xml_content, 'url' ) );
313  $vtp->setVar( $sub, 'remote_update.url', $url );
314
315  // 2. is the site already existing ?
316  $query = 'SELECT id';
317  $query.= ' FROM '.PREFIX_TABLE.'sites';
318  $query.= " WHERE galleries_url = '".$url."'";
319  $query.= ';';
320  $result = mysql_query( $query );
321  if ( mysql_num_rows($result ) == 0 )
322  {
323    // we have to register this site in the database
324    $query = 'INSERT INTO '.PREFIX_TABLE.'sites';
325    $query.= " (galleries_url) VALUES ('".$url."')";
326    $query.= ';';
327    mysql_query( $query );
328    $site_id = mysql_insert_id();
329  }
330  else
331  {
332    // we get the already registered id
333    $row = mysql_fetch_array( $result );
334    $site_id = $row['id'];
335  }
336
337  // 3. available dirs in the file
338  $categories = insert_remote_category( $xml_content, $site_id, 'NULL', 0 );
339  $vtp->setVar( $sub, 'remote_update.categories', $categories );
340}
341
342// insert_remote_category searchs the "dir" node of the xml_dir given and
343// insert the contained categories if the are not in the database yet. The
344// function also deletes the categories that are in the database and not in
345// the xml_file.
346function insert_remote_category( $xml_dir, $site_id, $id_uppercat, $level )
347{
348  global $conf,$user;
349
350  $output = '';
351  $categories = array();
352  $list_dirs = getChildren( $xml_dir, 'dir'.$level );
353  for ( $i = 0; $i < sizeof( $list_dirs ); $i++ )
354  {
355    // is the category already existing ?
356    $category_id = '';
357    $dir = getAttribute( $list_dirs[$i], 'name' );
358    $categories[$i] = $dir;
359
360    $src = '../template/'.$user['template'].'/admin/images/puce.gif';
361    $output.= '<img src="'.$src.'" alt="&gt;" />';
362    $output.= '<span style="font-weight:bold;">'.$dir.'</span>';
363    $output.= '<div class="retrait">';
364
365    $query = 'SELECT id';
366    $query.= ' FROM '.PREFIX_TABLE.'categories';
367    $query.= ' WHERE site_id = '.$site_id;
368    $query.= " AND dir = '".$dir."'";
369    if ( $id_uppercat == 'NULL' )
370    {
371      $query.= ' AND id_uppercat IS NULL';
372    }
373    else
374    {
375      $query.= ' AND id_uppercat = '.$id_uppercat;
376    }
377    $query.= ';';
378    $result = mysql_query( $query );
379    if ( mysql_num_rows( $result ) == 0 )
380    {
381      $name = str_replace( '_', ' ', $dir );
382      // we have to create the category
383      $query = 'INSERT INTO '.PREFIX_TABLE.'categories';
384      $query.= ' (name,dir,site_id,id_uppercat) VALUES ';
385      $query.= "('".$name."','".$dir."',".$site_id;
386      if ( !is_numeric( $id_uppercat ) )
387      {
388        $query.= ',NULL';
389      }
390      else
391      {
392        $query.= ','.$id_uppercat;
393      }
394      $query.= ');';
395      mysql_query( $query );
396      $category_id = mysql_insert_id();
397    }
398    else
399    {
400      // we get the already registered id
401      $row = mysql_fetch_array( $result );
402      $category_id = $row['id'];
403    }
404    $output.= insert_remote_image( $list_dirs[$i], $category_id );
405    $output.= insert_remote_category( $list_dirs[$i], $site_id,
406                                      $category_id, $level+1 );
407    $output.= '</div>';
408  }
409  // we have to remove the categories of the database not present in the xml
410  // file (ie deleted from the picture storage server)
411  $query = 'SELECT dir,id';
412  $query.= ' FROM '.PREFIX_TABLE.'categories';
413  $query.= ' WHERE site_id = '.$site_id;
414  if ( !is_numeric( $id_uppercat ) )
415  {
416    $query.= ' AND id_uppercat IS NULL';
417  }
418  else
419  {
420    $query.= ' AND id_uppercat = '.$id_uppercat;
421  }
422  $query.= ';';
423  $result = mysql_query( $query );
424  while ( $row = mysql_fetch_array( $result ) )
425  {
426    // is the category in the xml file ?
427    if ( !in_array( $row['dir'], $categories ) )
428    {
429      delete_category( $row['id'] );
430    }
431  }
432
433  return $output;
434}
435       
436// insert_remote_image searchs the "root" node of the xml_dir given and
437// insert the contained pictures if the are not in the database yet.
438function insert_remote_image( $xml_dir, $category_id )
439{
440  global $count_new,$lang;
441
442  $output = '';
443  $root = getChild( $xml_dir, 'root' );
444  $pictures = array();
445  $xml_pictures = getChildren( $root, 'picture' );
446  for ( $j = 0; $j < sizeof( $xml_pictures ); $j++ )
447  {
448    //<picture file="albatros.jpg" tn_ext="png" date="2002-04-14"
449    //  filesize="35" width="640" height="480" />
450    $file     = getAttribute( $xml_pictures[$j], 'file' );
451    $tn_ext   = getAttribute( $xml_pictures[$j], 'tn_ext' );
452    $date     = getAttribute( $xml_pictures[$j], 'date' ); 
453    $filesize = getAttribute( $xml_pictures[$j], 'filesize' );
454    $width    = getAttribute( $xml_pictures[$j], 'width' );
455    $height   = getAttribute( $xml_pictures[$j], 'height' );
456                       
457    $pictures[$j] = $file;
458                       
459    // is the picture already existing in the database ?
460    $query = 'SELECT id,tn_ext';
461    $query.= ' FROM '.PREFIX_TABLE.'images';
462    $query.= ' WHERE storage_category_id = '.$category_id;
463    $query.= " AND file = '".$file."'";
464    $query.= ';';
465    $result = mysql_query( $query );
466    $query = '';
467    if ( mysql_num_rows( $result ) == 0 )
468    {
469      $query = 'INSERT INTO '.PREFIX_TABLE.'images';
470      $query.= ' (file,storage_category_id,date_available,tn_ext';
471      $query.= ',filesize,width,height)';
472      $query.= ' VALUES (';
473      $query.= "'".$file."'";
474      $query.= ",'".$category_id."'";
475      $query.= ",'".$date."'";
476      $query.= ",'".$tn_ext."'";
477      $query.= ",'".$filesize."'";
478      $query.= ",'".$width."'";
479      $query.= ",'".$height."'";
480      $query.= ')';
481      $query.= ';';
482      mysql_query( $query );
483      // retrieving the id of newly inserted picture
484      $query = 'SELECT id';
485      $query.= ' FROM '.PREFIX_TABLE.'images';
486      $query.= ' WHERE storage_category_id = '.$category_id;
487      $query.= " AND file = '".$file."'";
488      $query.= ';';
489      list( $image_id ) = mysql_fetch_array( mysql_query( $query ) );
490      // adding the link between this picture and its storage category
491      $query = 'INSERT INTO '.PREFIX_TABLE.'image_category';
492      $query.= ' (image_id,category_id) VALUES ';
493      $query.= ' ('.$image_id.','.$category_id.')';
494      $query.= ';';
495      mysql_query( $query );
496
497      $output.= $file;
498      $output.= ' <span style="font-weight:bold;">';
499      $output.= $lang['update_research_added'].'</span>';
500      $output.= ' ('.$lang['update_research_tn_ext'].' '.$tn_ext.')<br />';
501
502      $count_new++;
503    }
504    else
505    {
506      // is the tn_ext the same in the xml file and in the database ?
507      $row = mysql_fetch_array( $result );
508      if ( $row['tn_ext'] != $tn_ext )
509      {
510        $query = 'UPDATE '.PREFIX_TABLE.'images';
511        $query.= ' SET';
512        $query.= " tn_ext = '".$tn_ext."'";
513        $query.= ' WHERE storage_category_id = '.$category_id;
514        $query.= " AND file = '".$file."'";
515        $query.= ';';
516      }
517    }
518    // execution of the query
519    if ( $query != '' )
520    {
521      mysql_query( $query );
522    }
523  }
524  // we have to remove the pictures of the database not present in the xml file
525  // (ie deleted from the picture storage server)
526  $query = 'SELECT id,file';
527  $query.= ' FROM '.PREFIX_TABLE.'images';
528  $query.= ' WHERE storage_category_id = '.$category_id;
529  $query.= ';';
530  $result = mysql_query( $query );
531  while ( $row = mysql_fetch_array( $result ) )
532  {
533    // is the file in the xml file ?
534    if ( !in_array( $row['file'], $pictures ) )
535    {
536      delete_image( $row['id'] );
537    }
538  }
539  return $output;
540}
541//----------------------------------------------------- template initialization
542$sub = $vtp->Open( '../template/'.$user['template'].'/admin/update.vtp' );
543$tpl = array( 'update_default_title', 'update_only_cat', 'update_all',
544              'update_research_conclusion', 'update_deletion_conclusion',
545              'remote_site', 'update_part_research' );
546templatize_array( $tpl, 'lang', $sub );
547$vtp->setGlobalVar( $sub, 'user_template', $user['template'] );
548//-------------------------------------------------------- categories structure
549$page['plain_structure'] = get_plain_structure();
550//-------------------------------------------- introduction : choices of update
551// Display choice if "update" var is not specified
552check_cat_id( $_GET['update'] );
553if ( !isset( $_GET['update'] )
554     and !( isset( $page['cat'] )
555            or $_GET['update'] == 'cats'
556            or $_GET['update'] == 'all' ) )
557{
558  $vtp->addSession( $sub, 'introduction' );
559  // only update the categories, not the pictures.
560  $url = add_session_id( './admin.php?page=update&amp;update=cats' );
561  $vtp->setVar( $sub, 'introduction.only_cat:url', $url );
562  // update the entire tree folder
563  $url = add_session_id( './admin.php?page=update&amp;update=all' );
564  $vtp->setVar( $sub, 'introduction.all:url', $url );
565  $vtp->closeSession( $sub, 'introduction' );
566}
567//------------------------------------------------- local update : ../galleries
568else
569{
570  $count_new = 0;
571  $count_deleted = 0;
572  $vtp->addSession( $sub, 'local_update' );
573  if ( isset( $page['cat'] ) )
574  {
575    $categories = insert_local_category( $page['cat'] );
576  }
577  else
578  {
579    $categories = insert_local_category( 'NULL' );
580  }
581  $vtp->setVar( $sub, 'local_update.categories', $categories );
582  $vtp->setVar( $sub, 'local_update.count_new', $count_new );
583  $vtp->setVar( $sub, 'local_update.count_deleted', $count_deleted );
584  $vtp->closeSession( $sub, 'local_update' );
585}
586//------------------------------------------------- remote update : listing.xml
587if ( @is_file( './listing.xml' ) )
588{
589  $count_new = 0;
590  $count_deleted = 0;
591  $vtp->addSession( $sub, 'remote_update' );
592
593  remote_images();
594  $vtp->setVar( $sub, 'remote_update.count_new', $count_new );
595  $vtp->setVar( $sub, 'remote_update.count_deleted', $count_deleted );
596
597  $vtp->closeSession( $sub, 'remote_update' );
598}
599//---------------------------------------- update informations about categories
600update_category( 'all' );
601//----------------------------------------------------------- sending html code
602$vtp->Parse( $handle , 'sub', $sub );
603?>
Note: See TracBrowser for help on using the repository browser.