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 | 'tags' => array('default' => null), |
---|
36 | ), |
---|
37 | '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.' |
---|
38 | ); |
---|
39 | } |
---|
40 | |
---|
41 | function ws_images_addSimple($params, &$service) |
---|
42 | { |
---|
43 | global $conf; |
---|
44 | if (!is_admin() || is_adviser() ) |
---|
45 | { |
---|
46 | return new PwgError(401, 'Access denied'); |
---|
47 | } |
---|
48 | |
---|
49 | if (!$service->isPost()) |
---|
50 | { |
---|
51 | return new PwgError(405, "This method requires HTTP POST"); |
---|
52 | } |
---|
53 | |
---|
54 | // category |
---|
55 | $params['category'] = (int)$params['category']; |
---|
56 | if ($params['category'] <= 0) |
---|
57 | { |
---|
58 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); |
---|
59 | } |
---|
60 | |
---|
61 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
---|
62 | |
---|
63 | $image_id = add_uploaded_file( |
---|
64 | $_FILES['image']['tmp_name'], |
---|
65 | $_FILES['image']['name'], |
---|
66 | array($params['category']), |
---|
67 | 8 |
---|
68 | ); |
---|
69 | |
---|
70 | $info_columns = array( |
---|
71 | 'name', |
---|
72 | 'author', |
---|
73 | 'comment', |
---|
74 | 'level', |
---|
75 | 'date_creation', |
---|
76 | ); |
---|
77 | |
---|
78 | foreach ($info_columns as $key) |
---|
79 | { |
---|
80 | if (isset($params[$key])) |
---|
81 | { |
---|
82 | $update[$key] = $params[$key]; |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | if (count(array_keys($update)) > 0) |
---|
87 | { |
---|
88 | $update['id'] = $image_id; |
---|
89 | |
---|
90 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
91 | mass_updates( |
---|
92 | IMAGES_TABLE, |
---|
93 | array( |
---|
94 | 'primary' => array('id'), |
---|
95 | 'update' => array_diff(array_keys($update), array('id')) |
---|
96 | ), |
---|
97 | array($update) |
---|
98 | ); |
---|
99 | } |
---|
100 | |
---|
101 | |
---|
102 | if (isset($params['tags']) and !empty($params['tags'])) |
---|
103 | { |
---|
104 | $tag_ids = array(); |
---|
105 | $tag_names = explode(',', $params['tags']); |
---|
106 | foreach ($tag_names as $tag_name) |
---|
107 | { |
---|
108 | $tag_id = tag_id_from_tag_name($tag_name); |
---|
109 | array_push($tag_ids, $tag_id); |
---|
110 | } |
---|
111 | |
---|
112 | add_tags($tag_ids, array($image_id)); |
---|
113 | } |
---|
114 | |
---|
115 | $query = ' |
---|
116 | SELECT id, name, permalink |
---|
117 | FROM '.CATEGORIES_TABLE.' |
---|
118 | WHERE id = '.$params['category'].' |
---|
119 | ;'; |
---|
120 | $result = pwg_query($query); |
---|
121 | $category = pwg_db_fetch_assoc($result); |
---|
122 | |
---|
123 | return array( |
---|
124 | 'image_id' => $image_id, |
---|
125 | 'url' => make_picture_url( |
---|
126 | array( |
---|
127 | 'image_id' => $image_id, |
---|
128 | 'section' => 'categories', |
---|
129 | 'category' => $category |
---|
130 | ) |
---|
131 | ), |
---|
132 | ); |
---|
133 | } |
---|
134 | ?> |
---|