source: extensions/pwg_images_addSimple/main.inc.php @ 7690

Last change on this file since 7690 was 7566, checked in by plg, 13 years ago

add method pwg.categories.move (pwg_token required) with many input checks

File size: 9.4 KB
Line 
1<?php
2/*
3Plugin Name: pwg.images.addSimple
4Version: auto
5Description: A simpler method to add photo with web API, based on HTTP file upload protocol
6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=411
7Author: plg
8Author URI: http://piwigo.wordpress.com
9*/
10
11if (!defined('PHPWG_ROOT_PATH'))
12{
13  die('Hacking attempt!');
14}
15
16add_event_handler('ws_add_methods', 'ws_add_method_addSimple');
17
18function 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
72function 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  include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
93 
94  $image_id = add_uploaded_file(
95    $_FILES['image']['tmp_name'],
96    $_FILES['image']['name'],
97    array($params['category']),
98    8
99    );
100
101  $info_columns = array(
102    'name',
103    'author',
104    'comment',
105    'level',
106    'date_creation',
107    );
108
109  foreach ($info_columns as $key)
110  {
111    if (isset($params[$key]))
112    {
113      $update[$key] = $params[$key];
114    }
115  }
116
117  if (count(array_keys($update)) > 0)
118  {
119    $update['id'] = $image_id;
120
121    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
122    mass_updates(
123      IMAGES_TABLE,
124      array(
125        'primary' => array('id'),
126        'update'  => array_diff(array_keys($update), array('id'))
127        ),
128      array($update)
129      );
130  }
131
132
133  if (isset($params['tags']) and !empty($params['tags']))
134  {
135    $tag_ids = array();
136    $tag_names = explode(',', $params['tags']);
137    foreach ($tag_names as $tag_name)
138    {
139      $tag_id = tag_id_from_tag_name($tag_name);
140      array_push($tag_ids, $tag_id);
141    }
142
143    add_tags($tag_ids, array($image_id));
144  }
145
146  $query = '
147SELECT id, name, permalink
148  FROM '.CATEGORIES_TABLE.'
149  WHERE id = '.$params['category'].'
150;';
151  $result = pwg_query($query);
152  $category = pwg_db_fetch_assoc($result);
153
154  return array(
155    'image_id' => $image_id,
156    'url' => make_picture_url(
157      array(
158        'image_id' => $image_id,
159        'section' => 'categories',
160        'category' => $category
161        )
162      ),
163    );
164}
165
166function ws_images_delete($params, &$service)
167{
168  global $conf;
169  if (!is_admin() || is_adviser() )
170  {
171    return new PwgError(401, 'Access denied');
172  }
173
174  if (!$service->isPost())
175  {
176    return new PwgError(405, "This method requires HTTP POST");
177  }
178
179  if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token'])
180  {
181    return new PwgError(403, 'Invalid security token');
182  }
183
184  $params['image_id'] = preg_split(
185    '/[\s,;\|]/',
186    $params['image_id'],
187    -1,
188    PREG_SPLIT_NO_EMPTY
189    );
190  $params['image_id'] = array_map('intval', $params['image_id']);
191
192  $image_ids = array();
193  foreach ($params['image_id'] as $image_id)
194  {
195    if ($image_id > 0)
196    {
197      array_push($image_ids, $image_id);
198    }
199  }
200
201  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
202  delete_elements($params['image_id'], true);
203}
204
205function ws_categories_delete($params, &$service)
206{
207  global $conf;
208  if (!is_admin() || is_adviser() )
209  {
210    return new PwgError(401, 'Access denied');
211  }
212
213  if (!$service->isPost())
214  {
215    return new PwgError(405, "This method requires HTTP POST");
216  }
217
218  if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token'])
219  {
220    return new PwgError(403, 'Invalid security token');
221  }
222
223  $params['category_id'] = preg_split(
224    '/[\s,;\|]/',
225    $params['category_id'],
226    -1,
227    PREG_SPLIT_NO_EMPTY
228    );
229  $params['category_id'] = array_map('intval', $params['category_id']);
230
231  $category_ids = array();
232  foreach ($params['category_id'] as $category_id)
233  {
234    if ($category_id > 0)
235    {
236      array_push($category_ids, $category_id);
237    }
238  }
239
240  // We don't want to create orphans. If a photo is belonging to a category
241  // that will be deleted and to no other category, we must delete the photo
242  // as well.
243  //
244  // In the future, this algorithm must be integrated into the
245  // delete_categories function.
246 
247  if (count($category_ids) == 0)
248  {
249    return;
250  }
251
252  // add sub-category ids to the given ids : if a category is deleted, all
253  // sub-categories must be so
254  $all_category_ids = get_subcat_ids($category_ids);
255
256  $query = '
257SELECT
258    DISTINCT(image_id)
259  FROM '.IMAGE_CATEGORY_TABLE.'
260  WHERE category_id IN ('.implode(',', $all_category_ids).')
261;';
262  $image_ids_linked = array_from_query($query, 'image_id');
263
264  $query = '
265SELECT
266    DISTINCT(image_id)
267  FROM '.IMAGE_CATEGORY_TABLE.'
268  WHERE image_id IN ('.implode(',', $image_ids_linked).')
269    AND category_id NOT IN ('.implode(',', $all_category_ids).')
270;';
271  $image_ids_not_orphans = array_from_query($query, 'image_id');
272
273  $image_ids_orphans = array_diff($image_ids_linked, $image_ids_not_orphans);
274 
275  // print_r($image_ids_not_orphans); exit();
276
277  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
278  delete_categories($category_ids);
279  update_global_rank();
280
281  delete_elements($image_ids_orphans, true);
282}
283
284function ws_categories_move($params, &$service)
285{
286  global $conf, $page;
287 
288  if (!is_admin() || is_adviser() )
289  {
290    return new PwgError(401, 'Access denied');
291  }
292
293  if (!$service->isPost())
294  {
295    return new PwgError(405, "This method requires HTTP POST");
296  }
297
298  if (empty($params['pwg_token']) or get_pwg_token() != $params['pwg_token'])
299  {
300    return new PwgError(403, 'Invalid security token');
301  }
302
303  $params['category_id'] = preg_split(
304    '/[\s,;\|]/',
305    $params['category_id'],
306    -1,
307    PREG_SPLIT_NO_EMPTY
308    );
309  $params['category_id'] = array_map('intval', $params['category_id']);
310
311  $category_ids = array();
312  foreach ($params['category_id'] as $category_id)
313  {
314    if ($category_id > 0)
315    {
316      array_push($category_ids, $category_id);
317    }
318  }
319
320  if (count($category_ids) == 0)
321  {
322    return new PwgError(403, 'Invalid category_id input parameter, no category to move');
323  }
324
325  // we can't move physical categories
326  $categories_in_db = array();
327 
328  $query = '
329SELECT
330    id,
331    name,
332    dir
333  FROM '.CATEGORIES_TABLE.'
334  WHERE id IN ('.implode(',', $category_ids).')
335;';
336  $result = pwg_query($query);
337  while ($row = pwg_db_fetch_assoc($result))
338  {
339    $categories_in_db[$row['id']] = $row;
340    // we break on error at first physical category detected
341    if (!empty($row['dir']))
342    {
343      $row['name'] = strip_tags(
344        trigger_event(
345          'render_category_name',
346          $row['name'],
347          'ws_categories_move'
348          )
349        );
350     
351      return new PwgError(
352        403,
353        sprintf(
354          'Category %s (%u) is not a virtual category, you cannot move it',
355          $row['name'],
356          $row['id']
357          )
358        );
359    }
360  }
361
362  if (count($categories_in_db) != count($category_ids))
363  {
364    $unknown_category_ids = array_diff($category_ids, array_keys($categories_in_db));
365   
366    return new PwgError(
367      403,
368      sprintf(
369        'Category %u does not exist',
370        $unknown_category_ids[0]
371        )
372      );
373  }
374
375  // does this parent exists? This check should be made in the
376  // move_categories function, not here
377  //
378  // 0 as parent means "move categories at gallery root"
379  if (!is_numeric($params['parent']))
380  {
381    return new PwgError(403, 'Invalid parent input parameter');
382  }
383 
384  if (0 != $params['parent']) {
385    $params['parent'] = intval($params['parent']);
386    $subcat_ids = get_subcat_ids(array($params['parent']));
387    if (count($subcat_ids) == 0)
388    {
389      return new PwgError(403, 'Unknown parent category id');
390    }
391  }
392
393  $page['infos'] = array();
394  $page['errors'] = array();
395  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
396  move_categories($category_ids, $params['parent']);
397
398  if (count($page['errors']) != 0)
399  {
400    return new PwgError(403, implode('; ', $page['errors']));
401  }
402}
403?>
Note: See TracBrowser for help on using the repository browser.