2006-02-28 02:13:16 +01:00
< ? php
// +-----------------------------------------------------------------------+
2008-04-05 00:57:23 +02:00
// | Piwigo - a PHP based picture gallery |
// +-----------------------------------------------------------------------+
2010-03-19 23:37:10 +01:00
// | Copyright(C) 2008-2010 Piwigo Team http://piwigo.org |
2008-04-05 00:57:23 +02:00
// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net |
// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick |
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License as published by |
2006-02-28 02:13:16 +01:00
// | the Free Software Foundation |
// | |
// | This program is distributed in the hope that it will be useful, but |
// | WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
// | General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA. |
// +-----------------------------------------------------------------------+
if ( ! defined ( 'PHPWG_ROOT_PATH' ))
{
2006-03-05 00:31:46 +01:00
die ( 'Hacking attempt!' );
2006-02-28 02:13:16 +01:00
}
2006-03-09 23:46:28 +01:00
include_once ( PHPWG_ROOT_PATH . 'admin/include/functions.php' );
// +-----------------------------------------------------------------------+
// | Check Access and exit when user status is not ok |
// +-----------------------------------------------------------------------+
check_status ( ACCESS_ADMINISTRATOR );
2006-02-28 02:13:16 +01:00
2006-03-05 00:31:46 +01:00
if ( ! is_numeric ( $_GET [ 'site' ]))
2006-02-28 02:13:16 +01:00
{
die ( 'site param missing or invalid' );
}
$site_id = $_GET [ 'site' ];
2006-03-05 00:31:46 +01:00
$query = '
SELECT galleries_url
FROM '.SITES_TABLE.'
2008-08-29 14:35:16 +02:00
WHERE id = ' . $site_id ;
2009-11-20 15:17:04 +01:00
list ( $site_url ) = pwg_db_fetch_row ( pwg_query ( $query ));
2006-03-05 00:31:46 +01:00
if ( ! isset ( $site_url ))
2006-02-28 02:13:16 +01:00
{
2006-03-05 00:31:46 +01:00
die ( 'site ' . $site_id . ' does not exist' );
2006-02-28 02:13:16 +01:00
}
$site_is_remote = url_is_remote ( $site_url );
2009-11-20 15:17:04 +01:00
list ( $dbnow ) = pwg_db_fetch_row ( pwg_query ( 'SELECT NOW();' ));
2006-02-28 02:13:16 +01:00
define ( 'CURRENT_DATE' , $dbnow );
$error_labels = array (
2006-03-05 00:31:46 +01:00
'PWG-UPDATE-1' => array (
2010-03-02 15:54:22 +01:00
l10n ( 'wrong filename' ),
2010-03-20 23:35:39 +01:00
l10n ( 'The name of directories and files must be composed of letters, numbers, "-", "_" or "."' )
2006-03-05 00:31:46 +01:00
),
'PWG-UPDATE-2' => array (
2010-03-02 15:54:22 +01:00
l10n ( 'missing thumbnail' ),
2010-03-20 23:35:39 +01:00
l10n ( 'a picture filetype requires a thumbnail. The thumbnail must be present in the sub-directory "thumbnail" of the category directory. The thumbnail filename must start with the configured thumbnail prefix and the extension must be among the following list :' ) . implode ( ',' , $conf [ 'picture_ext' ])
2006-03-05 00:31:46 +01:00
),
'PWG-ERROR-NO-FS' => array (
2010-03-02 15:54:22 +01:00
l10n ( 'File/directory read error' ),
l10n ( 'The file or directory cannot be accessed (either it does not exist or the access is denied)' )
2006-03-05 00:31:46 +01:00
),
'PWG-ERROR-VERSION' => array (
2010-03-02 15:54:22 +01:00
l10n ( 'Piwigo version differs on the remote site' ),
l10n ( 'Version of create_listing_file.php on the remote site and Piwigo must be the same' )
2006-03-05 00:31:46 +01:00
),
'PWG-ERROR-NOLISTING' => array (
2010-03-02 15:54:22 +01:00
l10n ( 'listing.xml file was not found' ),
2010-03-20 23:35:39 +01:00
l10n ( 'listing.xml file was not found on the remote site. This file is generated by choosing the "generate listing" command in the Site manager' )
2006-03-05 00:31:46 +01:00
)
);
2006-02-28 02:13:16 +01:00
$errors = array ();
$infos = array ();
if ( $site_is_remote )
{
2006-03-05 00:31:46 +01:00
include_once ( PHPWG_ROOT_PATH . 'admin/site_reader_remote.php' );
2006-03-28 03:26:37 +02:00
$local_listing = null ;
if ( isset ( $_GET [ 'local_listing' ])
and $_GET [ 'local_listing' ] )
{
$local_listing = PHPWG_ROOT_PATH . 'listing.xml' ;
}
$site_reader = new RemoteSiteReader ( $site_url , $local_listing );
2006-02-28 02:13:16 +01:00
}
else
{
include_once ( PHPWG_ROOT_PATH . 'admin/site_reader_local.php' );
$site_reader = new LocalSiteReader ( $site_url );
}
2006-03-05 00:31:46 +01:00
$general_failure = true ;
2006-02-28 02:13:16 +01:00
if ( isset ( $_POST [ 'submit' ]))
{
2006-04-19 01:46:53 +02:00
if ( ! isset ( $conf [ 'flip_picture_ext' ]))
{
$conf [ 'flip_picture_ext' ] = array_flip ( $conf [ 'picture_ext' ]);
}
2006-02-28 02:13:16 +01:00
if ( $site_reader -> open ())
{
$general_failure = false ;
}
2006-07-11 22:51:24 +02:00
2006-02-28 02:13:16 +01:00
// shall we simulate only
2006-07-11 22:51:24 +02:00
if (( isset ( $_POST [ 'simulate' ]) and $_POST [ 'simulate' ] == 1 ) or is_adviser ())
2006-02-28 02:13:16 +01:00
{
$simulate = true ;
}
else
{
$simulate = false ;
}
}
// +-----------------------------------------------------------------------+
// | directories / categories |
// +-----------------------------------------------------------------------+
if ( isset ( $_POST [ 'submit' ])
2007-06-21 22:49:45 +02:00
and ( $_POST [ 'sync' ] == 'dirs' or $_POST [ 'sync' ] == 'files' ))
2006-02-28 02:13:16 +01:00
{
$counts [ 'new_categories' ] = 0 ;
$counts [ 'del_categories' ] = 0 ;
$counts [ 'del_elements' ] = 0 ;
$counts [ 'new_elements' ] = 0 ;
2006-04-19 01:46:53 +02:00
$counts [ 'upd_elements' ] = 0 ;
2007-06-21 22:49:45 +02:00
}
2006-02-28 02:13:16 +01:00
2007-06-21 22:49:45 +02:00
if ( isset ( $_POST [ 'submit' ])
and ( $_POST [ 'sync' ] == 'dirs' or $_POST [ 'sync' ] == 'files' )
and ! $general_failure )
{
2006-02-28 02:13:16 +01:00
$start = get_moment ();
// which categories to update ?
$query = '
SELECT id , uppercats , global_rank , status , visible
FROM '.CATEGORIES_TABLE.'
WHERE dir IS NOT NULL
AND site_id = ' . $site_id ;
if ( isset ( $_POST [ 'cat' ]) and is_numeric ( $_POST [ 'cat' ]))
{
if ( isset ( $_POST [ 'subcats-included' ]) and $_POST [ 'subcats-included' ] == 1 )
{
$query .= '
2009-11-25 20:02:57 +01:00
AND uppercats '.DB_REGEX_OPERATOR.' \ '(^|,)' . $_POST [ 'cat' ] . ' (, | $ ) \ '
2006-02-28 02:13:16 +01:00
' ;
}
else
{
$query .= '
AND id = '.$_POST[' cat '].'
' ;
}
}
2008-08-29 14:35:16 +02:00
$db_categories = hash_from_query ( $query , 'id' );
2006-02-28 02:13:16 +01:00
// get categort full directories in an array for comparison with file
// system directory tree
$db_fulldirs = get_fulldirs ( array_keys ( $db_categories ));
// what is the base directory to search file system sub-directories ?
if ( isset ( $_POST [ 'cat' ]) and is_numeric ( $_POST [ 'cat' ]))
{
$basedir = $db_fulldirs [ $_POST [ 'cat' ]];
}
else
{
$basedir = preg_replace ( '#/*$#' , '' , $site_url );
}
// we need to have fulldirs as keys to make efficient comparison
$db_fulldirs = array_flip ( $db_fulldirs );
// finding next rank for each id_uppercat. By default, each category id
// has 1 for next rank on its sub-categories to create
$next_rank [ 'NULL' ] = 1 ;
$query = '
SELECT id
2008-08-29 14:35:16 +02:00
FROM ' . CATEGORIES_TABLE ;
2006-02-28 02:13:16 +01:00
$result = pwg_query ( $query );
2009-11-20 15:17:04 +01:00
while ( $row = pwg_db_fetch_assoc ( $result ))
2006-02-28 02:13:16 +01:00
{
$next_rank [ $row [ 'id' ]] = 1 ;
}
// let's see if some categories already have some sub-categories...
$query = '
SELECT id_uppercat , MAX ( rank ) + 1 AS next_rank
FROM '.CATEGORIES_TABLE.'
2008-08-29 14:35:16 +02:00
GROUP BY id_uppercat ' ;
2006-02-28 02:13:16 +01:00
$result = pwg_query ( $query );
2009-11-20 15:17:04 +01:00
while ( $row = pwg_db_fetch_assoc ( $result ))
2006-02-28 02:13:16 +01:00
{
// for the id_uppercat NULL, we write 'NULL' and not the empty string
if ( ! isset ( $row [ 'id_uppercat' ]) or $row [ 'id_uppercat' ] == '' )
{
$row [ 'id_uppercat' ] = 'NULL' ;
}
$next_rank [ $row [ 'id_uppercat' ]] = $row [ 'next_rank' ];
}
// next category id available
2009-11-25 20:02:57 +01:00
$next_id = pwg_db_nextval ( 'id' , CATEGORIES_TABLE );
2006-02-28 02:13:16 +01:00
// retrieve sub-directories fulldirs from the site reader
$fs_fulldirs = $site_reader -> get_full_directories ( $basedir );
// get_full_directories doesn't include the base directory, so if it's a
// category directory, we need to include it in our array
if ( isset ( $_POST [ 'cat' ]))
{
array_push ( $fs_fulldirs , $basedir );
}
2008-05-16 22:03:30 +02:00
// If $_POST['subcats-included'] != 1 ("Search in subcategories" is unchecked)
// $db_fulldirs doesn't include any subdirectories and $fs_fulldirs does
// So $fs_fulldirs will be limited to the selected basedir
// (if that one is in $fs_fulldirs)
if ( ! isset ( $_POST [ 'subcats-included' ]) or $_POST [ 'subcats-included' ] != 1 )
{
$fs_fulldirs = array_intersect ( $fs_fulldirs , array_keys ( $db_fulldirs ));
}
2006-02-28 02:13:16 +01:00
$inserts = array ();
// new categories are the directories not present yet in the database
foreach ( array_diff ( $fs_fulldirs , array_keys ( $db_fulldirs )) as $fulldir )
{
$dir = basename ( $fulldir );
if ( preg_match ( '/^[a-zA-Z0-9-_.]+$/' , $dir ))
{
2006-03-05 00:31:46 +01:00
$insert = array (
'id' => $next_id ++ ,
'dir' => $dir ,
'name' => str_replace ( '_' , ' ' , $dir ),
'site_id' => $site_id ,
2006-04-26 23:08:44 +02:00
'commentable' =>
boolean_to_string ( $conf [ 'newcat_default_commentable' ]),
2006-03-05 00:31:46 +01:00
'uploadable' => $site_is_remote
2006-04-29 18:35:07 +02:00
? 'false'
2006-04-26 23:08:44 +02:00
: boolean_to_string ( $conf [ 'newcat_default_uploadable' ]),
2009-11-20 15:17:04 +01:00
'status' => $conf [ 'newcat_default_status' ],
'visible' => boolean_to_string ( $conf [ 'newcat_default_visible' ]),
2006-03-05 00:31:46 +01:00
);
2006-02-28 02:13:16 +01:00
if ( isset ( $db_fulldirs [ dirname ( $fulldir )]))
{
$parent = $db_fulldirs [ dirname ( $fulldir )];
2009-11-20 15:17:04 +01:00
$insert [ 'id_uppercat' ] = $parent ;
$insert [ 'uppercats' ] =
$db_categories [ $parent ][ 'uppercats' ] . ',' . $insert [ 'id' ];
$insert [ 'rank' ] = $next_rank [ $parent ] ++ ;
$insert [ 'global_rank' ] =
$db_categories [ $parent ][ 'global_rank' ] . '.' . $insert [ 'rank' ];
2006-02-28 02:13:16 +01:00
if ( 'private' == $db_categories [ $parent ][ 'status' ])
{
2009-11-20 15:17:04 +01:00
$insert [ 'status' ] = 'private' ;
2006-02-28 02:13:16 +01:00
}
if ( 'false' == $db_categories [ $parent ][ 'visible' ])
{
2009-11-20 15:17:04 +01:00
$insert [ 'visible' ] = 'false' ;
2006-02-28 02:13:16 +01:00
}
}
else
{
2009-11-20 15:17:04 +01:00
$insert [ 'uppercats' ] = $insert [ 'id' ];
2006-02-28 02:13:16 +01:00
$insert { 'rank' } = $next_rank [ 'NULL' ] ++ ;
2009-11-20 15:17:04 +01:00
$insert [ 'global_rank' ] = $insert [ 'rank' ];
2006-02-28 02:13:16 +01:00
}
array_push ( $inserts , $insert );
2006-03-05 00:31:46 +01:00
array_push (
$infos ,
array (
'path' => $fulldir ,
2010-03-02 15:54:22 +01:00
'info' => l10n ( 'added' )
2006-03-05 00:31:46 +01:00
)
);
2006-02-28 02:13:16 +01:00
// add the new category to $db_categories and $db_fulldirs array
$db_categories [ $insert { 'id' }] =
array (
2009-11-20 15:17:04 +01:00
'id' => $insert [ 'id' ],
'status' => $insert [ 'status' ],
'visible' => $insert [ 'visible' ],
'uppercats' => $insert [ 'uppercats' ],
'global_rank' => $insert [ 'global_rank' ]
2006-02-28 02:13:16 +01:00
);
2009-11-20 15:17:04 +01:00
$db_fulldirs [ $fulldir ] = $insert [ 'id' ];
2006-02-28 02:13:16 +01:00
$next_rank [ $insert { 'id' }] = 1 ;
}
else
{
2006-03-05 00:31:46 +01:00
array_push (
$errors ,
array (
'path' => $fulldir ,
'type' => 'PWG-UPDATE-1'
)
);
2006-02-28 02:13:16 +01:00
}
}
if ( count ( $inserts ) > 0 )
{
if ( ! $simulate )
{
$dbfields = array (
'id' , 'dir' , 'name' , 'site_id' , 'id_uppercat' , 'uppercats' , 'commentable' ,
'uploadable' , 'visible' , 'status' , 'rank' , 'global_rank'
);
mass_inserts ( CATEGORIES_TABLE , $dbfields , $inserts );
}
$counts [ 'new_categories' ] = count ( $inserts );
}
// to delete categories
$to_delete = array ();
foreach ( array_diff ( array_keys ( $db_fulldirs ), $fs_fulldirs ) as $fulldir )
{
array_push ( $to_delete , $db_fulldirs [ $fulldir ]);
unset ( $db_fulldirs [ $fulldir ]);
array_push ( $infos , array ( 'path' => $fulldir ,
2010-03-02 15:54:22 +01:00
'info' => l10n ( 'deleted' )));
2006-02-28 02:13:16 +01:00
}
if ( count ( $to_delete ) > 0 )
{
if ( ! $simulate )
{
delete_categories ( $to_delete );
}
$counts [ 'del_categories' ] = count ( $to_delete );
}
2008-03-12 02:06:50 +01:00
$template -> append ( 'footer_elements' , '<!-- scanning dirs : '
2007-09-28 00:46:17 +02:00
. get_elapsed_time ( $start , get_moment ())
2008-03-12 02:06:50 +01:00
. ' -->' );
2006-02-28 02:13:16 +01:00
}
// +-----------------------------------------------------------------------+
// | files / elements |
// +-----------------------------------------------------------------------+
if ( isset ( $_POST [ 'submit' ]) and $_POST [ 'sync' ] == 'files'
and ! $general_failure )
{
$start_files = get_moment ();
$start = $start_files ;
$fs = $site_reader -> get_elements ( $basedir );
2008-03-12 02:06:50 +01:00
$template -> append ( 'footer_elements' , '<!-- get_elements: '
2007-09-28 00:46:17 +02:00
. get_elapsed_time ( $start , get_moment ())
2008-03-12 02:06:50 +01:00
. ' -->' );
2006-02-28 02:13:16 +01:00
$cat_ids = array_diff ( array_keys ( $db_categories ), $to_delete );
$db_elements = array ();
$db_unvalidated = array ();
if ( count ( $cat_ids ) > 0 )
{
$query = '
SELECT id , path
FROM '.IMAGES_TABLE.'
2006-04-05 00:29:35 +02:00
WHERE storage_category_id IN ( '
. wordwrap (
2006-03-05 00:31:46 +01:00
implode ( ', ' , $cat_ids ),
80 ,
" \n "
2008-08-29 14:35:16 +02:00
) . ')' ;
$db_elements = simple_hash_from_query ( $query , 'id' , 'path' );
2006-02-28 02:13:16 +01:00
// searching the unvalidated waiting elements (they must not be taken into
// account)
$query = '
SELECT file , storage_category_id
FROM '.WAITING_TABLE.'
WHERE storage_category_id IN (
'.wordwrap(implode(' , ', $cat_ids), 80, "\n").' )
2008-08-29 14:35:16 +02:00
AND validated = \ 'false\'' ;
2006-02-28 02:13:16 +01:00
$result = pwg_query ( $query );
2009-11-20 15:17:04 +01:00
while ( $row = pwg_db_fetch_assoc ( $result ))
2006-02-28 02:13:16 +01:00
{
array_push (
$db_unvalidated ,
2006-03-05 00:31:46 +01:00
array_search (
$row [ 'storage_category_id' ],
$db_fulldirs )
. '/' . $row [ 'file' ]
2006-02-28 02:13:16 +01:00
);
}
}
// next element id available
2009-11-25 20:02:57 +01:00
$next_element_id = pwg_db_nextval ( 'id' , IMAGES_TABLE );
2006-02-28 02:13:16 +01:00
$start = get_moment ();
$inserts = array ();
$insert_links = array ();
foreach ( array_diff ( array_keys ( $fs ), $db_elements , $db_unvalidated ) as $path )
{
$insert = array ();
// storage category must exist
$dirname = dirname ( $path );
if ( ! isset ( $db_fulldirs [ $dirname ]))
{
continue ;
}
$filename = basename ( $path );
if ( ! preg_match ( '/^[a-zA-Z0-9-_.]+$/' , $filename ))
{
2006-03-05 00:31:46 +01:00
array_push (
$errors ,
array (
'path' => $path ,
'type' => 'PWG-UPDATE-1'
)
);
2006-03-28 03:26:37 +02:00
2006-02-28 02:13:16 +01:00
continue ;
}
2008-04-21 00:20:20 +02:00
if ( isset ( $conf [ 'flip_picture_ext' ][ get_extension ( $filename )] )
2008-03-26 01:55:19 +01:00
and ! isset ( $fs [ $path ][ 'tn_ext' ]) )
2008-04-21 00:20:20 +02:00
{ // For a picture thumbnail is mandatory and for non picture element,
2008-03-26 01:55:19 +01:00
// thumbnail and representative are optionnal
array_push (
$errors ,
array (
'path' => $path ,
'type' => 'PWG-UPDATE-2'
)
);
2006-02-28 02:13:16 +01:00
}
else
{
2006-03-05 00:31:46 +01:00
$insert = array (
'id' => $next_element_id ++ ,
'file' => $filename ,
'date_available' => CURRENT_DATE ,
'path' => $path ,
'tn_ext' => isset ( $fs [ $path ][ 'tn_ext' ])
? $fs [ $path ][ 'tn_ext' ]
: null ,
2006-04-05 00:29:35 +02:00
'storage_category_id' => $db_fulldirs [ $dirname ],
2006-03-05 00:31:46 +01:00
);
2008-04-21 00:20:20 +02:00
2008-03-26 01:55:19 +01:00
if ( $_POST [ 'privacy_level' ] != 0 )
{
$insert [ 'level' ] = $_POST [ 'privacy_level' ];
}
2006-02-28 02:13:16 +01:00
2006-03-05 00:31:46 +01:00
array_push (
$inserts ,
$insert
);
2006-02-28 02:13:16 +01:00
2006-03-05 00:31:46 +01:00
array_push (
$insert_links ,
array (
2009-11-20 15:17:04 +01:00
'image_id' => $insert [ 'id' ],
2006-04-05 00:29:35 +02:00
'category_id' => $insert [ 'storage_category_id' ],
2006-03-05 00:31:46 +01:00
)
);
array_push (
$infos ,
array (
2009-11-20 15:17:04 +01:00
'path' => $insert [ 'path' ],
2010-03-02 15:54:22 +01:00
'info' => l10n ( 'added' )
2006-03-05 00:31:46 +01:00
)
);
2007-10-01 17:56:42 +02:00
$caddiables [] = $insert [ 'id' ];
2006-02-28 02:13:16 +01:00
}
}
if ( count ( $inserts ) > 0 )
{
if ( ! $simulate )
{
// inserts all new elements
2006-03-05 00:31:46 +01:00
mass_inserts (
IMAGES_TABLE ,
2006-04-05 01:07:40 +02:00
array_keys ( $inserts [ 0 ]),
2006-03-05 00:31:46 +01:00
$inserts
2006-02-28 02:13:16 +01:00
);
2006-03-05 00:31:46 +01:00
// inserts all links between new elements and their storage category
mass_inserts (
IMAGE_CATEGORY_TABLE ,
2006-04-05 01:07:40 +02:00
array_keys ( $insert_links [ 0 ]),
2006-03-05 00:31:46 +01:00
$insert_links
);
2007-10-01 17:56:42 +02:00
// add new elements to caddie
if ( isset ( $_POST [ 'add_to_caddie' ]) and $_POST [ 'add_to_caddie' ] == 1 )
{
fill_caddie ( $caddiables );
}
2006-02-28 02:13:16 +01:00
}
$counts [ 'new_elements' ] = count ( $inserts );
}
// delete elements that are in database but not in the filesystem
$to_delete_elements = array ();
foreach ( array_diff ( $db_elements , array_keys ( $fs )) as $path )
{
array_push ( $to_delete_elements , array_search ( $path , $db_elements ));
array_push ( $infos , array ( 'path' => $path ,
2010-03-02 15:54:22 +01:00
'info' => l10n ( 'deleted' )));
2006-02-28 02:13:16 +01:00
}
if ( count ( $to_delete_elements ) > 0 )
{
if ( ! $simulate )
{
delete_elements ( $to_delete_elements );
}
$counts [ 'del_elements' ] = count ( $to_delete_elements );
}
2008-03-12 02:06:50 +01:00
$template -> append ( 'footer_elements' , '<!-- scanning files : '
2007-09-28 00:46:17 +02:00
. get_elapsed_time ( $start_files , get_moment ())
2008-03-12 02:06:50 +01:00
. ' -->' );
2006-02-28 02:13:16 +01:00
// retrieving informations given by uploaders
if ( ! $simulate and count ( $cat_ids ) > 0 )
{
$query = '
SELECT id , file , storage_category_id , infos
FROM '.WAITING_TABLE.'
WHERE storage_category_id IN (
'.wordwrap(implode(' , ', $cat_ids), 80, "\n").' )
2008-08-29 14:35:16 +02:00
AND validated = \ 'true\'' ;
2006-02-28 02:13:16 +01:00
$result = pwg_query ( $query );
$datas = array ();
$fields =
array (
'primary' => array ( 'id' ),
'update' => array ( 'date_creation' , 'author' , 'name' , 'comment' )
);
$waiting_to_delete = array ();
2009-11-20 15:17:04 +01:00
while ( $row = pwg_db_fetch_assoc ( $result ))
2006-02-28 02:13:16 +01:00
{
$data = array ();
$query = '
SELECT id
FROM '.IMAGES_TABLE.'
2006-04-05 00:29:35 +02:00
WHERE storage_category_id = '.$row[' storage_category_id '].'
2008-08-29 14:35:16 +02:00
AND file = \ '' . $row [ 'file' ] . '\'' ;
2009-11-20 15:17:04 +01:00
list ( $data [ 'id' ]) = pwg_db_fetch_row ( pwg_query ( $query ));
2006-02-28 02:13:16 +01:00
foreach ( $fields [ 'update' ] as $field )
{
$data [ $field ] = addslashes ( getAttribute ( $row [ 'infos' ], $field ) );
}
array_push ( $datas , $data );
array_push ( $waiting_to_delete , $row [ 'id' ]);
}
if ( count ( $datas ) > 0 )
{
mass_updates ( IMAGES_TABLE , $fields , $datas );
// delete now useless waiting elements
$query = '
DELETE
FROM '.WAITING_TABLE.'
WHERE id IN ( '.implode(' , ', $waiting_to_delete).' )
; ' ;
pwg_query ( $query );
}
}
}
// +-----------------------------------------------------------------------+
// | synchronize files |
// +-----------------------------------------------------------------------+
if ( isset ( $_POST [ 'submit' ])
2006-04-19 01:46:53 +02:00
and ( $_POST [ 'sync' ] == 'dirs' or $_POST [ 'sync' ] == 'files' )
and ! $general_failure )
2006-02-28 02:13:16 +01:00
{
if ( ! $simulate )
{
$start = get_moment ();
update_category ( 'all' );
2008-03-12 02:06:50 +01:00
$template -> append ( 'footer_elements' , '<!-- update_category(all) : '
2007-09-28 00:46:17 +02:00
. get_elapsed_time ( $start , get_moment ())
2008-03-12 02:06:50 +01:00
. ' -->' );
2006-02-28 02:13:16 +01:00
$start = get_moment ();
update_global_rank ();
2008-03-12 02:06:50 +01:00
$template -> append ( 'footer_elements' , '<!-- ordering categories : '
2007-09-28 00:46:17 +02:00
. get_elapsed_time ( $start , get_moment ())
2008-03-12 02:06:50 +01:00
. ' -->' );
2006-02-28 02:13:16 +01:00
}
2006-04-19 01:46:53 +02:00
if ( $_POST [ 'sync' ] == 'files' )
{
$start = get_moment ();
$opts [ 'category_id' ] = '' ;
$opts [ 'recursive' ] = true ;
if ( isset ( $_POST [ 'cat' ]))
{
$opts [ 'category_id' ] = $_POST [ 'cat' ];
if ( ! isset ( $_POST [ 'subcats-included' ]) or $_POST [ 'subcats-included' ] != 1 )
{
$opts [ 'recursive' ] = false ;
}
}
$files = get_filelist ( $opts [ 'category_id' ], $site_id ,
$opts [ 'recursive' ],
false );
2008-03-12 02:06:50 +01:00
$template -> append ( 'footer_elements' , '<!-- get_filelist : '
2007-09-28 00:46:17 +02:00
. get_elapsed_time ( $start , get_moment ())
2008-03-12 02:06:50 +01:00
. ' -->' );
2006-04-19 01:46:53 +02:00
$start = get_moment ();
$datas = array ();
foreach ( $files as $id => $file )
{
$data = $site_reader -> get_element_update_attributes ( $file );
if ( ! is_array ( $data ) )
{
continue ;
}
$extension = get_extension ( $file );
if ( isset ( $conf [ 'flip_picture_ext' ][ $extension ]) )
{
if ( ! isset ( $data [ 'tn_ext' ]) )
{
array_push (
$errors ,
array (
'path' => $file ,
'type' => 'PWG-UPDATE-2'
)
);
continue ;
}
}
$data [ 'id' ] = $id ;
array_push ( $datas , $data );
} // end foreach file
$counts [ 'upd_elements' ] = count ( $datas );
if ( ! $simulate and count ( $datas ) > 0 )
{
mass_updates (
IMAGES_TABLE ,
// fields
array (
'primary' => array ( 'id' ),
'update' => $site_reader -> get_update_attributes (),
),
$datas
);
}
2008-03-12 02:06:50 +01:00
$template -> append ( 'footer_elements' , '<!-- update files : '
2007-09-28 00:46:17 +02:00
. get_elapsed_time ( $start , get_moment ())
2008-03-12 02:06:50 +01:00
. ' -->' );
2006-04-19 01:46:53 +02:00
} // end if sync files
}
// +-----------------------------------------------------------------------+
// | synchronize files |
// +-----------------------------------------------------------------------+
if ( isset ( $_POST [ 'submit' ])
and ( $_POST [ 'sync' ] == 'dirs' or $_POST [ 'sync' ] == 'files' ))
{
2008-03-12 02:06:50 +01:00
$template -> assign (
2006-04-19 01:46:53 +02:00
'update_result' ,
array (
'NB_NEW_CATEGORIES' => $counts [ 'new_categories' ],
'NB_DEL_CATEGORIES' => $counts [ 'del_categories' ],
'NB_NEW_ELEMENTS' => $counts [ 'new_elements' ],
'NB_DEL_ELEMENTS' => $counts [ 'del_elements' ],
'NB_UPD_ELEMENTS' => $counts [ 'upd_elements' ],
'NB_ERRORS' => count ( $errors ),
));
2006-02-28 02:13:16 +01:00
}
// +-----------------------------------------------------------------------+
// | synchronize metadata |
// +-----------------------------------------------------------------------+
2008-08-29 14:35:16 +02:00
if ( isset ( $_POST [ 'submit' ]) and isset ( $_POST [ 'sync_meta' ])
2006-02-28 02:13:16 +01:00
and ! $general_failure )
{
// sync only never synchronized files ?
2008-08-29 14:35:16 +02:00
$opts [ 'only_new' ] = isset ( $_POST [ 'meta_all' ]) ? false : true ;
2006-02-28 02:13:16 +01:00
$opts [ 'category_id' ] = '' ;
$opts [ 'recursive' ] = true ;
if ( isset ( $_POST [ 'cat' ]))
{
$opts [ 'category_id' ] = $_POST [ 'cat' ];
// recursive ?
if ( ! isset ( $_POST [ 'subcats-included' ]) or $_POST [ 'subcats-included' ] != 1 )
{
$opts [ 'recursive' ] = false ;
}
}
$start = get_moment ();
$files = get_filelist ( $opts [ 'category_id' ], $site_id ,
$opts [ 'recursive' ],
$opts [ 'only_new' ]);
2008-03-12 02:06:50 +01:00
$template -> append ( 'footer_elements' , '<!-- get_filelist : '
2007-09-28 00:46:17 +02:00
. get_elapsed_time ( $start , get_moment ())
2008-03-12 02:06:50 +01:00
. ' -->' );
2006-02-28 02:13:16 +01:00
$start = get_moment ();
$datas = array ();
2006-04-03 00:26:19 +02:00
$tags_of = array ();
2007-03-09 17:28:49 +01:00
$has_high_images = array ();
$image_ids = array ();
foreach ( $files as $id => $file )
{
array_push ( $image_ids , $id );
}
if ( count ( $image_ids ) > 0 )
{
$query = '
SELECT id
FROM '.IMAGES_TABLE.'
WHERE has_high = \ ' true \ '
AND id IN (
'.wordwrap(implode(' , ', $image_ids), 80, "\n").'
2008-08-29 14:35:16 +02:00
) ' ;
$has_high_images = array_from_query ( $query , 'id' );
2007-03-09 17:28:49 +01:00
}
2007-09-28 00:46:17 +02:00
2006-02-28 02:13:16 +01:00
foreach ( $files as $id => $file )
{
2007-03-09 17:28:49 +01:00
$data = $site_reader -> get_element_metadata (
$file ,
in_array ( $id , $has_high_images )
);
2007-09-28 00:46:17 +02:00
2006-02-28 02:13:16 +01:00
if ( is_array ( $data ) )
{
$data [ 'date_metadata_update' ] = CURRENT_DATE ;
$data [ 'id' ] = $id ;
array_push ( $datas , $data );
2006-04-03 00:26:19 +02:00
foreach ( array ( 'keywords' , 'tags' ) as $key )
{
if ( isset ( $data [ $key ]))
{
if ( ! isset ( $tags_of [ $id ]))
{
$tags_of [ $id ] = array ();
}
2006-04-19 01:46:53 +02:00
2006-04-03 00:26:19 +02:00
foreach ( explode ( ',' , $data [ $key ]) as $tag_name )
{
array_push (
$tags_of [ $id ],
tag_id_from_tag_name ( $tag_name )
);
}
}
}
2006-02-28 02:13:16 +01:00
}
else
{
array_push ( $errors , array ( 'path' => $file , 'type' => 'PWG-ERROR-NO-FS' ));
}
}
2006-04-03 00:26:19 +02:00
if ( ! $simulate )
2006-02-28 02:13:16 +01:00
{
2006-04-03 00:26:19 +02:00
if ( count ( $datas ) > 0 )
{
mass_updates (
IMAGES_TABLE ,
// fields
array (
'primary' => array ( 'id' ),
'update' => array_unique (
array_merge (
array_diff (
2006-04-19 01:46:53 +02:00
$site_reader -> get_metadata_attributes (),
2006-04-03 00:26:19 +02:00
// keywords and tags fields are managed separately
array ( 'keywords' , 'tags' )
),
array ( 'date_metadata_update' ))
)
),
2008-08-29 14:35:16 +02:00
$datas ,
isset ( $_POST [ 'meta_empty_overrides' ]) ? 0 : MASS_UPDATES_SKIP_EMPTY
2006-04-03 00:26:19 +02:00
);
}
set_tags_of ( $tags_of );
2006-02-28 02:13:16 +01:00
}
2008-03-12 02:06:50 +01:00
$template -> append ( 'footer_elements' , '<!-- metadata update : '
2007-09-28 00:46:17 +02:00
. get_elapsed_time ( $start , get_moment ())
2008-03-12 02:06:50 +01:00
. ' -->' );
2006-02-28 02:13:16 +01:00
2008-03-12 02:06:50 +01:00
$template -> assign (
2006-02-28 02:13:16 +01:00
'metadata_result' ,
array (
'NB_ELEMENTS_DONE' => count ( $datas ),
'NB_ELEMENTS_CANDIDATES' => count ( $files ),
'NB_ERRORS' => count ( $errors ),
));
}
// +-----------------------------------------------------------------------+
// | template initialization |
// +-----------------------------------------------------------------------+
2008-09-14 09:16:15 +02:00
$template -> set_filenames ( array ( 'update' => 'site_update.tpl' ));
2006-02-28 02:13:16 +01:00
$result_title = '' ;
if ( isset ( $simulate ) and $simulate )
{
2010-03-02 15:54:22 +01:00
$result_title .= l10n ( '[Simulation]' ) . ' ' ;
2006-02-28 02:13:16 +01:00
}
// used_metadata string is displayed to inform admin which metadata will be
// used from files for synchronization
2006-04-19 01:46:53 +02:00
$used_metadata = implode ( ', ' , $site_reader -> get_metadata_attributes ());
2006-02-28 02:13:16 +01:00
if ( $site_is_remote and ! isset ( $_POST [ 'submit' ]) )
{
$used_metadata .= ' + ...' ;
}
2008-03-12 02:06:50 +01:00
$template -> assign (
2006-02-28 02:13:16 +01:00
array (
'SITE_URL' => $site_url ,
2008-03-12 02:06:50 +01:00
'U_SITE_MANAGER' => get_root_url () . 'admin.php?page=site_manager' ,
2010-03-02 15:54:22 +01:00
'L_RESULT_UPDATE' => $result_title . l10n ( 'Search for new images in the directories' ),
'L_RESULT_METADATA' => $result_title . l10n ( 'Metadata synchronization results' ),
2008-03-12 02:06:50 +01:00
'METADATA_LIST' => $used_metadata ,
'U_HELP' => get_root_url () . 'popuphelp.php?page=synchronize' ,
2006-02-28 02:13:16 +01:00
));
// +-----------------------------------------------------------------------+
// | introduction : choices |
// +-----------------------------------------------------------------------+
2008-08-29 14:35:16 +02:00
if ( isset ( $_POST [ 'submit' ]))
2006-02-28 02:13:16 +01:00
{
2008-08-29 14:35:16 +02:00
$tpl_introduction = array (
'sync' => $_POST [ 'sync' ],
'sync_meta' => isset ( $_POST [ 'sync_meta' ]) ? true : false ,
'display_info' => isset ( $_POST [ 'display_info' ]) and $_POST [ 'display_info' ] == 1 ,
'add_to_caddie' => isset ( $_POST [ 'add_to_caddie' ]) and $_POST [ 'add_to_caddie' ] == 1 ,
'subcats_included' => isset ( $_POST [ 'subcats-included' ]) and $_POST [ 'subcats-included' ] == 1 ,
'privacy_level_selected' => ( int ) @ $_POST [ 'privacy_level' ],
'meta_all' => isset ( $_POST [ 'meta_all' ]) ? true : false ,
'meta_empty_overrides' => isset ( $_POST [ 'meta_empty_overrides' ]) ? true : false ,
);
2006-02-28 02:13:16 +01:00
2008-08-29 14:35:16 +02:00
if ( isset ( $_POST [ 'cat' ]) and is_numeric ( $_POST [ 'cat' ]))
{
$cat_selected = array ( $_POST [ 'cat' ]);
2006-02-28 02:13:16 +01:00
}
else
{
$cat_selected = array ();
}
2008-08-29 14:35:16 +02:00
}
else
{
$tpl_introduction = array (
'sync' => 'dirs' ,
'sync_meta' => true ,
'display_info' => false ,
'add_to_caddie' => false ,
'subcats_included' => true ,
'privacy_level_selected' => 0 ,
'meta_all' => false ,
'meta_empty_overrides' => false ,
);
$cat_selected = array ();
}
2006-02-28 02:13:16 +01:00
2008-08-29 14:35:16 +02:00
$tpl_introduction [ 'privacy_level_options' ] = array ();
foreach ( $conf [ 'available_permission_levels' ] as $level )
{
$tpl_introduction [ 'privacy_level_options' ][ $level ] = l10n ( sprintf ( 'Level %d' , $level ) );
}
2008-03-26 01:55:19 +01:00
2008-08-29 14:35:16 +02:00
$template -> assign ( 'introduction' , $tpl_introduction );
2008-03-12 02:06:50 +01:00
2008-08-29 14:35:16 +02:00
$query = '
2006-02-28 02:13:16 +01:00
SELECT id , name , uppercats , global_rank
FROM '.CATEGORIES_TABLE.'
2008-08-29 14:35:16 +02:00
WHERE site_id = ' . $site_id ;
display_select_cat_wrapper ( $query ,
$cat_selected ,
'category_options' ,
false );
2006-02-28 02:13:16 +01:00
if ( count ( $errors ) > 0 )
{
foreach ( $errors as $error )
{
2008-03-12 02:06:50 +01:00
$template -> append (
'sync_errors' ,
2006-02-28 02:13:16 +01:00
array (
'ELEMENT' => $error [ 'path' ],
'LABEL' => $error [ 'type' ] . ' (' . $error_labels [ $error [ 'type' ]][ 0 ] . ')'
));
}
foreach ( $error_labels as $error_type => $error_description )
{
2008-03-12 02:06:50 +01:00
$template -> append (
'sync_error_captions' ,
2006-02-28 02:13:16 +01:00
array (
'TYPE' => $error_type ,
'LABEL' => $error_description [ 1 ]
));
}
}
2008-03-12 02:06:50 +01:00
2006-02-28 02:13:16 +01:00
if ( count ( $infos ) > 0
and isset ( $_POST [ 'display_info' ])
and $_POST [ 'display_info' ] == 1 )
{
foreach ( $infos as $info )
{
2008-03-12 02:06:50 +01:00
$template -> append (
'sync_infos' ,
2006-02-28 02:13:16 +01:00
array (
'ELEMENT' => $info [ 'path' ],
'LABEL' => $info [ 'info' ]
));
}
}
// +-----------------------------------------------------------------------+
// | sending html code |
// +-----------------------------------------------------------------------+
$template -> assign_var_from_handle ( 'ADMIN_CONTENT' , 'update' );
2007-03-13 23:44:45 +01:00
?>