source: tags/bug-1_3beta-010/admin/update.php @ 26789

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

Adding the name when inserting a remote category in the database. $name
becomes $dir (even if named "name" in the XML file)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 20.9 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 126 2003-09-16 21:32:30Z 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    $dir = getAttribute( $list_dirs[$i], 'name' );
359    $categories[$i] = $dir;
360
361    $src = '../template/'.$user['template'].'/admin/images/puce.gif';
362    $output.= '<img src="'.$src.'" alt="&gt;" />';
363    $output.= '<span style="font-weight:bold;">'.$dir.'</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 = '".$dir."'";
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      $name = str_replace( '_', ' ', $dir );
383      // we have to create the category
384      $query = 'INSERT INTO '.PREFIX_TABLE.'categories';
385      $query.= ' (name,dir,site_id,id_uppercat) VALUES ';
386      $query.= "('".$name."','".$dir."',".$site_id;
387      if ( !is_numeric( $id_uppercat ) )
388      {
389        $query.= ',NULL';
390      }
391      else
392      {
393        $query.= ','.$id_uppercat;
394      }
395      $query.= ');';
396      mysql_query( $query );
397      $category_id = mysql_insert_id();
398    }
399    else
400    {
401      // we get the already registered id
402      $row = mysql_fetch_array( $result );
403      $category_id = $row['id'];
404    }
405    $output.= insert_remote_image( $list_dirs[$i], $category_id );
406    update_category( $category_id );
407    $output.= insert_remote_category( $list_dirs[$i], $site_id,
408                                      $category_id, $level+1 );
409    $output.= '</div>';
410  }
411  // we have to remove the categories of the database not present in the xml
412  // file (ie deleted from the picture storage server)
413  $query = 'SELECT dir,id';
414  $query.= ' FROM '.PREFIX_TABLE.'categories';
415  $query.= ' WHERE site_id = '.$site_id;
416  if ( !is_numeric( $id_uppercat ) )
417  {
418    $query.= ' AND id_uppercat IS NULL';
419  }
420  else
421  {
422    $query.= ' AND id_uppercat = '.$id_uppercat;
423  }
424  $query.= ';';
425  $result = mysql_query( $query );
426  while ( $row = mysql_fetch_array( $result ) )
427  {
428    // is the category in the xml file ?
429    if ( !in_array( $row['dir'], $categories ) )
430    {
431      delete_category( $row['id'] );
432    }
433  }
434
435  return $output;
436}
437       
438// insert_remote_image searchs the "root" node of the xml_dir given and
439// insert the contained pictures if the are not in the database yet.
440function insert_remote_image( $xml_dir, $category_id )
441{
442  global $count_new,$lang;
443
444  $output = '';
445  $root = getChild( $xml_dir, 'root' );
446  $pictures = array();
447  $xml_pictures = getChildren( $root, 'picture' );
448  for ( $j = 0; $j < sizeof( $xml_pictures ); $j++ )
449  {
450    //<picture file="albatros.jpg" tn_ext="png" date="2002-04-14"
451    //  filesize="35" width="640" height="480" />
452    $file     = getAttribute( $xml_pictures[$j], 'file' );
453    $tn_ext   = getAttribute( $xml_pictures[$j], 'tn_ext' );
454    $date     = getAttribute( $xml_pictures[$j], 'date' ); 
455    $filesize = getAttribute( $xml_pictures[$j], 'filesize' );
456    $width    = getAttribute( $xml_pictures[$j], 'width' );
457    $height   = getAttribute( $xml_pictures[$j], 'height' );
458                       
459    $pictures[$j] = $file;
460                       
461    // is the picture already existing in the database ?
462    $query = 'SELECT id,tn_ext';
463    $query.= ' FROM '.PREFIX_TABLE.'images';
464    $query.= ' WHERE storage_category_id = '.$category_id;
465    $query.= " AND file = '".$file."'";
466    $query.= ';';
467    $result = mysql_query( $query );
468    $query = '';
469    if ( mysql_num_rows( $result ) == 0 )
470    {
471      $query = 'INSERT INTO '.PREFIX_TABLE.'images';
472      $query.= ' (file,storage_category_id,date_available,tn_ext';
473      $query.= ',filesize,width,height)';
474      $query.= ' VALUES (';
475      $query.= "'".$file."'";
476      $query.= ",'".$category_id."'";
477      $query.= ",'".$date."'";
478      $query.= ",'".$tn_ext."'";
479      $query.= ",'".$filesize."'";
480      $query.= ",'".$width."'";
481      $query.= ",'".$height."'";
482      $query.= ')';
483      $query.= ';';
484      mysql_query( $query );
485      // retrieving the id of newly inserted picture
486      $query = 'SELECT id';
487      $query.= ' FROM '.PREFIX_TABLE.'images';
488      $query.= ' WHERE storage_category_id = '.$category_id;
489      $query.= " AND file = '".$file."'";
490      $query.= ';';
491      list( $image_id ) = mysql_fetch_array( mysql_query( $query ) );
492      // adding the link between this picture and its storage category
493      $query = 'INSERT INTO '.PREFIX_TABLE.'image_category';
494      $query.= ' (image_id,category_id) VALUES ';
495      $query.= ' ('.$image_id.','.$category_id.')';
496      $query.= ';';
497      mysql_query( $query );
498
499      $output.= $file;
500      $output.= ' <span style="font-weight:bold;">';
501      $output.= $lang['update_research_added'].'</span>';
502      $output.= ' ('.$lang['update_research_tn_ext'].' '.$tn_ext.')<br />';
503
504      $count_new++;
505    }
506    else
507    {
508      // is the tn_ext the same in the xml file and in the database ?
509      $row = mysql_fetch_array( $result );
510      if ( $row['tn_ext'] != $tn_ext )
511      {
512        $query = 'UPDATE '.PREFIX_TABLE.'images';
513        $query.= ' SET';
514        $query.= " tn_ext = '".$tn_ext."'";
515        $query.= ' WHERE storage_category_id = '.$category_id;
516        $query.= " AND file = '".$file."'";
517        $query.= ';';
518      }
519    }
520    // execution of the query
521    if ( $query != '' )
522    {
523      mysql_query( $query );
524    }
525  }
526  // we have to remove the pictures of the database not present in the xml file
527  // (ie deleted from the picture storage server)
528  $query = 'SELECT id,file';
529  $query.= ' FROM '.PREFIX_TABLE.'images';
530  $query.= ' WHERE storage_category_id = '.$category_id;
531  $query.= ';';
532  $result = mysql_query( $query );
533  while ( $row = mysql_fetch_array( $result ) )
534  {
535    // is the file in the xml file ?
536    if ( !in_array( $row['file'], $pictures ) )
537    {
538      delete_image( $row['id'] );
539    }
540  }
541  return $output;
542}
543//----------------------------------------------------- template initialization
544$sub = $vtp->Open( '../template/'.$user['template'].'/admin/update.vtp' );
545$tpl = array( 'update_default_title', 'update_only_cat', 'update_all',
546              'update_research_conclusion', 'update_deletion_conclusion',
547              'remote_site', 'update_part_research' );
548templatize_array( $tpl, 'lang', $sub );
549$vtp->setGlobalVar( $sub, 'user_template', $user['template'] );
550//-------------------------------------------------------- categories structure
551$page['plain_structure'] = get_plain_structure();
552//-------------------------------------------- introduction : choices of update
553// Display choice if "update" var is not specified
554check_cat_id( $_GET['update'] );
555if ( !isset( $_GET['update'] )
556     and !( isset( $page['cat'] )
557            or $_GET['update'] == 'cats'
558            or $_GET['update'] == 'all' ) )
559{
560  $vtp->addSession( $sub, 'introduction' );
561  // only update the categories, not the pictures.
562  $url = add_session_id( './admin.php?page=update&amp;update=cats' );
563  $vtp->setVar( $sub, 'introduction.only_cat:url', $url );
564  // update the entire tree folder
565  $url = add_session_id( './admin.php?page=update&amp;update=all' );
566  $vtp->setVar( $sub, 'introduction.all:url', $url );
567  $vtp->closeSession( $sub, 'introduction' );
568}
569//------------------------------------------------- local update : ../galleries
570else
571{
572  $count_new = 0;
573  $count_deleted = 0;
574  $vtp->addSession( $sub, 'local_update' );
575  if ( isset( $page['cat'] ) )
576  {
577    $categories = insert_local_category( $page['cat'] );
578  }
579  else
580  {
581    $categories = insert_local_category( 'NULL' );
582  }
583  $vtp->setVar( $sub, 'local_update.categories', $categories );
584  $vtp->setVar( $sub, 'local_update.count_new', $count_new );
585  $vtp->setVar( $sub, 'local_update.count_deleted', $count_deleted );
586  $vtp->closeSession( $sub, 'local_update' );
587}
588//------------------------------------------------- remote update : listing.xml
589if ( @is_file( './listing.xml' ) )
590{
591  $count_new = 0;
592  $count_deleted = 0;
593  $vtp->addSession( $sub, 'remote_update' );
594
595  remote_images();
596  $vtp->setVar( $sub, 'remote_update.count_new', $count_new );
597  $vtp->setVar( $sub, 'remote_update.count_deleted', $count_deleted );
598
599  $vtp->closeSession( $sub, 'remote_update' );
600}
601//----------------------------------------------------------- sending html code
602$vtp->Parse( $handle , 'sub', $sub );
603?>
Note: See TracBrowser for help on using the repository browser.