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

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

new system for shares : password protection, link timeout, management popup + for mails
handle lightbox conflicts
menublock is visible by AMM

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