source: trunk/admin/user_list.php @ 13840

Last change on this file since 13840 was 12922, checked in by mistic100, 12 years ago

update Piwigo headers to 2012, last change before the expected (or not) apocalypse

  • Property svn:eol-style set to LF
File size: 21.8 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 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('The passwords do not match'));
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// email notification
246if ( 
247  isset($_POST['submit_add']) 
248  and count($page['errors']) == 0 
249  and !empty($_POST['email']) 
250  and isset($_POST['send_password_by_mail']) 
251  )
252{
253  include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
254       
255  $keyargs_content = array(
256    get_l10n_args('Hello %s,', $_POST['login']),
257    get_l10n_args('Thank you for registering at %s!', $conf['gallery_title']),
258    get_l10n_args('', ''),
259    get_l10n_args('Here are your connection settings', ''),
260    get_l10n_args('Username: %s', $_POST['login']),
261    get_l10n_args('Password: %s', $_POST['password']),
262    get_l10n_args('Email: %s', $_POST['email']),
263    get_l10n_args('', ''),
264    get_l10n_args('If you think you\'ve received this email in error, please contact us at %s', get_webmaster_mail_address()),
265    );
266   
267  pwg_mail(
268    $_POST['email'],
269    array(
270      'subject' => '['.$conf['gallery_title'].'] '.l10n('Registration'),
271      'content' => l10n_args($keyargs_content),
272      'content_format' => 'text/plain',
273      )
274    );
275}
276
277// +-----------------------------------------------------------------------+
278// |                               user list                               |
279// +-----------------------------------------------------------------------+
280
281$page['filtered_users'] = get_filtered_user_list();
282
283// +-----------------------------------------------------------------------+
284// |                            selected users                             |
285// +-----------------------------------------------------------------------+
286
287if (isset($_POST['delete']) or isset($_POST['pref_submit']))
288{
289  $collection = array();
290
291  switch ($_POST['target'])
292  {
293    case 'all' :
294    {
295      foreach($page['filtered_users'] as $local_user)
296      {
297        array_push($collection, $local_user['id']);
298      }
299      break;
300    }
301    case 'selection' :
302    {
303      if (isset($_POST['selection']))
304      {
305        $collection = $_POST['selection'];
306      }
307      break;
308    }
309  }
310
311  if (count($collection) == 0)
312  {
313    array_push($page['errors'], l10n('Select at least one user'));
314  }
315}
316
317// +-----------------------------------------------------------------------+
318// |                             delete users                              |
319// +-----------------------------------------------------------------------+
320if (isset($_POST['delete']) and count($collection) > 0)
321{
322  if (in_array($conf['guest_id'], $collection))
323  {
324    array_push($page['errors'], l10n('Guest cannot be deleted'));
325  }
326  if (($conf['guest_id'] != $conf['default_user_id']) and
327      in_array($conf['default_user_id'], $collection))
328  {
329    array_push($page['errors'], l10n('Default user cannot be deleted'));
330  }
331  if (in_array($conf['webmaster_id'], $collection))
332  {
333    array_push($page['errors'], l10n('Webmaster cannot be deleted'));
334  }
335  if (in_array($user['id'], $collection))
336  {
337    array_push($page['errors'], l10n('You cannot delete your account'));
338  }
339
340  if (count($page['errors']) == 0)
341  {
342    if (isset($_POST['confirm_deletion']) and 1 == $_POST['confirm_deletion'])
343    {
344      foreach ($collection as $user_id)
345      {
346        delete_user($user_id);
347      }
348      array_push(
349        $page['infos'],
350        l10n_dec(
351          '%d user deleted', '%d users deleted',
352          count($collection)
353          )
354        );
355      foreach ($page['filtered_users'] as $filter_key => $filter_user)
356      {
357        if (in_array($filter_user['id'], $collection))
358        {
359          unset($page['filtered_users'][$filter_key]);
360        }
361      }
362    }
363    else
364    {
365      array_push($page['errors'], l10n('You need to confirm deletion'));
366    }
367  }
368}
369
370// +-----------------------------------------------------------------------+
371// |                       preferences form submission                     |
372// +-----------------------------------------------------------------------+
373
374if (isset($_POST['pref_submit']) and count($collection) > 0)
375{
376  if (-1 != $_POST['associate'])
377  {
378    $datas = array();
379
380    $query = '
381SELECT user_id
382  FROM '.USER_GROUP_TABLE.'
383  WHERE group_id = '.$_POST['associate'].'
384;';
385    $associated = array_from_query($query, 'user_id');
386
387    $associable = array_diff($collection, $associated);
388
389    if (count($associable) > 0)
390    {
391      foreach ($associable as $item)
392      {
393        array_push($datas,
394                   array('group_id'=>$_POST['associate'],
395                         'user_id'=>$item));
396      }
397
398      mass_inserts(USER_GROUP_TABLE,
399                   array('group_id', 'user_id'),
400                   $datas);
401    }
402  }
403
404  if (-1 != $_POST['dissociate'])
405  {
406    $query = '
407DELETE FROM '.USER_GROUP_TABLE.'
408  WHERE group_id = '.$_POST['dissociate'].'
409  AND user_id IN ('.implode(',', $collection).')
410';
411    pwg_query($query);
412  }
413
414  // properties to set for the collection (a user list)
415  $datas = array();
416  $dbfields = array('primary' => array('user_id'), 'update' => array());
417
418  $formfields = array(
419          'nb_image_page', 'theme', 'language',
420          'recent_period', 'expand', 'show_nb_hits', 
421          'status', 'enabled_high', 'level'
422          );
423 
424  $true_false_fields = array('expand', 'show_nb_hits', 'enabled_high');
425 
426  if ($conf['activate_comments'])
427  {
428    array_push($formfields, 'show_nb_comments');
429    array_push($true_false_fields, 'show_nb_comments');
430  }
431
432  foreach ($formfields as $formfield)
433  {
434    // special for true/false fields
435    if (in_array($formfield, $true_false_fields))
436    {
437      $test = $formfield;
438    }
439    else
440    {
441      $test = $formfield.'_action';
442    }
443
444    if ($_POST[$test] != 'leave')
445    {
446      array_push($dbfields['update'], $formfield);
447    }
448  }
449
450  // updating elements is useful only if needed...
451  if (count($dbfields['update']) > 0)
452  {
453    $datas = array();
454
455    foreach ($collection as $user_id)
456    {
457      $data = array();
458      $data['user_id'] = $user_id;
459
460      // TODO : verify if submited values are semanticaly correct
461      foreach ($dbfields['update'] as $dbfield)
462      {
463        // if the action is 'unset', the key won't be in row and
464        // mass_updates function will set this field to NULL
465        if (in_array($dbfield, $true_false_fields)
466            or 'set' == $_POST[$dbfield.'_action'])
467        {
468          $data[$dbfield] = $_POST[$dbfield];
469        }
470      }
471
472      // if the status is getting greater or equal to "admin", then level
473      // automatically switches to "admin" (8), unless the level is also
474      // defined in the same batch action.
475      if (isset($data['status']) and in_array($data['status'], array('webmaster', 'admin')))
476      {
477        if (!isset($data['level']))
478        {
479          $data['level'] = 8;
480          if (!in_array('level', $dbfields['update']))
481          {
482            array_push($dbfields['update'], 'level');
483          }
484        }
485      }
486
487      // special users checks
488      if
489        (
490          ($conf['webmaster_id'] == $user_id) or
491          ($conf['guest_id'] == $user_id) or
492          ($conf['default_user_id'] == $user_id)
493        )
494      {
495        // status must not be changed
496        if (isset($data['status']))
497        {
498          if ($conf['webmaster_id'] == $user_id)
499          {
500            $data['status'] = 'webmaster';
501          }
502          else
503          {
504            $data['status'] = 'guest';
505          }
506        }
507      }
508
509      array_push($datas, $data);
510    }
511
512    mass_updates(USER_INFOS_TABLE, $dbfields, $datas);
513  }
514
515  redirect(
516    get_root_url().
517    'admin.php'.
518    get_query_string_diff(array(), false)
519    );
520}
521
522// +-----------------------------------------------------------------------+
523// |                              groups list                              |
524// +-----------------------------------------------------------------------+
525
526$groups[-1] = '------------';
527
528$query = '
529SELECT id, name
530  FROM '.GROUPS_TABLE.'
531  ORDER BY name ASC
532;';
533$result = pwg_query($query);
534
535while ($row = pwg_db_fetch_assoc($result))
536{
537  $groups[$row['id']] = $row['name'];
538}
539
540// +-----------------------------------------------------------------------+
541// |                             template init                             |
542// +-----------------------------------------------------------------------+
543
544$template->set_filenames(array('user_list'=>'user_list.tpl'));
545
546$base_url = PHPWG_ROOT_PATH.'admin.php?page=user_list';
547
548if (isset($_GET['start']) and is_numeric($_GET['start']))
549{
550  $start = $_GET['start'];
551}
552else
553{
554  $start = 0;
555}
556
557$template->assign(
558  array(
559    'U_HELP' => get_root_url().'admin/popuphelp.php?page=user_list',
560
561    'F_ADD_ACTION' => $base_url,
562    'F_USERNAME' => @htmlentities($_GET['username'], ENT_COMPAT, 'UTF-8'),
563    'F_FILTER_ACTION' => get_root_url().'admin.php',
564   
565    'ACTIVATE_COMMENTS' => $conf['activate_comments'],
566    ));
567
568// Display or Hide double password type
569$template->assign('Double_Password', $conf['double_password_type_in_admin'] );
570
571// Filter status options
572$status_options[-1] = '------------';
573foreach (get_enums(USER_INFOS_TABLE, 'status') as $status)
574{
575  $status_options[$status] = l10n('user_status_'.$status);
576}
577$template->assign('status_options', $status_options);
578$template->assign('status_selected',
579    isset($_GET['status']) ? $_GET['status'] : '');
580
581// Filter group options
582$template->assign('group_options', $groups);
583$template->assign('group_selected',
584    isset($_GET['group']) ? $_GET['group'] : '');
585
586// Filter order options
587$template->assign('order_options', $page['order_by_items']);
588$template->assign('order_selected',
589    isset($_GET['order_by']) ? $_GET['order_by'] : '');
590
591// Filter direction options
592$template->assign('direction_options', $page['direction_items']);
593$template->assign('direction_selected',
594    isset($_GET['direction']) ? $_GET['direction'] : '');
595
596
597if (isset($_POST['pref_submit']))
598{
599  $template->assign(
600    array(
601      'NB_IMAGE_PAGE' => $_POST['nb_image_page'],
602      'RECENT_PERIOD' => $_POST['recent_period'],
603      ));
604}
605else
606{
607  $default_user = get_default_user_info(true);
608  $template->assign(
609    array(
610      'NB_IMAGE_PAGE' => $default_user['nb_image_page'],
611      'RECENT_PERIOD' => $default_user['recent_period'],
612      ));
613}
614
615// Template Options
616$template->assign('theme_options', get_pwg_themes());
617$template->assign('theme_selected',
618    isset($_POST['pref_submit']) ? $_POST['theme'] : get_default_theme());
619
620// Language options
621$template->assign('language_options', get_languages());
622$template->assign('language_selected',
623    isset($_POST['pref_submit']) ? $_POST['language'] : get_default_language());
624
625// Status options
626foreach (get_enums(USER_INFOS_TABLE, 'status') as $status)
627{
628  // Only status <= can be assign
629  if (is_autorize_status(get_access_type_status($status)))
630  {
631    $pref_status_options[$status] = l10n('user_status_'.$status);
632  }
633}
634$template->assign('pref_status_options', $pref_status_options);
635$template->assign('pref_status_selected',
636    isset($_POST['pref_submit']) ? $_POST['status'] : 'normal');
637
638// associate and dissociate options
639$template->assign('association_options', $groups);
640$template->assign('associate_selected',
641    isset($_POST['pref_submit']) ? $_POST['associate'] : '');
642$template->assign('dissociate_selected',
643    isset($_POST['pref_submit']) ? $_POST['dissociate'] : '');
644
645
646// user level options
647foreach ($conf['available_permission_levels'] as $level)
648{
649  $level_options[$level] = l10n(sprintf('Level %d', $level));
650}
651$template->assign('level_options', $level_options);
652$template->assign('level_selected',
653    isset($_POST['pref_submit']) ? $_POST['level'] : $default_user['level']);
654
655// +-----------------------------------------------------------------------+
656// |                            navigation bar                             |
657// +-----------------------------------------------------------------------+
658
659$url = PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start'));
660
661$navbar = create_navigation_bar(
662  $url,
663  count($page['filtered_users']),
664  $start,
665  $conf['users_page']
666  );
667
668$template->assign('navbar', $navbar);
669
670// +-----------------------------------------------------------------------+
671// |                               user list                               |
672// +-----------------------------------------------------------------------+
673
674$profile_url = get_root_url().'admin.php?page=profile&amp;user_id=';
675$perm_url = get_root_url().'admin.php?page=user_perm&amp;user_id=';
676
677$visible_user_list = array();
678foreach ($page['filtered_users'] as $num => $local_user)
679{
680  // simulate LIMIT $start, $conf['users_page']
681  if ($num < $start)
682  {
683    continue;
684  }
685  if ($num >= $start + $conf['users_page'])
686  {
687    break;
688  }
689
690  $visible_user_list[] = $local_user;
691}
692
693// allow plugins to fill template var plugin_user_list_column_titles and
694// plugin_columns/plugin_actions for each user in the list
695$visible_user_list = trigger_event('loc_visible_user_list', $visible_user_list);
696
697foreach ($visible_user_list as $local_user)
698{
699  $groups_string = preg_replace(
700    '/(\d+)/e',
701    "\$groups['$1']",
702    implode(
703      ', ',
704      $local_user['groups']
705      )
706    );
707
708  if (isset($_POST['pref_submit'])
709      and isset($_POST['selection'])
710      and in_array($local_user['id'], $_POST['selection']))
711  {
712    $checked = 'checked="checked"';
713  }
714  else
715  {
716    $checked = '';
717  }
718
719  $properties = array();
720  if ( $local_user['level'] != 0 )
721  {
722    $properties[] = l10n( sprintf('Level %d', $local_user['level']) );
723  }
724  $properties[] =
725    (isset($local_user['enabled_high']) and ($local_user['enabled_high'] == 'true'))
726        ? l10n('High definition') : l10n('');
727
728  $template->append(
729    'users',
730    array(
731      'ID' => $local_user['id'],
732      'CHECKED' => $checked,
733      'U_PROFILE' => $profile_url.$local_user['id'],
734      'U_PERM' => $perm_url.$local_user['id'],
735      'USERNAME' => stripslashes($local_user['username'])
736        .($local_user['id'] == $conf['guest_id']
737          ? '<br>['.l10n('guest').']' : '')
738        .($local_user['id'] == $conf['default_user_id']
739          ? '<br>['.l10n('default values').']' : ''),
740      'STATUS' => l10n('user_status_'.$local_user['status']),
741      'EMAIL' => get_email_address_as_display_text($local_user['email']),
742      'GROUPS' => $groups_string,
743      'PROPERTIES' => implode( ', ', $properties),
744      'plugin_columns' => isset($local_user['plugin_columns']) ? $local_user['plugin_columns'] : array(),
745      'plugin_actions' => isset($local_user['plugin_actions']) ? $local_user['plugin_actions'] : array(),
746      )
747    );
748}
749
750// +-----------------------------------------------------------------------+
751// |                           html code display                           |
752// +-----------------------------------------------------------------------+
753
754$template->assign_var_from_handle('ADMIN_CONTENT', 'user_list');
755?>
Note: See TracBrowser for help on using the repository browser.