source: trunk/admin/update.php @ 160

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

Filename must use the same characters as the directories

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.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 160 2003-09-22 20:45:44Z 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              // the name of the file must not use acentuated characters or
223              // blank space..
224              if ( preg_match( '/^[a-zA-Z0-9-_.]+$/', $file ) )
225              {
226                $picture = array();
227                $picture['file']     = $file;
228                $picture['tn_ext']   = $tn_ext;
229                $picture['date'] = date( 'Y-m-d', filemtime($rep.'/'.$file) );
230                $picture['filesize'] = floor( filesize($rep.'/'.$file) / 1024);
231                $image_size = @getimagesize( $rep.'/'.$file );
232                $picture['width']    = $image_size[0];
233                $picture['height']   = $image_size[1];
234                if ( $waiting['validated'] == 'true' )
235                {
236                  // retrieving infos from the XML description of
237                  // $waiting['infos']
238                  $infos = nl2br( $waiting['infos'] );
239                  $picture['author']        = getAttribute( $infos, 'author' );
240                  $picture['comment']       = getAttribute( $infos, 'comment');
241                  $unixtime = getAttribute( $infos, 'date_creation' );
242                  $picture['date_creation'] = '';
243                  if ( $unixtime != '' )
244                    $picture['date_creation'] = date( 'Y-m-d', $unixtime );
245                  $picture['name']          = getAttribute( $infos, 'name' );
246                  // deleting the waiting element
247                  $query = 'DELETE FROM '.PREFIX_TABLE.'waiting';
248                  $query.= ' WHERE id = '.$waiting['id'];
249                  $query.= ';';
250                  mysql_query( $query );
251                }
252                array_push( $pictures, $picture );
253              }
254              else
255              {
256                $output.= '<span style="color:red;">"'.$file.'" : ';
257                $output.= $lang['update_wrong_dirname'].'</span><br />';
258              }
259
260            }
261          }
262          else
263          {
264            $output.= '<span style="color:red;">';
265            $output.= $lang['update_missing_tn'].' : '.$file;
266            $output.= ' (<span style="font-weight:bold;">';
267            $output.= $conf['prefix_thumbnail'];
268            $output.= get_filename_wo_extension( $file ).'.XXX</span>';
269            $output.= ', XXX = ';
270            $output.= implode( ', ', $conf['picture_ext'] );
271            $output.= ')</span><br />';
272          }
273        }
274      }
275    }
276  }
277  // inserting the pictures found in the directory
278  foreach ( $pictures as $picture ) {
279    $query = 'INSERT INTO '.PREFIX_TABLE.'images';
280    $query.= ' (file,storage_category_id,date_available,tn_ext';
281    $query.= ',filesize,width,height';
282    $query.= ',name,author,comment,date_creation)';
283    $query.= ' VALUES ';
284    $query.= "('".$picture['file']."','".$category_id."'";
285    $query.= ",'".$picture['date']."','".$picture['tn_ext']."'";
286    $query.= ",'".$picture['filesize']."','".$picture['width']."'";
287    $query.= ",'".$picture['height']."','".$picture['name']."'";
288    $query.= ",'".$picture['author']."','".$picture['comment']."'";
289    if ( $picture['date_creation'] != '' )
290    {
291      $query.= ",'".$picture['date_creation']."'";
292    }
293    else
294    {
295      $query.= ',NULL';
296    }
297    $query.= ');';
298    mysql_query( $query );
299    $count_new++;
300    // retrieving the id of newly inserted picture
301    $query = 'SELECT id';
302    $query.= ' FROM '.PREFIX_TABLE.'images';
303    $query.= ' WHERE storage_category_id = '.$category_id;
304    $query.= " AND file = '".$picture['file']."'";
305    $query.= ';';
306    list( $image_id ) = mysql_fetch_array( mysql_query( $query ) );
307    // adding the link between this picture and its storage category
308    $query = 'INSERT INTO '.PREFIX_TABLE.'image_category';
309    $query.= ' (image_id,category_id) VALUES ';
310    $query.= ' ('.$image_id.','.$category_id.')';
311    $query.= ';';
312    mysql_query( $query );
313
314    $output.= $picture['file'];
315    $output.= ' <span style="font-weight:bold;">';
316    $output.= $lang['update_research_added'].'</span>';
317    $output.= ' ('.$lang['update_research_tn_ext'].' '.$picture['tn_ext'].')';
318    $output.= '<br />';
319  }
320  return $output;
321}
322
323// remote_images verifies if a file named "listing.xml" is present is the
324// admin directory. If it is the case, creation of a remote picture storage
325// site if it doesn't already exists. Then, the function calls
326// insert_remote_category for this remote site on the root category.
327function remote_images()
328{
329  global $conf, $lang, $vtp, $sub;
330
331  // 1. is there a file listing.xml ?
332  if ( !( $xml_content = getXmlCode( 'listing.xml' ) ) )
333  {
334    return false;
335  }
336  $url = getContent( getChild( $xml_content, 'url' ) );
337  $vtp->setVar( $sub, 'remote_update.url', $url );
338
339  // 2. is the site already existing ?
340  $query = 'SELECT id';
341  $query.= ' FROM '.PREFIX_TABLE.'sites';
342  $query.= " WHERE galleries_url = '".$url."'";
343  $query.= ';';
344  $result = mysql_query( $query );
345  if ( mysql_num_rows($result ) == 0 )
346  {
347    // we have to register this site in the database
348    $query = 'INSERT INTO '.PREFIX_TABLE.'sites';
349    $query.= " (galleries_url) VALUES ('".$url."')";
350    $query.= ';';
351    mysql_query( $query );
352    $site_id = mysql_insert_id();
353  }
354  else
355  {
356    // we get the already registered id
357    $row = mysql_fetch_array( $result );
358    $site_id = $row['id'];
359  }
360
361  // 3. available dirs in the file
362  $categories = insert_remote_category( $xml_content, $site_id, 'NULL', 0 );
363  $vtp->setVar( $sub, 'remote_update.categories', $categories );
364}
365
366// insert_remote_category searchs the "dir" node of the xml_dir given and
367// insert the contained categories if the are not in the database yet. The
368// function also deletes the categories that are in the database and not in
369// the xml_file.
370function insert_remote_category( $xml_dir, $site_id, $id_uppercat, $level )
371{
372  global $conf,$user;
373
374  $output = '';
375  $categories = array();
376  $list_dirs = getChildren( $xml_dir, 'dir'.$level );
377  for ( $i = 0; $i < sizeof( $list_dirs ); $i++ )
378  {
379    // is the category already existing ?
380    $category_id = '';
381    $dir = getAttribute( $list_dirs[$i], 'name' );
382    $categories[$i] = $dir;
383
384    $src = '../template/'.$user['template'].'/admin/images/puce.gif';
385    $output.= '<img src="'.$src.'" alt="&gt;" />';
386    $output.= '<span style="font-weight:bold;">'.$dir.'</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 = '".$dir."'";
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      $name = str_replace( '_', ' ', $dir );
406      // we have to create the category
407      $query = 'INSERT INTO '.PREFIX_TABLE.'categories';
408      $query.= ' (name,dir,site_id,id_uppercat) VALUES ';
409      $query.= "('".$name."','".$dir."',".$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    $output.= insert_remote_category( $list_dirs[$i], $site_id,
430                                      $category_id, $level+1 );
431    $output.= '</div>';
432  }
433  // we have to remove the categories of the database not present in the xml
434  // file (ie deleted from the picture storage server)
435  $query = 'SELECT dir,id';
436  $query.= ' FROM '.PREFIX_TABLE.'categories';
437  $query.= ' WHERE site_id = '.$site_id;
438  if ( !is_numeric( $id_uppercat ) )
439  {
440    $query.= ' AND id_uppercat IS NULL';
441  }
442  else
443  {
444    $query.= ' AND id_uppercat = '.$id_uppercat;
445  }
446  $query.= ';';
447  $result = mysql_query( $query );
448  while ( $row = mysql_fetch_array( $result ) )
449  {
450    // is the category in the xml file ?
451    if ( !in_array( $row['dir'], $categories ) )
452    {
453      delete_category( $row['id'] );
454    }
455  }
456
457  return $output;
458}
459       
460// insert_remote_image searchs the "root" node of the xml_dir given and
461// insert the contained pictures if the are not in the database yet.
462function insert_remote_image( $xml_dir, $category_id )
463{
464  global $count_new,$lang;
465
466  $output = '';
467  $root = getChild( $xml_dir, 'root' );
468  $pictures = array();
469  $xml_pictures = getChildren( $root, 'picture' );
470  for ( $j = 0; $j < sizeof( $xml_pictures ); $j++ )
471  {
472    //<picture file="albatros.jpg" tn_ext="png" date="2002-04-14"
473    //  filesize="35" width="640" height="480" />
474    $file     = getAttribute( $xml_pictures[$j], 'file' );
475    $tn_ext   = getAttribute( $xml_pictures[$j], 'tn_ext' );
476    $date     = getAttribute( $xml_pictures[$j], 'date' ); 
477    $filesize = getAttribute( $xml_pictures[$j], 'filesize' );
478    $width    = getAttribute( $xml_pictures[$j], 'width' );
479    $height   = getAttribute( $xml_pictures[$j], 'height' );
480                       
481    $pictures[$j] = $file;
482                       
483    // is the picture already existing in the database ?
484    $query = 'SELECT id,tn_ext';
485    $query.= ' FROM '.PREFIX_TABLE.'images';
486    $query.= ' WHERE storage_category_id = '.$category_id;
487    $query.= " AND file = '".$file."'";
488    $query.= ';';
489    $result = mysql_query( $query );
490    $query = '';
491    if ( mysql_num_rows( $result ) == 0 )
492    {
493      $query = 'INSERT INTO '.PREFIX_TABLE.'images';
494      $query.= ' (file,storage_category_id,date_available,tn_ext';
495      $query.= ',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      mysql_query( $query );
507      // retrieving the id of newly inserted picture
508      $query = 'SELECT id';
509      $query.= ' FROM '.PREFIX_TABLE.'images';
510      $query.= ' WHERE storage_category_id = '.$category_id;
511      $query.= " AND file = '".$file."'";
512      $query.= ';';
513      list( $image_id ) = mysql_fetch_array( mysql_query( $query ) );
514      // adding the link between this picture and its storage category
515      $query = 'INSERT INTO '.PREFIX_TABLE.'image_category';
516      $query.= ' (image_id,category_id) VALUES ';
517      $query.= ' ('.$image_id.','.$category_id.')';
518      $query.= ';';
519      mysql_query( $query );
520
521      $output.= $file;
522      $output.= ' <span style="font-weight:bold;">';
523      $output.= $lang['update_research_added'].'</span>';
524      $output.= ' ('.$lang['update_research_tn_ext'].' '.$tn_ext.')<br />';
525
526      $count_new++;
527    }
528    else
529    {
530      // is the tn_ext the same in the xml file and in the database ?
531      $row = mysql_fetch_array( $result );
532      if ( $row['tn_ext'] != $tn_ext )
533      {
534        $query = 'UPDATE '.PREFIX_TABLE.'images';
535        $query.= ' SET';
536        $query.= " tn_ext = '".$tn_ext."'";
537        $query.= ' WHERE storage_category_id = '.$category_id;
538        $query.= " AND file = '".$file."'";
539        $query.= ';';
540      }
541    }
542    // execution of the query
543    if ( $query != '' )
544    {
545      mysql_query( $query );
546    }
547  }
548  // we have to remove the pictures of the database not present in the xml file
549  // (ie deleted from the picture storage server)
550  $query = 'SELECT id,file';
551  $query.= ' FROM '.PREFIX_TABLE.'images';
552  $query.= ' WHERE storage_category_id = '.$category_id;
553  $query.= ';';
554  $result = mysql_query( $query );
555  while ( $row = mysql_fetch_array( $result ) )
556  {
557    // is the file in the xml file ?
558    if ( !in_array( $row['file'], $pictures ) )
559    {
560      delete_image( $row['id'] );
561    }
562  }
563  return $output;
564}
565//----------------------------------------------------- template initialization
566$sub = $vtp->Open( '../template/'.$user['template'].'/admin/update.vtp' );
567$tpl = array( 'update_default_title', 'update_only_cat', 'update_all',
568              'update_research_conclusion', 'update_deletion_conclusion',
569              'remote_site', 'update_part_research' );
570templatize_array( $tpl, 'lang', $sub );
571$vtp->setGlobalVar( $sub, 'user_template', $user['template'] );
572//-------------------------------------------------------- categories structure
573$page['plain_structure'] = get_plain_structure();
574//-------------------------------------------- introduction : choices of update
575// Display choice if "update" var is not specified
576check_cat_id( $_GET['update'] );
577if ( !isset( $_GET['update'] )
578     and !( isset( $page['cat'] )
579            or $_GET['update'] == 'cats'
580            or $_GET['update'] == 'all' ) )
581{
582  $vtp->addSession( $sub, 'introduction' );
583  // only update the categories, not the pictures.
584  $url = add_session_id( './admin.php?page=update&amp;update=cats' );
585  $vtp->setVar( $sub, 'introduction.only_cat:url', $url );
586  // update the entire tree folder
587  $url = add_session_id( './admin.php?page=update&amp;update=all' );
588  $vtp->setVar( $sub, 'introduction.all:url', $url );
589  $vtp->closeSession( $sub, 'introduction' );
590}
591//------------------------------------------------- local update : ../galleries
592else
593{
594  $count_new = 0;
595  $count_deleted = 0;
596  $vtp->addSession( $sub, 'local_update' );
597  if ( isset( $page['cat'] ) )
598  {
599    $categories = insert_local_category( $page['cat'] );
600  }
601  else
602  {
603    $categories = insert_local_category( 'NULL' );
604  }
605  $vtp->setVar( $sub, 'local_update.categories', $categories );
606  $vtp->setVar( $sub, 'local_update.count_new', $count_new );
607  $vtp->setVar( $sub, 'local_update.count_deleted', $count_deleted );
608  $vtp->closeSession( $sub, 'local_update' );
609}
610//------------------------------------------------- remote update : listing.xml
611if ( @is_file( './listing.xml' ) )
612{
613  $count_new = 0;
614  $count_deleted = 0;
615  $vtp->addSession( $sub, 'remote_update' );
616
617  remote_images();
618  $vtp->setVar( $sub, 'remote_update.count_new', $count_new );
619  $vtp->setVar( $sub, 'remote_update.count_deleted', $count_deleted );
620
621  $vtp->closeSession( $sub, 'remote_update' );
622}
623//---------------------------------------- update informations about categories
624update_category( 'all' );
625//----------------------------------------------------------- sending html code
626$vtp->Parse( $handle , 'sub', $sub );
627?>
Note: See TracBrowser for help on using the repository browser.