diff --git a/admin/configuration.php b/admin/configuration.php index 871784758..1d8356444 100644 --- a/admin/configuration.php +++ b/admin/configuration.php @@ -166,24 +166,38 @@ if ( isset( $_POST['submit'] ) ) array_push( $error, $lang['err_maxheight'] ); }*/ // updating configuraiton if no error found - if ( count( $error ) == 0 ) + if (count($error) == 0) { - $result = mysql_query( "SELECT * FROM ".CONFIG_TABLE ); - while ( $row = mysql_fetch_array( $result ) ) - { - $config_name = $row['param']; - $conf[$config_name] = ( isset($_POST[$config_name]) ) ? $_POST[$config_name] : $row['value']; - if ( isset( $_POST[$config_name] ) ) + $result = mysql_query('SELECT * FROM '.CONFIG_TABLE); + while ($row = mysql_fetch_array($result)) + { + $config_name = $row['param']; + if (isset($_POST[$config_name])) { - $query = 'UPDATE '.CONFIG_TABLE; - $query.= " SET value = '". str_replace("\'", "''", $conf[$config_name]) ; - $query.= "' WHERE param = '$config_name'"; - mysql_query( $query ); + $conf[$config_name] = $_POST[$config_name]; + } + else + { + $conf[$config_name] = $row['value']; + } + + if (isset($_POST[$config_name])) + { + $query = ' +UPDATE '.CONFIG_TABLE.' + SET value = \''. str_replace("\'", "''", $conf[$config_name]).'\' + WHERE param = \''.$config_name.'\' +;'; + mysql_query($query); } } } } +// echo '
'; +// print_r($conf); +// echo ''; + $access = ($conf['access']=='free')?'ACCESS_FREE':'ACCESS_RESTRICTED'; $log = ($conf['log']=='true')?'HISTORY_YES':'HISTORY_NO'; $mail_notif = ($conf['mail_notification']=='true')?'MAIL_NOTIFICATION_YES':'MAIL_NOTIFICATION_NO'; @@ -194,6 +208,8 @@ $expand = ($conf['auto_expand']=='true')?'EXPAND_TREE_YES':'EXPAND_TREE_NO'; $nb_comments = ($conf['show_nb_comments']=='true')?'NB_COMMENTS_YES':'NB_COMMENTS_NO'; $upload = ($conf['upload_available']=='true')?'UPLOAD_YES':'UPLOAD_NO'; $cookie = ($conf['authorize_cookies']=='true')?'COOKIE_YES':'COOKIE_NO'; +$use_exif = ($conf['use_exif']=='true')?'USE_EXIF_YES':'USE_EXIF_NO'; +$use_iptc = ($conf['use_iptc']=='true')?'USE_IPTC_YES':'USE_IPTC_NO'; //----------------------------------------------------- template initialization $template->set_filenames( array('config'=>'admin/configuration.tpl') ); @@ -226,6 +242,8 @@ $template->assign_vars(array( $nb_comments=>'checked="checked"', $upload=>'checked="checked"', $cookie=>'checked="checked"', + $use_exif=>'checked="checked"', + $use_iptc=>'checked="checked"', 'L_CONFIRM'=>$lang['conf_confirmation'], 'L_CONF_GENERAL'=>$lang['conf_general_title'], @@ -290,9 +308,14 @@ $template->assign_vars(array( 'L_YES'=>$lang['yes'], 'L_NO'=>$lang['no'], 'L_SUBMIT'=>$lang['submit'], + 'L_CONF_METADATA'=>$lang['conf_metadata_title'], + 'L_USE_EXIF'=>$lang['conf_use_exif'], + 'L_USE_EXIF_INFO'=>$lang['conf_use_exif_info'], + 'L_USE_IPTC'=>$lang['conf_use_iptc'], + 'L_USE_IPTC_INFO'=>$lang['conf_use_iptc_info'], 'F_ACTION'=>add_session_id(PHPWG_ROOT_PATH.'admin.php?page=configuration') - )); + )); //-------------------------------------------------------------- errors display if ( sizeof( $error ) != 0 ) diff --git a/admin/include/functions_metadata.php b/admin/include/functions_metadata.php new file mode 100644 index 000000000..9a7d45193 --- /dev/null +++ b/admin/include/functions_metadata.php @@ -0,0 +1,363 @@ + | +// | branch : BSF (Best So Far) | +// +-----------------------------------------------------------------------+ +// | file : $RCSfile$ +// | last update : $Date$ +// | last modifier : $Author$ +// | revision : $Revision$ +// +-----------------------------------------------------------------------+ +// | 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 | +// | 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. | +// +-----------------------------------------------------------------------+ + +/** + * returns informations from IPTC metadata, mapping is done at the beginning + * of the function + * + * @param string $filename + * @return array + */ +function get_iptc_data($filename) +{ + global $getimagesize_time; + + $map = array( + 'keywords' => '2#025', + 'date_creation' => '2#055', + 'author' => '2#122', + 'name' => '2#085', + 'comment' => '2#120' + ); + $datefields = array('date_creation', 'date_available'); + + $result = array(); + + // Read IPTC data + $iptc = array(); + + $start = get_moment(); + getimagesize($filename, &$imginfo); + $getimagesize_time+= get_moment() - $start; + + if (is_array($imginfo) and isset($imginfo['APP13'])) + { + $iptc = iptcparse($imginfo['APP13']); + if (is_array($iptc)) + { + $rmap = array_flip($map); + foreach (array_keys($rmap) as $iptc_key) + { + if (isset($iptc[$iptc_key][0]) and $value = $iptc[$iptc_key][0]) + { + // strip leading zeros (weird Kodak Scanner software) + while ($value[0] == chr(0)) + { + $value = substr($value, 1); + } + // remove binary nulls + $value = str_replace(chr(0x00), ' ', $value); + + foreach (array_keys($map, $iptc_key) as $pwg_key) + { + if (in_array($pwg_key, $datefields)) + { + if ( preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches)) + { + $value = $matches[1].'-'.$matches[2].'-'.$matches[3]; + } + else + { + continue; + } + } + $result[$pwg_key] = $value; + } + } + } + } + } + return $result; +} + +function update_metadata($files) +{ + global $conf; + +// $conf['use_iptc'] = true; +// $conf['use_exif'] = true; + + $inserts = array(); + + foreach ($files as $id => $file) + { + $insert = array(); + $insert['id'] = $id; + $insert['filesize'] = floor(filesize($file)/1024); + + if ($image_size = @getimagesize($file)) + { + $insert['width'] = $image_size[0]; + $insert['height'] = $image_size[1]; + } + + if ($conf['use_exif']) + { + if ($exif = @read_exif_data($file)) + { + if (isset($exif['DateTime'])) + { + preg_match('/^(\d{4}).(\d{2}).(\d{2})/' + ,$exif['DateTime'] + ,$matches); + $insert['date_creation'] = + "'".$matches[1].'-'.$matches[2].'-'.$matches[3]."'"; + } + } + } + + if ($conf['use_iptc']) + { + $iptc = get_iptc_data($file); + foreach (array_keys($iptc) as $key) + { + $insert[$key] = "'".addslashes($iptc[$key])."'"; + } + } + + $insert['date_metadata_update'] = CURRENT_DATE; + + array_push($inserts, $insert); + } + + if (count($inserts) > 0) + { + $dbfields = array( + 'id','filesize','width','height','name','author','comment' + ,'date_creation','keywords','date_metadata_update' + ); + + // depending on the MySQL version, we use the multi table update or N + // update queries + $query = 'SELECT VERSION() AS version;'; + $row = mysql_fetch_array(mysql_query($query)); + if (version_compare($row['version'],'4.0.4') < 0) + { + // MySQL is prior to version 4.0.4, multi table update feature is not + // available + echo 'MySQL is prior to version 4.0.4, multi table update feature is not available
'.$query.''; + mysql_query($query); + } + } + else + { + // creation of the temporary table + $query = ' +DESCRIBE '.IMAGES_TABLE.' +;'; + $result = mysql_query($query); + $columns = array(); + while ($row = mysql_fetch_array($result)) + { + if (in_array($row['Field'], $dbfields)) + { + $column = $row['Field']; + $column.= ' '.$row['Type']; + if (!isset($row['Null']) or $row['Null'] == '') + { + $column.= ' NOT NULL'; + } + if (isset($row['Default'])) + { + $column.= " default '".$row['Default']."'"; + } + array_push($columns, $column); + } + } + $query = ' +CREATE TEMPORARY TABLE '.IMAGE_METADATA_TABLE.' +( +'.implode(",\n", $columns).', +PRIMARY KEY (id) +) +;'; + // echo '
'.$query.''; + mysql_query($query); + // inserts all found pictures + $query = ' +INSERT INTO '.IMAGE_METADATA_TABLE.' + ('.implode(',', $dbfields).') + VALUES + '; + foreach ($inserts as $insert_id => $insert) + { + $query.= ' +'; + if ($insert_id > 0) + { + $query.= ','; + } + $query.= '('; + foreach ($dbfields as $field_id => $dbfield) + { + if ($field_id > 0) + { + $query.= ','; + } + + if (!isset($insert[$dbfield]) or $insert[$dbfield] == '') + { + $query.= 'NULL'; + } + else + { + $query.= $insert[$dbfield]; + } + } + $query.=')'; + } + $query.= ' +;'; + // echo '
'.$query.''; + mysql_query($query); + // update of images table by joining with temporary table + $query = ' +UPDATE '.IMAGES_TABLE.' AS images, '.IMAGE_METADATA_TABLE.' as metadata + SET '.implode("\n , ", + array_map( + create_function('$s', 'return "images.$s = metadata.$s";') + , array_diff($dbfields, array('id')))).' + WHERE images.id = metadata.id +;'; + echo '
'.$query.''; + mysql_query($query); + } + } +} + +/** + * returns an array associating element id (images.id) with its complete + * path in the filesystem + * + * @param int id_uppercat + * @param boolean recursive ? + * @param boolean only newly added files ? + * @return array + */ +function get_filelist($category_id = '', $recursive = false, $only_new = false) +{ + $files = array(); + + $query = ' +SELECT id, dir + FROM '.CATEGORIES_TABLE.' +;'; + $result = mysql_query($query); + $cat_dirs = array(); + while ($row = mysql_fetch_array($result)) + { + $cat_dirs[$row['id']] = $row['dir']; + } + + // filling $uppercats_array : to each category id the uppercats list is + // associated + $uppercats_array = array(); + + $query = ' +SELECT id, uppercats + FROM '.CATEGORIES_TABLE; + if (is_numeric($category_id)) + { + if ($recursive) + { + $query.= ' + WHERE uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\' +'; + } + else + { + $query.= ' + WHERE id = '.$category_id.' +'; + } + } + $query.= ' +;'; + $result = mysql_query($query); + while ($row = mysql_fetch_array($result)) + { + $uppercats_array[$row['id']] = $row['uppercats']; + } + + $query = ' +SELECT galleries_url + FROM '.SITES_TABLE.' + WHERE id = 1 +'; + $row = mysql_fetch_array(mysql_query($query)); + $basedir = $row['galleries_url']; + + // filling $cat_fulldirs + $cat_fulldirs = array(); + foreach ($uppercats_array as $cat_id => $uppercats) + { + $uppercats = str_replace(',', '/', $uppercats); + $cat_fulldirs[$cat_id] = $basedir.preg_replace('/(\d+)/e', + "\$cat_dirs['$1']", + $uppercats); + } + + $query = ' +SELECT id, file, storage_category_id + FROM '.IMAGES_TABLE.' + WHERE storage_category_id IN ('.implode(',' + ,array_keys($uppercats_array)).')'; + if ($only_new) + { + $query.= ' + AND date_metadata_update IS NULL +'; + } + $query.= ' +;'; + $result = mysql_query($query); + while ($row = mysql_fetch_array($result)) + { + $files[$row['id']] + = $cat_fulldirs[$row['storage_category_id']].'/'.$row['file']; + } + + return $files; +} +?> \ No newline at end of file diff --git a/admin/update.php b/admin/update.php index ba4f823ac..a99b58e62 100644 --- a/admin/update.php +++ b/admin/update.php @@ -26,33 +26,43 @@ // +-----------------------------------------------------------------------+ include_once( PHPWG_ROOT_PATH.'admin/include/isadmin.inc.php'); + +define('CURRENT_DATE', "'".date('Y-m-d')."'"); //------------------------------------------------------------------- functions -function ordering( $id_uppercat) +/** + * order categories (update categories.rank database field) + * + * the purpose of this function is to give a rank for all categories + * (insides its sub-category), even the newer that have none at te + * beginning. For this, ordering function selects all categories ordered by + * rank ASC then name ASC for each uppercat. + * + * @returns void + */ +function ordering() { - $rank = 1; + $current_rank = 0; + $current_uppercat = ''; - $query = 'SELECT id'; - $query.= ' FROM '.CATEGORIES_TABLE; - if ( !is_numeric( $id_uppercat)) + $query = ' +SELECT id, if(id_uppercat is null,\'\',id_uppercat) AS id_uppercat + FROM '.CATEGORIES_TABLE.' + ORDER BY id_uppercat,rank,name +;'; + $result = mysql_query($query); + while ($row = mysql_fetch_array($result)) { - $query.= ' WHERE id_uppercat IS NULL'; - } - else - { - $query.= ' WHERE id_uppercat = '.$id_uppercat; - } - $query.= ' ORDER BY rank ASC, dir ASC'; - $query.= ';'; - $result = mysql_query( $query); - while ( $row = mysql_fetch_array( $result)) - { - $query = 'UPDATE '.CATEGORIES_TABLE; - $query.= ' SET rank = '.$rank; - $query.= ' WHERE id = '.$row['id']; - $query.= ';'; - mysql_query( $query); - $rank++; - ordering( $row['id']); + if ($row['id_uppercat'] != $current_uppercat) + { + $current_rank = 0; + $current_uppercat = $row['id_uppercat']; + } + $query = ' +UPDATE '.CATEGORIES_TABLE.' + SET rank = '.++$current_rank.' + WHERE id = '.$row['id'].' +;'; + mysql_query($query); } } @@ -110,7 +120,7 @@ SELECT id,dir FROM '.CATEGORIES_TABLE.' } } - $sub_dirs = get_category_directories($cat_directory); + $fs_subdirs = get_category_directories($cat_directory); $sub_category_dirs = array(); $query = ' @@ -136,29 +146,34 @@ SELECT id,dir FROM '.CATEGORIES_TABLE.' } // 3. we have to remove the categories of the database not present anymore + $to_delete_categories = array(); foreach ($sub_category_dirs as $id => $dir) { - if (!in_array($dir, $sub_dirs)) + if (!in_array($dir, $fs_subdirs)) { - delete_category($id); + array_push($to_delete_categories,$id); } } + if (count($to_delete_categories) > 0) + { + delete_categories($to_delete_categories); + } // array of new categories to insert $inserts = array(); - foreach ($sub_dirs as $sub_dir) + foreach ($fs_subdirs as $fs_subdir) { // 5. Is the category already existing ? we create a subcat if not // existing - $category_id = array_search($sub_dir, $sub_category_dirs); + $category_id = array_search($fs_subdir, $sub_category_dirs); if (!is_numeric($category_id)) { - if (preg_match('/^[a-zA-Z0-9-_.]+$/', $sub_dir)) + if (preg_match('/^[a-zA-Z0-9-_.]+$/', $fs_subdir)) { - $name = str_replace('_', ' ', $sub_dir); + $name = str_replace('_', ' ', $fs_subdir); - $value = "('".$sub_dir."','".$name."',1"; + $value = "('".$fs_subdir."','".$name."',1"; if (!is_numeric($id_uppercat)) { $value.= ',NULL'; @@ -173,7 +188,7 @@ SELECT id,dir FROM '.CATEGORIES_TABLE.' } else { - $output.= '"'.$sub_dir.'" : '; + $output.= '"'.$fs_subdir.'" : '; $output.= $lang['update_wrong_dirname'].'