source: trunk/admin/update.php @ 466

Last change on this file since 466 was 466, checked in by z0rglub, 20 years ago
  • non picture files management
  • all remote site update functions moved to nex file update_remote.php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.1 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// |                              update.php                               |
4// +-----------------------------------------------------------------------+
5// | application   : PhpWebGallery <http://phpwebgallery.net>              |
6// | branch        : BSF (Best So Far)                                     |
7// +-----------------------------------------------------------------------+
8// | file          : $RCSfile$
9// | last update   : $Date: 2004-08-05 17:33:14 +0000 (Thu, 05 Aug 2004) $
10// | last modifier : $Author: z0rglub $
11// | revision      : $Revision: 466 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28include_once( PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php');
29//------------------------------------------------------------------- functions
30function ordering( $id_uppercat)
31{
32  $rank = 1;
33               
34  $query = 'SELECT id';
35  $query.= ' FROM '.CATEGORIES_TABLE;
36  if ( !is_numeric( $id_uppercat))
37  {
38    $query.= ' WHERE id_uppercat IS NULL';
39  }
40  else
41  {
42    $query.= ' WHERE id_uppercat = '.$id_uppercat;
43  }
44  $query.= ' ORDER BY rank ASC, dir ASC';
45  $query.= ';';
46  $result = mysql_query( $query);
47  while ( $row = mysql_fetch_array( $result))
48  {
49    $query = 'UPDATE '.CATEGORIES_TABLE;
50    $query.= ' SET rank = '.$rank;
51    $query.= ' WHERE id = '.$row['id'];
52    $query.= ';';
53    mysql_query( $query);
54    $rank++;
55    ordering( $row['id']);
56  }
57}
58
59function insert_local_category($id_uppercat)
60{
61  global $conf, $page, $user, $lang;
62 
63  $uppercats = '';
64  $output = '';
65
66  // 0. retrieving informations on the category to display
67  $cat_directory = PHPWG_ROOT_PATH.'galleries';
68  if (is_numeric($id_uppercat))
69  {
70    $query = 'SELECT name,uppercats,dir FROM '.CATEGORIES_TABLE;
71    $query.= ' WHERE id = '.$id_uppercat;
72    $query.= ';';
73    $row = mysql_fetch_array( mysql_query( $query));
74    $uppercats = $row['uppercats'];
75    $name      = $row['name'];
76    $dir       = $row['dir'];
77
78    $upper_array = explode( ',', $uppercats);
79
80    $local_dir = '';
81
82    $database_dirs = array();
83    $query = '
84SELECT id,dir FROM '.CATEGORIES_TABLE.'
85  WHERE id IN ('.$uppercats.')
86;';
87    $result = mysql_query( $query);
88    while ($row = mysql_fetch_array($result))
89    {
90      $database_dirs[$row['id']] = $row['dir'];
91    }
92    foreach ($upper_array as $id)
93    {
94      $local_dir.= $database_dirs[$id].'/';
95    }
96
97    $cat_directory.= '/'.$local_dir;
98
99    // 1. display the category name to update
100    $output = '<ul class="menu">';
101    $output.= '<li><strong>'.$name.'</strong>';
102    $output.= ' [ '.$dir.' ]';
103    $output.= '</li>';
104
105    // 2. we search pictures of the category only if the update is for all
106    //    or a cat_id is specified
107    if (isset($page['cat']) or $_GET['update'] == 'all')
108    {
109      $output.= insert_local_element($cat_directory, $id_uppercat);
110    }
111  }
112
113  $sub_dirs = get_category_directories($cat_directory);
114
115  $sub_category_dirs = array();
116  $query = '
117SELECT id,dir FROM '.CATEGORIES_TABLE.'
118  WHERE site_id = 1
119';
120  if (!is_numeric($id_uppercat))
121  {
122    $query.= ' AND id_uppercat IS NULL';
123  }
124  else
125  {
126    $query.= ' AND id_uppercat = '.$id_uppercat;
127  }
128  $query.= '
129    AND dir IS NOT NULL'; // virtual categories not taken
130  $query.= '
131;';
132  $result = mysql_query($query);
133  while ($row = mysql_fetch_array($result))
134  {
135    $sub_category_dirs[$row['id']] = $row['dir'];
136  }
137 
138  // 3. we have to remove the categories of the database not present anymore
139  foreach ($sub_category_dirs as $id => $dir)
140  {
141    if (!in_array($dir, $sub_dirs))
142    {
143      delete_category($id);
144    }
145  }
146
147  // array of new categories to insert
148  $inserts = array();
149 
150  foreach ($sub_dirs as $sub_dir)
151  {
152    // 5. Is the category already existing ? we create a subcat if not
153    //    existing
154    $category_id = array_search($sub_dir, $sub_category_dirs);
155    if (!is_numeric($category_id))
156    {
157      if (preg_match('/^[a-zA-Z0-9-_.]+$/', $sub_dir))
158      {
159        $name = str_replace('_', ' ', $sub_dir);
160
161        $value = "('".$sub_dir."','".$name."',1";
162        if (!is_numeric($id_uppercat))
163        {
164          $value.= ',NULL';
165        }
166        else
167        {
168          $value.= ','.$id_uppercat;
169        }
170        $value.= ",'undef'";
171        $value.= ')';
172        array_push($inserts, $value);
173      }
174      else
175      {
176        $output.= '<span style="color:red;">"'.$sub_dir.'" : ';
177        $output.= $lang['update_wrong_dirname'].'</span><br />';
178      }
179    }
180  }
181
182  // we have to create the category
183  if (count($inserts) > 0)
184  {
185    $query = '
186INSERT INTO '.CATEGORIES_TABLE.'
187  (dir,name,site_id,id_uppercat,uppercats) VALUES
188';
189    $query.= implode(',', $inserts);
190    $query.= '
191;';
192    mysql_query($query);
193    // updating uppercats field
194    $query = '
195UPDATE '.CATEGORIES_TABLE.'
196  SET uppercats = ';
197    if ($uppercats != '')
198    {
199      $query.= "CONCAT('".$uppercats."',',',id)";
200    }
201    else
202    {
203      $query.= 'id';
204    }
205    $query.= '
206  WHERE id_uppercat ';
207    if (!is_numeric($id_uppercat))
208    {
209      $query.= 'IS NULL';
210    }
211    else
212    {
213      $query.= '= '.$id_uppercat;
214    }
215    $query.= '
216;';
217    mysql_query($query);
218  }
219
220  // Recursive call on the sub-categories (not virtual ones)
221  $query = '
222SELECT id
223  FROM '.CATEGORIES_TABLE.'
224  WHERE site_id = 1
225';
226  if (!is_numeric($id_uppercat))
227  {
228    $query.= '    AND id_uppercat IS NULL';
229  }
230  else
231  {
232    $query.= '    AND id_uppercat = '.$id_uppercat;
233  }
234  $query.= '
235    AND dir IS NOT NULL'; // virtual categories not taken
236  $query.= '
237;';
238  $result = mysql_query($query);
239  while ($row = mysql_fetch_array($result))
240  {
241    $output.= insert_local_category($row['id']);
242  }
243
244  if (is_numeric($id_uppercat))
245  {
246    $output.= '</ul>';
247  }
248  return $output;
249}
250
251function insert_local_element($dir, $category_id)
252{
253  global $lang,$conf,$count_new, $count_deleted;
254
255  $output = '';
256
257  // fs means FileSystem : $fs_files contains files in the filesystem found
258  // in $dir that can be managed by PhpWebGallery (see get_pwg_files
259  // function), $fs_thumbnails contains thumbnails, $fs_representatives
260  // contains potentially representative pictures for non picture files
261  $fs_files = get_pwg_files($dir);
262  $fs_thumbnails = get_thumb_files($dir);
263  $fs_representatives = get_representative_files($dir);
264
265  // element deletion
266  $to_delete_elements = array();
267  // deletion of element if the correspond file doesn't exist anymore
268  $query = '
269SELECT id,file
270  FROM '.IMAGES_TABLE.'
271  WHERE storage_category_id = '.$category_id.'
272;';
273  $result = mysql_query($query);
274  while ($row = mysql_fetch_array($result))
275  {
276    if (!in_array($row['file'], $fs_files))
277    {
278      $output.= $row['file'];
279      $output.= ' <span style="font-weight:bold;">';
280      $output.= $lang['update_disappeared'].'</span><br />';
281      array_push($to_delete_elements, $row['id']);
282    }
283  }
284  // in picture case, we also delete the element if the thumbnail doesn't
285  // existe anymore
286  $query = '
287SELECT id,file,tn_ext
288  FROM '.IMAGES_TABLE.'
289  WHERE storage_category_id = '.$category_id.'
290    AND ('.implode(' OR ',
291                   array_map(
292                     create_function('$s', 'return "file LIKE \'%".$s."\'";')
293                     , $conf['picture_ext'])).')
294;';
295  $result = mysql_query($query);
296  while ($row = mysql_fetch_array($result))
297  {
298    $thumbnail = $conf['prefix_thumbnail'];
299    $thumbnail.= get_filename_wo_extension($row['file']);
300    $thumbnail.= '.'.$row['tn_ext'];
301    if (!in_array($thumbnail, $fs_thumbnails))
302    {
303      $output.= $row['file'];
304      $output.= ' : <span style="font-weight:bold;">';
305      $output.= $lang['update_disappeared_tn'].'</span><br />';
306      array_push($to_delete_elements, $row['id']);
307    }
308  }
309
310  $to_delete_elements = array_unique($to_delete_elements);
311  $count_deleted+= count($to_delete_elements);
312  if ($count_deleted > 0)
313  {
314    delete_elements($to_delete_elements);
315  }
316 
317  $registered_elements = array();
318  $query = '
319SELECT file FROM '.IMAGES_TABLE.'
320   WHERE storage_category_id = '.$category_id.'
321;';
322  $result = mysql_query($query);
323  while ($row = mysql_fetch_array($result))
324  {
325    array_push($registered_elements, $row['file']);
326  }
327
328  // validated pictures are picture uploaded by users, validated by an admin
329  // and not registered (visible) yet
330  $validated_pictures    = array();
331  $unvalidated_pictures  = array();
332 
333  $query = '
334SELECT file,infos,validated
335  FROM '.WAITING_TABLE.'
336  WHERE storage_category_id = '.$category_id.'
337;';
338  $result = mysql_query($query);
339  while ($row = mysql_fetch_array($result))
340  {
341    if ($row['validated'] == 'true')
342    {
343      $validated_pictures[$row['file']] = $row['infos'];
344    }
345    else
346    {
347      array_push($unvalidated_pictures, $row['file']);
348    }
349  }
350
351  // we only search among the picture present in the filesystem and not
352  // present in the database yet. If we know that this picture is known as
353  // an uploaded one but not validated, it's not tested neither
354  $unregistered_elements = array_diff($fs_files
355                                      ,$registered_elements
356                                      ,$unvalidated_pictures);
357
358  $inserts = array();
359 
360  foreach ($unregistered_elements as $unregistered_element)
361  {
362    if (preg_match('/^[a-zA-Z0-9-_.]+$/', $unregistered_element))
363    {
364      $file_wo_ext = get_filename_wo_extension($unregistered_element);
365      $tn_ext = '';
366      foreach ($conf['picture_ext'] as $ext)
367      {
368        $test = $conf['prefix_thumbnail'].$file_wo_ext.'.'.$ext;
369        if (!in_array($test, $fs_thumbnails))
370        {
371          continue;
372        }
373        else
374        {
375          $tn_ext = $ext;
376          break;
377        }
378      }
379
380      // 2 cases : the element is a picture or not. Indeed, for a picture
381      // thumbnail is mandatory and for non picture element, thumbnail and
382      // representative is optionnal
383      if (in_array(get_extension($unregistered_element), $conf['picture_ext']))
384      {
385        // if we found a thumnbnail corresponding to our picture...
386        if ($tn_ext != '')
387        {
388          $image_size = @getimagesize($dir.$unregistered_element);
389          // (file, storage_category_id, date_available, tn_ext, filesize,
390          // width, height, name, author, comment, date_creation,
391          // representative_ext)'
392          $value = '(';
393          $value.= "'".$unregistered_element."'";
394          $value.= ','.$category_id;
395          $value.= ",'".date('Y-m-d')."'";
396          $value.= ",'".$tn_ext."'";
397          $value.= ','.floor(filesize($dir.$unregistered_element) / 1024);
398          $value.= ','.$image_size[0];
399          $value.= ','.$image_size[1];
400          if (isset($validated_pictures[$unregistered_element]))
401          {
402            // retrieving infos from the XML description from waiting table
403            $infos = nl2br($validated_pictures[$unregistered_element]);
404
405            $unixtime = getAttribute($infos, 'date_creation');
406            if ($unixtime != '')
407            {
408              $date_creation ="'".date('Y-m-d',$unixtime)."'";
409            }
410            else
411            {
412              $date_creation = 'NULL';
413            }
414         
415            $value.= ",'".getAttribute($infos, 'name')."'";
416            $value.= ",'".getAttribute($infos, 'author')."'";
417            $value.= ",'".getAttribute($infos, 'comment')."'";
418            $value.= ','.$date_creation;
419
420            // deleting the waiting element
421            $query = '
422DELETE FROM '.WAITING_TABLE.'
423  WHERE file = \''.$unregistered_element.'\'
424    AND storage_category_id = '.$category_id.'
425;';
426            mysql_query($query);
427          }
428          else
429          {
430            $value.= ",'','','',NULL";
431          }
432          $value.= ',NULL'; // representative_ext
433          $value.= ')';
434       
435          $count_new++;
436          $output.= $unregistered_element;
437          $output.= ' <span style="font-weight:bold;">';
438          $output.= $lang['update_research_added'].'</span>';
439          $output.= ' ('.$lang['update_research_tn_ext'].' '.$tn_ext.')';
440          $output.= '<br />';
441          array_push($inserts, $value);
442        }
443        else
444        {
445          $output.= '<span style="color:red;">';
446          $output.= $lang['update_missing_tn'].' : '.$unregistered_element;
447          $output.= ' (<span style="font-weight:bold;">';
448          $output.= $conf['prefix_thumbnail'];
449          $output.= get_filename_wo_extension($unregistered_element);
450          $output.= '.XXX</span>';
451          $output.= ', XXX = ';
452          $output.= implode(', ', $conf['picture_ext']);
453          $output.= ')</span><br />';
454        }
455      }
456      else
457      {
458        $representative_ext = '';
459        foreach ($conf['picture_ext'] as $ext)
460        {
461          $test = $conf['prefix_thumbnail'].$file_wo_ext.'.'.$ext;
462          if (!in_array($test, $fs_thumbnails))
463          {
464            continue;
465          }
466          else
467          {
468            $representative_ext = $ext;
469            break;
470          }
471        }
472
473        // (file, storage_category_id, date_available, tn_ext, filesize,
474        // width, height, name, author, comment, date_creation,
475        // representative_ext)'
476        $value = '(';
477        $value.= "'".$unregistered_element."'";
478        $value.= ','.$category_id;
479        $value.= ",'".date('Y-m-d')."'";
480        if ( $tn_ext != '' )
481        {
482          $value.= ",'".$tn_ext."'";
483        }
484        else
485        {
486          $value.= ',NULL';
487        }
488        $value.= ','.floor(filesize($dir.$unregistered_element) / 1024);
489        $value.= ',NULL';
490        $value.= ',NULL';
491        $value.= ',NULL';
492        $value.= ',NULL';
493        $value.= ',NULL';
494        $value.= ',NULL';
495        if ( $representative_ext != '' )
496        {
497          $value.= ",'".$representative_ext."'";
498        }
499        else
500        {
501          $value.= ',NULL';
502        }
503        $value.= ')';
504
505        $count_new++;
506        $output.= $unregistered_element;
507        $output.= ' <span style="font-weight:bold;">';
508        $output.= $lang['update_research_added'].'</span>';
509        $output.= '<br />';
510        array_push($inserts, $value);
511      }
512    }
513    else
514    {
515      $output.= '<span style="color:red;">"'.$unregistered_element.'" : ';
516      $output.= $lang['update_wrong_dirname'].'</span><br />';
517    }
518  }
519 
520  if (count($inserts) > 0)
521  {
522    // inserts all found pictures
523    $query = '
524INSERT INTO '.IMAGES_TABLE.'
525  (file,storage_category_id,date_available,tn_ext,filesize,width,height
526   ,name,author,comment,date_creation,representative_ext)
527   VALUES
528   '.implode(',', $inserts).'
529;';
530    mysql_query($query);
531
532    // what are the ids of the pictures in the $category_id ?
533    $ids = array();
534
535    $query = '
536SELECT id
537  FROM '.IMAGES_TABLE.'
538  WHERE storage_category_id = '.$category_id.'
539;';
540    $result = mysql_query($query);
541    while ($row = mysql_fetch_array($result))
542    {
543      array_push($ids, $row['id']);
544    }
545
546    // recreation of the links between this storage category pictures and
547    // its storage category
548    $query = '
549DELETE FROM '.IMAGE_CATEGORY_TABLE.'
550  WHERE category_id = '.$category_id.'
551    AND image_id IN ('.implode(',', $ids).')
552;';
553    mysql_query($query);
554
555    foreach ($ids as $num => $image_id)
556    {
557      $ids[$num] =  '('.$category_id.','.$image_id.')';
558    }
559    $query = '
560INSERT INTO '.IMAGE_CATEGORY_TABLE.'
561  (category_id,image_id) VALUES
562  '.implode(',', $ids).'
563;';
564    mysql_query($query);
565  }
566  return $output;
567}
568//----------------------------------------------------- template initialization
569$template->set_filenames(array('update'=>'admin/update.tpl'));
570
571$template->assign_vars(array(
572  'L_UPDATE_TITLE'=>$lang['update_default_title'],
573  'L_CAT_UPDATE'=>$lang['update_only_cat'],
574  'L_ALL_UPDATE'=>$lang['update_all'],
575  'L_RESULT_UPDATE'=>$lang['update_part_research'],
576  'L_NEW_CATEGORY'=>$lang['update_research_conclusion'],
577  'L_DEL_CATEGORY'=>$lang['update_deletion_conclusion'],
578 
579  'U_CAT_UPDATE'=>add_session_id(PHPWG_ROOT_PATH.'admin.php?page=update&amp;update=cats'),
580  'U_ALL_UPDATE'=>add_session_id(PHPWG_ROOT_PATH.'admin.php?page=update&amp;update=all')
581 ));
582 
583//-------------------------------------------- introduction : choices of update
584// Display choice if "update" var is not specified
585if (!isset($_GET['update']))
586{
587 $template->assign_block_vars('introduction',array());
588}
589//-------------------------------------------------- local update : ./galleries
590else
591{
592  check_cat_id($_GET['update']);
593  $start = get_moment();
594  $count_new = 0;
595  $count_deleted = 0;
596 
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  $end = get_moment();
606  //echo get_elapsed_time($start, $end).' for update <br />';
607  $template->assign_block_vars('update',array(
608    'CATEGORIES'=>$categories,
609        'NEW_CAT'=>$count_new,
610        'DEL_CAT'=>$count_deleted
611   ));
612}
613//---------------------------------------- update informations about categories
614if (isset($_GET['update'])
615     or isset($page['cat'])
616     or @is_file('./listing.xml') && DEBUG)
617{
618  $start = get_moment();
619  update_category('all');
620  ordering('NULL');
621  $end = get_moment();
622  echo get_elapsed_time($start, $end).' for update_category(all)<br />';
623}
624//----------------------------------------------------------- sending html code
625$template->assign_var_from_handle('ADMIN_CONTENT', 'update');
626?>
Note: See TracBrowser for help on using the repository browser.