source: trunk/admin/update.php @ 77

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

Multi categories for the same picture

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