source: trunk/admin/update.php @ 19

Last change on this file since 19 was 19, 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: 18.5 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';
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        if ( mysql_num_rows( $result ) == 0 )
204        {
205          if ( $tn_ext = TN_exists( $rep, $file ) )
206          {
207            // is the picture already in the database ?
208            $query = 'SELECT id';
209            $query.= ' FROM '.PREFIX_TABLE.'images';
210            $query.= ' WHERE cat_id = '.$category_id;
211            $query.= " AND file = '".$file."'";
212            $query.= ';';
213            $result = mysql_query( $query );
214            if ( mysql_num_rows( $result ) == 0 )
215            {
216              $picture = array();
217              $picture['file'] = $file;
218              $picture['tn_ext'] = $tn_ext;
219              $picture['date'] = date( 'Y-m-d', filemtime ( $rep.'/'.$file ) );
220              $picture['filesize'] = floor( filesize( $rep.'/'.$file ) / 1024);
221              $image_size = @getimagesize( $rep.'/'.$file );
222              $picture['width'] = $image_size[0];
223              $picture['height'] = $image_size[1];
224              array_push( $pictures, $picture );
225            }
226          }
227          else
228          {
229            $output.= '<span style="color:red;">';
230            $output.= $lang['update_missing_tn'].' : '.$file;
231            $output.= ' (<span style="font-weight:bold;">';
232            $output.= $conf['prefix_thumbnail'];
233            $output.= get_filename_wo_extension( $file ).'.XXX</span>';
234            $output.= ', XXX = ';
235            $output.= implode( ', ', $conf['picture_ext'] );
236            $output.= ')</span><br />';
237          }
238        }
239      }
240    }
241  }
242  // inserting the pictures found in the directory
243  foreach ( $pictures as $picture ) {
244    $query = 'INSERT INTO '.PREFIX_TABLE.'images';
245    $query.= ' (file,cat_id,date_available,tn_ext,filesize,width,height)';
246    $query.= ' VALUES ';
247    $query.= "('".$picture['file']."','".$category_id."'";
248    $query.= ",'".$picture['date']."','".$picture['tn_ext']."'";
249    $query.= ",'".$picture['filesize']."','".$picture['width']."'";
250    $query.= ",'".$picture['height']."')";
251    $query.= ';';
252    mysql_query( $query );
253    $count_new++;
254   
255    $output.= $picture['file'];
256    $output.= ' <span style="font-weight:bold;">';
257    $output.= $lang['update_research_added'].'</span>';
258    $output.= ' ('.$lang['update_research_tn_ext'].' '.$picture['tn_ext'].')';
259    $output.= '<br />';
260  }
261  return $output;
262}
263       
264// The function "update_cat_info" updates the information about the last
265// online image and the number of images in the category
266function update_cat_info( $category_id )
267{
268  $query = 'SELECT date_available';
269  $query.= ' FROM '.PREFIX_TABLE.'images';
270  $query.= ' WHERE cat_id = '.$category_id;
271  $query.= ' ORDER BY date_available DESC';
272  $query.= ' LIMIT 0,1';
273  $query.= ';';
274  $result = mysql_query( $query );
275  $row = mysql_fetch_array( $result );
276  $date_last = $row['date_available'];
277               
278  $query = 'SELECT COUNT(*) as nb_images';
279  $query.= ' FROM '.PREFIX_TABLE.'images';
280  $query.= ' WHERE cat_id = '.$category_id;
281  $result = mysql_query( $query );
282  $row = mysql_fetch_array( $result );
283  $nb_images = $row['nb_images'];
284               
285  $query = 'UPDATE '.PREFIX_TABLE.'categories';
286  $query.= " SET date_dernier = '".$date_last."'";
287  $query.= ', nb_images = '.$nb_images;
288  $query.= ' where id = '.$category_id;
289  $query.= ';';
290  mysql_query( $query );
291}
292
293// remote_images verifies if a file named "listing.xml" is present is the
294// admin directory. If it is the case, creation of a remote picture storage
295// site if it doesn't already exists. Then, the function calls
296// insert_remote_category for this remote site on the root category.
297function remote_images()
298{
299  global $conf, $lang, $vtp, $sub;
300
301  // 1. is there a file listing.xml ?
302  if ( !( $xml_content = getXmlCode( 'listing.xml' ) ) )
303  {
304    return false;
305  }
306  $url = getContent( getChild( $xml_content, 'url' ) );
307  $vtp->setVar( $sub, 'remote_update.url', $url );
308
309  // 2. is the site already existing ?
310  $query = 'select id';
311  $query.= ' from '.PREFIX_TABLE.'sites';
312  $query.= " where galleries_url = '".$url."'";
313  $query.= ';';
314  $result = mysql_query( $query );
315  if ( mysql_num_rows($result ) == 0 )
316  {
317    // we have to register this site in the database
318    $query = 'insert into '.PREFIX_TABLE.'sites';
319    $query.= " (galleries_url) values ('".$url."')";
320    $query.= ';';
321    mysql_query( $query );
322    $site_id = mysql_insert_id();
323  }
324  else
325  {
326    // we get the already registered id
327    $row = mysql_fetch_array( $result );
328    $site_id = $row['id'];
329  }
330
331  // 3. available dirs in the file
332  $categories = insert_remote_category( $xml_content, $site_id, 'NULL', 0 );
333  $vtp->setVar( $sub, 'remote_update.categories', $categories );
334}
335
336// insert_remote_category searchs the "dir" node of the xml_dir given and
337// insert the contained categories if the are not in the database yet. The
338// function also deletes the categories that are in the database and not in
339// the xml_file.
340function insert_remote_category( $xml_dir, $site_id, $id_uppercat, $level )
341{
342  global $conf;
343
344  $output = '';
345  $categories = array();
346  $list_dirs = getChildren( $xml_dir, 'dir'.$level );
347  for ( $i = 0; $i < sizeof( $list_dirs ); $i++ )
348  {
349    // is the category already existing ?
350    $category_id = '';
351    $name = getAttribute( $list_dirs[$i], 'name' );
352    $categories[$i] = $name;
353
354    $output.= '<img src="./images/puce.gif">';
355    $output.= '<span style="font-weight:bold;">'.$name.'</span>';
356    $output.= '<div class="retrait">';
357
358    $query = 'select id';
359    $query.= ' from '.PREFIX_TABLE.'categories';
360    $query.= ' where site_id = '.$site_id;
361    $query.= " and dir = '".$name."'";
362    if ( $id_uppercat == 'NULL' )
363    {
364      $query.= ' and id_uppercat is NULL';
365    }
366    else
367    {
368      $query.= ' and id_uppercat = '.$id_uppercat;
369    }
370    $query.= ';';
371    $result = mysql_query( $query );
372    if ( mysql_num_rows( $result ) == 0 )
373    {
374      // we have to create the category
375      $query = 'insert into '.PREFIX_TABLE.'categories';
376      $query.= " (dir,site_id,id_uppercat) values ('".$name."',".$site_id;
377      if ( !is_numeric( $id_uppercat ) )
378      {
379        $query.= ',NULL';
380      }
381      else
382      {
383        $query.= ','.$id_uppercat;
384      }
385      $query.= ');';
386      mysql_query( $query );
387      $category_id = mysql_insert_id();
388    }
389    else
390    {
391      // we get the already registered id
392      $row = mysql_fetch_array( $result );
393      $category_id = $row['id'];
394    }
395    $output.= insert_remote_image( $list_dirs[$i], $category_id );
396    update_cat_info( $category_id );
397    $output.= insert_remote_category( $list_dirs[$i], $site_id,
398                                      $category_id, $level+1 );
399    $output.= '</div>';
400  }
401  // we have to remove the categories of the database not present in the xml
402  // file (ie deleted from the picture storage server)
403  $query = 'select dir,id';
404  $query.= ' from '.PREFIX_TABLE.'categories';
405  $query.= ' where site_id = '.$site_id;
406  if ( !is_numeric( $id_uppercat ) )
407  {
408    $query.= ' and id_uppercat is NULL';
409  }
410  else
411  {
412    $query.= ' and id_uppercat = '.$id_uppercat;
413  }
414  $query.= ';';
415  $result = mysql_query( $query );
416  while ( $row = mysql_fetch_array( $result ) )
417  {
418    // is the category in the xml file ?
419    if ( !in_array( $row['dir'], $categories ) )
420    {
421      delete_category( $row['id'] );
422    }
423  }
424
425  return $output;
426}
427       
428// insert_remote_image searchs the "root" node of the xml_dir given and
429// insert the contained pictures if the are not in the database yet.
430function insert_remote_image( $xml_dir, $category_id )
431{
432  global $count_new,$lang;
433
434  $output = '';
435  $root = getChild( $xml_dir, 'root' );
436  $pictures = array();
437  $xml_pictures = getChildren( $root, 'picture' );
438  for ( $j = 0; $j < sizeof( $xml_pictures ); $j++ )
439  {
440    //<picture file="albatros.jpg" tn_ext="png" date="2002-04-14"
441    //  filesize="35" width="640" height="480" />
442    $file     = getAttribute( $xml_pictures[$j], 'file' );
443    $tn_ext   = getAttribute( $xml_pictures[$j], 'tn_ext' );
444    $date     = getAttribute( $xml_pictures[$j], 'date' ); 
445    $filesize = getAttribute( $xml_pictures[$j], 'filesize' );
446    $width    = getAttribute( $xml_pictures[$j], 'width' );
447    $height   = getAttribute( $xml_pictures[$j], 'height' );
448                       
449    $pictures[$j] = $file;
450                       
451    // is the picture already existing in the database ?
452    $query = 'select id,tn_ext';
453    $query.= ' from '.PREFIX_TABLE.'images';
454    $query.= ' where cat_id = '.$category_id;
455    $query.= " and file = '".$file."'";
456    $query.= ';';
457    $result = mysql_query( $query );
458    $query = '';
459    if ( mysql_num_rows( $result ) == 0 )
460    {
461      $query = 'insert into '.PREFIX_TABLE.'images';
462      $query.= ' (file,cat_id,date_available,tn_ext,filesize,width,height)';
463      $query.= ' values (';
464      $query.= "'".$file."'";
465      $query.= ",'".$category_id."'";
466      $query.= ",'".$date."'";
467      $query.= ",'".$tn_ext."'";
468      $query.= ",'".$filesize."'";
469      $query.= ",'".$width."'";
470      $query.= ",'".$height."'";
471      $query.= ')';
472      $query.= ';';
473
474      $output.= $file;
475      $output.= ' <span style="font-weight:bold;">';
476      $output.= $lang['update_research_added'].'</span>';
477      $output.= ' ('.$lang['update_research_tn_ext'].' '.$tn_ext.')<br />';
478
479      $count_new++;
480    }
481    else
482    {
483      // is the tn_ext the same in the xml file and in the database ?
484      $row = mysql_fetch_array( $result );
485      if ( $row['tn_ext'] != $tn_ext )
486      {
487        $query = 'update '.PREFIX_TABLE.'images';
488        $query.= ' set';
489        $query.= " tn_ext = '".$tn_ext."'";
490        $query.= ' where cat_id = '.$category_id;
491        $query.= " and file = '".$file."'";
492        $query.= ';';
493      }
494    }
495    // execution of the query
496    if ( $query != '' )
497    {
498      mysql_query( $query );
499    }
500  }
501  // we have to remove the pictures of the database not present in the xml file
502  // (ie deleted from the picture storage server)
503  $query = 'select id,file';
504  $query.= ' from '.PREFIX_TABLE.'images';
505  $query.= ' where cat_id = '.$category_id;
506  $query.= ';';
507  $result = mysql_query( $query );
508  while ( $row = mysql_fetch_array( $result ) )
509  {
510    // is the file in the xml file ?
511    if ( !in_array( $row['file'], $pictures ) )
512    {
513      delete_image( $row['id'] );
514    }
515  }
516  return $output;
517}
518//----------------------------------------------------- template initialization
519$sub = $vtp->Open( '../template/'.$user['template'].'/admin/update.vtp' );
520$tpl = array( 'update_default_title', 'update_only_cat', 'update_all',
521              'update_research_conclusion', 'update_deletion_conclusion',
522              'remote_site', 'update_part_research' );
523templatize_array( $tpl, 'lang', $sub );
524//-------------------------------------------- introduction : choices of update
525// Display choice if "update" var is not specified
526check_cat_id( $_GET['update'] );
527if ( !isset( $_GET['update'] )
528     and !( isset( $page['cat'] )
529            or $_GET['update'] == 'cats'
530            or $_GET['update'] == 'all' ) )
531{
532  $vtp->addSession( $sub, 'introduction' );
533  // only update the categories, not the pictures.
534  $url = add_session_id( './admin.php?page=update&amp;update=cats' );
535  $vtp->setVar( $sub, 'introduction.only_cat:url', $url );
536  // update the entire tree folder
537  $url = add_session_id( './admin.php?page=update&amp;update=all' );
538  $vtp->setVar( $sub, 'introduction.all:url', $url );
539  $vtp->closeSession( $sub, 'introduction' );
540}
541//------------------------------------------------- local update : ../galleries
542else
543{
544  $count_new = 0;
545  $count_deleted = 0;
546  $vtp->addSession( $sub, 'local_update' );
547  if ( isset( $page['cat'] ) )
548  {
549    $categories = insert_local_category( $page['cat'] );
550  }
551  else
552  {
553    $categories = insert_local_category( 'NULL' );
554  }
555  $vtp->setVar( $sub, 'local_update.categories', $categories );
556  $vtp->setVar( $sub, 'local_update.count_new', $count_new );
557  $vtp->setVar( $sub, 'local_update.count_deleted', $count_deleted );
558  $vtp->closeSession( $sub, 'local_update' );
559}
560//------------------------------------------------- remote update : listing.xml
561if ( @is_file( './listing.xml' ) )
562{
563  $count_new = 0;
564  $count_deleted = 0;
565  $vtp->addSession( $sub, 'remote_update' );
566
567  remote_images();
568  $vtp->setVar( $sub, 'remote_update.count_new', $count_new );
569  $vtp->setVar( $sub, 'remote_update.count_deleted', $count_deleted );
570
571  $vtp->closeSession( $sub, 'remote_update' );
572}
573//----------------------------------------------------------- sending html code
574$vtp->Parse( $handle , 'sub', $sub );
575?>
Note: See TracBrowser for help on using the repository browser.