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

Last change on this file since 23719 was 23719, checked in by mistic100, 11 years ago

fix mysql error when no photos on the page, remove "create collection" link in menu

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