aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorplegall <plg@piwigo.org>2010-12-23 10:22:51 +0000
committerplegall <plg@piwigo.org>2010-12-23 10:22:51 +0000
commit16371ed5fd9f0df6e8c3d6d349845bc2ec057081 (patch)
tree72de44930e073221bae0f0145f3c6c3b9ef46f5c
parent732eb6bfba05cd1969fb5898e5bc50c978b2ff0b (diff)
feature 2083 added: implement method pwg.images.addSimple in core
makes admin/include/function_upload.inc.php not dependant from include/ws_functions.inc.php (moves functions file_path_for_type and ready_for_upload_message) cleaner method to initialize the upload settings git-svn-id: http://piwigo.org/svn/trunk@8249 68402e56-0260-453c-a942-63ccdbb3a9ee
-rw-r--r--admin/include/functions_upload.inc.php288
-rw-r--r--admin/photos_add.php134
-rw-r--r--include/ws_functions.inc.php159
-rw-r--r--ws.php17
4 files changed, 377 insertions, 221 deletions
diff --git a/admin/include/functions_upload.inc.php b/admin/include/functions_upload.inc.php
index 7eaeb90f5..2dc21d7bf 100644
--- a/admin/include/functions_upload.inc.php
+++ b/admin/include/functions_upload.inc.php
@@ -1,30 +1,197 @@
<?php
-// TODO
-// * check md5sum (already exists?)
+// +-----------------------------------------------------------------------+
+// | Piwigo - a PHP based picture gallery |
+// +-----------------------------------------------------------------------+
+// | Copyright(C) 2008-2010 Piwigo Team http://piwigo.org |
+// | 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 |
+// | 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. |
+// +-----------------------------------------------------------------------+
-include_once(PHPWG_ROOT_PATH.'include/common.inc.php');
-include_once(PHPWG_ROOT_PATH.'include/ws_functions.inc.php');
include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
-// Here is the plan
-//
-// 1) move uploaded file to upload/2010/01/22/20100122003814-449ada00.jpg
-//
-// 2) if taller than max_height or wider than max_width, move to pwg_high
-// + web sized creation
-//
-// 3) thumbnail creation from web sized
-//
-// 4) register in database
-
// add default event handler for image and thumbnail resize
add_event_handler('upload_image_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 7);
add_event_handler('upload_thumbnail_resize', 'pwg_image_resize', EVENT_HANDLER_PRIORITY_NEUTRAL, 7);
-function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null)
+function get_upload_form_config()
+{
+ // default configuration for upload
+ $upload_form_config = array(
+ 'websize_resize' => array(
+ 'default' => true,
+ 'can_be_null' => false,
+ ),
+
+ 'websize_maxwidth' => array(
+ 'default' => 800,
+ 'min' => 100,
+ 'max' => 1600,
+ 'pattern' => '/^\d+$/',
+ 'can_be_null' => true,
+ 'error_message' => l10n('The websize maximum width must be a number between %d and %d'),
+ ),
+
+ 'websize_maxheight' => array(
+ 'default' => 600,
+ 'min' => 100,
+ 'max' => 1200,
+ 'pattern' => '/^\d+$/',
+ 'can_be_null' => true,
+ 'error_message' => l10n('The websize maximum height must be a number between %d and %d'),
+ ),
+
+ 'websize_quality' => array(
+ 'default' => 95,
+ 'min' => 50,
+ 'max' => 100,
+ 'pattern' => '/^\d+$/',
+ 'can_be_null' => false,
+ 'error_message' => l10n('The websize image quality must be a number between %d and %d'),
+ ),
+
+ 'thumb_maxwidth' => array(
+ 'default' => 128,
+ 'min' => 50,
+ 'max' => 300,
+ 'pattern' => '/^\d+$/',
+ 'can_be_null' => false,
+ 'error_message' => l10n('The thumbnail maximum width must be a number between %d and %d'),
+ ),
+
+ 'thumb_maxheight' => array(
+ 'default' => 96,
+ 'min' => 50,
+ 'max' => 300,
+ 'pattern' => '/^\d+$/',
+ 'can_be_null' => false,
+ 'error_message' => l10n('The thumbnail maximum height must be a number between %d and %d'),
+ ),
+
+ 'thumb_quality' => array(
+ 'default' => 95,
+ 'min' => 50,
+ 'max' => 100,
+ 'pattern' => '/^\d+$/',
+ 'can_be_null' => false,
+ 'error_message' => l10n('The thumbnail image quality must be a number between %d and %d'),
+ ),
+
+ 'hd_keep' => array(
+ 'default' => true,
+ 'can_be_null' => false,
+ ),
+
+ 'hd_resize' => array(
+ 'default' => false,
+ 'can_be_null' => false,
+ ),
+
+ 'hd_maxwidth' => array(
+ 'default' => 2000,
+ 'min' => 500,
+ 'max' => 20000,
+ 'pattern' => '/^\d+$/',
+ 'can_be_null' => false,
+ 'error_message' => l10n('The high definition maximum width must be a number between %d and %d'),
+ ),
+
+ 'hd_maxheight' => array(
+ 'default' => 2000,
+ 'min' => 500,
+ 'max' => 20000,
+ 'pattern' => '/^\d+$/',
+ 'can_be_null' => false,
+ 'error_message' => l10n('The high definition maximum height must be a number between %d and %d'),
+ ),
+
+ 'hd_quality' => array(
+ 'default' => 95,
+ 'min' => 50,
+ 'max' => 100,
+ 'pattern' => '/^\d+$/',
+ 'can_be_null' => false,
+ 'error_message' => l10n('The high definition image quality must be a number between %d and %d'),
+ ),
+ );
+
+ return $upload_form_config;
+}
+
+/*
+ * automatic fill of configuration parameters
+ */
+function prepare_upload_configuration()
{
global $conf;
+ $inserts = array();
+
+ foreach (get_upload_form_config() as $param_shortname => $param)
+ {
+ $param_name = 'upload_form_'.$param_shortname;
+
+ if (!isset($conf[$param_name]))
+ {
+ $param_value = boolean_to_string($param['default']);
+
+ array_push(
+ $inserts,
+ array(
+ 'param' => $param_name,
+ 'value' => $param_value,
+ )
+ );
+
+ $conf[$param_name] = $param_value;
+ if (is_bool($param['default']))
+ {
+ $conf[$param_name] = get_boolean($param_value);
+ }
+ }
+ }
+
+ if (count($inserts) > 0)
+ {
+ mass_inserts(
+ CONFIG_TABLE,
+ array_keys($inserts[0]),
+ $inserts
+ );
+ }
+}
+
+function add_uploaded_file($source_filepath, $original_filename=null, $categories=null, $level=null)
+{
+ // Here is the plan
+ //
+ // 1) move uploaded file to upload/2010/01/22/20100122003814-449ada00.jpg
+ //
+ // 2) if taller than max_height or wider than max_width, move to pwg_high
+ // + web sized creation
+ //
+ // 3) thumbnail creation from web sized
+ //
+ // 4) register in database
+
+ // TODO
+ // * check md5sum (already exists?)
+
+ global $conf;
+
// current date
list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
list($year, $month, $day) = preg_split('/[^\d]/', $dbnow, 4);
@@ -89,20 +256,23 @@ function add_uploaded_file($source_filepath, $original_filename=null, $categorie
{
if ($conf['upload_form_hd_keep'])
{
- $need_resize = need_resize($high_path, $conf['upload_form_hd_maxwidth'], $conf['upload_form_hd_maxheight']);
-
- if ($conf['upload_form_hd_resize'] and $need_resize)
+ if ($conf['upload_form_hd_resize'])
{
- pwg_image_resize(
- false,
- $high_path,
- $high_path,
- $conf['upload_form_hd_maxwidth'],
- $conf['upload_form_hd_maxheight'],
- $conf['upload_form_hd_quality'],
- false
- );
- $high_infos = pwg_image_infos($high_path);
+ $need_resize = need_resize($high_path, $conf['upload_form_hd_maxwidth'], $conf['upload_form_hd_maxheight']);
+
+ if ($need_resize)
+ {
+ pwg_image_resize(
+ false,
+ $high_path,
+ $high_path,
+ $conf['upload_form_hd_maxwidth'],
+ $conf['upload_form_hd_maxheight'],
+ $conf['upload_form_hd_quality'],
+ false
+ );
+ $high_infos = pwg_image_infos($high_path);
+ }
}
}
else
@@ -557,4 +727,64 @@ function is_imagick()
return false;
}
+
+function ready_for_upload_message()
+{
+ global $conf;
+
+ $relative_dir = preg_replace('#^'.PHPWG_ROOT_PATH.'#', '', $conf['upload_dir']);
+
+ if (!is_dir($conf['upload_dir']))
+ {
+ if (!is_writable(dirname($conf['upload_dir'])))
+ {
+ return sprintf(
+ l10n('Create the "%s" directory at the root of your Piwigo installation'),
+ $relative_dir
+ );
+ }
+ }
+ else
+ {
+ if (!is_writable($conf['upload_dir']))
+ {
+ @chmod($conf['upload_dir'], 0777);
+
+ if (!is_writable($conf['upload_dir']))
+ {
+ return sprintf(
+ l10n('Give write access (chmod 777) to "%s" directory at the root of your Piwigo installation'),
+ $relative_dir
+ );
+ }
+ }
+ }
+
+ return null;
+}
+
+function file_path_for_type($file_path, $type='thumb')
+{
+ // resolve the $file_path depending on the $type
+ if ('thumb' == $type) {
+ $file_path = get_thumbnail_location(
+ array(
+ 'path' => $file_path,
+ 'tn_ext' => 'jpg',
+ )
+ );
+ }
+
+ if ('high' == $type) {
+ @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
+ $file_path = get_high_location(
+ array(
+ 'path' => $file_path,
+ 'has_high' => 'true'
+ )
+ );
+ }
+
+ return $file_path;
+}
?> \ No newline at end of file
diff --git a/admin/photos_add.php b/admin/photos_add.php
index 889395985..a4db30bf2 100644
--- a/admin/photos_add.php
+++ b/admin/photos_add.php
@@ -43,134 +43,9 @@ check_status(ACCESS_ADMINISTRATOR);
// | Load configuration |
// +-----------------------------------------------------------------------+
-// automatic fill of configuration parameters
-$upload_form_config = array(
- 'websize_resize' => array(
- 'default' => true,
- 'can_be_null' => false,
- ),
-
- 'websize_maxwidth' => array(
- 'default' => 800,
- 'min' => 100,
- 'max' => 1600,
- 'pattern' => '/^\d+$/',
- 'can_be_null' => true,
- 'error_message' => l10n('The websize maximum width must be a number between %d and %d'),
- ),
-
- 'websize_maxheight' => array(
- 'default' => 600,
- 'min' => 100,
- 'max' => 1200,
- 'pattern' => '/^\d+$/',
- 'can_be_null' => true,
- 'error_message' => l10n('The websize maximum height must be a number between %d and %d'),
- ),
-
- 'websize_quality' => array(
- 'default' => 95,
- 'min' => 50,
- 'max' => 100,
- 'pattern' => '/^\d+$/',
- 'can_be_null' => false,
- 'error_message' => l10n('The websize image quality must be a number between %d and %d'),
- ),
-
- 'thumb_maxwidth' => array(
- 'default' => 128,
- 'min' => 50,
- 'max' => 300,
- 'pattern' => '/^\d+$/',
- 'can_be_null' => false,
- 'error_message' => l10n('The thumbnail maximum width must be a number between %d and %d'),
- ),
-
- 'thumb_maxheight' => array(
- 'default' => 96,
- 'min' => 50,
- 'max' => 300,
- 'pattern' => '/^\d+$/',
- 'can_be_null' => false,
- 'error_message' => l10n('The thumbnail maximum height must be a number between %d and %d'),
- ),
-
- 'thumb_quality' => array(
- 'default' => 95,
- 'min' => 50,
- 'max' => 100,
- 'pattern' => '/^\d+$/',
- 'can_be_null' => false,
- 'error_message' => l10n('The thumbnail image quality must be a number between %d and %d'),
- ),
-
- 'hd_keep' => array(
- 'default' => true,
- 'can_be_null' => false,
- ),
-
- 'hd_resize' => array(
- 'default' => false,
- 'can_be_null' => false,
- ),
-
- 'hd_maxwidth' => array(
- 'default' => 2000,
- 'min' => 500,
- 'max' => 20000,
- 'pattern' => '/^\d+$/',
- 'can_be_null' => false,
- 'error_message' => l10n('The high definition maximum width must be a number between %d and %d'),
- ),
-
- 'hd_maxheight' => array(
- 'default' => 2000,
- 'min' => 500,
- 'max' => 20000,
- 'pattern' => '/^\d+$/',
- 'can_be_null' => false,
- 'error_message' => l10n('The high definition maximum height must be a number between %d and %d'),
- ),
-
- 'hd_quality' => array(
- 'default' => 95,
- 'min' => 50,
- 'max' => 100,
- 'pattern' => '/^\d+$/',
- 'can_be_null' => false,
- 'error_message' => l10n('The high definition image quality must be a number between %d and %d'),
- ),
- );
-
-$inserts = array();
-
-foreach ($upload_form_config as $param_shortname => $param)
-{
- $param_name = 'upload_form_'.$param_shortname;
-
- if (!isset($conf[$param_name]))
- {
- $param_value = boolean_to_string($param['default']);
-
- array_push(
- $inserts,
- array(
- 'param' => $param_name,
- 'value' => $param_value,
- )
- );
- $conf[$param_name] = $param_value;
- }
-}
+prepare_upload_configuration();
-if (count($inserts) > 0)
-{
- mass_inserts(
- CONFIG_TABLE,
- array_keys($inserts[0]),
- $inserts
- );
-}
+$upload_form_config = get_upload_form_config();
// +-----------------------------------------------------------------------+
// | Tabs |
@@ -238,11 +113,6 @@ $template->set_filenames(
)
);
-// $template->append(
-// 'head_elements',
-// '<link rel="stylesheet" type="text/css" href="'.UPLOAD_FORM_PATH.'upload.css">'."\n"
-// );
-
// +-----------------------------------------------------------------------+
// | Load the tab |
// +-----------------------------------------------------------------------+
diff --git a/include/ws_functions.inc.php b/include/ws_functions.inc.php
index c46ae6e26..91e73e8a2 100644
--- a/include/ws_functions.inc.php
+++ b/include/ws_functions.inc.php
@@ -1076,6 +1076,8 @@ function merge_chunks($output_filepath, $original_sum, $type)
*/
function add_file($file_path, $type, $original_sum, $file_sum)
{
+ include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
+
$file_path = file_path_for_type($file_path, $type);
$upload_dir = dirname($file_path);
@@ -1337,6 +1339,101 @@ SELECT
invalidate_user_cache();
}
+function ws_images_addSimple($params, &$service)
+{
+ global $conf;
+ if (!is_admin() || is_adviser() )
+ {
+ return new PwgError(401, 'Access denied');
+ }
+
+ if (!$service->isPost())
+ {
+ return new PwgError(405, "This method requires HTTP POST");
+ }
+
+ // category
+ $params['category'] = (int)$params['category'];
+ if ($params['category'] <= 0)
+ {
+ return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id");
+ }
+
+ include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
+ prepare_upload_configuration();
+
+ $image_id = add_uploaded_file(
+ $_FILES['image']['tmp_name'],
+ $_FILES['image']['name'],
+ array($params['category']),
+ 8
+ );
+
+ $info_columns = array(
+ 'name',
+ 'author',
+ 'comment',
+ 'level',
+ 'date_creation',
+ );
+
+ foreach ($info_columns as $key)
+ {
+ if (isset($params[$key]))
+ {
+ $update[$key] = $params[$key];
+ }
+ }
+
+ if (count(array_keys($update)) > 0)
+ {
+ $update['id'] = $image_id;
+
+ include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
+ mass_updates(
+ IMAGES_TABLE,
+ array(
+ 'primary' => array('id'),
+ 'update' => array_diff(array_keys($update), array('id'))
+ ),
+ array($update)
+ );
+ }
+
+
+ if (isset($params['tags']) and !empty($params['tags']))
+ {
+ $tag_ids = array();
+ $tag_names = explode(',', $params['tags']);
+ foreach ($tag_names as $tag_name)
+ {
+ $tag_id = tag_id_from_tag_name($tag_name);
+ array_push($tag_ids, $tag_id);
+ }
+
+ add_tags($tag_ids, array($image_id));
+ }
+
+ $query = '
+SELECT id, name, permalink
+ FROM '.CATEGORIES_TABLE.'
+ WHERE id = '.$params['category'].'
+;';
+ $result = pwg_query($query);
+ $category = pwg_db_fetch_assoc($result);
+
+ return array(
+ 'image_id' => $image_id,
+ 'url' => make_picture_url(
+ array(
+ 'image_id' => $image_id,
+ 'section' => 'categories',
+ 'category' => $category
+ )
+ ),
+ );
+}
+
/**
* perform a login (web service method)
*/
@@ -1744,6 +1841,7 @@ SELECT
}
if (isset($params[$param_name.'_sum'])) {
+ include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
$type_path = file_path_for_type($path, $type);
if (!is_file($type_path)) {
$ret[$param_name] = 'missing';
@@ -1762,31 +1860,6 @@ SELECT
return $ret;
}
-function file_path_for_type($file_path, $type='thumb')
-{
- // resolve the $file_path depending on the $type
- if ('thumb' == $type) {
- $file_path = get_thumbnail_location(
- array(
- 'path' => $file_path,
- 'tn_ext' => 'jpg',
- )
- );
- }
-
- if ('high' == $type) {
- @include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php');
- $file_path = get_high_location(
- array(
- 'path' => $file_path,
- 'has_high' => 'true'
- )
- );
- }
-
- return $file_path;
-}
-
function ws_images_setInfo($params, &$service)
{
global $conf;
@@ -2152,6 +2225,7 @@ function ws_images_checkUpload($params, &$service)
return new PwgError(401, 'Access denied');
}
+ include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
$ret['message'] = ready_for_upload_message();
$ret['ready_for_upload'] = true;
@@ -2162,39 +2236,4 @@ function ws_images_checkUpload($params, &$service)
return $ret;
}
-
-function ready_for_upload_message()
-{
- global $conf;
-
- $relative_dir = preg_replace('#^'.PHPWG_ROOT_PATH.'#', '', $conf['upload_dir']);
-
- if (!is_dir($conf['upload_dir']))
- {
- if (!is_writable(dirname($conf['upload_dir'])))
- {
- return sprintf(
- l10n('Create the "%s" directory at the root of your Piwigo installation'),
- $relative_dir
- );
- }
- }
- else
- {
- if (!is_writable($conf['upload_dir']))
- {
- @chmod($conf['upload_dir'], 0777);
-
- if (!is_writable($conf['upload_dir']))
- {
- return sprintf(
- l10n('Give write access (chmod 777) to "%s" directory at the root of your Piwigo installation'),
- $relative_dir
- );
- }
- }
- }
-
- return null;
-}
?>
diff --git a/ws.php b/ws.php
index 1287a3c12..33006d413 100644
--- a/ws.php
+++ b/ws.php
@@ -221,6 +221,23 @@ function ws_addDefaultMethods( $arr )
);
$service->addMethod(
+ 'pwg.images.addSimple',
+ 'ws_images_addSimple',
+ array(
+ 'category' => array('default' => null),
+ 'name' => array('default' => null),
+ 'author' => array('default' => null),
+ 'comment' => array('default' => null),
+ 'level' => array(
+ 'default' => 0,
+ 'maxValue' => $conf['available_permission_levels']
+ ),
+ 'tags' => array('default' => null),
+ ),
+ 'POST method only.<br>Use the <b>image</b> field for uploading file.<br>Set the form encoding to "form-data"<br><b>category</b> is the numeric identifier of the destination category.'
+ );
+
+ $service->addMethod(
'pwg.categories.getAdminList',
'ws_categories_getAdminList',
array(),