source: tags/version_1_3/admin/miseajour.php @ 31208

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

Initial revision

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.2 KB
Line 
1<?
2/***************************************************************************
3 *                  miseajour.php is a part of PhpWebGallery               *
4 *                            -------------------                          *
5 *   last update          : Tuesday, July 16, 2002                         *
6 *   email                : pierrick@z0rglub.com                           *
7 *                                                                         *
8 ***************************************************************************/
9
10/***************************************************************************
11 *                                                                         *
12 *   This program is free software; you can redistribute it and/or modify  *
13 *   it under the terms of the GNU General Public License as published by  *
14 *   the Free Software Foundation;                                         *
15 *                                                                         *
16 ***************************************************************************/
17        include_once( "./include/isadmin.inc.php" );
18       
19        function insert_local_category( $cat_id )
20        {
21                global $prefixeTable,$conf,$page,$HTTP_GET_VARS;
22               
23                $site_id = 1;
24               
25                // 0. retrieving informations on the category to display
26                $cat_directory = "../galleries";
27               
28                if ( is_numeric( $cat_id ) )
29                {
30                        $result = get_cat_info( $cat_id );
31                        $cat_directory.= "/".$result['local_dir'];
32                        // 1. display the category name to update
33                        echo "
34                                <img src=\"".$conf['lien_puce']."\" alt=\"&gt;\" /><span style=\"font-weight:bold;\">".$result['name'][0]."</span> [ dir : ".$result['last_dir']." ]
35                                <div class=\"retrait\">";
36                       
37                        // 2. we search pictures of the category only if the update is for all or a cat_id is specified
38                        if ( isset( $page['cat'] ) || $HTTP_GET_VARS['update'] == 'all' )
39                        {
40                                insert_local_image( $cat_directory, $cat_id );
41                                update_cat_info( $cat_id );
42                        }
43                }
44               
45                // 3. we have to remove the categories of the database not present anymore
46                $query = "select id from $prefixeTable"."categories";
47                $query.= " where site_id = $site_id";
48                if ( !is_numeric( $cat_id ) )
49                {
50                        $query.= " and id_uppercat is NULL;";
51                }
52                else
53                {
54                        $query.= " and id_uppercat = $cat_id;";
55                }
56                $result = mysql_query( $query );
57                while ( $row = mysql_fetch_array( $result ) )
58                {
59                        // retrieving the directory
60                        $rep = "../galleries";
61                        $resultat = get_cat_info( $row['id'] );
62                        $rep.= "/".$resultat['local_dir'];
63                       
64                        // is the directory present ?
65                        if ( !is_dir( $rep ) )
66                        {
67                                delete_category( $row['id'] );
68                        }
69                }
70               
71                // 4. retrieving the sub-directories
72                $sub_rep = array();
73                $i = 0;
74                $dirs = "";
75                if ( $opendir = opendir ( $cat_directory ) )
76                {
77                        while ( $file = readdir ( $opendir ) )
78                        {
79                                if ( $file != "." && $file != ".." && is_dir ( $cat_directory."/".$file ) && $file != "thumbnail" )
80                                {
81                                        $sub_rep[$i++] = $file;
82                                }
83                        }
84                }
85               
86                for ( $i = 0; $i < sizeof( $sub_rep ); $i++ )
87                {
88                        // 5. is the category already existing ? we create a subcat if not existing
89                        $category_id = "";
90                        $query = "select id from $prefixeTable"."categories";
91                        $query.= " where site_id = $site_id";
92                        $query.= " and dir = '".$sub_rep[$i]."'";
93                        if ( !is_numeric( $cat_id ) )
94                        {
95                                $query.= " and id_uppercat is NULL;";
96                        }
97                        else
98                        {
99                                $query.= " and id_uppercat = $cat_id;";
100                        }
101                        $result = mysql_query( $query );
102                        if ( mysql_num_rows( $result ) == 0 )
103                        {
104                                // we have to create the category
105                                $query = "insert into $prefixeTable"."categories (dir,site_id,id_uppercat) values ('".$sub_rep[$i]."','$site_id'";
106                                if ( !is_numeric( $cat_id ) )
107                                {
108                                        $query.= ",NULL";
109                                }
110                                else
111                                {
112                                        $query.= ",'$cat_id'";
113                                }
114                                $query.= ");";
115                                mysql_query( $query );
116                                $category_id = mysql_insert_id();
117                        }
118                        else
119                        {
120                                // we get the already registered id
121                                $row = mysql_fetch_array( $result );
122                                $category_id = $row['id'];
123                        }
124                        // 6. recursive call
125                        insert_local_category( $category_id );
126                }
127               
128                if ( is_numeric( $cat_id ) )
129                {
130                        echo "
131                                </div>";
132                }
133        }
134       
135        function insert_local_image( $rep, $category_id )
136        {
137                global $prefixeTable,$lang,$conf,$count_new;
138               
139                // we have to delete all the images from the database that :
140                //     - are not in the directory anymore
141                //     - don't have the associated thumbnail available anymore
142                $query = "select id,file,tn_ext from $prefixeTable"."images";
143                $query.= " where cat_id = $category_id;";
144                $result = mysql_query( $query );
145                while ( $row = mysql_fetch_array( $result ) )
146                {
147                        $lien_image = $rep."/".$row['file'];
148                        $lien_thumbnail = $rep."/"."thumbnail/".$conf['prefixe_thumbnail'].substr( $row['file'], 0, strrpos( $row['file'], "." ) ).".".$row['tn_ext'];
149               
150                        if ( !is_file ( $lien_image ) || !is_file ( $lien_thumbnail ) )
151                        {
152                                if ( !is_file ( $lien_image ) )
153                                {
154                                        echo $row['file']." <span style=\"font-weight:bold;\">".$lang['update_disappeared']."</span><br />";
155                                }
156                                if ( !is_file ( $lien_thumbnail ) )
157                                {
158                                        echo $row['file']." : <span style=\"font-weight:bold;\">".$lang['update_disappeared_tn']."</span><br />";
159                                }
160                                // suppression de la base :
161                                delete_image( $row['id'] );
162                        }
163                }
164               
165                // searching the new images in the directory
166                $pictures = array();           
167                $i = 0;
168                $tn_ext = "";
169                $root = "";
170                if ( $opendir = opendir ( $rep ) )
171                {
172                        while ( $file = readdir ( $opendir ) )
173                        {
174                                if ( is_file( $rep."/".$file ) && is_image( $rep."/".$file ) )
175                                {
176                                        // is the picture waiting for validation by an administrator ?
177                                        $query = "select id from $prefixeTable"."waiting";
178                                        $query.= " where cat_id = $category_id";
179                                        $query.= " and file = '$file';";
180                                        $result = mysql_query( $query );
181                                        if ( mysql_num_rows( $result ) == 0 )
182                                        {
183                                                if ( $tn_ext = TN_exist( $rep, $file ) )
184                                                {
185                                                        // is the picture already in the database ?
186                                                        $query = "select id from $prefixeTable"."images";
187                                                        $query.= " where cat_id = $category_id";
188                                                        $query.= " and file = '$file';";
189                                                        $result = mysql_query( $query );
190                                                        if ( mysql_num_rows( $result ) == 0 )
191                                                        {
192                                                                $pictures[$i] = array();
193                                                                $pictures[$i]['file'] = $file;
194                                                                $pictures[$i]['tn_ext'] = $tn_ext;
195                                                                $pictures[$i]['date'] = date( "Y-m-d", filemtime ( $rep."/".$file ) );
196                                                                $pictures[$i]['filesize'] = floor ( filesize( $rep."/".$file ) / 1024 );
197                                                                $image_size = @getimagesize( $rep."/".$file );
198                                                                $pictures[$i]['width'] = $image_size[0];
199                                                                $pictures[$i]['height'] = $image_size[1];
200                                                                $i++;
201                                                        }
202                                                }
203                                                else
204                                                {
205                                                        echo "<span style=\"color:red;\">".$lang['update_missing_tn']." : $file (<span style=\"font-weight:bold;\">".$conf['prefixe_thumbnail'].substr( $file, 0, strrpos( $file, "." ) ).".XXX</span>, XXX = gif, png or jpg)</span><br />";
206                                                }
207                                        }
208                                }
209                        }
210                }
211                // inserting the pictures found in the directory
212                $root.= "\n".$indent."<root>";
213                if ( sizeof( $pictures ) > 0 )
214                {
215                        for( $i = 0; $i < sizeof( $pictures ); $i++ )
216                        {
217                                $query = "insert into $prefixeTable"."images (file,cat_id,date_available,tn_ext,filesize,width,height) values ('".$pictures[$i]['file']."','".$category_id."','".$pictures[$i]['date']."','".$pictures[$i]['tn_ext']."','".$pictures[$i]['filesize']."','".$pictures[$i]['width']."','".$pictures[$i]['height']."');";
218                                echo"
219                                                        ".$pictures[$i]['file']." <span style=\"font-weight:bold;\">".$lang['update_research_added']."</span> (".$lang['update_research_tn_ext']." ".$pictures[$i]['tn_ext'].")<br />";
220                                $count_new++;
221                                mysql_query( $query );
222                        }
223                }
224        }
225       
226        // The function "update_cat_info" updates the information about the last online image
227        // and the number of images in the category
228        function update_cat_info( $category_id )
229        {
230                global $prefixeTable;
231               
232                $query = "select date_available from $prefixeTable"."images";
233                $query.= " where cat_id = $category_id";
234                $query.= " order by date_available desc limit 0,1;";
235                $result = mysql_query( $query );
236                $row = mysql_fetch_array( $result );
237                $date_last = $row['date_available'];
238               
239                $query = "select count(*) as nb_images from $prefixeTable"."images";
240                $query.= " where cat_id = $category_id";
241                $result = mysql_query( $query );
242                $row = mysql_fetch_array( $result );
243                $nb_images = $row['nb_images'];
244               
245                $query = "update $prefixeTable"."categories";
246                $query.= " set date_dernier = '$date_last'";
247                $query.= ", nb_images = $nb_images";
248                $query.= " where id = $category_id;";
249                mysql_query( $query );
250        }
251       
252        function getContent( $element, $node )
253        {               
254                $content = str_replace( "<".$node.">", "", $element );
255                $content = str_replace( "</".$node.">", "", $content );
256                return $content;
257        }
258       
259        function getChild( $document, $node )
260        {
261                preg_match("/\<".$node.">.*\<\/".$node."\>/U", $document, $retour);
262                return $retour[0];
263        }
264       
265        function getChildren( $document, $node )
266        {
267                preg_match_all("/\<".$node.">.*\<\/".$node."\>/U", $document, $retour);
268                return $retour[0];
269        }
270       
271        function remote_images()
272        {
273                global $conf, $prefixeTable, $lang;
274               
275                // 1.is there a file listing.xml ?
276                $filename = "listing.xml";
277                $xml_content = "";
278                if ( $fp = @fopen ( $filename, "r" ) )
279                {
280                        while ( !feof ( $fp ) )
281                        {
282                                $xml_content .= fgets ( $fp, 1024 );
283                        }
284                        @fclose( $file );
285                        $xml_content = str_replace("\n","",$xml_content);
286                        $xml_content = str_replace("\t","",$xml_content);
287                }
288                else
289                {
290                        return false;
291                }
292                $url = getContent( getChild( $xml_content, "url" ), "url" );
293                echo "<span style=\"font-weight:bold;color:navy;\">$url</span><br /><br />";
294               
295                // 2. is the site already existing ?
296                $site_id = "";
297                $result = mysql_query( "select id from $prefixeTable"."sites where galleries_url = '$url';" );
298                if ( mysql_num_rows($result ) == 0 )
299                {
300                        // we have to register this site in the database
301                        mysql_query( "insert into $prefixeTable"."sites (galleries_url) values ('$url');" );
302                        $site_id = mysql_insert_id();
303                }
304                else
305                {
306                        // we get the already registered id
307                        $row = mysql_fetch_array( $result );
308                        $site_id = $row['id'];
309                }
310               
311                // 3. available dirs in the file
312                insert_remote_category( $xml_content, $site_id, "NULL", 0 );
313        }
314       
315        // insert_remote_category search the "dir" node of the xml_dir given
316        // and insert the contained categories if the are not in the database yet.
317        // The function also delete the categories that are in the database
318        // and not in the xml_file
319        function insert_remote_category( $xml_dir, $site_id, $id_uppercat, $level )
320        {
321                global $prefixeTable,$conf;;
322               
323                $categories = array();
324                $list_dirs = getChildren( $xml_dir, "dir".$level );
325                for ( $i = 0; $i < sizeof( $list_dirs ); $i++ )
326                {
327                        // is the category already existing ?
328                        $category_id = "";
329                        $name = getContent( getChild( $list_dirs[$i], "name" ), "name" );
330                        $categories[$i] = $name;
331                        echo "
332                                <img src=\"".$conf['lien_puce']."\"><span style=\"font-weight:bold;\">$name</span>
333                                <div class=\"retrait\">";
334                        $query = "select id from $prefixeTable"."categories";
335                        $query.= " where site_id = '$site_id'";
336                        $query.= " and dir = '$name'";
337                        if ( $id_uppercat == "NULL" )
338                        {
339                                $query.= " and id_uppercat is NULL;";
340                        }
341                        else
342                        {
343                                $query.= " and id_uppercat = '$id_uppercat';";
344                        }
345                        //echo "<br />".$query;
346                        $result = mysql_query( $query );
347                        if ( mysql_num_rows( $result ) == 0 )
348                        {
349                                // we have to create the category
350                                $query = "insert into $prefixeTable"."categories (dir,site_id,id_uppercat) values ('$name','$site_id'";
351                                if ( $id_uppercat == "NULL" )
352                                {
353                                        $query.= ",NULL";
354                                }
355                                else
356                                {
357                                        $query.= ",'$id_uppercat'";
358                                }
359                                $query.= ");";
360                                //echo "<br />".$query;
361                                mysql_query( $query );
362                                $category_id = mysql_insert_id();
363                        }
364                        else
365                        {
366                                // we get the already registered id
367                                $row = mysql_fetch_array( $result );
368                                $category_id = $row['id'];
369                        }
370                        insert_remote_image( $list_dirs[$i], $category_id );
371                        update_cat_info( $category_id );
372                        insert_remote_category( $list_dirs[$i], $site_id, $category_id, $level + 1 );
373                        echo "
374                                </div>";
375                }
376                // we have to remove the categories of the database not present in the xml file
377                // (ie deleted from the picture storage server)
378                $query = "select dir,id from $prefixeTable"."categories";
379                $query.= " where site_id = '$site_id'";
380                if ( $id_uppercat == "NULL" )
381                {
382                        $query.= " and id_uppercat is NULL;";
383                }
384                else
385                {
386                        $query.= " and id_uppercat = '$id_uppercat';";
387                }
388                $result = mysql_query( $query );
389                while ( $row = mysql_fetch_array( $result ) )
390                {
391                        // is the category in the xml file ?
392                        if ( !in_array( $row['dir'], $categories ) )
393                        {
394                                delete_category( $row['id'] );
395                        }
396                }
397        }
398       
399        // insert_remote_image search the "root" node of the xml_dir given
400        // and insert the contained pictures if the are not in the database yet
401        function insert_remote_image( $xml_dir, $category_id )
402        {
403                global $prefixeTable,$count_new,$lang;
404               
405                $root = getChild( $xml_dir, "root" );
406                $pictures = array();
407                $xml_pictures = getChildren( $root, "picture" );
408                for ( $j = 0; $j < sizeof( $xml_pictures ); $j++ )
409                {
410                        //<picture>
411                        //      <file>albatros.jpg</file>
412                        //      <tn_ext>png</tn_ext>
413                        //      <date>2002-04-14</date>
414                        //      <filesize>35</filesize>
415                        //      <width>640</width>
416                        //      <height>480</height>
417                        //</picture>
418                        $file = getContent( getChild( $xml_pictures[$j], "file" ), "file" );
419                        $tn_ext = getContent( getChild( $xml_pictures[$j], "tn_ext" ), "tn_ext" );
420                        $date = getContent( getChild( $xml_pictures[$j], "date" ), "date" );
421                        $filesize = getContent( getChild( $xml_pictures[$j], "filesize" ), "filesize" );
422                        $width = getContent( getChild( $xml_pictures[$j], "width" ), "width" );
423                        $height = getContent( getChild( $xml_pictures[$j], "height" ), "height" );
424                       
425                        $pictures[$j] = $file;
426                       
427                        // is the picture already existing in the database ?
428                        $query = "select id,tn_ext from $prefixeTable"."images where cat_id = '$category_id' and file = '$file';";
429                        $result = mysql_query( $query );
430                        $query = "";
431                        if ( mysql_num_rows( $result ) == 0 )
432                        {
433                                $query = "insert into $prefixeTable"."images (file,cat_id,date_available,tn_ext,filesize,width,height) values ('$file','$category_id','$date','$tn_ext','$filesize','$width','$height');";
434                                echo"
435                                                        $file <span style=\"font-weight:bold;\">".$lang['update_research_added']."</span> (".$lang['update_research_tn_ext']." $tn_ext)<br />";
436                                $count_new++;
437                        }
438                        else
439                        {
440                                // is the tn_ext the same in the xml file and in the database ?
441                                $row = mysql_fetch_array( $result );
442                                if ( $row['tn_ext'] != $tn_ext )
443                                {
444                                        $query = "update $prefixeTable"."images set tn_ext = '$tn_ext' where cat_id = '$category_id' and file = '$file';";
445                                }
446                        }
447                        // execution of the query
448                        if ( $query != "" )
449                        {
450                                mysql_query( $query );
451                        }
452                }
453                // we have to remove the pictures of the database not present in the xml file
454                // (ie deleted from the picture storage server)
455                $query = "select id,file from $prefixeTable"."images where cat_id = '$category_id';";
456                $result = mysql_query( $query );
457                while ( $row = mysql_fetch_array( $result ) )
458                {
459                        // is the file in the xml file ?
460                        if ( !in_array( $row['file'], $pictures ) )
461                        {
462                                delete_image( $row['id'] );
463                        }
464                }
465        }
466        //------------------------------------------------------------------------------
467        echo "<table style=\"width:100%;\">";   
468        //------------------------------------------------------------------------------
469        // Display choice if "update" var is not specified
470        check_cat_id( $HTTP_GET_VARS['update'] );
471        if ( !isset( $HTTP_GET_VARS['update'] ) && !( isset( $page['cat'] ) || $HTTP_GET_VARS['update'] == 'cats' || $HTTP_GET_VARS['update'] == 'all' ) )
472        {
473                echo"
474        <tr><th>".$lang['update_default_title']."</th></tr>
475        <tr>
476                <td>
477                        <div class=\"retrait\">
478                                <img src=\"".$conf['lien_puce']."\" alt=\"&gt;\" /><a href=\"".add_session_id_to_url( "./admin.php?page=miseajour&amp;update=cats" )."\">".$lang['update_only_cat']."</a>
479                                <br /><img src=\"".$conf['lien_puce']."\" alt=\"&gt;\" /><a href=\"".add_session_id_to_url( "./admin.php?page=miseajour&amp;update=all" )."\">".$lang['update_all']."</a>
480                        </div>
481                </td>
482        </tr>";
483        }
484        //------------------------------------------------------------------------------
485        // Recherche des nouvelles images dans les repertoires
486        else
487        {
488                $count_new = 0;
489                $count_deleted = 0;
490                echo"
491        <tr><th>".$lang['update_part_research']."</th></tr>
492        <tr>
493                <td>
494                        <div class=\"retrait\">";
495                if ( isset( $page['cat'] ) )
496                {
497                        insert_local_category( $page['cat'] );
498                }
499                else
500                {
501                        insert_local_category( "NULL" );
502                }
503                echo "<br /><span style=\"color:blue;\">$count_new ".$lang['update_research_conclusion']."</span>";
504                echo "<br /><span style=\"color:red;\">$count_deleted ".$lang['update_deletion_conclusion']."</span>";
505                echo "
506                        </div>
507                </td>
508        </tr>";
509        }
510        //------------------------------------------------------------------------------
511        // Searching new pictures in the file listing.xml from a remote storage server
512        if ( @is_file( "./listing.xml" ) )
513        {
514                $count_new = 0;
515                $count_deleted = 0;
516                echo"
517                <tr><th>Site distant</th></tr>
518                <tr>
519                        <td>
520                                <div class=\"retrait\">";
521                remote_images();
522                echo "<br /><span style=\"color:blue;\">$count_new ".$lang['update_research_conclusion']."</span>";
523                echo "<br /><span style=\"color:red;\">$count_deleted ".$lang['update_deletion_conclusion']."</span>";
524                echo "
525                                        </div>
526                        </td>
527                </tr>";
528        }
529        //------------------------------------------------------------------------------
530        echo "</table>";
531?>
Note: See TracBrowser for help on using the repository browser.