source: trunk/admin/update.php @ 34

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

template as user_template for displaying pictures in the template

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