source: trunk/admin/user_list.php @ 12756

Last change on this file since 12756 was 12681, checked in by rvelices, 12 years ago

language cleanup (exact duplicates or almost same duplicate - capital/lower case first letter ...)

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