source: trunk/admin/user_list.php @ 8131

Last change on this file since 8131 was 8131, checked in by patdenice, 13 years ago

feature 2060: Remove adviser from db structure.
Remove adviser from user_list page and some db queries.

  • Property svn:eol-style set to LF
File size: 20.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 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 * Add users and manage users list
26 */
27
28// +-----------------------------------------------------------------------+
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();
45
46  // filter
47  $filter = array();
48
49  if (isset($_GET['username']) and !empty($_GET['username']))
50  {
51    $username = str_replace('*', '%', $_GET['username']);
52    $filter['username'] = pwg_db_real_escape_string($username);
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  }
75
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,
88                ui.status,
89                ui.enabled_high,
90                ui.level
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
96  WHERE u.'.$conf['user_fields']['id'].' > 0';
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);
117  while ($row = pwg_db_fetch_assoc($result))
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);
132
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);
141    while ($row = pwg_db_fetch_assoc($result))
142    {
143      array_push(
144        $users[$user_nums[$row['user_id']]]['groups'],
145        $row['group_id']
146        );
147    }
148  }
149
150  return $users;
151}
152
153// +-----------------------------------------------------------------------+
154// |                           initialization                              |
155// +-----------------------------------------------------------------------+
156
157if (!defined('PHPWG_ROOT_PATH'))
158{
159  die('Hacking attempt!');
160}
161
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
169$page['order_by_items'] = array(
170  'id' => l10n('registration date'),
171  'username' => l10n('Username'),
172  'level' => l10n('Privacy level'),
173  'Language' => l10n('Language'),
174  );
175
176$page['direction_items'] = array(
177  'asc' => l10n('ascending'),
178  'desc' => l10n('descending')
179  );
180
181// +-----------------------------------------------------------------------+
182// |                              add a user                               |
183// +-----------------------------------------------------------------------+
184
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)
188{
189  if (isset($_POST['submit_add']))
190  {
191    if(empty($_POST['password']))
192    {
193      array_push($page['errors'], l10n('Password is missing. Please enter the password.'));
194    }
195    else if(empty($_POST['password_conf']))
196    {
197      array_push($page['errors'], l10n('Password confirmation is missing. Please confirm the chosen password.'));
198    }
199    else if(empty($_POST['email']))
200    {
201      array_push($page['errors'], l10n('Email address is missing. Please specify an email address.'));
202    }
203    else if ($_POST['password'] != $_POST['password_conf'])
204    {
205      array_push($page['errors'], l10n('Password confirmation error.'));
206    }
207    else
208    {
209      $page['errors'] = register_user(
210        $_POST['login'], $_POST['password'], $_POST['email'], false);
211
212      if (count($page['errors']) == 0)
213      {
214        array_push(
215          $page['infos'],
216          sprintf(
217            l10n('user "%s" added'),
218            $_POST['login']
219          )
220        );
221      }
222    }
223  }
224}
225else if ($conf['double_password_type_in_admin'] == false)
226{
227  if (isset($_POST['submit_add']))
228  {
229    $page['errors'] = register_user(
230      $_POST['login'], $_POST['password'], $_POST['email'], false);
231
232    if (count($page['errors']) == 0)
233    {
234      array_push(
235        $page['infos'],
236        sprintf(
237          l10n('user "%s" added'),
238          $_POST['login']
239          )
240        );
241    }
242  }
243}
244
245// +-----------------------------------------------------------------------+
246// |                               user list                               |
247// +-----------------------------------------------------------------------+
248
249$page['filtered_users'] = get_filtered_user_list();
250
251// +-----------------------------------------------------------------------+
252// |                            selected users                             |
253// +-----------------------------------------------------------------------+
254
255if (isset($_POST['delete']) or isset($_POST['pref_submit']))
256{
257  $collection = array();
258
259  switch ($_POST['target'])
260  {
261    case 'all' :
262    {
263      foreach($page['filtered_users'] as $local_user)
264      {
265        array_push($collection, $local_user['id']);
266      }
267      break;
268    }
269    case 'selection' :
270    {
271      if (isset($_POST['selection']))
272      {
273        $collection = $_POST['selection'];
274      }
275      break;
276    }
277  }
278
279  if (count($collection) == 0)
280  {
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{
290  if (in_array($conf['guest_id'], $collection))
291  {
292    array_push($page['errors'], l10n('Guest cannot be deleted'));
293  }
294  if (($conf['guest_id'] != $conf['default_user_id']) and
295      in_array($conf['default_user_id'], $collection))
296  {
297    array_push($page['errors'], l10n('Default user cannot be deleted'));
298  }
299  if (in_array($conf['webmaster_id'], $collection))
300  {
301    array_push($page['errors'], l10n('Webmaster cannot be deleted'));
302  }
303  if (in_array($user['id'], $collection))
304  {
305    array_push($page['errors'], l10n('You cannot delete your account'));
306  }
307
308  if (count($page['errors']) == 0)
309  {
310    if (isset($_POST['confirm_deletion']) and 1 == $_POST['confirm_deletion'])
311    {
312      foreach ($collection as $user_id)
313      {
314        delete_user($user_id);
315      }
316      array_push(
317        $page['infos'],
318        l10n_dec(
319          '%d user deleted', '%d users deleted',
320          count($collection)
321          )
322        );
323      foreach ($page['filtered_users'] as $filter_key => $filter_user)
324      {
325        if (in_array($filter_user['id'], $collection))
326        {
327          unset($page['filtered_users'][$filter_key]);
328        }
329      }
330    }
331    else
332    {
333      array_push($page['errors'], l10n('You need to confirm deletion'));
334    }
335  }
336}
337
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();
347
348    $query = '
349SELECT user_id
350  FROM '.USER_GROUP_TABLE.'
351  WHERE group_id = '.$_POST['associate'].'
352;';
353    $associated = array_from_query($query, 'user_id');
354
355    $associable = array_diff($collection, $associated);
356
357    if (count($associable) > 0)
358    {
359      foreach ($associable as $item)
360      {
361        array_push($datas,
362                   array('group_id'=>$_POST['associate'],
363                         'user_id'=>$item));
364      }
365
366      mass_inserts(USER_GROUP_TABLE,
367                   array('group_id', 'user_id'),
368                   $datas);
369    }
370  }
371
372  if (-1 != $_POST['dissociate'])
373  {
374    $query = '
375DELETE FROM '.USER_GROUP_TABLE.'
376  WHERE group_id = '.$_POST['dissociate'].'
377  AND user_id IN ('.implode(',', $collection).')
378';
379    pwg_query($query);
380  }
381
382  // properties to set for the collection (a user list)
383  $datas = array();
384  $dbfields = array('primary' => array('user_id'), 'update' => array());
385
386  $formfields =
387    array('nb_image_line', 'nb_line_page', 'theme', 'language',
388          'recent_period', 'maxwidth', 'expand', 'show_nb_comments',
389          'show_nb_hits', 'maxheight', 'status', 'enabled_high',
390          'level');
391
392  $true_false_fields = array('expand', 'show_nb_comments',
393                       'show_nb_hits', 'enabled_high');
394
395  foreach ($formfields as $formfield)
396  {
397    // special for true/false fields
398    if (in_array($formfield, $true_false_fields))
399    {
400      $test = $formfield;
401    }
402    else
403    {
404      $test = $formfield.'_action';
405    }
406
407    if ($_POST[$test] != 'leave')
408    {
409      array_push($dbfields['update'], $formfield);
410    }
411  }
412
413  // updating elements is useful only if needed...
414  if (count($dbfields['update']) > 0)
415  {
416    $datas = array();
417
418    foreach ($collection as $user_id)
419    {
420      $data = array();
421      $data['user_id'] = $user_id;
422
423      // TODO : verify if submited values are semanticaly correct
424      foreach ($dbfields['update'] as $dbfield)
425      {
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'])
430        {
431          $data[$dbfield] = $_POST[$dbfield];
432        }
433      }
434
435      // special users checks
436      if
437        (
438          ($conf['webmaster_id'] == $user_id) or
439          ($conf['guest_id'] == $user_id) or
440          ($conf['default_user_id'] == $user_id)
441        )
442      {
443        // status must not be changed
444        if (isset($data['status']))
445        {
446          if ($conf['webmaster_id'] == $user_id)
447          {
448            $data['status'] = 'webmaster';
449          }
450          else
451          {
452            $data['status'] = 'guest';
453          }
454        }
455      }
456
457      array_push($datas, $data);
458    }
459
460    mass_updates(USER_INFOS_TABLE, $dbfields, $datas);
461  }
462
463  redirect(
464    get_root_url().
465    'admin.php'.
466    get_query_string_diff(array(), false)
467    );
468}
469
470// +-----------------------------------------------------------------------+
471// |                              groups list                              |
472// +-----------------------------------------------------------------------+
473
474$groups[-1] = '------------';
475
476$query = '
477SELECT id, name
478  FROM '.GROUPS_TABLE.'
479  ORDER BY name ASC
480;';
481$result = pwg_query($query);
482
483while ($row = pwg_db_fetch_assoc($result))
484{
485  $groups[$row['id']] = $row['name'];
486}
487
488// +-----------------------------------------------------------------------+
489// |                             template init                             |
490// +-----------------------------------------------------------------------+
491
492$template->set_filenames(array('user_list'=>'user_list.tpl'));
493
494$base_url = PHPWG_ROOT_PATH.'admin.php?page=user_list';
495
496if (isset($_GET['start']) and is_numeric($_GET['start']))
497{
498  $start = $_GET['start'];
499}
500else
501{
502  $start = 0;
503}
504
505$template->assign(
506  array(
507    'U_HELP' => get_root_url().'admin/popuphelp.php?page=user_list',
508
509    'F_ADD_ACTION' => $base_url,
510    'F_USERNAME' => @htmlentities($_GET['username']),
511    'F_FILTER_ACTION' => get_root_url().'admin.php'
512    ));
513
514// Display or Hide double password type
515$template->assign('Double_Password', $conf['double_password_type_in_admin'] );
516
517// Filter status options
518$status_options[-1] = '------------';
519foreach (get_enums(USER_INFOS_TABLE, 'status') as $status)
520{
521  $status_options[$status] = l10n('user_status_'.$status);
522}
523$template->assign('status_options', $status_options);
524$template->assign('status_selected',
525    isset($_GET['status']) ? $_GET['status'] : '');
526
527// Filter group options
528$template->assign('group_options', $groups);
529$template->assign('group_selected',
530    isset($_GET['group']) ? $_GET['group'] : '');
531
532// Filter order options
533$template->assign('order_options', $page['order_by_items']);
534$template->assign('order_selected',
535    isset($_GET['order_by']) ? $_GET['order_by'] : '');
536
537// Filter direction options
538$template->assign('direction_options', $page['direction_items']);
539$template->assign('direction_selected',
540    isset($_GET['direction']) ? $_GET['direction'] : '');
541
542
543if (isset($_POST['pref_submit']))
544{
545  $template->assign(
546    array(
547      'NB_IMAGE_LINE' => $_POST['nb_image_line'],
548      'NB_LINE_PAGE' => $_POST['nb_line_page'],
549      'MAXWIDTH' => $_POST['maxwidth'],
550      'MAXHEIGHT' => $_POST['maxheight'],
551      'RECENT_PERIOD' => $_POST['recent_period'],
552      ));
553}
554else
555{
556  $default_user = get_default_user_info(true);
557  $template->assign(
558    array(
559      'NB_IMAGE_LINE' => $default_user['nb_image_line'],
560      'NB_LINE_PAGE' => $default_user['nb_line_page'],
561      'MAXWIDTH' => $default_user['maxwidth'],
562      'MAXHEIGHT' => $default_user['maxheight'],
563      'RECENT_PERIOD' => $default_user['recent_period'],
564      ));
565}
566
567// Template Options
568$template->assign('theme_options', get_pwg_themes());
569$template->assign('theme_selected',
570    isset($_POST['pref_submit']) ? $_POST['theme'] : get_default_theme());
571
572// Language options
573$template->assign('language_options', get_languages());
574$template->assign('language_selected',
575    isset($_POST['pref_submit']) ? $_POST['language'] : get_default_language());
576
577// Status options
578foreach (get_enums(USER_INFOS_TABLE, 'status') as $status)
579{
580  // Only status <= can be assign
581  if (is_autorize_status(get_access_type_status($status)))
582  {
583    $pref_status_options[$status] = l10n('user_status_'.$status);
584  }
585}
586$template->assign('pref_status_options', $pref_status_options);
587$template->assign('pref_status_selected',
588    isset($_POST['pref_submit']) ? $_POST['status'] : 'normal');
589
590// associate and dissociate options
591$template->assign('association_options', $groups);
592$template->assign('associate_selected',
593    isset($_POST['pref_submit']) ? $_POST['associate'] : '');
594$template->assign('dissociate_selected',
595    isset($_POST['pref_submit']) ? $_POST['dissociate'] : '');
596
597
598// user level options
599foreach ($conf['available_permission_levels'] as $level)
600{
601  $level_options[$level] = l10n(sprintf('Level %d', $level));
602}
603$template->assign('level_options', $level_options);
604$template->assign('level_selected',
605    isset($_POST['pref_submit']) ? $_POST['level'] : $default_user['level']);
606
607// +-----------------------------------------------------------------------+
608// |                            navigation bar                             |
609// +-----------------------------------------------------------------------+
610
611$url = PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start'));
612
613$navbar = create_navigation_bar(
614  $url,
615  count($page['filtered_users']),
616  $start,
617  $conf['users_page']
618  );
619
620$template->assign('navbar', $navbar);
621
622// +-----------------------------------------------------------------------+
623// |                               user list                               |
624// +-----------------------------------------------------------------------+
625
626$profile_url = get_root_url().'admin.php?page=profile&amp;user_id=';
627$perm_url = get_root_url().'admin.php?page=user_perm&amp;user_id=';
628
629$visible_user_list = array();
630foreach ($page['filtered_users'] as $num => $local_user)
631{
632  // simulate LIMIT $start, $conf['users_page']
633  if ($num < $start)
634  {
635    continue;
636  }
637  if ($num >= $start + $conf['users_page'])
638  {
639    break;
640  }
641
642  $visible_user_list[] = $local_user;
643}
644
645// allow plugins to fill template var plugin_user_list_column_titles and
646// plugin_columns/plugin_actions for each user in the list
647$visible_user_list = trigger_event('loc_visible_user_list', $visible_user_list);
648
649foreach ($visible_user_list as $local_user)
650{
651  $groups_string = preg_replace(
652    '/(\d+)/e',
653    "\$groups['$1']",
654    implode(
655      ', ',
656      $local_user['groups']
657      )
658    );
659
660  if (isset($_POST['pref_submit'])
661      and isset($_POST['selection'])
662      and in_array($local_user['id'], $_POST['selection']))
663  {
664    $checked = 'checked="checked"';
665  }
666  else
667  {
668    $checked = '';
669  }
670
671  $properties = array();
672  if ( $local_user['level'] != 0 )
673  {
674    $properties[] = l10n( sprintf('Level %d', $local_user['level']) );
675  }
676  $properties[] =
677    (isset($local_user['enabled_high']) and ($local_user['enabled_high'] == 'true'))
678        ? l10n('High definition') : l10n('');
679
680  $template->append(
681    'users',
682    array(
683      'ID' => $local_user['id'],
684      'CHECKED' => $checked,
685      'U_PROFILE' => $profile_url.$local_user['id'],
686      'U_PERM' => $perm_url.$local_user['id'],
687      'USERNAME' => stripslashes($local_user['username'])
688        .($local_user['id'] == $conf['guest_id']
689          ? '<br>['.l10n('guest').']' : '')
690        .($local_user['id'] == $conf['default_user_id']
691          ? '<br>['.l10n('default values').']' : ''),
692      'STATUS' => l10n('user_status_'.$local_user['status']),
693      'EMAIL' => get_email_address_as_display_text($local_user['email']),
694      'GROUPS' => $groups_string,
695      'PROPERTIES' => implode( ', ', $properties),
696      'plugin_columns' => isset($local_user['plugin_columns']) ? $local_user['plugin_columns'] : array(),
697      'plugin_actions' => isset($local_user['plugin_actions']) ? $local_user['plugin_actions'] : array(),
698      )
699    );
700}
701
702// +-----------------------------------------------------------------------+
703// |                           html code display                           |
704// +-----------------------------------------------------------------------+
705
706$template->assign_var_from_handle('ADMIN_CONTENT', 'user_list');
707?>
Note: See TracBrowser for help on using the repository browser.