source: trunk/include/ws_functions/pwg.users.php @ 25358

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

splits ws_functions.inc.php in 8 files + comments + code cleaning

File size: 12.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2013 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24/**
25 * API method
26 * Returns a list of users
27 * @param mixed[] $params
28 *    @option int[] user_id (optional)
29 *    @option string username (optional)
30 *    @option string[] status (optional)
31 *    @option int min_level (optional)
32 *    @option int[] group_id (optional)
33 *    @option int per_page
34 *    @option int page
35 *    @option string order
36 */
37function ws_users_getList($params, &$service)
38{
39  global $conf;
40
41  $where_clauses = array('1=1');
42
43  if (!empty($params['user_id']))
44  {
45    $where_clauses[] = 'u.'.$conf['user_fields']['id'].' IN('. implode(',', $params['user_id']) .')';
46  }
47
48  if (!empty($params['username']))
49  {
50    $where_clauses[] = 'u.'.$conf['user_fields']['username'].' LIKE \''.pwg_db_real_escape_string($params['username']).'\'';
51  }
52
53  if (!empty($params['status']))
54  {
55    $params['status'] = array_intersect($params['status'], get_enums(USER_INFOS_TABLE, 'status'));
56    if (count($params['status']) > 0)
57    {
58      $where_clauses[] = 'ui.status IN("'. implode('","', $params['status']) .'")';
59    }
60  }
61
62  if (!empty($params['min_level']))
63  {
64    if ( !in_array($params['min_level'], $conf['available_permission_levels']) )
65    {
66      return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid level');
67    }
68    $where_clauses[] = 'ui.level >= '.$params['min_level'];
69  }
70
71  if (!empty($params['group_id']))
72  {
73    $where_clauses[] = 'ug.group_id IN('. implode(',', $params['group_id']) .')';
74  }
75
76  $display = array('u.'.$conf['user_fields']['id'] => 'id');
77
78  if ($params['display'] != 'none')
79  {
80    $params['display'] = explode(',', $params['display']);
81
82    if (in_array('all', $params['display']))
83    {
84      $params['display'] = array_merge($params['display'], array(
85        'username','email','status','level','groups','language','theme',
86        'nb_image_page','recent_period','expand','show_nb_comments','show_nb_hits',
87        'enabled_high',
88        ));
89    }
90    else if (in_array('basics', $params['display']))
91    {
92      $params['display'] = array_merge($params['display'], array(
93        'username','email','status','level','groups',
94        ));
95    }
96
97    if (in_array('username', $params['display']))
98    {
99      $display['u.'.$conf['user_fields']['username']] = 'username';
100    }
101    if (in_array('email', $params['display']))
102    {
103      $display['u.'.$conf['user_fields']['email']] = 'email';
104    }
105
106    $ui_fields = array(
107      'status','level','language','theme','nb_image_page','recent_period','expand',
108      'show_nb_comments','show_nb_hits','enabled_high',
109      );
110    foreach ($ui_fields as $field)
111    {
112      if (in_array($field, $params['display']))
113      {
114        $display['ui.'.$field] = $field;
115      }
116    }
117  }
118  else
119  {
120    $params['display'] = array();
121  }
122
123  $query = '
124SELECT DISTINCT ';
125
126  $first = true;
127  foreach ($display as $field => $name)
128  {
129    if (!$first) $query.= ', ';
130    else $first = false;
131    $query.= $field .' AS '. $name;
132  }
133  if (in_array('groups', $params['display']))
134  {
135    if (!$first) $query.= ', ';
136    $query.= '"" AS groups';
137  }
138
139  $query.= '
140  FROM '. USERS_TABLE .' AS u
141    INNER JOIN '. USER_INFOS_TABLE .' AS ui
142      ON u.'. $conf['user_fields']['id'] .' = ui.user_id
143    LEFT JOIN '. USER_GROUP_TABLE .' AS ug
144      ON u.'. $conf['user_fields']['id'] .' = ug.user_id
145  WHERE
146    '. implode(' AND ', $where_clauses) .'
147  ORDER BY '. $params['order'] .'
148  LIMIT '. $params['per_page'] .'
149  OFFSET '. ($params['per_page']*$params['page']) .'
150;';
151
152  $users = hash_from_query($query, 'id');
153
154  if (count($users) > 0 and in_array('groups', $params['display']))
155  {
156    $query = '
157SELECT user_id, group_id
158  FROM '. USER_GROUP_TABLE .'
159  WHERE user_id IN ('. implode(',', array_keys($users)) .')
160;';
161    $result = pwg_query($query);
162
163    while ($row = pwg_db_fetch_assoc($result))
164    {
165      $users[ $row['user_id'] ]['groups'][] = $row['group_id'];
166    }
167  }
168
169  return array(
170    'paging' => new PwgNamedStruct(
171      array(
172        'page' => $params['page'],
173        'per_page' => $params['per_page'],
174        'count' => count($users)
175        )
176      ),
177    'users' => new PwgNamedArray(array_values($users), 'user')
178    );
179}
180
181/**
182 * API method
183 * Adds a user
184 * @param mixed[] $params
185 *    @option string username
186 *    @option string password (optional)
187 *    @option string email (optional)
188 */
189function ws_users_add($params, &$service)
190{
191  global $conf;
192
193  if ($conf['double_password_type_in_admin'])
194  {
195    if ($params['password'] != $params['password_confirm'])
196    {
197      return new PwgError(WS_ERR_INVALID_PARAM, l10n('The passwords do not match'));
198    }
199  }
200
201  $user_id = register_user(
202    $params['username'],
203    $params['password'],
204    $params['email'],
205    false, // notify admin
206    $errors,
207    $params['send_password_by_mail']
208    );
209
210  if (!$user_id)
211  {
212    return new PwgError(WS_ERR_INVALID_PARAM, $errors[0]);
213  }
214
215  return $service->invoke('pwg.users.getList', array('user_id'=>$user_id));
216}
217
218/**
219 * API method
220 * Deletes users
221 * @param mixed[] $params
222 *    @option int[] user_id
223 *    @option string pwg_token
224 */
225function ws_users_delete($params, &$service)
226{
227  if (get_pwg_token() != $params['pwg_token'])
228  {
229    return new PwgError(403, 'Invalid security token');
230  }
231
232  global $conf, $user;
233
234  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
235
236  // protect some users
237  $params['user_id'] = array_diff(
238    $params['user_id'],
239    array(
240      $user['id'],
241      $conf['guest_id'],
242      $conf['default_user_id'],
243      $conf['webmaster_id'],
244      )
245    );
246
247  foreach ($params['user_id'] as $user_id)
248  {
249    delete_user($user_id);
250  }
251
252  return l10n_dec(
253        '%d user deleted', '%d users deleted',
254        count($params['user_id'])
255        );
256}
257
258/**
259 * API method
260 * Updates users
261 * @param mixed[] $params
262 *    @option int[] user_id
263 *    @option string username (optional)
264 *    @option string password (optional)
265 *    @option string email (optional)
266 *    @option string status (optional)
267 *    @option int level (optional)
268 *    @option string language (optional)
269 *    @option string theme (optional)
270 *    @option int nb_image_page (optional)
271 *    @option int recent_period (optional)
272 *    @option bool expand (optional)
273 *    @option bool show_nb_comments (optional)
274 *    @option bool show_nb_hits (optional)
275 *    @option bool enabled_high (optional)
276 */
277function ws_users_setInfo($params, &$service)
278{
279  global $conf, $user;
280
281  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
282
283  $updates = $updates_infos = array();
284  $update_status = null;
285
286  if (count($params['user_id']) == 1)
287  {
288    if (get_username($params['user_id'][0]) === false)
289    {
290      return new PwgError(WS_ERR_INVALID_PARAM, 'This user does not exist.');
291    }
292
293    if (!empty($params['username']))
294    {
295      $user_id = get_userid($params['username']);
296      if ($user_id and $user_id != $params['user_id'][0])
297      {
298        return new PwgError(WS_ERR_INVALID_PARAM, l10n('this login is already used'));
299      }
300      if ($params['username'] != strip_tags($params['username']))
301      {
302        return new PwgError(WS_ERR_INVALID_PARAM, l10n('html tags are not allowed in login'));
303      }
304      $updates[ $conf['user_fields']['username'] ] = $params['username'];
305    }
306
307    if (!empty($params['email']))
308    {
309      if ( ($error = validate_mail_address($params['user_id'][0], $params['email'])) != '')
310      {
311        return new PwgError(WS_ERR_INVALID_PARAM, $error);
312      }
313      $updates[ $conf['user_fields']['email'] ] = $params['email'];
314    }
315
316    if (!empty($params['password']))
317    {
318      $updates[ $conf['user_fields']['password'] ] = $conf['password_hash']($params['password']);
319    }
320  }
321
322  if (!empty($params['status']))
323  {
324    if ( $params['status'] == 'webmaster' and !is_webmaster() )
325    {
326      return new PwgError(403, 'Only webmasters can grant "webmaster" status');
327    }
328    if ( !in_array($params['status'], array('guest','generic','normal','admin','webmaster')) )
329    {
330      return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid status');
331    }
332
333    // status update query is separated from the rest as not applying to the same
334    // set of users (current, guest and webmaster can't be changed)
335    $params['user_id_for_status'] = array_diff(
336      $params['user_id'],
337      array(
338        $user['id'],
339        $conf['guest_id'],
340        $conf['webmaster_id'],
341        )
342      );
343
344    $update_status = $params['status'];
345  }
346
347  if (!empty($params['level']) or @$params['level']===0)
348  {
349    if ( !in_array($params['level'], $conf['available_permission_levels']) )
350    {
351      return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid level');
352    }
353    $updates_infos['level'] = $params['level'];
354  }
355
356  if (!empty($params['language']))
357  {
358    if ( !in_array($params['language'], array_keys(get_languages())) )
359    {
360      return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid language');
361    }
362    $updates_infos['language'] = $params['language'];
363  }
364
365  if (!empty($params['theme']))
366  {
367    if ( !in_array($params['theme'], array_keys(get_pwg_themes())) )
368    {
369      return new PwgError(WS_ERR_INVALID_PARAM, 'Invalid theme');
370    }
371    $updates_infos['theme'] = $params['theme'];
372  }
373
374  if (!empty($params['nb_image_page']))
375  {
376    $updates_infos['nb_image_page'] = $params['nb_image_page'];
377  }
378
379  if (!empty($params['recent_period']) or @$params['recent_period']===0)
380  {
381    $updates_infos['recent_period'] = $params['recent_period'];
382  }
383
384  if (!empty($params['expand']) or @$params['expand']===false)
385  {
386    $updates_infos['expand'] = boolean_to_string($params['expand']);
387  }
388
389  if (!empty($params['show_nb_comments']) or @$params['show_nb_comments']===false)
390  {
391    $updates_infos['show_nb_comments'] = boolean_to_string($params['show_nb_comments']);
392  }
393
394  if (!empty($params['show_nb_hits']) or @$params['show_nb_hits']===false)
395  {
396    $updates_infos['show_nb_hits'] = boolean_to_string($params['show_nb_hits']);
397  }
398
399  if (!empty($params['enabled_high']) or @$params['enabled_high']===false)
400  {
401    $updates_infos['enabled_high'] = boolean_to_string($params['enabled_high']);
402  }
403
404  // perform updates
405  single_update(
406    USERS_TABLE,
407    $updates,
408    array($conf['user_fields']['id'] => $params['user_id'][0])
409    );
410
411  if (isset($update_status) and count($params['user_id_for_status']) > 0)
412  {
413    $query = '
414UPDATE '. USER_INFOS_TABLE .' SET
415    status = "'. $update_status .'"
416  WHERE user_id IN('. implode(',', $params['user_id_for_status']) .')
417;';
418    pwg_query($query);
419  }
420
421  if (count($updates_infos) > 0)
422  {
423    $query = '
424UPDATE '. USER_INFOS_TABLE .' SET ';
425
426    $first = true;
427    foreach ($updates_infos as $field => $value)
428    {
429      if (!$first) $query.= ', ';
430      else $first = false;
431      $query.= $field .' = "'. $value .'"';
432    }
433
434    $query.= '
435  WHERE user_id IN('. implode(',', $params['user_id']) .')
436;';
437    pwg_query($query);
438  }
439
440  return $service->invoke('pwg.users.getList', array(
441    'user_id' => $params['user_id'],
442    'display' => 'basics,'.implode(',', array_keys($updates_infos)),
443    ));
444}
445
446?>
Note: See TracBrowser for help on using the repository browser.