source: branches/2.2/admin/user_list.php @ 11127

Last change on this file since 11127 was 11127, checked in by mistic100, 13 years ago

merge r10856 from trunk

  • Property svn:eol-style set to LF
File size: 21.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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          stripslashes($_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      // 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
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        )
457      {
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        }
470      }
471
472      array_push($datas, $data);
473    }
474
475    mass_updates(USER_INFOS_TABLE, $dbfields, $datas);
476  }
477
478  redirect(
479    get_root_url().
480    'admin.php'.
481    get_query_string_diff(array(), false)
482    );
483}
484
485// +-----------------------------------------------------------------------+
486// |                              groups list                              |
487// +-----------------------------------------------------------------------+
488
489$groups[-1] = '------------';
490
491$query = '
492SELECT id, name
493  FROM '.GROUPS_TABLE.'
494  ORDER BY name ASC
495;';
496$result = pwg_query($query);
497
498while ($row = pwg_db_fetch_assoc($result))
499{
500  $groups[$row['id']] = $row['name'];
501}
502
503// +-----------------------------------------------------------------------+
504// |                             template init                             |
505// +-----------------------------------------------------------------------+
506
507$template->set_filenames(array('user_list'=>'user_list.tpl'));
508
509$base_url = PHPWG_ROOT_PATH.'admin.php?page=user_list';
510
511if (isset($_GET['start']) and is_numeric($_GET['start']))
512{
513  $start = $_GET['start'];
514}
515else
516{
517  $start = 0;
518}
519
520$template->assign(
521  array(
522    'U_HELP' => get_root_url().'admin/popuphelp.php?page=user_list',
523
524    'F_ADD_ACTION' => $base_url,
525    'F_USERNAME' => @htmlentities($_GET['username'], ENT_COMPAT, 'UTF-8'),
526    'F_FILTER_ACTION' => get_root_url().'admin.php'
527    ));
528
529// Display or Hide double password type
530$template->assign('Double_Password', $conf['double_password_type_in_admin'] );
531
532// Filter status options
533$status_options[-1] = '------------';
534foreach (get_enums(USER_INFOS_TABLE, 'status') as $status)
535{
536  $status_options[$status] = l10n('user_status_'.$status);
537}
538$template->assign('status_options', $status_options);
539$template->assign('status_selected',
540    isset($_GET['status']) ? $_GET['status'] : '');
541
542// Filter group options
543$template->assign('group_options', $groups);
544$template->assign('group_selected',
545    isset($_GET['group']) ? $_GET['group'] : '');
546
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'] : '');
551
552// Filter direction options
553$template->assign('direction_options', $page['direction_items']);
554$template->assign('direction_selected',
555    isset($_GET['direction']) ? $_GET['direction'] : '');
556
557
558if (isset($_POST['pref_submit']))
559{
560  $template->assign(
561    array(
562      'NB_IMAGE_LINE' => $_POST['nb_image_line'],
563      'NB_LINE_PAGE' => $_POST['nb_line_page'],
564      'MAXWIDTH' => $_POST['maxwidth'],
565      'MAXHEIGHT' => $_POST['maxheight'],
566      'RECENT_PERIOD' => $_POST['recent_period'],
567      ));
568}
569else
570{
571  $default_user = get_default_user_info(true);
572  $template->assign(
573    array(
574      'NB_IMAGE_LINE' => $default_user['nb_image_line'],
575      'NB_LINE_PAGE' => $default_user['nb_line_page'],
576      'MAXWIDTH' => $default_user['maxwidth'],
577      'MAXHEIGHT' => $default_user['maxheight'],
578      'RECENT_PERIOD' => $default_user['recent_period'],
579      ));
580}
581
582// Template Options
583$template->assign('theme_options', get_pwg_themes());
584$template->assign('theme_selected',
585    isset($_POST['pref_submit']) ? $_POST['theme'] : get_default_theme());
586
587// Language options
588$template->assign('language_options', get_languages());
589$template->assign('language_selected',
590    isset($_POST['pref_submit']) ? $_POST['language'] : get_default_language());
591
592// Status options
593foreach (get_enums(USER_INFOS_TABLE, 'status') as $status)
594{
595  // Only status <= can be assign
596  if (is_autorize_status(get_access_type_status($status)))
597  {
598    $pref_status_options[$status] = l10n('user_status_'.$status);
599  }
600}
601$template->assign('pref_status_options', $pref_status_options);
602$template->assign('pref_status_selected',
603    isset($_POST['pref_submit']) ? $_POST['status'] : 'normal');
604
605// associate and dissociate options
606$template->assign('association_options', $groups);
607$template->assign('associate_selected',
608    isset($_POST['pref_submit']) ? $_POST['associate'] : '');
609$template->assign('dissociate_selected',
610    isset($_POST['pref_submit']) ? $_POST['dissociate'] : '');
611
612
613// user level options
614foreach ($conf['available_permission_levels'] as $level)
615{
616  $level_options[$level] = l10n(sprintf('Level %d', $level));
617}
618$template->assign('level_options', $level_options);
619$template->assign('level_selected',
620    isset($_POST['pref_submit']) ? $_POST['level'] : $default_user['level']);
621
622// +-----------------------------------------------------------------------+
623// |                            navigation bar                             |
624// +-----------------------------------------------------------------------+
625
626$url = PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start'));
627
628$navbar = create_navigation_bar(
629  $url,
630  count($page['filtered_users']),
631  $start,
632  $conf['users_page']
633  );
634
635$template->assign('navbar', $navbar);
636
637// +-----------------------------------------------------------------------+
638// |                               user list                               |
639// +-----------------------------------------------------------------------+
640
641$profile_url = get_root_url().'admin.php?page=profile&amp;user_id=';
642$perm_url = get_root_url().'admin.php?page=user_perm&amp;user_id=';
643
644$visible_user_list = array();
645foreach ($page['filtered_users'] as $num => $local_user)
646{
647  // simulate LIMIT $start, $conf['users_page']
648  if ($num < $start)
649  {
650    continue;
651  }
652  if ($num >= $start + $conf['users_page'])
653  {
654    break;
655  }
656
657  $visible_user_list[] = $local_user;
658}
659
660// allow plugins to fill template var plugin_user_list_column_titles and
661// plugin_columns/plugin_actions for each user in the list
662$visible_user_list = trigger_event('loc_visible_user_list', $visible_user_list);
663
664foreach ($visible_user_list as $local_user)
665{
666  $groups_string = preg_replace(
667    '/(\d+)/e',
668    "\$groups['$1']",
669    implode(
670      ', ',
671      $local_user['groups']
672      )
673    );
674
675  if (isset($_POST['pref_submit'])
676      and isset($_POST['selection'])
677      and in_array($local_user['id'], $_POST['selection']))
678  {
679    $checked = 'checked="checked"';
680  }
681  else
682  {
683    $checked = '';
684  }
685
686  $properties = array();
687  if ( $local_user['level'] != 0 )
688  {
689    $properties[] = l10n( sprintf('Level %d', $local_user['level']) );
690  }
691  $properties[] =
692    (isset($local_user['enabled_high']) and ($local_user['enabled_high'] == 'true'))
693        ? l10n('High definition') : l10n('');
694
695  $template->append(
696    'users',
697    array(
698      'ID' => $local_user['id'],
699      'CHECKED' => $checked,
700      'U_PROFILE' => $profile_url.$local_user['id'],
701      'U_PERM' => $perm_url.$local_user['id'],
702      'USERNAME' => stripslashes($local_user['username'])
703        .($local_user['id'] == $conf['guest_id']
704          ? '<br>['.l10n('guest').']' : '')
705        .($local_user['id'] == $conf['default_user_id']
706          ? '<br>['.l10n('default values').']' : ''),
707      'STATUS' => l10n('user_status_'.$local_user['status']),
708      'EMAIL' => get_email_address_as_display_text($local_user['email']),
709      'GROUPS' => $groups_string,
710      'PROPERTIES' => implode( ', ', $properties),
711      'plugin_columns' => isset($local_user['plugin_columns']) ? $local_user['plugin_columns'] : array(),
712      'plugin_actions' => isset($local_user['plugin_actions']) ? $local_user['plugin_actions'] : array(),
713      )
714    );
715}
716
717// +-----------------------------------------------------------------------+
718// |                           html code display                           |
719// +-----------------------------------------------------------------------+
720
721$template->assign_var_from_handle('ADMIN_CONTENT', 'user_list');
722?>
Note: See TracBrowser for help on using the repository browser.