source: extensions/UserCollections/include/ws_functions.inc.php @ 20090

Last change on this file since 20090 was 20090, checked in by mistic100, 11 years ago
  • add webservices
  • add mail function
  • add admin list
  • add admin export function
  • try to deal with Gthumb+
  • activate multisize dropdown menu of collection page

TODO : use webservices instead of toggle_image.php

File size: 11.7 KB
Line 
1<?php
2defined('USER_COLLEC_PATH') or die('Hacking attempt!');
3
4function user_collections_ws_add_methods($arr)
5{
6  $service = &$arr[0];
7  global $conf;
8 
9  $service->addMethod(
10    'pwg.collections.create',
11    'ws_collections_create',
12    array(
13      'name' => array(),
14      'user_id' => array('default' => null),
15      'active' => array('default' => 0),
16      'public' => array('default' => 0),
17      ),
18    'Create a new User Collection. If "user_id" is empty, the collection is created for the current user.'
19    );
20   
21  $service->addMethod(
22    'pwg.collections.delete',
23    'ws_collections_delete',
24    array(
25      'col_id' => array(),
26      ),
27    'Delete a User Collection. The current user must be admin or owner of the collection.'
28    );
29   
30  $service->addMethod(
31    'pwg.collections.getList',
32    'ws_collections_getList',
33    array(
34      'col_id' => array('default' => null),
35      'user_id' => array('default' => null),
36      'name' => array('default' => null),
37      'public' => array('default' => null),
38      'active' => array('default' => null),
39      ),
40    'Retrive a list of collections.'
41    );
42   
43  $service->addMethod(
44    'pwg.collections.addImages',
45    'ws_collections_addImages',
46    array(
47      'col_id' => array(),
48      'image_ids' => array('flags'=>WS_PARAM_FORCE_ARRAY),
49      ),
50    'Add images to a collection. The current user must be admin or owner of the collection.'
51    );
52   
53  $service->addMethod(
54    'pwg.collections.removeImages',
55    'ws_collections_removeImages',
56    array(
57      'col_id' => array(),
58      'image_ids' => array('flags'=>WS_PARAM_FORCE_ARRAY),
59      ),
60    'Remove images from a collection. The current user must be admin or owner of the collection.'
61    );
62   
63  $service->addMethod(
64    'pwg.collections.getImages',
65    'ws_collections_getImages',
66    array(
67      'col_id' => array(),
68      'per_page' => array('default'=>100, 'maxValue'=>$conf['ws_max_images_per_page']),
69      'page' => array('default'=>0),
70      'order' => array('default'=>null),
71      ),
72    'Returns elements for the corresponding  collection.'
73    );
74   
75  $service->addMethod(
76    'pwg.collections.getSerialized',
77    'ws_collections_getSerialized',
78    array(
79      'col_id' => array(),
80      'content' => array('default'=>array('id','name','url','path'), 'flags'=>WS_PARAM_FORCE_ARRAY),
81      ),
82    'Returns a serialized version of the collection in CSV.<br>Available options for "content" are : id, file, name, url, path.<br>The return type is plain/text whatever you select as response format.'
83    );
84}
85
86/**
87 * create a new collection
88 */
89function ws_collections_create($params, &$service)
90{
91  global $conf, $user;
92 
93  // check status
94  if (is_a_guest())
95  {
96    return new PwgError(403, 'Forbidden');
97  }
98 
99  // check name
100  if (empty($params['name']))
101  {
102    return new PwgError(WS_ERR_MISSING_PARAM, 'Empty collection name');
103  }
104 
105  // check user id
106  if (!empty($params['user_id']))
107  {
108    if (!is_admin() and $params['user_id'] != $user['id'])
109    {
110      return new PwgError(403, 'Forbidden');
111    }
112    include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php');
113    if (get_username($params['user_id']) === false)
114    {
115      return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid user id');
116    }
117  }
118  else
119  {
120    $params['user_id'] = $user['id'];
121  }
122 
123  // check public
124  if ($params['public'] != 0 and $params['public'] != 1)
125  {
126    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid "public" value, 0 or 1.');
127  }
128  if (!$conf['user_collections']['allow_public'])
129  {
130    $params['public'] = 0;
131  }
132 
133  // check active
134  if ($params['active'] != 0 and $params['active'] != 1)
135  {
136    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid "active" value, 0 or 1.');
137  }
138 
139  $UserCollection = new UserCollection('new', array(), $params['name'], $params['active'], $params['public'], $params['user_id']);
140 
141  $infos = array_change_key_case($UserCollection->getCollectionInfo(), CASE_LOWER);
142 
143  return $infos;
144}
145
146/**
147 * delete a collection
148 */
149function ws_collections_delete($params, &$service)
150{
151  global $user;
152 
153  // check status
154  if (is_a_guest())
155  {
156    return new PwgError(403, 'Forbidden');
157  }
158 
159  // check collection id
160  if (!preg_match('#^[0-9]+$#', $params['col_id']))
161  {
162    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid collection id');
163  }
164 
165  $query = '
166SELECT user_id
167  FROM '.COLLECTIONS_TABLE.'
168  WHERE id = '.$params['col_id'].'
169;';
170  $result = pwg_query($query);
171 
172  if (!pwg_db_num_rows($result))
173  {
174    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid collection id');
175  }
176  else
177  {
178    // check owner
179    list($user_id) = pwg_db_fetch_row($result);
180   
181    if (!is_admin() and $user_id != $user['id'])
182    {
183      return new PwgError(403, 'Forbidden');
184    }
185   
186    // delete
187    $query = '
188DELETE ci, c
189  FROM '.COLLECTION_IMAGES_TABLE.' AS ci
190    RIGHT JOIN '.COLLECTIONS_TABLE.' AS c
191    ON ci.col_id = c.id
192  WHERE
193    c.user_id = '.$user_id.'
194    AND c.id = '.$params['col_id'].'
195;';
196    pwg_query($query);
197  }
198}
199
200/**
201 * get a list of collections
202 */
203function ws_collections_getList($params, &$service)
204{
205  global $user, $conf;
206 
207  // check status
208  if (is_a_guest())
209  {
210    return new PwgError(403, 'Forbidden');
211  }
212 
213  // check user_id
214  if (!empty($params['user_id']))
215  {
216    if (!is_admin() and $params['user_id'] != $user['id'])
217    {
218      return new PwgError(403, 'Forbidden');
219    }
220    include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php');
221    if (get_username($params['user_id']) === false)
222    {
223      return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid user id');
224    }
225  }
226  else if (!is_admin())
227  {
228    $params['user_id'] = $user['id'];
229  }
230 
231  // check collection id
232  if ( !empty($params['col_id']) and !preg_match('#^[0-9]+$#', $params['col_id']) )
233  {
234    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid collection id');
235  }
236 
237  // check public
238  if ( !empty($params['public']) and $params['public'] != 0 and $params['public'] != 1 )
239  {
240    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid "public" value, 0 or 1.');
241  }
242 
243  // check active
244  if ( !empty($params['active']) and $params['active'] != 0 and $params['active'] != 1 )
245  {
246    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid "active" value, 0 or 1.');
247  }
248 
249  // search
250  $where_clauses = array('1=1');
251  if (!empty($params['col_id']))
252  {
253    $where_clauses[] = 'c.id = '.$params['col_id'];
254  }
255  if (!empty($params['user_id']))
256  {
257    $where_clauses[] = 'user_id = '.$params['user_id'];
258  }
259  if (!empty($params['public']))
260  {
261    $where_clauses[] = 'public = '.$params['public'];
262  }
263  if (!empty($params['active']))
264  {
265    $where_clauses[] = 'active = '.$params['active'];
266  }
267  if (!empty($params['name']))
268  {
269    $where_clauses[] = 'name LIKE("%'.pwg_db_real_escape_string($params['name']).'%")';
270  }
271 
272  $query = '
273SELECT
274    c.*,
275    u.'.$conf['user_fields']['username'].' AS username
276  FROM '.COLLECTIONS_TABLE.' AS c
277    INNER JOIN '.USERS_TABLE.' AS u
278    ON c.user_id = u.'.$conf['user_fields']['id'].'
279  WHERE
280    '.implode("\n    AND ", $where_clauses).'
281  ORDER BY username ASC, name ASC
282;';
283  $sets = hash_from_query($query, 'id');
284 
285  $ret = array();
286  foreach ($sets as $row)
287  {
288    $ret[] = array(
289      'id' => $row['id'],
290      'name' => $row['name'],
291      'nb_images' => $row['nb_images'],
292      'active' => (bool)$row['active'],
293      'public' => (bool)$row['public'],
294      'date_creation' => $row['date_creation'],
295      'is_temp' => $row['name'] == 'temp',
296      'u_public' => USER_COLLEC_PUBLIC . 'view/'.$row['public_id'],
297      'user_id' => $row['user_id'],
298      'username' => $row['username'],
299      );
300  }
301 
302  return $ret;
303}
304
305/**
306 * add images to a collection
307 */
308function ws_collections_addImages($params, &$service)
309{
310  global $conf, $user;
311 
312  // check status
313  if (is_a_guest())
314  {
315    return new PwgError(403, 'Forbidden');
316  }
317 
318  // check collection id
319  if (!preg_match('#^[0-9]+$#', $params['col_id']))
320  {
321    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid collection id');
322  }
323
324  try {
325    $UserCollection = new UserCollection($params['col_id']);
326   
327    $UserCollection->addImages($params['image_ids']);
328   
329    return array('nb_images' => $UserCollection->getParam('nb_images'));
330  }
331  catch (Exception $e)
332  {
333    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid collection id');
334  }
335}
336
337/**
338 * remove images from a collection
339 */
340function ws_collections_removeImages($params, &$service)
341{
342  global $conf, $user;
343 
344  // check status
345  if (is_a_guest())
346  {
347    return new PwgError(403, 'Forbidden');
348  }
349 
350  // check collection id
351  if (!preg_match('#^[0-9]+$#', $params['col_id']))
352  {
353    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid collection id');
354  }
355
356  try {
357    $UserCollection = new UserCollection($params['col_id']);
358   
359    $UserCollection->removeImages($params['image_ids']);
360   
361    return array('nb_images' => $UserCollection->getParam('nb_images'));
362  }
363  catch (Exception $e)
364  {
365    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid collection id');
366  }
367}
368
369/**
370 * get images from a collection
371 */
372function ws_collections_getImages($params, &$service)
373{
374  global $conf, $user;
375 
376  // check status
377  if (is_a_guest())
378  {
379    return new PwgError(403, 'Forbidden');
380  }
381 
382  // check collection id
383  if (!preg_match('#^[0-9]+$#', $params['col_id']))
384  {
385    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid collection id');
386  }
387
388  try {
389    $UserCollection = new UserCollection($params['col_id']);
390   
391    $image_ids = $UserCollection->getImages();
392    $images = array();
393   
394    if (!empty($image_ids))
395    {
396      $where_clauses = array();
397      $where_clauses[] = 'i.id IN ('.implode(',', $image_ids ).')';
398      $where_clauses[] = get_sql_condition_FandF( array(
399            'visible_images' => 'i.id'
400          ), null, true
401        );
402
403      $order_by = ws_std_image_sql_order($params, 'i.');
404      $order_by = empty($order_by) ? $conf['order_by'] : 'ORDER BY '.$order_by;
405
406      $query = '
407SELECT i.*
408  FROM '.IMAGES_TABLE.' i
409  WHERE
410    '. implode("\n AND ", $where_clauses).'
411GROUP BY i.id
412'.$order_by.'
413LIMIT '.(int)$params['per_page'].' OFFSET '.(int)($params['per_page']*$params['page']);
414
415    $result = pwg_query($query);
416    while ($row = pwg_db_fetch_assoc($result))
417    {
418      $image = array();
419      foreach (array('id', 'width', 'height', 'hit') as $k)
420      {
421        if (isset($row[$k]))
422        {
423          $image[$k] = (int)$row[$k];
424        }
425      }
426      foreach (array('file', 'name', 'comment', 'date_creation', 'date_available') as $k)
427      {
428        $image[$k] = $row[$k];
429      }
430      $image = array_merge($image, ws_std_get_urls($row));
431     
432      array_push($images, $image);
433    }
434  }
435
436  return array('images' => array(
437    WS_XML_ATTRIBUTES => array(
438      'page' => $params['page'],
439      'per_page' => $params['per_page'],
440      'count' => count($images)
441      ),
442    WS_XML_CONTENT => new PwgNamedArray(
443      $images, 
444      'image', 
445      ws_std_get_image_xml_attributes()
446      ),
447    ));
448  }
449  catch (Exception $e)
450  {
451    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid collection id');
452  }
453}
454
455/**
456 * get serialised collection
457 */
458function ws_collections_getSerialized($params, &$service)
459{
460  global $conf, $user;
461 
462  // check status
463  if (is_a_guest())
464  {
465    return new PwgError(403, 'Forbidden');
466  }
467 
468  // check collection id
469  if (!preg_match('#^[0-9]+$#', $params['col_id']))
470  {
471    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid collection id');
472  }
473
474  try {
475    $UserCollection = new UserCollection($params['col_id']);
476   
477    // change encoder to plain text
478    include_once(USER_COLLEC_PATH.'include/plain_encoder.php');
479    $encoder = new PwgPlainEncoder();
480    $service->setEncoder('plain', $encoder);
481 
482    return $UserCollection->serialize($params['content']);
483  }
484  catch (Exception $e)
485  {
486    return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid collection id');
487  }
488}
489?>
Note: See TracBrowser for help on using the repository browser.