source: trunk/admin/update.php @ 97

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