source: tags/release-1_3RC1/admin/update.php @ 13022

Last change on this file since 13022 was 147, checked in by z0rglub, 21 years ago

If a category with a wrong directory name (accentuated characters) even
exists (from a previous release of PhpWebGallery), a warning message is
displayed but the update goes on

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