1 | <?php |
---|
2 | /* |
---|
3 | Plugin Name: pwg.images.addSimple |
---|
4 | Version: auto |
---|
5 | Description: A simpler method to add photo with web API, based on HTTP file upload protocol |
---|
6 | Plugin URI: http://piwigo.org/ext/extension_view.php?eid=411 |
---|
7 | Author: plg |
---|
8 | Author URI: http://piwigo.wordpress.com |
---|
9 | */ |
---|
10 | |
---|
11 | if (!defined('PHPWG_ROOT_PATH')) |
---|
12 | { |
---|
13 | die('Hacking attempt!'); |
---|
14 | } |
---|
15 | |
---|
16 | add_event_handler('ws_add_methods', 'ws_add_method_addSimple'); |
---|
17 | |
---|
18 | function ws_add_method_addSimple($arr) |
---|
19 | { |
---|
20 | global $conf; |
---|
21 | $service = &$arr[0]; |
---|
22 | |
---|
23 | $service->addMethod( |
---|
24 | 'pwg.images.addSimple', |
---|
25 | 'ws_images_addSimple', |
---|
26 | array( |
---|
27 | 'category' => array('default' => null), |
---|
28 | 'name' => array('default' => null), |
---|
29 | 'author' => array('default' => null), |
---|
30 | 'comment' => array('default' => null), |
---|
31 | 'level' => array( |
---|
32 | 'default' => 0, |
---|
33 | 'maxValue' => $conf['available_permission_levels'] |
---|
34 | ), |
---|
35 | ), |
---|
36 | '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.' |
---|
37 | ); |
---|
38 | } |
---|
39 | |
---|
40 | function ws_images_addSimple($params, &$service) |
---|
41 | { |
---|
42 | global $conf; |
---|
43 | if (!is_admin() || is_adviser() ) |
---|
44 | { |
---|
45 | return new PwgError(401, 'Access denied'); |
---|
46 | } |
---|
47 | |
---|
48 | if (!$service->isPost()) |
---|
49 | { |
---|
50 | return new PwgError(405, "This method requires HTTP POST"); |
---|
51 | } |
---|
52 | |
---|
53 | // category |
---|
54 | $params['category'] = (int)$params['category']; |
---|
55 | if ($params['category'] <= 0) |
---|
56 | { |
---|
57 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); |
---|
58 | } |
---|
59 | |
---|
60 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
---|
61 | |
---|
62 | $image_id = add_uploaded_file( |
---|
63 | $_FILES['image']['tmp_name'], |
---|
64 | $_FILES['image']['name'], |
---|
65 | array($params['category']), |
---|
66 | 8 |
---|
67 | ); |
---|
68 | |
---|
69 | $info_columns = array( |
---|
70 | 'name', |
---|
71 | 'author', |
---|
72 | 'comment', |
---|
73 | 'level', |
---|
74 | 'date_creation', |
---|
75 | ); |
---|
76 | |
---|
77 | foreach ($info_columns as $key) |
---|
78 | { |
---|
79 | if (isset($params[$key])) |
---|
80 | { |
---|
81 | $update[$key] = $params[$key]; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | if (count(array_keys($update)) > 0) |
---|
86 | { |
---|
87 | $update['id'] = $image_id; |
---|
88 | |
---|
89 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
90 | mass_updates( |
---|
91 | IMAGES_TABLE, |
---|
92 | array( |
---|
93 | 'primary' => array('id'), |
---|
94 | 'update' => array_diff(array_keys($update), array('id')) |
---|
95 | ), |
---|
96 | array($update) |
---|
97 | ); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | if (isset($params['tags'])) |
---|
102 | { |
---|
103 | $tag_ids = array(); |
---|
104 | $tag_names = explode(',', $params['tags']); |
---|
105 | foreach ($tag_names as $tag_name) |
---|
106 | { |
---|
107 | $tag_id = tag_id_from_tag_name($tag_name); |
---|
108 | array_push($tag_ids, $tag_id); |
---|
109 | } |
---|
110 | |
---|
111 | add_tags($tag_ids, array($image_id)); |
---|
112 | } |
---|
113 | } |
---|
114 | ?> |
---|