source: trunk/admin/user_list.php @ 2639

Last change on this file since 2639 was 2530, checked in by vdigital, 16 years ago

Wigo becomes "goto".
Admin tpl files are moved.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 19.7 KB
RevLine 
[768]1<?php
2// +-----------------------------------------------------------------------+
[2297]3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      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// +-----------------------------------------------------------------------+
[768]23
24/**
25 * Add users and manage users list
26 */
27
28// +-----------------------------------------------------------------------+
[880]29// |                              functions                                |
30// +-----------------------------------------------------------------------+
31
32/**
33 * returns a list of users depending on page filters (in $_GET)
34 *
35 * Each user comes with his related informations : id, username, mail
36 * address, list of groups.
37 *
38 * @return array
39 */
40function get_filtered_user_list()
41{
42  global $conf, $page;
43
44  $users = array();
[1620]45
[880]46  // filter
47  $filter = array();
[1620]48
[880]49  if (isset($_GET['username']) and !empty($_GET['username']))
50  {
51    $username = str_replace('*', '%', $_GET['username']);
52    if (function_exists('mysql_real_escape_string'))
53    {
54      $filter['username'] = mysql_real_escape_string($username);
55    }
56    else
57    {
58      $filter['username'] = mysql_escape_string($username);
59    }
60  }
61
62  if (isset($_GET['group'])
63      and -1 != $_GET['group']
64      and is_numeric($_GET['group']))
65  {
66    $filter['group'] = $_GET['group'];
67  }
68
69  if (isset($_GET['status'])
70      and in_array($_GET['status'], get_enums(USER_INFOS_TABLE, 'status')))
71  {
72    $filter['status'] = $_GET['status'];
73  }
74
75  // how to order the list?
76  $order_by = 'id';
77  if (isset($_GET['order_by'])
78      and in_array($_GET['order_by'], array_keys($page['order_by_items'])))
79  {
80    $order_by = $_GET['order_by'];
81  }
[1620]82
[880]83  $direction = 'ASC';
84  if (isset($_GET['direction'])
85      and in_array($_GET['direction'], array_keys($page['direction_items'])))
86  {
87    $direction = strtoupper($_GET['direction']);
88  }
89
90  // search users depending on filters and order
91  $query = '
92SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
93                u.'.$conf['user_fields']['username'].' AS username,
94                u.'.$conf['user_fields']['email'].' AS email,
[1079]95                ui.status,
[1085]96                ui.adviser,
[2084]97                ui.enabled_high,
98                ui.level
[880]99  FROM '.USERS_TABLE.' AS u
100    INNER JOIN '.USER_INFOS_TABLE.' AS ui
101      ON u.'.$conf['user_fields']['id'].' = ui.user_id
102    LEFT JOIN '.USER_GROUP_TABLE.' AS ug
103      ON u.'.$conf['user_fields']['id'].' = ug.user_id
[1930]104  WHERE u.'.$conf['user_fields']['id'].' > 0';
[880]105  if (isset($filter['username']))
106  {
107    $query.= '
108  AND u.'.$conf['user_fields']['username'].' LIKE \''.$filter['username'].'\'';
109  }
110  if (isset($filter['group']))
111  {
112    $query.= '
113    AND ug.group_id = '.$filter['group'];
114  }
115  if (isset($filter['status']))
116  {
117    $query.= '
118    AND ui.status = \''.$filter['status']."'";
119  }
120  $query.= '
121  ORDER BY '.$order_by.' '.$direction.'
122;';
123
124  $result = pwg_query($query);
125  while ($row = mysql_fetch_array($result))
126  {
127    $user = $row;
128    $user['groups'] = array();
129
130    array_push($users, $user);
131  }
132
133  // add group lists
134  $user_ids = array();
135  foreach ($users as $i => $user)
136  {
137    $user_ids[$i] = $user['id'];
138  }
139  $user_nums = array_flip($user_ids);
[1620]140
[880]141  if (count($user_ids) > 0)
142  {
143    $query = '
144SELECT user_id, group_id
145  FROM '.USER_GROUP_TABLE.'
146  WHERE user_id IN ('.implode(',', $user_ids).')
147;';
148    $result = pwg_query($query);
149    while ($row = mysql_fetch_array($result))
150    {
151      array_push(
152        $users[$user_nums[$row['user_id']]]['groups'],
153        $row['group_id']
154        );
155    }
156  }
[1620]157
[880]158  return $users;
159}
160
161// +-----------------------------------------------------------------------+
[768]162// |                           initialization                              |
163// +-----------------------------------------------------------------------+
164
165if (!defined('PHPWG_ROOT_PATH'))
166{
167  die('Hacking attempt!');
168}
169
[1072]170include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
171
172// +-----------------------------------------------------------------------+
173// | Check Access and exit when user status is not ok                      |
174// +-----------------------------------------------------------------------+
175check_status(ACCESS_ADMINISTRATOR);
176
[880]177$page['order_by_items'] = array(
[2201]178  'id' => l10n('registration_date'),
179  'username' => l10n('Username'),
[2090]180  'level' => l10n('Privacy level'),
181  'language' => l10n('language'),
[880]182  );
183
184$page['direction_items'] = array(
[2201]185  'asc' => l10n('ascending'),
186  'desc' => l10n('descending')
[880]187  );
188
[768]189// +-----------------------------------------------------------------------+
190// |                              add a user                               |
191// +-----------------------------------------------------------------------+
192
193if (isset($_POST['submit_add']))
194{
[1753]195  $page['errors'] = register_user(
[2178]196    $_POST['login'], $_POST['password'], $_POST['email'], false);
[906]197
198  if (count($page['errors']) == 0)
199  {
200    array_push(
201      $page['infos'],
202      sprintf(
203        l10n('user "%s" added'),
204        $_POST['login']
205        )
206      );
207  }
[768]208}
209
210// +-----------------------------------------------------------------------+
[914]211// |                               user list                               |
212// +-----------------------------------------------------------------------+
213
214$page['filtered_users'] = get_filtered_user_list();
215
216// +-----------------------------------------------------------------------+
[858]217// |                            selected users                             |
[787]218// +-----------------------------------------------------------------------+
219
[858]220if (isset($_POST['delete']) or isset($_POST['pref_submit']))
[787]221{
222  $collection = array();
[1620]223
[787]224  switch ($_POST['target'])
225  {
226    case 'all' :
227    {
[880]228      foreach($page['filtered_users'] as $local_user)
229      {
230        array_push($collection, $local_user['id']);
231      }
[787]232      break;
233    }
234    case 'selection' :
235    {
[805]236      if (isset($_POST['selection']))
237      {
238        $collection = $_POST['selection'];
239      }
[787]240      break;
241    }
242  }
243
[858]244  if (count($collection) == 0)
[787]245  {
[858]246    array_push($page['errors'], l10n('Select at least one user'));
247  }
248}
249
250// +-----------------------------------------------------------------------+
251// |                             delete users                              |
252// +-----------------------------------------------------------------------+
253if (isset($_POST['delete']) and count($collection) > 0)
254{
[2024]255  if (in_array($conf['guest_id'], $collection))
256  {
257    array_push($page['errors'], l10n('Guest cannot be deleted'));
258  }
[2084]259  if (($conf['guest_id'] != $conf['default_user_id']) and
[2024]260      in_array($conf['default_user_id'], $collection))
261  {
262    array_push($page['errors'], l10n('Default user cannot be deleted'));
263  }
[858]264  if (in_array($conf['webmaster_id'], $collection))
265  {
266    array_push($page['errors'], l10n('Webmaster cannot be deleted'));
267  }
[2024]268  if (in_array($user['id'], $collection))
[1489]269  {
270    array_push($page['errors'], l10n('You cannot delete your account'));
271  }
[2024]272
273  if (count($page['errors']) == 0)
[858]274  {
275    if (isset($_POST['confirm_deletion']) and 1 == $_POST['confirm_deletion'])
[805]276    {
[858]277      foreach ($collection as $user_id)
278      {
279        delete_user($user_id);
280      }
281      array_push(
282        $page['infos'],
[1932]283        l10n_dec(
284          '%d user deleted', '%d users deleted',
[1620]285          count($collection)
[858]286          )
287        );
[998]288      foreach ($page['filtered_users'] as $filter_key => $filter_user)
289      {
[1079]290        if (in_array($filter_user['id'], $collection))
291        {
292          unset($page['filtered_users'][$filter_key]);
293        }
[998]294      }
[858]295    }
296    else
297    {
298      array_push($page['errors'], l10n('You need to confirm deletion'));
299    }
300  }
301}
[787]302
[858]303// +-----------------------------------------------------------------------+
304// |                       preferences form submission                     |
305// +-----------------------------------------------------------------------+
306
307if (isset($_POST['pref_submit']) and count($collection) > 0)
308{
309  if (-1 != $_POST['associate'])
310  {
311    $datas = array();
[1620]312
[858]313    $query = '
[787]314SELECT user_id
315  FROM '.USER_GROUP_TABLE.'
316  WHERE group_id = '.$_POST['associate'].'
317;';
[858]318    $associated = array_from_query($query, 'user_id');
[1620]319
[858]320    $associable = array_diff($collection, $associated);
[1620]321
[858]322    if (count($associable) > 0)
323    {
324      foreach ($associable as $item)
[805]325      {
[858]326        array_push($datas,
327                   array('group_id'=>$_POST['associate'],
328                         'user_id'=>$item));
329      }
[1620]330
[858]331      mass_inserts(USER_GROUP_TABLE,
332                   array('group_id', 'user_id'),
333                   $datas);
[787]334    }
[858]335  }
[1620]336
[858]337  if (-1 != $_POST['dissociate'])
338  {
339    $query = '
[787]340DELETE FROM '.USER_GROUP_TABLE.'
341  WHERE group_id = '.$_POST['dissociate'].'
342  AND user_id IN ('.implode(',', $collection).')
343';
[858]344    pwg_query($query);
345  }
[1620]346
[858]347  // properties to set for the collection (a user list)
348  $datas = array();
349  $dbfields = array('primary' => array('user_id'), 'update' => array());
[1620]350
[858]351  $formfields =
352    array('nb_image_line', 'nb_line_page', 'template', 'language',
353          'recent_period', 'maxwidth', 'expand', 'show_nb_comments',
[2084]354          'show_nb_hits', 'maxheight', 'status', 'enabled_high',
355          'level');
[1620]356
[2084]357  $true_false_fields = array('expand', 'show_nb_comments',
[1763]358                       'show_nb_hits', 'enabled_high');
[1170]359  if ($conf['allow_adviser'])
360  {
361    array_push($formfields, 'adviser');
362    array_push($true_false_fields, 'adviser');
363  }
[1620]364
[858]365  foreach ($formfields as $formfield)
366  {
367    // special for true/false fields
368    if (in_array($formfield, $true_false_fields))
369    {
370      $test = $formfield;
[805]371    }
[858]372    else
373    {
374      $test = $formfield.'_action';
375    }
[1620]376
[858]377    if ($_POST[$test] != 'leave')
[787]378    {
[858]379      array_push($dbfields['update'], $formfield);
[787]380    }
[858]381  }
[1620]382
[858]383  // updating elements is useful only if needed...
384  if (count($dbfields['update']) > 0)
385  {
386    $datas = array();
[1620]387
[858]388    foreach ($collection as $user_id)
[787]389    {
[858]390      $data = array();
391      $data['user_id'] = $user_id;
[1620]392
[858]393      // TODO : verify if submited values are semanticaly correct
394      foreach ($dbfields['update'] as $dbfield)
[787]395      {
[858]396        // if the action is 'unset', the key won't be in row and
397        // mass_updates function will set this field to NULL
398        if (in_array($dbfield, $true_false_fields)
399            or 'set' == $_POST[$dbfield.'_action'])
[787]400        {
[858]401          $data[$dbfield] = $_POST[$dbfield];
[787]402        }
403      }
[1085]404
[2024]405      // special users checks
406      if
407        (
408          ($conf['webmaster_id'] == $user_id) or
409          ($conf['guest_id'] == $user_id) or
410          ($conf['default_user_id'] == $user_id)
411        )
[858]412      {
[2024]413        // status must not be changed
414        if (isset($data['status']))
415        {
416          if ($conf['webmaster_id'] == $user_id)
417          {
418            $data['status'] = 'webmaster';
419          }
420          else
421          {
422            $data['status'] = 'guest';
423          }
424        }
[1085]425
[2024]426        // could not be adivser
427        if (isset($data['adviser']))
428        {
429          $data['adviser'] = 'false';
430        }
[1085]431      }
432
[858]433      array_push($datas, $data);
434    }
[1620]435
[858]436    mass_updates(USER_INFOS_TABLE, $dbfields, $datas);
[787]437  }
[931]438
439  redirect(
[2286]440    get_root_url().
[931]441    'admin.php'.
[2089]442    get_query_string_diff(array(), false)
[931]443    );
[787]444}
445
446// +-----------------------------------------------------------------------+
447// |                              groups list                              |
448// +-----------------------------------------------------------------------+
449
[2253]450$groups[-1] = '------------';
[787]451
452$query = '
453SELECT id, name
454  FROM '.GROUPS_TABLE.'
[1960]455  ORDER BY name ASC
[787]456;';
457$result = pwg_query($query);
458
459while ($row = mysql_fetch_array($result))
460{
461  $groups[$row['id']] = $row['name'];
462}
463
464// +-----------------------------------------------------------------------+
[768]465// |                             template init                             |
466// +-----------------------------------------------------------------------+
467
[2530]468$template->set_filenames(array('user_list'=>'user_list.tpl'));
[768]469
[1004]470$base_url = PHPWG_ROOT_PATH.'admin.php?page=user_list';
[768]471
472if (isset($_GET['start']) and is_numeric($_GET['start']))
473{
474  $start = $_GET['start'];
475}
476else
477{
478  $start = 0;
479}
480
[2253]481$template->assign(
[768]482  array(
[2286]483    'U_HELP' => get_root_url().'popuphelp.php?page=user_list',
[1620]484
[768]485    'F_ADD_ACTION' => $base_url,
[1696]486    'F_USERNAME' => @htmlentities($_GET['username']),
[2286]487    'F_FILTER_ACTION' => get_root_url().'admin.php'
[768]488    ));
489
[1087]490// Hide radio-button if not allow to assign adviser
491if ($conf['allow_adviser'])
492{
[2253]493  $template->assign('adviser', true);
[1087]494}
495
[2253]496// Filter status options
497$status_options[-1] = '------------';
498foreach (get_enums(USER_INFOS_TABLE, 'status') as $status)
[768]499{
[2253]500  $status_options[$status] = l10n('user_status_'.$status);
[768]501}
[2253]502$template->assign('status_options', $status_options);
503$template->assign('status_selected',
504    isset($_GET['status']) ? $_GET['status'] : '');
[768]505
[2253]506// Filter group options
507$template->assign('group_options', $groups);
508$template->assign('group_selected',
509    isset($_GET['group']) ? $_GET['group'] : '');
[768]510
[2253]511// Filter order options
512$template->assign('order_options', $page['order_by_items']);
513$template->assign('order_selected',
514    isset($_GET['order_by']) ? $_GET['order_by'] : '');
[776]515
[2253]516// Filter direction options
517$template->assign('direction_options', $page['direction_items']);
518$template->assign('direction_selected',
519    isset($_GET['direction']) ? $_GET['direction'] : '');
[776]520
521
[787]522if (isset($_POST['pref_submit']))
523{
[2253]524  $template->assign(
[787]525    array(
526      'NB_IMAGE_LINE' => $_POST['nb_image_line'],
527      'NB_LINE_PAGE' => $_POST['nb_line_page'],
528      'MAXWIDTH' => $_POST['maxwidth'],
529      'MAXHEIGHT' => $_POST['maxheight'],
530      'RECENT_PERIOD' => $_POST['recent_period'],
531      ));
532}
533else
534{
[1926]535  $default_user = get_default_user_info(true);
[2253]536  $template->assign(
[787]537    array(
[1926]538      'NB_IMAGE_LINE' => $default_user['nb_image_line'],
539      'NB_LINE_PAGE' => $default_user['nb_line_page'],
540      'MAXWIDTH' => $default_user['maxwidth'],
541      'MAXHEIGHT' => $default_user['maxheight'],
542      'RECENT_PERIOD' => $default_user['recent_period'],
[787]543      ));
544}
545
[2253]546// Template Options
547$template->assign('template_options', get_pwg_themes());
548$template->assign('template_selected', 
549    isset($_POST['pref_submit']) ? $_POST['template'] : get_default_template());
[787]550
[2253]551// Language options
552$template->assign('language_options', get_languages());
553$template->assign('language_selected', 
554    isset($_POST['pref_submit']) ? $_POST['language'] : get_default_language());
[1620]555
[2253]556// Status options
[808]557foreach (get_enums(USER_INFOS_TABLE, 'status') as $status)
[787]558{
[1085]559  // Only status <= can be assign
560  if (is_autorize_status(get_access_type_status($status)))
561  {
[2253]562    $pref_status_options[$status] = l10n('user_status_'.$status);
[1085]563  }
[787]564}
[2253]565$template->assign('pref_status_options', $pref_status_options);
566$template->assign('pref_status_selected', 
567    isset($_POST['pref_submit']) ? $_POST['status'] : 'normal');
[787]568
[2253]569// associate and dissociate options
570$template->assign('association_options', $groups);
571$template->assign('associate_selected',
572    isset($_POST['pref_submit']) ? $_POST['associate'] : '');
573$template->assign('dissociate_selected',
574    isset($_POST['pref_submit']) ? $_POST['dissociate'] : '');
[787]575
576
[2084]577// user level options
578foreach ($conf['available_permission_levels'] as $level)
579{
[2253]580  $level_options[$level] = l10n(sprintf('Level %d', $level));
[2084]581}
[2253]582$template->assign('level_options', $level_options);
583$template->assign('level_selected', 
584    isset($_POST['pref_submit']) ? $_POST['level'] : $default_user['level']);
[2084]585
[768]586// +-----------------------------------------------------------------------+
587// |                            navigation bar                             |
588// +-----------------------------------------------------------------------+
589
590$url = PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start'));
591
[880]592$navbar = create_navigation_bar(
593  $url,
594  count($page['filtered_users']),
595  $start,
[1084]596  $conf['users_page']
[880]597  );
[768]598
[2253]599$template->assign('NAVBAR', $navbar);
[768]600
601// +-----------------------------------------------------------------------+
602// |                               user list                               |
603// +-----------------------------------------------------------------------+
604
[1753]605$profile_url = get_root_url().'admin.php?page=profile&amp;user_id=';
606$perm_url = get_root_url().'admin.php?page=user_perm&amp;user_id=';
[768]607
[2041]608$visible_user_list = array();
[880]609foreach ($page['filtered_users'] as $num => $local_user)
[768]610{
[880]611  // simulate LIMIT $start, $conf['users_page']
612  if ($num < $start)
613  {
614    continue;
615  }
616  if ($num >= $start + $conf['users_page'])
617  {
618    break;
619  }
[768]620
[2041]621  $visible_user_list[] = $local_user;
622}
623
[2286]624// allow plugins to fill template var plugin_user_list_column_titles and
625// plugin_columns/plugin_actions for each user in the list
[2041]626$visible_user_list = trigger_event('loc_visible_user_list', $visible_user_list);
627
[2286]628foreach ($visible_user_list as $local_user)
[2041]629{
[880]630  $groups_string = preg_replace(
631    '/(\d+)/e',
632    "\$groups['$1']",
633    implode(
634      ', ',
635      $local_user['groups']
636      )
637    );
[768]638
[880]639  if (isset($_POST['pref_submit'])
640      and isset($_POST['selection'])
641      and in_array($local_user['id'], $_POST['selection']))
[768]642  {
[880]643    $checked = 'checked="checked"';
[768]644  }
[880]645  else
[768]646  {
[880]647    $checked = '';
648  }
[1087]649
[2084]650  $properties = array();
[2090]651  if ( $local_user['level'] != 0 )
652  {
653    $properties[] = l10n( sprintf('Level %d', $local_user['level']) );
654  }
[2084]655  $properties[] =
656    (isset($local_user['enabled_high']) and ($local_user['enabled_high'] == 'true'))
[2201]657        ? l10n('is_high_enabled') : l10n('is_high_disabled');
[2084]658
[2253]659  $template->append(
660    'users',
[880]661    array(
662      'ID' => $local_user['id'],
663      'CHECKED' => $checked,
[1753]664      'U_PROFILE' => $profile_url.$local_user['id'],
[1004]665      'U_PERM' => $perm_url.$local_user['id'],
[1930]666      'USERNAME' => $local_user['username']
667        .($local_user['id'] == $conf['guest_id']
668          ? '<BR />['.l10n('is_the_guest').']' : '')
669        .($local_user['id'] == $conf['default_user_id']
670          ? '<BR />['.l10n('is_the_default').']' : ''),
[2201]671      'STATUS' => l10n('user_status_'.
672        $local_user['status']).(($local_user['adviser'] == 'true')
[1930]673        ? '<BR />['.l10n('adviser').']' : ''),
[1462]674      'EMAIL' => get_email_address_as_display_text($local_user['email']),
[1079]675      'GROUPS' => $groups_string,
[2090]676      'PROPERTIES' => implode( ', ', $properties),
[2286]677      'plugin_columns' => isset($local_user['plugin_columns']) ? $local_user['plugin_columns'] : array(),
678      'plugin_actions' => isset($local_user['plugin_actions']) ? $local_user['plugin_actions'] : array(),
[880]679      )
680    );
[768]681}
682
683// +-----------------------------------------------------------------------+
684// |                           html code display                           |
685// +-----------------------------------------------------------------------+
686
687$template->assign_var_from_handle('ADMIN_CONTENT', 'user_list');
688?>
Note: See TracBrowser for help on using the repository browser.