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 | $service->addMethod( |
---|
41 | 'pwg.images.delete', |
---|
42 | 'ws_images_delete', |
---|
43 | array( |
---|
44 | 'image_id'=>array('default'=>0), |
---|
45 | 'pwg_token' => array('default' => null), |
---|
46 | ), |
---|
47 | 'Delete photos. You can give several image_ids, comma separated' |
---|
48 | ); |
---|
49 | |
---|
50 | $service->addMethod( |
---|
51 | 'pwg.categories.delete', |
---|
52 | 'ws_categories_delete', |
---|
53 | array( |
---|
54 | 'category_id'=>array('default'=>0), |
---|
55 | 'pwg_token' => array('default' => null), |
---|
56 | ), |
---|
57 | 'Delete categories. You can give several category_ids, comma separated' |
---|
58 | ); |
---|
59 | |
---|
60 | $service->addMethod( |
---|
61 | 'pwg.categories.move', |
---|
62 | 'ws_categories_move', |
---|
63 | array( |
---|
64 | 'category_id'=>array('default'=>0), |
---|
65 | 'parent'=>array('default'=>0), |
---|
66 | 'pwg_token' => array('default' => null), |
---|
67 | ), |
---|
68 | 'Move categories. You can give several category_ids, comma separated. Set parent as 0 to move to gallery root. Only virtual categories can be moved.' |
---|
69 | ); |
---|
70 | } |
---|
71 | |
---|
72 | function ws_images_addSimple($params, &$service) |
---|
73 | { |
---|
74 | global $conf; |
---|
75 | if (!is_admin() || is_adviser() ) |
---|
76 | { |
---|
77 | return new PwgError(401, 'Access denied'); |
---|
78 | } |
---|
79 | |
---|
80 | if (!$service->isPost()) |
---|
81 | { |
---|
82 | return new PwgError(405, "This method requires HTTP POST"); |
---|
83 | } |
---|
84 | |
---|
85 | // category |
---|
86 | $params['category'] = (int)$params['category']; |
---|
87 | if ($params['category'] <= 0) |
---|
88 | { |
---|
89 | return new PwgError(WS_ERR_INVALID_PARAM, "Invalid category_id"); |
---|
90 | } |
---|
91 | |
---|
92 | prepare_upload_configuration(); |
---|
93 | |
---|
94 | include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php'); |
---|
95 | |
---|
96 | $image_id = add_uploaded_file( |
---|
97 | $_FILES['image']['tmp_name'], |
---|
98 | $_FILES['image']['name'], |
---|
99 | array($params['category']), |
---|
100 | 8 |
---|
101 | ); |
---|
102 | |
---|
103 | $info_columns = array( |
---|
104 | 'name', |
---|
105 | 'author', |
---|
106 | 'comment', |
---|
107 | 'level', |
---|
108 | 'date_creation', |
---|
109 | ); |
---|
110 | |
---|
111 | foreach ($info_columns as $key) |
---|
112 | { |
---|
113 | if (isset($params[$key])) |
---|
114 | { |
---|
115 | $update[$key] = $params[$key]; |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | if (count(array_keys($update)) > 0) |
---|
120 | { |
---|
121 | $update['id'] = $image_id; |
---|
122 | |
---|
123 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
124 | mass_updates( |
---|
125 | IMAGES_TABLE, |
---|
126 | array( |
---|
127 | 'primary' => array('id'), |
---|
128 | 'update' => array_diff(array_keys($update), array('id')) |
---|
129 | ), |
---|
130 | array($update) |
---|
131 | ); |
---|
132 | } |
---|
133 | |
---|
134 | |
---|
135 | if (isset($params['tags']) and !empty($params['tags'])) |
---|
136 | { |
---|
137 | $tag_ids = array(); |
---|
138 | $tag_names = explode(',', $params['tags']); |
---|
139 | foreach ($tag_names as $tag_name) |
---|
140 | { |
---|
141 | $tag_id = tag_id_from_tag_name($tag_name); |
---|
142 | array_push($tag_ids, $tag_id); |
---|
143 | } |
---|
144 | |
---|
145 | add_tags($tag_ids, array($image_id)); |
---|
146 | } |
---|
147 | |
---|
148 | $query = ' |
---|
149 | SELECT id, name, permalink |
---|
150 | FROM '.CATEGORIES_TABLE.' |
---|
151 | WHERE id = '.$params['category'].' |
---|
152 | ;'; |
---|
153 | $result = pwg_query($query); |
---|
154 | $category = pwg_db_fetch_assoc($result); |
---|
155 | |
---|
156 | return array( |
---|
157 | 'image_id' => $image_id, |
---|
158 | 'url' => make_picture_url( |
---|
159 | array( |
---|
160 | 'image_id' => $image_id, |
---|
161 | 'section' => 'categories', |
---|
162 | 'category' => $category |
---|
163 | ) |
---|
164 | ), |
---|
165 | ); |
---|
166 | } |
---|
167 | |
---|
168 | // this function should not be here, this is a code duplication from |
---|
169 | // admin/photos_add.php, unfortunately pwg.images.addSimple needs upload |
---|
170 | // settings to be defined |
---|
171 | function prepare_upload_configuration() |
---|
172 | { |
---|
173 | global $conf; |
---|
174 | |
---|
175 | // automatic fill of configuration parameters |
---|
176 | $upload_form_config = array( |
---|
177 | 'websize_resize' => array( |
---|
178 | 'default' => true, |
---|
179 | 'can_be_null' => false, |
---|
180 | ), |
---|
181 | |
---|
182 | 'websize_maxwidth' => array( |
---|
183 | 'default' => 800, |
---|
184 | 'min' => 100, |
---|
185 | 'max' => 1600, |
---|
186 | 'pattern' => '/^\d+$/', |
---|
187 | 'can_be_null' => true, |
---|
188 | 'error_message' => l10n('The websize maximum width must be a number between %d and %d'), |
---|
189 | ), |
---|
190 | |
---|
191 | 'websize_maxheight' => array( |
---|
192 | 'default' => 600, |
---|
193 | 'min' => 100, |
---|
194 | 'max' => 1200, |
---|
195 | 'pattern' => '/^\d+$/', |
---|
196 | 'can_be_null' => true, |
---|
197 | 'error_message' => l10n('The websize maximum height must be a number between %d and %d'), |
---|
198 | ), |
---|
199 | |
---|
200 | 'websize_quality' => array( |
---|
201 | 'default' => 95, |
---|
202 | 'min' => 50, |
---|
203 | 'max' => 100, |
---|
204 | 'pattern' => '/^\d+$/', |
---|
205 | 'can_be_null' => false, |
---|
206 | 'error_message' => l10n('The websize image quality must be a number between %d and %d'), |
---|
207 | ), |
---|
208 | |
---|
209 | 'thumb_maxwidth' => array( |
---|
210 | 'default' => 128, |
---|
211 | 'min' => 50, |
---|
212 | 'max' => 300, |
---|
213 | 'pattern' => '/^\d+$/', |
---|
214 | 'can_be_null' => false, |
---|
215 | 'error_message' => l10n('The thumbnail maximum width must be a number between %d and %d'), |
---|
216 | ), |
---|
217 | |
---|
218 | 'thumb_maxheight' => array( |
---|
219 | 'default' => 96, |
---|
220 | 'min' => 50, |
---|
221 | 'max' => 300, |
---|
222 | 'pattern' => '/^\d+$/', |
---|
223 | 'can_be_null' => false, |
---|
224 | 'error_message' => l10n('The thumbnail maximum height must be a number between %d and %d'), |
---|
225 | ), |
---|
226 | |
---|
227 | 'thumb_quality' => array( |
---|
228 | 'default' => 95, |
---|
229 | 'min' => 50, |
---|
230 | 'max' => 100, |
---|
231 | 'pattern' => '/^\d+$/', |
---|
232 | 'can_be_null' => false, |
---|
233 | 'error_message' => l10n('The thumbnail image quality must be a number between %d and %d'), |
---|
234 | ), |
---|
235 | ); |
---|
236 | |
---|
237 | $inserts = array(); |
---|
238 | |
---|
239 | foreach ($upload_form_config as $param_shortname => $param) |
---|
240 | { |
---|
241 | $param_name = 'upload_form_'.$param_shortname; |
---|
242 | |
---|
243 | if (!isset($conf[$param_name])) |
---|
244 | { |
---|
245 | $param_value = boolean_to_string($param['default']); |
---|
246 | |
---|
247 | array_push( |
---|
248 | $inserts, |
---|
249 | array( |
---|
250 | 'param' => $param_name, |
---|
251 | 'value' => $param_value, |
---|
252 | ) |
---|
253 | ); |
---|
254 | $conf[$param_name] = $param_value; |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | if (count($inserts) > 0) |
---|
259 | { |
---|
260 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
261 | mass_inserts( |
---|
262 | CONFIG_TABLE, |
---|
263 | array_keys($inserts[0]), |
---|
264 | $inserts |
---|
265 | ); |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | function ws_images_delete($params, &$service) |
---|
270 | { |
---|
271 | global $conf; |
---|
272 | if (!is_admin() || is_adviser() ) |
---|
273 | { |
---|
274 | return new PwgError(401, 'Access denied'); |
---|
275 | } |
---|
276 | |
---|
277 | if (!$service->isPost()) |
---|
278 | { |
---|
279 | return new PwgError(405, "This method requires HTTP POST"); |
---|
280 | } |
---|
281 | |
---|
282 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
283 | { |
---|
284 | return new PwgError(403, 'Invalid security token'); |
---|
285 | } |
---|
286 | |
---|
287 | $params['image_id'] = preg_split( |
---|
288 | '/[\s,;\|]/', |
---|
289 | $params['image_id'], |
---|
290 | -1, |
---|
291 | PREG_SPLIT_NO_EMPTY |
---|
292 | ); |
---|
293 | $params['image_id'] = array_map('intval', $params['image_id']); |
---|
294 | |
---|
295 | $image_ids = array(); |
---|
296 | foreach ($params['image_id'] as $image_id) |
---|
297 | { |
---|
298 | if ($image_id > 0) |
---|
299 | { |
---|
300 | array_push($image_ids, $image_id); |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
305 | delete_elements($params['image_id'], true); |
---|
306 | } |
---|
307 | |
---|
308 | function ws_categories_delete($params, &$service) |
---|
309 | { |
---|
310 | global $conf; |
---|
311 | if (!is_admin() || is_adviser() ) |
---|
312 | { |
---|
313 | return new PwgError(401, 'Access denied'); |
---|
314 | } |
---|
315 | |
---|
316 | if (!$service->isPost()) |
---|
317 | { |
---|
318 | return new PwgError(405, "This method requires HTTP POST"); |
---|
319 | } |
---|
320 | |
---|
321 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
322 | { |
---|
323 | return new PwgError(403, 'Invalid security token'); |
---|
324 | } |
---|
325 | |
---|
326 | $params['category_id'] = preg_split( |
---|
327 | '/[\s,;\|]/', |
---|
328 | $params['category_id'], |
---|
329 | -1, |
---|
330 | PREG_SPLIT_NO_EMPTY |
---|
331 | ); |
---|
332 | $params['category_id'] = array_map('intval', $params['category_id']); |
---|
333 | |
---|
334 | $category_ids = array(); |
---|
335 | foreach ($params['category_id'] as $category_id) |
---|
336 | { |
---|
337 | if ($category_id > 0) |
---|
338 | { |
---|
339 | array_push($category_ids, $category_id); |
---|
340 | } |
---|
341 | } |
---|
342 | |
---|
343 | // We don't want to create orphans. If a photo is belonging to a category |
---|
344 | // that will be deleted and to no other category, we must delete the photo |
---|
345 | // as well. |
---|
346 | // |
---|
347 | // In the future, this algorithm must be integrated into the |
---|
348 | // delete_categories function. |
---|
349 | |
---|
350 | if (count($category_ids) == 0) |
---|
351 | { |
---|
352 | return; |
---|
353 | } |
---|
354 | |
---|
355 | // add sub-category ids to the given ids : if a category is deleted, all |
---|
356 | // sub-categories must be so |
---|
357 | $all_category_ids = get_subcat_ids($category_ids); |
---|
358 | |
---|
359 | $query = ' |
---|
360 | SELECT |
---|
361 | DISTINCT(image_id) |
---|
362 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
363 | WHERE category_id IN ('.implode(',', $all_category_ids).') |
---|
364 | ;'; |
---|
365 | $image_ids_linked = array_from_query($query, 'image_id'); |
---|
366 | |
---|
367 | if (count($image_ids_linked) > 0) |
---|
368 | { |
---|
369 | $query = ' |
---|
370 | SELECT |
---|
371 | DISTINCT(image_id) |
---|
372 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
373 | WHERE image_id IN ('.implode(',', $image_ids_linked).') |
---|
374 | AND category_id NOT IN ('.implode(',', $all_category_ids).') |
---|
375 | ;'; |
---|
376 | $image_ids_not_orphans = array_from_query($query, 'image_id'); |
---|
377 | $image_ids_orphans = array_diff($image_ids_linked, $image_ids_not_orphans); |
---|
378 | |
---|
379 | // print_r($image_ids_not_orphans); exit(); |
---|
380 | } |
---|
381 | |
---|
382 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
383 | delete_categories($category_ids); |
---|
384 | update_global_rank(); |
---|
385 | |
---|
386 | if (isset($image_ids_orphans)) |
---|
387 | { |
---|
388 | delete_elements($image_ids_orphans, true); |
---|
389 | } |
---|
390 | } |
---|
391 | |
---|
392 | function ws_categories_move($params, &$service) |
---|
393 | { |
---|
394 | global $conf, $page; |
---|
395 | |
---|
396 | if (!is_admin() || is_adviser() ) |
---|
397 | { |
---|
398 | return new PwgError(401, 'Access denied'); |
---|
399 | } |
---|
400 | |
---|
401 | if (!$service->isPost()) |
---|
402 | { |
---|
403 | return new PwgError(405, "This method requires HTTP POST"); |
---|
404 | } |
---|
405 | |
---|
406 | if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token']) |
---|
407 | { |
---|
408 | return new PwgError(403, 'Invalid security token'); |
---|
409 | } |
---|
410 | |
---|
411 | $params['category_id'] = preg_split( |
---|
412 | '/[\s,;\|]/', |
---|
413 | $params['category_id'], |
---|
414 | -1, |
---|
415 | PREG_SPLIT_NO_EMPTY |
---|
416 | ); |
---|
417 | $params['category_id'] = array_map('intval', $params['category_id']); |
---|
418 | |
---|
419 | $category_ids = array(); |
---|
420 | foreach ($params['category_id'] as $category_id) |
---|
421 | { |
---|
422 | if ($category_id > 0) |
---|
423 | { |
---|
424 | array_push($category_ids, $category_id); |
---|
425 | } |
---|
426 | } |
---|
427 | |
---|
428 | if (count($category_ids) == 0) |
---|
429 | { |
---|
430 | return new PwgError(403, 'Invalid category_id input parameter, no category to move'); |
---|
431 | } |
---|
432 | |
---|
433 | // we can't move physical categories |
---|
434 | $categories_in_db = array(); |
---|
435 | |
---|
436 | $query = ' |
---|
437 | SELECT |
---|
438 | id, |
---|
439 | name, |
---|
440 | dir |
---|
441 | FROM '.CATEGORIES_TABLE.' |
---|
442 | WHERE id IN ('.implode(',', $category_ids).') |
---|
443 | ;'; |
---|
444 | $result = pwg_query($query); |
---|
445 | while ($row = pwg_db_fetch_assoc($result)) |
---|
446 | { |
---|
447 | $categories_in_db[$row['id']] = $row; |
---|
448 | // we break on error at first physical category detected |
---|
449 | if (!empty($row['dir'])) |
---|
450 | { |
---|
451 | $row['name'] = strip_tags( |
---|
452 | trigger_event( |
---|
453 | 'render_category_name', |
---|
454 | $row['name'], |
---|
455 | 'ws_categories_move' |
---|
456 | ) |
---|
457 | ); |
---|
458 | |
---|
459 | return new PwgError( |
---|
460 | 403, |
---|
461 | sprintf( |
---|
462 | 'Category %s (%u) is not a virtual category, you cannot move it', |
---|
463 | $row['name'], |
---|
464 | $row['id'] |
---|
465 | ) |
---|
466 | ); |
---|
467 | } |
---|
468 | } |
---|
469 | |
---|
470 | if (count($categories_in_db) != count($category_ids)) |
---|
471 | { |
---|
472 | $unknown_category_ids = array_diff($category_ids, array_keys($categories_in_db)); |
---|
473 | |
---|
474 | return new PwgError( |
---|
475 | 403, |
---|
476 | sprintf( |
---|
477 | 'Category %u does not exist', |
---|
478 | $unknown_category_ids[0] |
---|
479 | ) |
---|
480 | ); |
---|
481 | } |
---|
482 | |
---|
483 | // does this parent exists? This check should be made in the |
---|
484 | // move_categories function, not here |
---|
485 | // |
---|
486 | // 0 as parent means "move categories at gallery root" |
---|
487 | if (!is_numeric($params['parent'])) |
---|
488 | { |
---|
489 | return new PwgError(403, 'Invalid parent input parameter'); |
---|
490 | } |
---|
491 | |
---|
492 | if (0 != $params['parent']) { |
---|
493 | $params['parent'] = intval($params['parent']); |
---|
494 | $subcat_ids = get_subcat_ids(array($params['parent'])); |
---|
495 | if (count($subcat_ids) == 0) |
---|
496 | { |
---|
497 | return new PwgError(403, 'Unknown parent category id'); |
---|
498 | } |
---|
499 | } |
---|
500 | |
---|
501 | $page['infos'] = array(); |
---|
502 | $page['errors'] = array(); |
---|
503 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
504 | move_categories($category_ids, $params['parent']); |
---|
505 | |
---|
506 | if (count($page['errors']) != 0) |
---|
507 | { |
---|
508 | return new PwgError(403, implode('; ', $page['errors'])); |
---|
509 | } |
---|
510 | } |
---|
511 | ?> |
---|