source: trunk/admin/update.php @ 26

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

* empty log message *

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