From 0e78db47de2041912447dd5bbbfafb7684e7480f Mon Sep 17 00:00:00 2001 From: rvelices Date: Wed, 8 Feb 2006 01:17:07 +0000 Subject: - remake of Remote sites and Synchronize: - synchronization for remote and local sites are done by the same code - remote sites can update metadata now (not before) - bug 279 - fixes bug 82: has_high column - improve feature 280: user sort by filename - fix path to template mimetypes icons - bug 284: session cookie lifetime, deletion on logout and corrected issue when db upgrades were missing git-svn-id: http://piwigo.org/svn/trunk@1029 68402e56-0260-453c-a942-63ccdbb3a9ee --- admin.php | 1 + admin/include/functions_metadata.php | 6 +- admin/site_manager.php | 260 +++++++++ admin/site_reader_local.php | 254 +++++++++ admin/site_reader_remote.php | 191 +++++++ admin/site_update.php | 813 ++++++++++++++++++++++++++++ admin/update.php | 2 +- category.php | 2 +- identification.php | 1 + include/common.inc.php | 1 + include/functions.inc.php | 14 + include/functions_category.inc.php | 11 +- include/functions_session.inc.php | 20 +- language/en_UK.iso-8859-1/common.lang.php | 1 + language/fr_FR.iso-8859-1/common.lang.php | 1 + picture.php | 18 +- template/yoga/admin.tpl | 3 +- template/yoga/admin/site_manager.tpl | 49 ++ template/yoga/admin/site_update.tpl | 108 ++++ template/yoga/theme/clear/themeconf.inc.php | 2 +- template/yoga/theme/dark/themeconf.inc.php | 2 +- tools/create_listing_file.php | 46 +- 22 files changed, 1770 insertions(+), 36 deletions(-) create mode 100644 admin/site_manager.php create mode 100644 admin/site_reader_local.php create mode 100644 admin/site_reader_remote.php create mode 100644 admin/site_update.php create mode 100644 template/yoga/admin/site_manager.tpl create mode 100644 template/yoga/admin/site_update.tpl diff --git a/admin.php b/admin.php index 5036e369e..7b2ae8c70 100644 --- a/admin.php +++ b/admin.php @@ -65,6 +65,7 @@ $template->set_filenames(array('admin' => 'admin.tpl')); $template->assign_vars( array( + 'U_SITE_MANAGER'=> $link_start.'site_manager', 'U_HISTORY'=> $link_start.'stats', 'U_FAQ'=> $link_start.'help', 'U_SITES'=> $link_start.'remote_site', diff --git a/admin/include/functions_metadata.php b/admin/include/functions_metadata.php index 3979e0e40..23dca79a9 100644 --- a/admin/include/functions_metadata.php +++ b/admin/include/functions_metadata.php @@ -175,11 +175,13 @@ function update_metadata($files) * path in the filesystem * * @param int id_uppercat + * @param int site_id * @param boolean recursive ? * @param boolean only newly added files ? * @return array */ -function get_filelist($category_id = '', $recursive = false, $only_new = false) +function get_filelist($category_id = '', $site_id=1, $recursive = false, + $only_new = false) { // filling $cat_ids : all categories required $cat_ids = array(); @@ -187,7 +189,7 @@ function get_filelist($category_id = '', $recursive = false, $only_new = false) $query = ' SELECT id FROM '.CATEGORIES_TABLE.' - WHERE site_id = 1 + WHERE site_id = '.$site_id.' AND dir IS NOT NULL'; if (is_numeric($category_id)) { diff --git a/admin/site_manager.php b/admin/site_manager.php new file mode 100644 index 000000000..478041dbf --- /dev/null +++ b/admin/site_manager.php @@ -0,0 +1,260 @@ +assign_block_vars('remote_output', array()); + // cleaning lines from HTML tags + foreach ($lines as $line) + { + $line = trim(strip_tags($line)); + if (preg_match('/^PWG-([A-Z]+)-/', $line, $matches)) + { + $template->assign_block_vars( + 'remote_output.remote_line', + array( + 'CLASS' => 'remote'.ucfirst(strtolower($matches[1])), + 'CONTENT' => $line + ) + ); + } + } + } + else + { + array_push($page['errors'], get_lang('remote_site_file_not_found')); + } +} + + +// +-----------------------------------------------------------------------+ +// | template init | +// +-----------------------------------------------------------------------+ +$template->set_filenames(array('site_manager'=>'admin/site_manager.tpl')); + +// +-----------------------------------------------------------------------+ +// | new site creation form | +// +-----------------------------------------------------------------------+ +if (isset($_POST['submit'])) +{ + $is_remote = url_is_remote( $_POST['galleries_url'] ); + $url = preg_replace('/[\/]*$/', '', $_POST['galleries_url']); + $url.= '/'; + if (! $is_remote) + { + if ( ! (strpos($url, '.') === 0 ) ) + { + $url = './' . $url; + } + } + + // site must not exists + $query = ' +SELECT COUNT(id) AS count + FROM '.SITES_TABLE.' + WHERE galleries_url = \''.$url.'\' +;'; + $row = mysql_fetch_array(pwg_query($query)); + if ($row['count'] > 0) + { + array_push($page['errors'], + get_lang('remote_site_already_exists').' ['.$url.']'); + } + if (count($page['errors']) == 0) + { + if ($is_remote) + { + $clf_url = $url.'create_listing_file.php'; + $clf_url.= '?action=test'; + $clf_url.= '&version='.PHPWG_VERSION; + if ($lines = @file($clf_url)) + { + $first_line = strip_tags($lines[0]); + if (!preg_match('/^PWG-INFO-2:/', $first_line)) + { + array_push($page['errors'], + get_lang('remote_site_error').' : '.$first_line); + } + } + else + { + array_push($page['errors'], get_lang('remote_site_file_not_found') ); + } + } + else + { // local directory + if ( ! file_exists($url) ) + { + array_push($page['errors'], + get_lang('Directory does not exist').' ['.$url.']'); + } + } + } + + if (count($page['errors']) == 0) + { + $query = ' +INSERT INTO '.SITES_TABLE.' + (galleries_url) + VALUES + (\''.$url.'\') +;'; + pwg_query($query); + array_push($page['infos'], + $url.' '.get_lang('remote_site_created')); + } +} + +// +-----------------------------------------------------------------------+ +// | actions on site | +// +-----------------------------------------------------------------------+ +if (isset($_GET['site']) and is_numeric($_GET['site'])) +{ + $page['site'] = $_GET['site']; +} +if (isset($_GET['action']) and isset($page['site']) ) +{ + $query = ' +SELECT galleries_url + FROM '.SITES_TABLE.' + WHERE id = '.$page['site'].' +;'; + list($galleries_url) = mysql_fetch_array(pwg_query($query)); + switch($_GET['action']) + { + case 'generate' : + { + $title = $galleries_url.' : '.get_lang('remote_site_generate'); + $template->assign_vars(array('REMOTE_SITE_TITLE'=>$title)); + remote_output($galleries_url.'create_listing_file.php?action=generate'); + break; + } + case 'test' : + { + $title = $galleries_url.' : '.get_lang('remote_site_test'); + $template->assign_vars(array('REMOTE_SITE_TITLE'=>$title)); + remote_output($galleries_url.'create_listing_file.php?action=test&version='.PHPWG_VERSION); + break; + } + case 'clean' : + { + $title = $galleries_url.' : '.get_lang('remote_site_clean'); + $template->assign_vars(array('REMOTE_SITE_TITLE'=>$title)); + remote_output($galleries_url.'create_listing_file.php?action=clean'); + break; + } + case 'delete' : + { + delete_site($page['site']); + array_push($page['infos'], + $galleries_url.' '.get_lang('remote_site_deleted')); + break; + } + } +} + +$template->assign_vars( array( + 'F_ACTION' => PHPWG_ROOT_PATH.'admin.php' + .get_query_string_diff( array('action','site') ) + ) ); + +// +-----------------------------------------------------------------------+ +// | remote sites list | +// +-----------------------------------------------------------------------+ + +$query = ' +SELECT s.*, COUNT(c.id) AS nb_categories, SUM(c.nb_images) AS nb_images +FROM '.SITES_TABLE.' AS s LEFT JOIN '.CATEGORIES_TABLE.' AS c +ON s.id=c.site_id +GROUP BY s.id'. +';'; +$result = pwg_query($query); + +if (mysql_num_rows($result) > 0) +{ + $template->assign_block_vars('sites', array()); +} +while ($row = mysql_fetch_array($result)) +{ + $is_remote = url_is_remote($row['galleries_url']); + $base_url = PHPWG_ROOT_PATH.'admin.php'; + $base_url.= '?page=site_manager'; + $base_url.= '&site='.$row['id']; + $base_url.= '&action='; + + $update_url = PHPWG_ROOT_PATH.'admin.php'; + $update_url.= '?page=site_update'; + $update_url.= '&site='.$row['id']; + + $template->assign_block_vars( + 'sites.site', + array( + 'NAME' => $row['galleries_url'], + 'TYPE' => get_lang( $is_remote ? 'Remote' : 'Local' ), + 'CATEGORIES' => $row['nb_categories'], + 'IMAGES' => isset($row['nb_images']) ? $row['nb_images'] : 0, + 'U_UPDATE' => $update_url + ) + ); + + if ($is_remote) + { + $template->assign_block_vars('sites.site.remote', + array( + 'U_TEST' => $base_url.'test', + 'U_GENERATE' => $base_url.'generate', + 'U_CLEAN' => $base_url.'clean' + ) + ); + } + + if ($row['id'] != 1) + { + $template->assign_block_vars( 'sites.site.delete', + array('U_DELETE' => $base_url.'delete') ); + } +} + +$template->assign_var_from_handle('ADMIN_CONTENT', 'site_manager'); +?> \ No newline at end of file diff --git a/admin/site_reader_local.php b/admin/site_reader_local.php new file mode 100644 index 000000000..758906777 --- /dev/null +++ b/admin/site_reader_local.php @@ -0,0 +1,254 @@ +site_url = $url; +} + +/** + * Is this local site ok ? + * + * @return true on success, false otherwise + */ +function open() +{ + global $errors; + if (!is_dir($this->site_url)) + { + array_push($errors, array('path' => $this->site_url, 'type' => 'PWG-ERROR-NODIR')); + return false; + } + return true; +} + +// retrieve file system sub-directories fulldirs +function get_full_directories($basedir) +{ + $fs_fulldirs = get_fs_directories($basedir); + return $fs_fulldirs; +} + +/** + * Returns an array with all file system files according to $conf['file_ext'] + * and $conf['picture_ext'] + * @param string $path recurse in this directory + * @return array like "pic.jpg"=>array('tn_ext'=>'jpg' ... ) + */ +function get_elements($path) +{ + global $conf; + if (!isset($conf['flip_picture_ext'])) + { + $conf['flip_picture_ext'] = array_flip($conf['picture_ext']); + } + if (!isset($conf['flip_file_ext'])) + { + $conf['flip_file_ext'] = array_flip($conf['file_ext']); + } + + $subdirs = array(); + $fs = array(); + if (is_dir($path) && $contents = opendir($path) ) + { + while (($node = readdir($contents)) !== false) + { + if (is_file($path.'/'.$node)) + { + $extension = get_extension($node); + $filename_wo_ext = get_filename_wo_extension($node); + + // searching the thumbnail + $tn_ext = $this->get_tn_ext($path, $filename_wo_ext); + + if (isset($conf['flip_picture_ext'][$extension])) + { + $fs[ $path.'/'.$node ] = array( + 'tn_ext' => $tn_ext, + 'has_high' => $this->get_has_high($path, $node) + ); + } + else if (isset($conf['flip_file_ext'][$extension])) + { // file not a picture + $representative_ext = $this->get_representative_ext($path, $filename_wo_ext); + + $fs[ $path.'/'.$node ] = array( + 'tn_ext' => $tn_ext, + 'representative_ext' => $representative_ext + ); + } + + } + elseif (is_dir($path.'/'.$node) + and $node != '.' + and $node != '..' + and $node != 'pwg_high' + and $node != 'pwg_representative' + and $node != 'thumbnail' ) + { + array_push($subdirs, $node); + } + } //end while readdir + closedir($contents); + + foreach ($subdirs as $subdir) + { + $tmp_fs = $this->get_elements($path.'/'.$subdir); + $fs = array_merge($fs, $tmp_fs); + } + } //end if is_dir + return $fs; +} + +// returns the name of the attributes that are supported for +// update/synchronization according to configuration +function get_update_attributes() +{ + global $conf; + $update_fields = array( 'has_high', 'representative_ext', + 'filesize', 'width', 'height' ); + if ($conf['use_exif']) + { + $update_fields = + array_merge( + $update_fields, + array_keys($conf['use_exif_mapping']) + ); + } + + if ($conf['use_iptc']) + { + $update_fields = + array_merge( + $update_fields, + array_keys($conf['use_iptc_mapping']) + ); + } + return $update_fields; +} + +// returns a hash of attributes (metadata+filesize+width,...) for file +function get_element_update_attributes($file) +{ + global $conf; + if (!is_file($file)) + { + return null; + } + + $data = array(); + + $filename = basename($file); + $data['has_high'] = $this->get_has_high( dirname($file), $filename ); + $data['representative_ext'] = $this->get_representative_ext( dirname($file), + get_filename_wo_extension($filename) ); + + $data['filesize'] = floor(filesize($file)/1024); + if ($image_size = @getimagesize($file)) + { + $data['width'] = $image_size[0]; + $data['height'] = $image_size[1]; + } + + if ($conf['use_exif']) + { + $exif = get_sync_exif_data($file); + + if (count($exif) > 0) + { + foreach (array_keys($exif) as $key) + { + $data[$key] = addslashes($exif[$key]); + } + } + } + + if ($conf['use_iptc']) + { + $iptc = get_sync_iptc_data($file); + if (count($iptc) > 0) + { + foreach (array_keys($iptc) as $key) + { + $data[$key] = addslashes($iptc[$key]); + } + } + } + return $data; +} + + +//-------------------------------------------------- private functions -------- +function get_representative_ext($path, $filename_wo_ext) +{ + global $conf; + $base_test = $path.'/pwg_representative/'; + $base_test.= $filename_wo_ext.'.'; + foreach ($conf['picture_ext'] as $ext) + { + $test = $base_test.$ext; + if (is_file($test)) + { + return $ext; + } + } + return null; +} + +function get_tn_ext($path, $filename_wo_ext) +{ + global $conf; + $base_test = $path.'/thumbnail/'; + $base_test.= $conf['prefix_thumbnail'].$filename_wo_ext.'.'; + foreach ($conf['picture_ext'] as $ext) + { + $test = $base_test.$ext; + if (is_file($test)) + { + return $ext; + } + } + return null; +} + +function get_has_high($path, $filename) +{ + if (is_file($path.'/pwg_high/'.$filename)) + { + return 'true'; + } + return null; +} + +} +?> \ No newline at end of file diff --git a/admin/site_reader_remote.php b/admin/site_reader_remote.php new file mode 100644 index 000000000..c76a37d04 --- /dev/null +++ b/admin/site_reader_remote.php @@ -0,0 +1,191 @@ +site_url = $url; + $this->insert_attributes = array('tn_ext', 'representative_ext', 'has_high'); + $this->update_attributes = array( 'representative_ext', 'has_high', 'filesize', 'width', 'height' ); +} + +/** + * Is this remote site ok ? + * + * @return true on success, false otherwise + */ +function open() +{ + global $errors; + $listing_file = $this->site_url.'/listing.xml'; + if (@fopen($listing_file, 'r')) + { + $this->site_dirs = array(); + $this->site_files = array(); + $xml_content = getXmlCode($listing_file); + $info_xml_element = getChild($xml_content, 'informations'); + if ( getAttribute($info_xml_element , 'phpwg_version') != PHPWG_VERSION ) + { + array_push($errors, array('path' => $listing_file, 'type' => 'PWG-ERROR-VERSION')); + return false; + } + $meta_attributes = explode ( ',', + getAttribute($info_xml_element , 'metadata') ); + $this->update_attributes = array_merge( $this->update_attributes, $meta_attributes ); + $this->build_structure($xml_content, '', 0); + return true; + } + else + { + array_push($errors, array('path' => $listing_file, 'type' => 'PWG-ERROR-NO-FS')); + return false; + } +} + +// retrieve xml sub-directories fulldirs +function get_full_directories($basedir) +{ + $dirs = array(); + foreach ( array_keys($this->site_dirs) as $dir) + { + $full_dir = $this->site_url . $dir; + if ( $full_dir!=$basedir + and strpos($full_dir, $basedir)===0 + ) + { + array_push($dirs, $full_dir); + } + } + return $dirs; +} + +/** + * Returns a hash with all elements (images and files) inside the full $path + * according to listing.xml + * @param string $path recurse in this directory only + * @return array like "pic.jpg"=>array('tn_ext'=>'jpg' ... ) + */ +function get_elements($path) +{ + $elements = array(); + foreach ( $this->site_dirs as $dir=>$files) + { + $full_dir = $this->site_url . $dir; + if ( strpos($full_dir, $path)===0 ) + { + foreach ( $files as $file) + { + $data = $this->get_element_attributes($file, + $this->insert_attributes); + $elements[$file] = $data; + } + } + } + + return $elements; +} + +// returns the name of the attributes that are supported for +// update/synchronization according to listing.xml +function get_update_attributes() +{ + return $this->update_attributes; +} + +// returns a hash of attributes (metadata+filesize+width,...) for file +function get_element_update_attributes($file) +{ + return $this->get_element_attributes($file, + $this->update_attributes); +} + +//-------------------------------------------------- private functions -------- +/** + * Returns a hash of image/file attributes + * @param string $file fully qualified file name + * @param array $attributes specifies which attributes to retrieve + * returned +*/ +function get_element_attributes($file, $attributes) +{ + $xml_element = $this->site_files[$file]; + if ( ! isset($xml_element) ) + { + return null; + } + $data = array(); + foreach($attributes as $att) + { + if (getAttribute($xml_element, $att) != '') + { + $data[$att] = getAttribute($xml_element, $att); + } + } + return $data; +} + +// recursively parse the xml_content for later usage +function build_structure($xml_content, $basedir, $level) +{ + $temp_dirs = getChildren($xml_content, 'dir'.$level); + foreach ($temp_dirs as $temp_dir) + { + $dir_name = $basedir; + if ($dir_name != '' ) + { + $dir_name .= '/'; + } + $dir_name .= getAttribute($temp_dir, 'name'); + $this->site_dirs[ $dir_name ] = array(); + $this->build_structure($temp_dir, $dir_name, $level+1); + } + + if ($basedir != '') + { + $xml_elements = getChildren( getChild($xml_content, 'root'), 'element' ); + foreach ($xml_elements as $xml_element) + { + $path = getAttribute($xml_element, 'path'); + $this->site_files[$path] = $xml_element; + array_push( $this->site_dirs[$basedir], $path); + } + } +} + +} + +?> \ No newline at end of file diff --git a/admin/site_update.php b/admin/site_update.php new file mode 100644 index 000000000..5d37ed114 --- /dev/null +++ b/admin/site_update.php @@ -0,0 +1,813 @@ + array( get_lang('update_wrong_dirname_short'), + get_lang('update_wrong_dirname_info') ), + 'PWG-UPDATE-2' => array( get_lang('update_missing_tn_short'), + get_lang('update_missing_tn_info') + . implode(',', $conf['picture_ext']) ), + 'PWG-ERROR-NO-FS' => array( get_lang('Does not exist'), + get_lang('update_missing_file_or_dir_info')), + 'PWG-ERROR-VERSION' => array( get_lang('Invalid PhpWebGalley version'), + get_lang('update_pwg_version_differs_info')), + 'PWG-ERROR-NOLISTING' => array( get_lang('remote_site_listing_not_found'), + get_lang('remote_site_listing_not_found_info')) + ); +$errors = array(); +$infos = array(); + +if ($site_is_remote) +{ + include_once( PHPWG_ROOT_PATH.'admin/site_reader_remote.php'); + $site_reader = new RemoteSiteReader($site_url); +} +else +{ + include_once( PHPWG_ROOT_PATH.'admin/site_reader_local.php'); + $site_reader = new LocalSiteReader($site_url); +} + +$general_failure=true; +if (isset($_POST['submit'])) +{ + if ($site_reader->open()) + { + $general_failure = false; + } + // shall we simulate only + if (isset($_POST['simulate']) and $_POST['simulate'] == 1) + { + $simulate = true; + } + else + { + $simulate = false; + } +} + +// +-----------------------------------------------------------------------+ +// | directories / categories | +// +-----------------------------------------------------------------------+ +if (isset($_POST['submit']) + and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files') + and !$general_failure) +{ + $counts['new_categories'] = 0; + $counts['del_categories'] = 0; + $counts['del_elements'] = 0; + $counts['new_elements'] = 0; + + $start = get_moment(); + // which categories to update ? + $cat_ids = array(); + + $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.= ' + AND uppercats REGEXP \'(^|,)'.$_POST['cat'].'(,|$)\' +'; + } + else + { + $query.= ' + AND id = '.$_POST['cat'].' +'; + } + } + $query.= ' +;'; + $result = pwg_query($query); + + $db_categories = array(); + while ($row = mysql_fetch_array($result)) + { + $db_categories[$row['id']] = $row; + } + + // 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 + FROM '.CATEGORIES_TABLE.' +;'; + $result = pwg_query($query); + while ($row = mysql_fetch_array($result)) + { + $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.' + GROUP BY id_uppercat +;'; + $result = pwg_query($query); + while ($row = mysql_fetch_array($result)) + { + // 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 + $query = ' +SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_id + FROM '.CATEGORIES_TABLE.' +;'; + list($next_id) = mysql_fetch_array(pwg_query($query)); + + // retrieve sub-directories fulldirs from the site reader + $fs_fulldirs = $site_reader->get_full_directories($basedir); + //print_r( $fs_fulldirs ); echo "
"; + + // 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); + } + + $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)) + { + $insert = array(); + + $insert{'id'} = $next_id++; + $insert{'dir'} = $dir; + $insert{'name'} = str_replace('_', ' ', $dir); + $insert{'site_id'} = $site_id; + $insert{'commentable'} = $conf['newcat_default_commentable']; + if (! $site_is_remote) + { + $insert{'uploadable'} = $conf['newcat_default_uploadable']; + } + else + { + $insert{'uploadable'} = false; + } + $insert{'status'} = $conf{'newcat_default_status'}; + $insert{'visible'} = $conf{'newcat_default_visible'}; + + if (isset($db_fulldirs[dirname($fulldir)])) + { + $parent = $db_fulldirs[dirname($fulldir)]; + + $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'}; + if ('private' == $db_categories[$parent]['status']) + { + $insert{'status'} = 'private'; + } + if ('false' == $db_categories[$parent]['visible']) + { + $insert{'visible'} = 'false'; + } + } + else + { + $insert{'uppercats'} = $insert{'id'}; + $insert{'rank'} = $next_rank['NULL']++; + $insert{'global_rank'} = $insert{'rank'}; + } + + array_push($inserts, $insert); + array_push($infos, array('path' => $fulldir, + 'info' => get_lang('update_research_added'))); + + // add the new category to $db_categories and $db_fulldirs array + $db_categories[$insert{'id'}] = + array( + 'id' => $insert{'id'}, + 'status' => $insert{'status'}, + 'visible' => $insert{'visible'}, + 'uppercats' => $insert{'uppercats'}, + 'global_rank' => $insert{'global_rank'} + ); + $db_fulldirs[$fulldir] = $insert{'id'}; + $next_rank[$insert{'id'}] = 1; + } + else + { + array_push($errors, array('path' => $fulldir, 'type' => 'PWG-UPDATE-1')); + } + } + + 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, + 'info' => get_lang('update_research_deleted'))); + } + if (count($to_delete) > 0) + { + if (!$simulate) + { + delete_categories($to_delete); + } + $counts['del_categories'] = count($to_delete); + } + + echo ''."\n"; +} +// +-----------------------------------------------------------------------+ +// | 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); + //print_r($fs); echo "
"; + echo '\n"; + + $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.' + WHERE storage_category_id IN ( +'.wordwrap(implode(', ', $cat_ids), 80, "\n").') +;'; + $result = pwg_query($query); + while ($row = mysql_fetch_array($result)) + { + $db_elements[$row['id']] = $row['path']; + } + + // 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").') + AND validated = \'false\' +;'; + $result = pwg_query($query); + while ($row = mysql_fetch_array($result)) + { + array_push( + $db_unvalidated, + array_search($row['storage_category_id'], + $db_fulldirs).'/'.$row['file'] + ); + } + } + + // next element id available + $query = ' +SELECT IF(MAX(id)+1 IS NULL, 1, MAX(id)+1) AS next_element_id + FROM '.IMAGES_TABLE.' +;'; + list($next_element_id) = mysql_fetch_array(pwg_query($query)); + + $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)) + { + array_push($errors, array('path' => $path, 'type' => 'PWG-UPDATE-1')); + continue; + } + + // 2 cases : the element is a picture or not. Indeed, for a picture + // thumbnail is mandatory and for non picture element, thumbnail and + // representative are optionnal + if (in_array(get_extension($filename), $conf['picture_ext'])) + { + // if we found a thumnbnail corresponding to our picture... + if ( isset($fs[$path]['tn_ext']) ) + { + $insert{'id'} = $next_element_id++; + $insert{'file'} = $filename; + $insert{'storage_category_id'} = $db_fulldirs[$dirname]; + $insert{'date_available'} = CURRENT_DATE; + $insert{'tn_ext'} = $fs[$path]['tn_ext']; + $insert{'has_high'} = $fs[$path]['has_high']; + $insert{'path'} = $path; + + array_push($inserts, $insert); + array_push($insert_links, + array('image_id' => $insert{'id'}, + 'category_id' => $insert{'storage_category_id'})); + array_push($infos, array('path' => $insert{'path'}, + 'info' => get_lang('update_research_added'))); + } + else + { + array_push($errors, array('path' => $path, 'type' => 'PWG-UPDATE-2')); + } + } + else + { + $insert{'id'} = $next_element_id++; + $insert{'file'} = $filename; + $insert{'storage_category_id'} = $db_fulldirs[$dirname]; + $insert{'date_available'} = CURRENT_DATE; + $insert{'has_high'} = $fs[$path]['has_high']; + $insert{'path'} = $path; + + if ( isset($fs[$path]['tn_ext']) ) + { + $insert{'tn_ext'} = $fs[$path]['tn_ext']; + } + if (isset($fs[$path]['representative_ext'])) + { + $insert{'representative_ext'} = $fs[$path]['representative_ext']; + } + + array_push($inserts, $insert); + array_push($insert_links, + array('image_id' => $insert{'id'}, + 'category_id' => $insert{'storage_category_id'})); + array_push($infos, array('path' => $insert{'path'}, + 'info' => get_lang('update_research_added'))); + } + } + + if (count($inserts) > 0) + { + if (!$simulate) + { + // inserts all new elements + $dbfields = array( + 'id','file','storage_category_id','date_available','tn_ext' + ,'representative_ext', 'has_high', 'path' + ); + mass_inserts(IMAGES_TABLE, $dbfields, $inserts); + + // insert all links between new elements and their storage category + $dbfields = array('image_id','category_id'); + mass_inserts(IMAGE_CATEGORY_TABLE, $dbfields, $insert_links); + } + $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, + 'info' => get_lang('update_research_deleted'))); + } + if (count($to_delete_elements) > 0) + { + if (!$simulate) + { + delete_elements($to_delete_elements); + } + $counts['del_elements'] = count($to_delete_elements); + } + + echo ''."\n"; + + // 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").') + AND validated = \'true\' +;'; + $result = pwg_query($query); + + $datas = array(); + $fields = + array( + 'primary' => array('id'), + 'update' => array('date_creation', 'author', 'name', 'comment') + ); + + $waiting_to_delete = array(); + + while ($row = mysql_fetch_array($result)) + { + $data = array(); + + $query = ' +SELECT id + FROM '.IMAGES_TABLE.' + WHERE storage_category_id = \''.$row['storage_category_id'].'\' + AND file = \''.$row['file'].'\' +;'; + list($data['id']) = mysql_fetch_array(pwg_query($query)); + + foreach ($fields['update'] as $field) + { + $data[$field] = 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']) + and ($_POST['sync'] == 'dirs' or $_POST['sync'] == 'files')) +{ + $template->assign_block_vars( + '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_ERRORS'=>count($errors), + )); + + if (!$simulate) + { + $start = get_moment(); + update_category('all'); + echo ''."\n"; + $start = get_moment(); + ordering(); + update_global_rank(); + echo ''."\n"; + } +} + +// +-----------------------------------------------------------------------+ +// | synchronize metadata | +// +-----------------------------------------------------------------------+ +if (isset($_POST['submit']) and preg_match('/^metadata/', $_POST['sync']) + and !$general_failure) +{ + // sync only never synchronized files ? + if ($_POST['sync'] == 'metadata_new') + { + $opts['only_new'] = true; + } + else + { + $opts['only_new'] = false; + } + $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']); + + echo ''."\n"; + + $start = get_moment(); + $datas = array(); + foreach ( $files as $id=>$file ) + { + $data = $site_reader->get_element_update_attributes($file); + if ( is_array($data) ) + { + $data['date_metadata_update'] = CURRENT_DATE; + $data['id']=$id; + array_push($datas, $data); + } + else + { + array_push($errors, array('path' => $file, 'type' => 'PWG-ERROR-NO-FS')); + } + } + $update_fields = $site_reader->get_update_attributes(); + $update_fields = array_merge($update_fields, 'date_metadata_update'); + $fields = + array( + 'primary' => array('id'), + 'update' => array_unique($update_fields) + ); + //print_r($datas); + if (!$simulate and count($datas)>0 ) + { + mass_updates(IMAGES_TABLE, $fields, $datas); + } + + echo ''."\n"; + + $template->assign_block_vars( + 'metadata_result', + array( + 'NB_ELEMENTS_DONE' => count($datas), + 'NB_ELEMENTS_CANDIDATES' => count($files), + 'NB_ERRORS' => count($errors), + )); +} + +// +-----------------------------------------------------------------------+ +// | template initialization | +// +-----------------------------------------------------------------------+ +$template->set_filenames(array('update'=>'admin/site_update.tpl')); +$result_title = ''; +if (isset($simulate) and $simulate) +{ + $result_title.= get_lang('update_simulation_title').' '; +} + +// used_metadata string is displayed to inform admin which metadata will be +// used from files for synchronization +$used_metadata = implode( ', ', $site_reader->get_update_attributes()); +if ($site_is_remote and !isset($_POST['submit']) ) +{ + $used_metadata.= ' + ' . get_lang('Aditionnal remote attributes'); +} + +$template->assign_vars( + array( + 'SITE_URL'=>$site_url, + 'U_SITE_MANAGER'=> PHPWG_ROOT_PATH.'admin.php?page=site_manager', + 'L_RESULT_UPDATE'=>$result_title.get_lang('update_part_research'), + 'L_RESULT_METADATA'=>$result_title.get_lang('update_result_metadata'), + 'METADATA_LIST' => $used_metadata + )); + +$template->assign_vars( + array( + 'U_HELP' => PHPWG_ROOT_PATH.'/popuphelp.php?page=synchronize' + ) + ); +// +-----------------------------------------------------------------------+ +// | introduction : choices | +// +-----------------------------------------------------------------------+ +if (!isset($_POST['submit']) or (isset($simulate) and $simulate)) +{ + $template->assign_block_vars('introduction', array()); + + if (isset($simulate) and $simulate) + { + switch ($_POST['sync']) + { + case 'dirs' : + { + $template->assign_vars( + array('SYNC_DIRS_CHECKED'=>'checked="checked"')); + break; + } + case 'files' : + { + $template->assign_vars( + array('SYNC_ALL_CHECKED'=>'checked="checked"')); + break; + } + case 'metadata_new' : + { + $template->assign_vars( + array('SYNC_META_NEW_CHECKED'=>'checked="checked"')); + break; + } + case 'metadata_all' : + { + $template->assign_vars( + array('SYNC_META_ALL_CHECKED'=>'checked="checked"')); + break; + } + } + + if (isset($_POST['display_info']) and $_POST['display_info'] == 1) + { + $template->assign_vars( + array('DISPLAY_INFO_CHECKED'=>'checked="checked"')); + } + + if (isset($_POST['subcats-included']) and $_POST['subcats-included'] == 1) + { + $template->assign_vars( + array('SUBCATS_INCLUDED_CHECKED'=>'checked="checked"')); + } + + if (isset($_POST['cat']) and is_numeric($_POST['cat'])) + { + $cat_selected = array($_POST['cat']); + } + else + { + $cat_selected = array(); + } + } + else + { + $template->assign_vars( + array('SYNC_DIRS_CHECKED' => 'checked="checked"', + 'SUBCATS_INCLUDED_CHECKED'=>'checked="checked"')); + + $cat_selected = array(); + } + + $query = ' +SELECT id,name,uppercats,global_rank + FROM '.CATEGORIES_TABLE.' + WHERE site_id = '.$site_id.' +;'; + display_select_cat_wrapper($query, + $cat_selected, + 'introduction.category_option', + false); +} + +if (count($errors) > 0) +{ + $template->assign_block_vars('sync_errors', array()); + foreach ($errors as $error) + { + $template->assign_block_vars( + 'sync_errors.error', + array( + 'ELEMENT' => $error['path'], + 'LABEL' => $error['type'].' ('.$error_labels[$error['type']][0].')' + )); + } + + foreach ($error_labels as $error_type=>$error_description) + { + $template->assign_block_vars( + 'sync_errors.error_caption', + array( + 'TYPE' => $error_type, + 'LABEL' => $error_description[1] + )); + } + +} +if (count($infos) > 0 + and isset($_POST['display_info']) + and $_POST['display_info'] == 1) +{ + $template->assign_block_vars('sync_infos', array()); + foreach ($infos as $info) + { + $template->assign_block_vars( + 'sync_infos.info', + array( + 'ELEMENT' => $info['path'], + 'LABEL' => $info['info'] + )); + } +} + +// +-----------------------------------------------------------------------+ +// | sending html code | +// +-----------------------------------------------------------------------+ +$template->assign_var_from_handle('ADMIN_CONTENT', 'update'); +?> \ No newline at end of file diff --git a/admin/update.php b/admin/update.php index bb7133000..f19bc8a9e 100644 --- a/admin/update.php +++ b/admin/update.php @@ -743,7 +743,7 @@ else if (isset($_POST['submit']) and preg_match('/^metadata/', $_POST['sync'])) } } $start = get_moment(); - $files = get_filelist($opts['category_id'], + $files = get_filelist($opts['category_id'], 1, $opts['recursive'], $opts['only_new']); diff --git a/category.php b/category.php index 7b141d70a..ba7e741eb 100644 --- a/category.php +++ b/category.php @@ -37,7 +37,7 @@ if ( isset( $_GET['act'] ) $_SESSION = array(); session_unset(); session_destroy(); - setcookie(session_name(),'',0,'/'); + setcookie(session_name(),'',0, cookie_path() ); $url = 'category.php'; redirect( $url ); } diff --git a/identification.php b/identification.php index 223e6c1e9..3ff9d468d 100644 --- a/identification.php +++ b/identification.php @@ -51,6 +51,7 @@ SELECT '.$conf['user_fields']['id'].' AS id, { $session_length = $conf['remember_me_length']; } + session_set_cookie_params($session_length); session_start(); $_SESSION['id'] = $row['id']; redirect('category.php'); diff --git a/include/common.inc.php b/include/common.inc.php index 6e8bbf697..83e1bf30d 100644 --- a/include/common.inc.php +++ b/include/common.inc.php @@ -147,6 +147,7 @@ SELECT id // which upgrades need to be applied? if (count(array_diff($existing, $applied)) > 0) { + ob_start();// buffer output so that cookies work echo '

' .'Some database upgrades are missing, ' diff --git a/include/functions.inc.php b/include/functions.inc.php index d49317d22..49f1b44bd 100644 --- a/include/functions.inc.php +++ b/include/functions.inc.php @@ -973,6 +973,20 @@ SELECT '.$conf['user_fields']['email'].' return $email; } +/** + * returns the $str in current language if possible or $str enclosed + * in special chars + */ +function get_lang($str) +{ + global $lang; + if ( isset($lang[$str]) ) + { + return $lang[$str]; + } + return '@@'.$str.'@@'; +} + /** * which upgrades are available ? * diff --git a/include/functions_category.inc.php b/include/functions_category.inc.php index 370ab48e6..9e0e16a1d 100644 --- a/include/functions_category.inc.php +++ b/include/functions_category.inc.php @@ -321,13 +321,14 @@ SELECT galleries_url // returns an array of image orders available for users/visitors function get_category_preferred_image_orders() { - global $lang, $conf; + global $conf; return array( array('Default', '', true), - array($lang['best_rated_cat'], 'average_rate DESC', $conf['rate']), - array($lang['most_visited_cat'], 'hit DESC', true), - array($lang['Creation date'], 'date_creation DESC', true), - array($lang['Availability date'], 'date_available DESC', true) + array(get_lang('best_rated_cat'), 'average_rate DESC', $conf['rate']), + array(get_lang('most_visited_cat'), 'hit DESC', true), + array(get_lang('Creation date'), 'date_creation DESC', true), + array(get_lang('Availability date'), 'date_available DESC', true), + array(get_lang('File name'), 'file ASC', true) ); } diff --git a/include/functions_session.inc.php b/include/functions_session.inc.php index 4ec814a5f..03a6dd155 100644 --- a/include/functions_session.inc.php +++ b/include/functions_session.inc.php @@ -69,7 +69,25 @@ if (isset($conf['session_save_handler']) ini_set('session.use_only_cookies', $conf['session_use_only_cookies']); ini_set('session.use_trans_sid', intval($conf['session_use_trans_sid'])); ini_set('session.name', $conf['session_name']); - ini_set('session.cookie_path', dirname($_SERVER['PHP_SELF'])); + ini_set('session.cookie_path', cookie_path() ); +} + +// cookie_path returns the path to use for the PhpWebGallery cookie. +// If PhpWebGallery is installed on : +// http://domain.org/meeting/gallery/category.php +// cookie_path will return : "/meeting/gallery" +function cookie_path() +{ + if ( isset($_SERVER['REDIRECT_URL']) ) + { // mod_rewrite is activated for upper level directories. we must set the + // cookie to the path shown in the browser otherwise it will be discarded. + $scr = $_SERVER['REDIRECT_URL']; + } + else + { + $scr = $_SERVER['PHP_SELF']; + } + return substr($scr,0,strrpos( $scr,'/')); } /** diff --git a/language/en_UK.iso-8859-1/common.lang.php b/language/en_UK.iso-8859-1/common.lang.php index 13bd4eefc..ccc53e24d 100644 --- a/language/en_UK.iso-8859-1/common.lang.php +++ b/language/en_UK.iso-8859-1/common.lang.php @@ -64,6 +64,7 @@ $lang['Email address'] = 'Email address'; $lang['Enter your personnal informations'] = 'Enter your personnal informations'; $lang['Error sending email'] = 'Error sending email'; $lang['File'] = 'File'; +$lang['File name'] = 'File name'; $lang['Filesize'] = 'Filesize'; $lang['Filter and display'] = 'Filter and display'; $lang['Filter'] = 'Filter'; diff --git a/language/fr_FR.iso-8859-1/common.lang.php b/language/fr_FR.iso-8859-1/common.lang.php index 79e3cd55b..d6782367a 100644 --- a/language/fr_FR.iso-8859-1/common.lang.php +++ b/language/fr_FR.iso-8859-1/common.lang.php @@ -63,6 +63,7 @@ $lang['Email address'] = 'Adresse e-mail'; $lang['Enter your personnal informations'] = 'Entrer vos informations personnelles'; $lang['Error sending email'] = 'Erreur à l\'envoi du mail'; $lang['File'] = 'Fichier'; +$lang['File name'] = 'Nom du fichier'; $lang['Filesize'] = 'Poids'; $lang['Filter and display'] = 'Filtrer et afficher'; $lang['Filter'] = 'Filtre'; diff --git a/picture.php b/picture.php index 2b53b4868..f63301a24 100644 --- a/picture.php +++ b/picture.php @@ -249,20 +249,10 @@ foreach (array('prev', 'current', 'next') as $i) // high quality picture if ($i == 'current') { - $url_high=$cat_directory.'/pwg_high/'.$row['file']; - if (url_is_remote($cat_directory)) + if ($row['has_high']=='true') { - if ($row['has_high']) - { - $picture[$i]['high'] = $url_high; - } - } - else - { - if (@fopen($url_high, 'r')) - { - $picture[$i]['high'] = $url_high; - } + $url_high=$cat_directory.'/pwg_high/'.$row['file']; + $picture[$i]['high'] = $url_high; } } } @@ -1054,7 +1044,7 @@ if ($page['show_comments']) // navigation bar creation $url = PHPWG_ROOT_PATH.'picture.php'; - $url.= get_query_string_diff(array('rate','add_fav')); + $url.= get_query_string_diff(array('rate','add_fav','start')); if (!isset( $_GET['start'] ) or !is_numeric( $_GET['start'] ) diff --git a/template/yoga/admin.tpl b/template/yoga/admin.tpl index 2192f90f6..c5b8e7e03 100644 --- a/template/yoga/admin.tpl +++ b/template/yoga/admin.tpl @@ -14,6 +14,7 @@