2004-08-21 12:52:43 +00:00
|
|
|
<?php
|
|
|
|
// +-----------------------------------------------------------------------+
|
2011-01-18 00:02:52 +00:00
|
|
|
// | Piwigo - a PHP based photo gallery |
|
2008-04-04 22:57:23 +00:00
|
|
|
// +-----------------------------------------------------------------------+
|
2014-01-05 00:19:25 +00:00
|
|
|
// | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org |
|
2008-04-04 22:57:23 +00: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 |
|
|
|
|
// | 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 |
|
2004-08-21 12:52:43 +00:00
|
|
|
// | 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. |
|
|
|
|
// +-----------------------------------------------------------------------+
|
|
|
|
|
2004-08-25 21:09:09 +00:00
|
|
|
include_once(PHPWG_ROOT_PATH.'/include/functions_metadata.inc.php');
|
|
|
|
|
2005-09-03 16:36:05 +00:00
|
|
|
|
2004-08-25 21:09:09 +00:00
|
|
|
function get_sync_iptc_data($file)
|
2004-08-21 12:52:43 +00:00
|
|
|
{
|
2008-09-21 11:06:20 +00:00
|
|
|
global $conf;
|
2008-09-12 02:17:35 +00:00
|
|
|
|
2004-11-10 22:31:51 +00:00
|
|
|
$map = $conf['use_iptc_mapping'];
|
2008-09-12 02:17:35 +00:00
|
|
|
|
2004-08-25 21:09:09 +00:00
|
|
|
$iptc = get_iptc_data($file, $map);
|
|
|
|
|
|
|
|
foreach ($iptc as $pwg_key => $value)
|
2004-08-21 12:52:43 +00:00
|
|
|
{
|
2008-09-21 11:06:20 +00:00
|
|
|
if (in_array($pwg_key, array('date_creation', 'date_available')))
|
2004-08-21 12:52:43 +00:00
|
|
|
{
|
2004-11-23 22:31:24 +00:00
|
|
|
if (preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches))
|
2004-08-21 12:52:43 +00:00
|
|
|
{
|
2010-10-30 20:22:15 +00:00
|
|
|
$year = $matches[1];
|
|
|
|
$month = $matches[2];
|
|
|
|
$day = $matches[3];
|
|
|
|
|
|
|
|
if (!checkdate($month, $day, $year))
|
|
|
|
{
|
|
|
|
// we suppose the year is correct
|
|
|
|
$month = 1;
|
|
|
|
$day = 1;
|
|
|
|
}
|
2012-01-03 20:21:13 +00:00
|
|
|
|
2010-10-30 20:22:15 +00:00
|
|
|
$iptc[$pwg_key] = $year.'-'.$month.'-'.$day;
|
2004-08-21 12:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-08-25 21:09:09 +00:00
|
|
|
|
2004-10-09 10:04:23 +00:00
|
|
|
if (isset($iptc['keywords']))
|
|
|
|
{
|
2006-04-05 18:02:10 +00:00
|
|
|
// official keywords separator is the comma
|
|
|
|
$iptc['keywords'] = preg_replace('/[.;]/', ',', $iptc['keywords']);
|
2012-02-10 13:57:11 +00:00
|
|
|
$iptc['keywords'] = preg_replace('/,+/', ',', $iptc['keywords']);
|
2004-10-09 10:04:23 +00:00
|
|
|
$iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']);
|
2006-04-20 21:13:47 +00:00
|
|
|
|
|
|
|
$iptc['keywords'] = implode(
|
|
|
|
',',
|
|
|
|
array_unique(
|
|
|
|
explode(
|
|
|
|
',',
|
|
|
|
$iptc['keywords']
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2004-10-09 10:04:23 +00:00
|
|
|
}
|
|
|
|
|
2007-01-11 23:56:36 +00:00
|
|
|
foreach ($iptc as $pwg_key => $value)
|
|
|
|
{
|
|
|
|
$iptc[$pwg_key] = addslashes($iptc[$pwg_key]);
|
|
|
|
}
|
|
|
|
|
2004-08-25 21:09:09 +00:00
|
|
|
return $iptc;
|
2004-08-21 12:52:43 +00:00
|
|
|
}
|
|
|
|
|
2005-09-03 16:36:05 +00:00
|
|
|
function get_sync_exif_data($file)
|
|
|
|
{
|
2008-09-21 11:06:20 +00:00
|
|
|
global $conf;
|
2005-09-03 16:36:05 +00:00
|
|
|
|
|
|
|
$exif = get_exif_data($file, $conf['use_exif_mapping']);
|
|
|
|
|
|
|
|
foreach ($exif as $pwg_key => $value)
|
|
|
|
{
|
2008-09-21 11:06:20 +00:00
|
|
|
if (in_array($pwg_key, array('date_creation', 'date_available')))
|
2005-09-03 16:36:05 +00:00
|
|
|
{
|
2011-02-03 08:18:38 +00:00
|
|
|
if (preg_match('/^(\d{4}).(\d{2}).(\d{2}) (\d{2}).(\d{2}).(\d{2})/', $value, $matches))
|
|
|
|
{
|
|
|
|
$exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3].' '.$matches[4].':'.$matches[5].':'.$matches[6];
|
|
|
|
}
|
|
|
|
elseif (preg_match('/^(\d{4}).(\d{2}).(\d{2})/', $value, $matches))
|
2005-09-03 16:36:05 +00:00
|
|
|
{
|
|
|
|
$exif[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
|
|
|
|
}
|
2011-07-14 22:02:27 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
unset($exif[$pwg_key]);
|
|
|
|
continue;
|
|
|
|
}
|
2005-09-03 16:36:05 +00:00
|
|
|
}
|
2007-01-11 23:56:36 +00:00
|
|
|
$exif[$pwg_key] = addslashes($exif[$pwg_key]);
|
2005-09-03 16:36:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $exif;
|
|
|
|
}
|
|
|
|
|
2012-01-03 20:21:13 +00:00
|
|
|
|
|
|
|
function get_sync_metadata_attributes()
|
|
|
|
{
|
|
|
|
global $conf;
|
|
|
|
|
|
|
|
$update_fields = array('filesize', 'width', 'height');
|
|
|
|
|
|
|
|
if ($conf['use_exif'])
|
|
|
|
{
|
|
|
|
$update_fields =
|
|
|
|
array_merge(
|
|
|
|
$update_fields,
|
2013-12-06 19:14:44 +00:00
|
|
|
array_keys($conf['use_exif_mapping']),
|
|
|
|
array('latitude', 'longitude')
|
2012-01-03 20:21:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($conf['use_iptc'])
|
|
|
|
{
|
|
|
|
$update_fields =
|
|
|
|
array_merge(
|
|
|
|
$update_fields,
|
|
|
|
array_keys($conf['use_iptc_mapping'])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique($update_fields);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_sync_metadata($infos)
|
|
|
|
{
|
|
|
|
global $conf;
|
|
|
|
$file = PHPWG_ROOT_PATH.$infos['path'];
|
|
|
|
$fs = @filesize($file);
|
|
|
|
|
|
|
|
if ($fs===false)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$infos['filesize'] = floor($fs/1024);
|
|
|
|
|
|
|
|
if (isset($infos['representative_ext']))
|
|
|
|
{
|
|
|
|
$file = original_to_representative($file, $infos['representative_ext']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($image_size = @getimagesize($file))
|
|
|
|
{
|
|
|
|
$infos['width'] = $image_size[0];
|
|
|
|
$infos['height'] = $image_size[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($conf['use_exif'])
|
|
|
|
{
|
|
|
|
$exif = get_sync_exif_data($file);
|
|
|
|
$infos = array_merge($infos, $exif);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($conf['use_iptc'])
|
|
|
|
{
|
|
|
|
$iptc = get_sync_iptc_data($file);
|
|
|
|
$infos = array_merge($infos, $iptc);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $infos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function sync_metadata($ids)
|
2004-08-21 12:52:43 +00:00
|
|
|
{
|
|
|
|
global $conf;
|
2004-10-24 18:57:19 +00:00
|
|
|
|
|
|
|
if (!defined('CURRENT_DATE'))
|
|
|
|
{
|
2004-11-30 19:53:33 +00:00
|
|
|
define('CURRENT_DATE', date('Y-m-d'));
|
2004-10-24 18:57:19 +00:00
|
|
|
}
|
|
|
|
|
2004-11-30 20:26:44 +00:00
|
|
|
$datas = array();
|
2006-04-02 22:26:19 +00:00
|
|
|
$tags_of = array();
|
2008-09-12 02:17:35 +00:00
|
|
|
|
2007-03-09 16:28:49 +00:00
|
|
|
$query = '
|
2012-01-03 20:21:13 +00:00
|
|
|
SELECT id, path, representative_ext
|
2007-03-09 16:28:49 +00:00
|
|
|
FROM '.IMAGES_TABLE.'
|
2012-01-03 20:21:13 +00:00
|
|
|
WHERE id IN (
|
|
|
|
'.wordwrap(implode(', ', $ids), 160, "\n").'
|
2007-03-09 16:28:49 +00:00
|
|
|
)
|
|
|
|
;';
|
|
|
|
|
2012-01-03 20:21:13 +00:00
|
|
|
$result = pwg_query($query);
|
|
|
|
while ($data = pwg_db_fetch_assoc($result))
|
2004-08-21 12:52:43 +00:00
|
|
|
{
|
2012-01-03 20:21:13 +00:00
|
|
|
$data = get_sync_metadata($data);
|
|
|
|
if ($data === false)
|
2004-08-21 12:52:43 +00:00
|
|
|
{
|
2012-01-03 20:21:13 +00:00
|
|
|
continue;
|
2007-03-09 16:28:49 +00:00
|
|
|
}
|
2008-09-12 02:17:35 +00:00
|
|
|
|
2012-01-03 20:21:13 +00:00
|
|
|
$id = $data['id'];
|
|
|
|
foreach (array('keywords', 'tags') as $key)
|
2004-08-21 12:52:43 +00:00
|
|
|
{
|
2012-01-03 20:21:13 +00:00
|
|
|
if (isset($data[$key]))
|
2010-01-15 10:47:42 +00:00
|
|
|
{
|
2012-01-03 20:21:13 +00:00
|
|
|
if (!isset($tags_of[$id]))
|
|
|
|
{
|
|
|
|
$tags_of[$id] = array();
|
|
|
|
}
|
2004-08-21 12:52:43 +00:00
|
|
|
|
2012-01-03 20:21:13 +00:00
|
|
|
foreach (explode(',', $data[$key]) as $tag_name)
|
2004-08-25 21:09:09 +00:00
|
|
|
{
|
2013-10-19 17:43:04 +00:00
|
|
|
$tags_of[$id][] = tag_id_from_tag_name($tag_name);
|
2004-08-25 21:09:09 +00:00
|
|
|
}
|
2004-08-21 12:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-11-30 20:26:44 +00:00
|
|
|
$data['date_metadata_update'] = CURRENT_DATE;
|
2004-08-21 12:52:43 +00:00
|
|
|
|
2013-10-19 17:43:04 +00:00
|
|
|
$datas[] = $data;
|
2004-08-21 12:52:43 +00:00
|
|
|
}
|
2008-09-12 02:17:35 +00:00
|
|
|
|
2004-11-30 20:26:44 +00:00
|
|
|
if (count($datas) > 0)
|
2004-08-21 12:52:43 +00:00
|
|
|
{
|
2012-01-03 20:21:13 +00:00
|
|
|
$update_fields = get_sync_metadata_attributes();
|
2013-10-19 17:43:04 +00:00
|
|
|
$update_fields[] = 'date_metadata_update';
|
2008-09-12 02:17:35 +00:00
|
|
|
|
2012-01-03 20:21:13 +00:00
|
|
|
$update_fields = array_diff(
|
|
|
|
$update_fields,
|
|
|
|
array('tags', 'keywords')
|
|
|
|
);
|
2005-09-03 16:36:05 +00:00
|
|
|
|
2006-04-05 18:02:10 +00:00
|
|
|
mass_updates(
|
|
|
|
IMAGES_TABLE,
|
2005-10-19 21:56:53 +00:00
|
|
|
array(
|
|
|
|
'primary' => array('id'),
|
2012-01-03 20:21:13 +00:00
|
|
|
'update' => $update_fields
|
2006-04-05 18:02:10 +00:00
|
|
|
),
|
2008-09-12 02:17:35 +00:00
|
|
|
$datas,
|
|
|
|
MASS_UPDATES_SKIP_EMPTY
|
2006-04-05 18:02:10 +00:00
|
|
|
);
|
2004-08-21 12:52:43 +00:00
|
|
|
}
|
2006-04-02 22:26:19 +00:00
|
|
|
|
2006-04-05 18:02:10 +00:00
|
|
|
set_tags_of($tags_of);
|
2004-08-21 12:52:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns an array associating element id (images.id) with its complete
|
|
|
|
* path in the filesystem
|
|
|
|
*
|
|
|
|
* @param int id_uppercat
|
2006-02-08 01:17:07 +00:00
|
|
|
* @param int site_id
|
2004-08-21 12:52:43 +00:00
|
|
|
* @param boolean recursive ?
|
|
|
|
* @param boolean only newly added files ?
|
|
|
|
* @return array
|
|
|
|
*/
|
2008-09-12 02:17:35 +00:00
|
|
|
function get_filelist($category_id = '', $site_id=1, $recursive = false,
|
2006-02-08 01:17:07 +00:00
|
|
|
$only_new = false)
|
2004-08-21 12:52:43 +00:00
|
|
|
{
|
2004-12-25 19:33:36 +00:00
|
|
|
// filling $cat_ids : all categories required
|
|
|
|
$cat_ids = array();
|
2008-09-12 02:17:35 +00:00
|
|
|
|
2004-08-21 12:52:43 +00:00
|
|
|
$query = '
|
2004-12-25 19:33:36 +00:00
|
|
|
SELECT id
|
2004-10-24 18:57:19 +00:00
|
|
|
FROM '.CATEGORIES_TABLE.'
|
2006-02-08 01:17:07 +00:00
|
|
|
WHERE site_id = '.$site_id.'
|
2004-10-24 18:57:19 +00:00
|
|
|
AND dir IS NOT NULL';
|
2004-08-21 12:52:43 +00:00
|
|
|
if (is_numeric($category_id))
|
|
|
|
{
|
|
|
|
if ($recursive)
|
|
|
|
{
|
|
|
|
$query.= '
|
2009-11-25 19:02:57 +00:00
|
|
|
AND uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\'
|
2004-08-21 12:52:43 +00:00
|
|
|
';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$query.= '
|
2004-10-24 18:57:19 +00:00
|
|
|
AND id = '.$category_id.'
|
2004-08-21 12:52:43 +00:00
|
|
|
';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$query.= '
|
|
|
|
;';
|
2004-10-30 15:42:29 +00:00
|
|
|
$result = pwg_query($query);
|
2009-11-20 14:17:04 +00:00
|
|
|
while ($row = pwg_db_fetch_assoc($result))
|
2004-08-21 12:52:43 +00:00
|
|
|
{
|
2013-10-19 17:43:04 +00:00
|
|
|
$cat_ids[] = $row['id'];
|
2004-08-21 12:52:43 +00:00
|
|
|
}
|
2004-11-02 23:25:27 +00:00
|
|
|
|
2004-12-25 19:33:36 +00:00
|
|
|
if (count($cat_ids) == 0)
|
2004-11-02 23:25:27 +00:00
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
2004-08-21 12:52:43 +00:00
|
|
|
|
|
|
|
$query = '
|
2012-01-03 20:21:13 +00:00
|
|
|
SELECT id, path, representative_ext
|
2004-08-21 12:52:43 +00:00
|
|
|
FROM '.IMAGES_TABLE.'
|
2006-04-04 22:29:35 +00:00
|
|
|
WHERE storage_category_id IN ('.implode(',', $cat_ids).')';
|
2004-08-21 12:52:43 +00:00
|
|
|
if ($only_new)
|
|
|
|
{
|
|
|
|
$query.= '
|
|
|
|
AND date_metadata_update IS NULL
|
|
|
|
';
|
|
|
|
}
|
|
|
|
$query.= '
|
|
|
|
;';
|
2012-01-03 20:21:13 +00:00
|
|
|
return hash_from_query($query, 'id');
|
2004-08-21 12:52:43 +00:00
|
|
|
}
|
|
|
|
?>
|