source: trunk/admin/user_list.php @ 1932

Last change on this file since 1932 was 1932, checked in by rub, 17 years ago

o add missing $lang
o use of l10n_dec
o normalize file header

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 21.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | file          : $Id: user_list.php 1932 2007-03-29 19:04:54Z rub $
8// | last update   : $Date: 2007-03-29 19:04:54 +0000 (Thu, 29 Mar 2007) $
9// | last modifier : $Author: rub $
10// | revision      : $Revision: 1932 $
11// +-----------------------------------------------------------------------+
12// | This program is free software; you can redistribute it and/or modify  |
13// | it under the terms of the GNU General Public License as published by  |
14// | the Free Software Foundation                                          |
15// |                                                                       |
16// | This program is distributed in the hope that it will be useful, but   |
17// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
18// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
19// | General Public License for more details.                              |
20// |                                                                       |
21// | You should have received a copy of the GNU General Public License     |
22// | along with this program; if not, write to the Free Software           |
23// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
24// | USA.                                                                  |
25// +-----------------------------------------------------------------------+
26
27/**
28 * Add users and manage users list
29 */
30
31// +-----------------------------------------------------------------------+
32// |                              functions                                |
33// +-----------------------------------------------------------------------+
34
35/**
36 * returns a list of users depending on page filters (in $_GET)
37 *
38 * Each user comes with his related informations : id, username, mail
39 * address, list of groups.
40 *
41 * @return array
42 */
43function get_filtered_user_list()
44{
45  global $conf, $page;
46
47  $users = array();
48
49  // filter
50  $filter = array();
51
52  if (isset($_GET['username']) and !empty($_GET['username']))
53  {
54    $username = str_replace('*', '%', $_GET['username']);
55    if (function_exists('mysql_real_escape_string'))
56    {
57      $filter['username'] = mysql_real_escape_string($username);
58    }
59    else
60    {
61      $filter['username'] = mysql_escape_string($username);
62    }
63  }
64
65  if (isset($_GET['group'])
66      and -1 != $_GET['group']
67      and is_numeric($_GET['group']))
68  {
69    $filter['group'] = $_GET['group'];
70  }
71
72  if (isset($_GET['status'])
73      and in_array($_GET['status'], get_enums(USER_INFOS_TABLE, 'status')))
74  {
75    $filter['status'] = $_GET['status'];
76  }
77
78  // how to order the list?
79  $order_by = 'id';
80  if (isset($_GET['order_by'])
81      and in_array($_GET['order_by'], array_keys($page['order_by_items'])))
82  {
83    $order_by = $_GET['order_by'];
84  }
85
86  $direction = 'ASC';
87  if (isset($_GET['direction'])
88      and in_array($_GET['direction'], array_keys($page['direction_items'])))
89  {
90    $direction = strtoupper($_GET['direction']);
91  }
92
93  // search users depending on filters and order
94  $query = '
95SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
96                u.'.$conf['user_fields']['username'].' AS username,
97                u.'.$conf['user_fields']['email'].' AS email,
98                ui.status,
99                ui.adviser,
100                ui.enabled_high
101  FROM '.USERS_TABLE.' AS u
102    INNER JOIN '.USER_INFOS_TABLE.' AS ui
103      ON u.'.$conf['user_fields']['id'].' = ui.user_id
104    LEFT JOIN '.USER_GROUP_TABLE.' AS ug
105      ON u.'.$conf['user_fields']['id'].' = ug.user_id
106  WHERE u.'.$conf['user_fields']['id'].' > 0';
107  if (isset($filter['username']))
108  {
109    $query.= '
110  AND u.'.$conf['user_fields']['username'].' LIKE \''.$filter['username'].'\'';
111  }
112  if (isset($filter['group']))
113  {
114    $query.= '
115    AND ug.group_id = '.$filter['group'];
116  }
117  if (isset($filter['status']))
118  {
119    $query.= '
120    AND ui.status = \''.$filter['status']."'";
121  }
122  $query.= '
123  ORDER BY '.$order_by.' '.$direction.'
124;';
125
126  $result = pwg_query($query);
127  while ($row = mysql_fetch_array($result))
128  {
129    $user = $row;
130    $user['groups'] = array();
131
132    array_push($users, $user);
133  }
134
135  // add group lists
136  $user_ids = array();
137  foreach ($users as $i => $user)
138  {
139    $user_ids[$i] = $user['id'];
140  }
141  $user_nums = array_flip($user_ids);
142
143  if (count($user_ids) > 0)
144  {
145    $query = '
146SELECT user_id, group_id
147  FROM '.USER_GROUP_TABLE.'
148  WHERE user_id IN ('.implode(',', $user_ids).')
149;';
150    $result = pwg_query($query);
151    while ($row = mysql_fetch_array($result))
152    {
153      array_push(
154        $users[$user_nums[$row['user_id']]]['groups'],
155        $row['group_id']
156        );
157    }
158  }
159
160  return $users;
161}
162
163// +-----------------------------------------------------------------------+
164// |                           initialization                              |
165// +-----------------------------------------------------------------------+
166
167if (!defined('PHPWG_ROOT_PATH'))
168{
169  die('Hacking attempt!');
170}
171
172include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
173
174// +-----------------------------------------------------------------------+
175// | Check Access and exit when user status is not ok                      |
176// +-----------------------------------------------------------------------+
177check_status(ACCESS_ADMINISTRATOR);
178
179$page['order_by_items'] = array(
180  'id' => $lang['registration_date'],
181  'username' => $lang['Username']
182  );
183
184$page['direction_items'] = array(
185  'asc' => $lang['ascending'],
186  'desc' => $lang['descending']
187  );
188
189// +-----------------------------------------------------------------------+
190// |                              add a user                               |
191// +-----------------------------------------------------------------------+
192
193if (isset($_POST['submit_add']))
194{
195  $page['errors'] = register_user(
196    $_POST['login'], $_POST['password'], $_POST['email']);
197
198  if (count($page['errors']) == 0)
199  {
200    array_push(
201      $page['infos'],
202      sprintf(
203        l10n('user "%s" added'),
204        $_POST['login']
205        )
206      );
207  }
208}
209
210// +-----------------------------------------------------------------------+
211// |                               user list                               |
212// +-----------------------------------------------------------------------+
213
214$page['filtered_users'] = get_filtered_user_list();
215
216// +-----------------------------------------------------------------------+
217// |                            selected users                             |
218// +-----------------------------------------------------------------------+
219
220if (isset($_POST['delete']) or isset($_POST['pref_submit']))
221{
222  $collection = array();
223
224  switch ($_POST['target'])
225  {
226    case 'all' :
227    {
228      foreach($page['filtered_users'] as $local_user)
229      {
230        array_push($collection, $local_user['id']);
231      }
232      break;
233    }
234    case 'selection' :
235    {
236      if (isset($_POST['selection']))
237      {
238        $collection = $_POST['selection'];
239      }
240      break;
241    }
242  }
243
244  if (count($collection) == 0)
245  {
246    array_push($page['errors'], l10n('Select at least one user'));
247  }
248}
249
250// +-----------------------------------------------------------------------+
251// |                             delete users                              |
252// +-----------------------------------------------------------------------+
253if (isset($_POST['delete']) and count($collection) > 0)
254{
255  if (in_array($conf['webmaster_id'], $collection))
256  {
257    array_push($page['errors'], l10n('Webmaster cannot be deleted'));
258  }
259  elseif (in_array($user['id'], $collection))
260  {
261    array_push($page['errors'], l10n('You cannot delete your account'));
262  }
263  else
264  {
265    if (isset($_POST['confirm_deletion']) and 1 == $_POST['confirm_deletion'])
266    {
267      foreach ($collection as $user_id)
268      {
269        delete_user($user_id);
270      }
271      array_push(
272        $page['infos'],
273        l10n_dec(
274          '%d user deleted', '%d users deleted',
275          count($collection)
276          )
277        );
278      foreach ($page['filtered_users'] as $filter_key => $filter_user)
279      {
280        if (in_array($filter_user['id'], $collection))
281        {
282          unset($page['filtered_users'][$filter_key]);
283        }
284      }
285    }
286    else
287    {
288      array_push($page['errors'], l10n('You need to confirm deletion'));
289    }
290  }
291}
292
293// +-----------------------------------------------------------------------+
294// |                       preferences form submission                     |
295// +-----------------------------------------------------------------------+
296
297if (isset($_POST['pref_submit']) and count($collection) > 0)
298{
299  if (-1 != $_POST['associate'])
300  {
301    $datas = array();
302
303    $query = '
304SELECT user_id
305  FROM '.USER_GROUP_TABLE.'
306  WHERE group_id = '.$_POST['associate'].'
307;';
308    $associated = array_from_query($query, 'user_id');
309
310    $associable = array_diff($collection, $associated);
311
312    if (count($associable) > 0)
313    {
314      foreach ($associable as $item)
315      {
316        array_push($datas,
317                   array('group_id'=>$_POST['associate'],
318                         'user_id'=>$item));
319      }
320
321      mass_inserts(USER_GROUP_TABLE,
322                   array('group_id', 'user_id'),
323                   $datas);
324    }
325  }
326
327  if (-1 != $_POST['dissociate'])
328  {
329    $query = '
330DELETE FROM '.USER_GROUP_TABLE.'
331  WHERE group_id = '.$_POST['dissociate'].'
332  AND user_id IN ('.implode(',', $collection).')
333';
334    pwg_query($query);
335  }
336
337  // properties to set for the collection (a user list)
338  $datas = array();
339  $dbfields = array('primary' => array('user_id'), 'update' => array());
340
341  $formfields =
342    array('nb_image_line', 'nb_line_page', 'template', 'language',
343          'recent_period', 'maxwidth', 'expand', 'show_nb_comments',
344          'show_nb_hits', 'maxheight', 'status', 'enabled_high');
345
346  $true_false_fields = array('expand', 'show_nb_comments', 
347                       'show_nb_hits', 'enabled_high');
348  if ($conf['allow_adviser'])
349  {
350    array_push($formfields, 'adviser');
351    array_push($true_false_fields, 'adviser');
352  }
353
354  foreach ($formfields as $formfield)
355  {
356    // special for true/false fields
357    if (in_array($formfield, $true_false_fields))
358    {
359      $test = $formfield;
360    }
361    else
362    {
363      $test = $formfield.'_action';
364    }
365
366    if ($_POST[$test] != 'leave')
367    {
368      array_push($dbfields['update'], $formfield);
369    }
370  }
371
372  // updating elements is useful only if needed...
373  if (count($dbfields['update']) > 0)
374  {
375    $datas = array();
376
377    foreach ($collection as $user_id)
378    {
379      $data = array();
380      $data['user_id'] = $user_id;
381
382      // TODO : verify if submited values are semanticaly correct
383      foreach ($dbfields['update'] as $dbfield)
384      {
385        // if the action is 'unset', the key won't be in row and
386        // mass_updates function will set this field to NULL
387        if (in_array($dbfield, $true_false_fields)
388            or 'set' == $_POST[$dbfield.'_action'])
389        {
390          $data[$dbfield] = $_POST[$dbfield];
391        }
392      }
393
394      // Webmaster status must not be changed
395      if ($conf['webmaster_id'] == $user_id and isset($data['status']))
396      {
397        $data['status'] = 'webmaster';
398      }
399
400      // Webmaster and guest adviser must not be changed
401      if ((($conf['webmaster_id'] == $user_id) or ($conf['guest_id'] == $user_id)) and isset($data['adviser']))
402      {
403        $data['adviser'] = 'false';
404      }
405
406      array_push($datas, $data);
407    }
408
409    mass_updates(USER_INFOS_TABLE, $dbfields, $datas);
410  }
411
412  redirect(
413    PHPWG_ROOT_PATH.
414    'admin.php'.
415    get_query_string_diff(
416      array(
417        'start'
418        )
419      )
420    );
421}
422
423// +-----------------------------------------------------------------------+
424// |                              groups list                              |
425// +-----------------------------------------------------------------------+
426
427$groups = array();
428
429$query = '
430SELECT id, name
431  FROM '.GROUPS_TABLE.'
432;';
433$result = pwg_query($query);
434
435while ($row = mysql_fetch_array($result))
436{
437  $groups[$row['id']] = $row['name'];
438}
439
440// +-----------------------------------------------------------------------+
441// |                             template init                             |
442// +-----------------------------------------------------------------------+
443
444$template->set_filenames(array('user_list'=>'admin/user_list.tpl'));
445
446$base_url = PHPWG_ROOT_PATH.'admin.php?page=user_list';
447
448if (isset($_GET['start']) and is_numeric($_GET['start']))
449{
450  $start = $_GET['start'];
451}
452else
453{
454  $start = 0;
455}
456
457$template->assign_vars(
458  array(
459    'U_HELP' => PHPWG_ROOT_PATH.'popuphelp.php?page=user_list',
460
461    'F_ADD_ACTION' => $base_url,
462    'F_USERNAME' => @htmlentities($_GET['username']),
463    'F_FILTER_ACTION' => PHPWG_ROOT_PATH.'admin.php'
464    ));
465
466if (isset($_GET['id']))
467{
468  $template->assign_block_vars('session', array('ID' => $_GET['id']));
469}
470
471// Hide radio-button if not allow to assign adviser
472if ($conf['allow_adviser'])
473{
474  $template->assign_block_vars('adviser', array());
475}
476
477foreach ($page['order_by_items'] as $item => $label)
478{
479  $selected = (isset($_GET['order_by']) and $_GET['order_by'] == $item) ?
480    'selected="selected"' : '';
481  $template->assign_block_vars(
482    'order_by',
483    array(
484      'VALUE' => $item,
485      'CONTENT' => $label,
486      'SELECTED' => $selected
487      ));
488}
489
490foreach ($page['direction_items'] as $item => $label)
491{
492  $selected = (isset($_GET['direction']) and $_GET['direction'] == $item) ?
493    'selected="selected"' : '';
494  $template->assign_block_vars(
495    'direction',
496    array(
497      'VALUE' => $item,
498      'CONTENT' => $label,
499      'SELECTED' => $selected
500      ));
501}
502
503$blockname = 'group_option';
504
505$template->assign_block_vars(
506  $blockname,
507  array(
508    'VALUE'=> -1,
509    'CONTENT' => '------------',
510    'SELECTED' => ''
511    ));
512
513foreach ($groups as $group_id => $group_name)
514{
515  $selected = (isset($_GET['group']) and $_GET['group'] == $group_id) ?
516    'selected="selected"' : '';
517  $template->assign_block_vars(
518    $blockname,
519    array(
520      'VALUE' => $group_id,
521      'CONTENT' => $group_name,
522      'SELECTED' => $selected
523      ));
524}
525
526$blockname = 'status_option';
527
528$template->assign_block_vars(
529  $blockname,
530  array(
531    'VALUE'=> -1,
532    'CONTENT' => '------------',
533    'SELECTED' => ''
534    ));
535
536foreach (get_enums(USER_INFOS_TABLE, 'status') as $status)
537{
538  $selected = (isset($_GET['status']) and $_GET['status'] == $status) ?
539    'selected="selected"' : '';
540  $template->assign_block_vars(
541    $blockname,
542    array(
543      'VALUE' => $status,
544      'CONTENT' => $lang['user_status_'.$status],
545      'SELECTED' => $selected
546      ));
547}
548
549if (isset($_POST['pref_submit']))
550{
551//  echo '<pre>'; print_r($_POST); echo '</pre>';
552  $template->assign_vars(
553    array(
554      'ADVISER_YES' => 'true' == (isset($_POST['adviser']) and $_POST['adviser']) ? 'checked="checked"' : '',
555      'ADVISER_NO' => 'false' == (isset($_POST['adviser']) and $_POST['adviser']) ? 'checked="checked"' : '',
556      'NB_IMAGE_LINE' => $_POST['nb_image_line'],
557      'NB_LINE_PAGE' => $_POST['nb_line_page'],
558      'MAXWIDTH' => $_POST['maxwidth'],
559      'MAXHEIGHT' => $_POST['maxheight'],
560      'RECENT_PERIOD' => $_POST['recent_period'],
561      'EXPAND_YES' => 'true' == $_POST['expand'] ? 'checked="checked"' : '',
562      'EXPAND_NO' => 'false' == $_POST['expand'] ? 'checked="checked"' : '',
563      'SHOW_NB_COMMENTS_YES' =>
564        'true' == $_POST['show_nb_comments'] ? 'checked="checked"' : '',
565      'SHOW_NB_COMMENTS_NO' =>
566        'false' == $_POST['show_nb_comments'] ? 'checked="checked"' : '',
567      'SHOW_NB_HITS_YES' =>
568        'true' == $_POST['show_nb_hits'] ? 'checked="checked"' : '',
569      'SHOW_NB_HITS_NO' =>
570        'false' == $_POST['show_nb_hits'] ? 'checked="checked"' : '',
571      'ENABLED_HIGH_YES' => 'true' == $_POST['enabled_high'] ? 'checked="checked"' : '',
572      'ENABLED_HIGH_NO' => 'false' == $_POST['enabled_high'] ? 'checked="checked"' : '',
573      ));
574}
575else
576{
577  $default_user = get_default_user_info(true);
578  $template->assign_vars(
579    array(
580      'NB_IMAGE_LINE' => $default_user['nb_image_line'],
581      'NB_LINE_PAGE' => $default_user['nb_line_page'],
582      'MAXWIDTH' => $default_user['maxwidth'],
583      'MAXHEIGHT' => $default_user['maxheight'],
584      'RECENT_PERIOD' => $default_user['recent_period'],
585      ));
586}
587
588$blockname = 'template_option';
589
590foreach (get_pwg_themes() as $pwg_template)
591{
592  if (isset($_POST['pref_submit']))
593  {
594    $selected = $_POST['template']==$pwg_template ? 'selected="selected"' : '';
595  }
596  else if (get_default_template() == $pwg_template)
597  {
598    $selected = 'selected="selected"';
599  }
600  else
601  {
602    $selected = '';
603  }
604
605  $template->assign_block_vars(
606    $blockname,
607    array(
608      'VALUE'=> $pwg_template,
609      'CONTENT' => $pwg_template,
610      'SELECTED' => $selected
611      ));
612}
613
614$blockname = 'language_option';
615
616foreach (get_languages() as $language_code => $language_name)
617{
618  if (isset($_POST['pref_submit']))
619  {
620    $selected = $_POST['language']==$language_code ? 'selected="selected"':'';
621  }
622  else if (get_default_language() == $language_code)
623  {
624    $selected = 'selected="selected"';
625  }
626  else
627  {
628    $selected = '';
629  }
630
631  $template->assign_block_vars(
632    $blockname,
633    array(
634      'VALUE'=> $language_code,
635      'CONTENT' => $language_name,
636      'SELECTED' => $selected
637      ));
638}
639
640$blockname = 'pref_status_option';
641
642foreach (get_enums(USER_INFOS_TABLE, 'status') as $status)
643{
644  if (isset($_POST['pref_submit']))
645  {
646    $selected = $_POST['status'] == $status ? 'selected="selected"' : '';
647  }
648  else if ('normal' == $status)
649  {
650    $selected = 'selected="selected"';
651  }
652  else
653  {
654    $selected = '';
655  }
656
657  // Only status <= can be assign
658  if (is_autorize_status(get_access_type_status($status)))
659  {
660    $template->assign_block_vars(
661      $blockname,
662      array(
663        'VALUE' => $status,
664        'CONTENT' => $lang['user_status_'.$status],
665        'SELECTED' => $selected
666        ));
667  }
668}
669
670// associate
671$blockname = 'associate_option';
672
673$template->assign_block_vars(
674  $blockname,
675  array(
676    'VALUE'=> -1,
677    'CONTENT' => '------------',
678    'SELECTED' => ''
679    ));
680
681foreach ($groups as $group_id => $group_name)
682{
683  if (isset($_POST['pref_submit']))
684  {
685    $selected = $_POST['associate'] == $group_id ? 'selected="selected"' : '';
686  }
687  else
688  {
689    $selected = '';
690  }
691
692  $template->assign_block_vars(
693    $blockname,
694    array(
695      'VALUE' => $group_id,
696      'CONTENT' => $group_name,
697      'SELECTED' => $selected
698      ));
699}
700
701// dissociate
702$blockname = 'dissociate_option';
703
704$template->assign_block_vars(
705  $blockname,
706  array(
707    'VALUE'=> -1,
708    'CONTENT' => '------------',
709    'SELECTED' => ''
710    ));
711
712foreach ($groups as $group_id => $group_name)
713{
714  if (isset($_POST['pref_submit']))
715  {
716    $selected = $_POST['dissociate'] == $group_id ? 'selected="selected"' : '';
717  }
718  else
719  {
720    $selected = '';
721  }
722
723  $template->assign_block_vars(
724    $blockname,
725    array(
726      'VALUE' => $group_id,
727      'CONTENT' => $group_name,
728      'SELECTED' => $selected
729      ));
730}
731
732// +-----------------------------------------------------------------------+
733// |                            navigation bar                             |
734// +-----------------------------------------------------------------------+
735
736$url = PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start'));
737
738$navbar = create_navigation_bar(
739  $url,
740  count($page['filtered_users']),
741  $start,
742  $conf['users_page']
743  );
744
745$template->assign_vars(array('NAVBAR' => $navbar));
746
747// +-----------------------------------------------------------------------+
748// |                               user list                               |
749// +-----------------------------------------------------------------------+
750
751$profile_url = get_root_url().'admin.php?page=profile&amp;user_id=';
752$perm_url = get_root_url().'admin.php?page=user_perm&amp;user_id=';
753
754foreach ($page['filtered_users'] as $num => $local_user)
755{
756  // simulate LIMIT $start, $conf['users_page']
757  if ($num < $start)
758  {
759    continue;
760  }
761  if ($num >= $start + $conf['users_page'])
762  {
763    break;
764  }
765
766  $groups_string = preg_replace(
767    '/(\d+)/e',
768    "\$groups['$1']",
769    implode(
770      ', ',
771      $local_user['groups']
772      )
773    );
774
775  if (isset($_POST['pref_submit'])
776      and isset($_POST['selection'])
777      and in_array($local_user['id'], $_POST['selection']))
778  {
779    $checked = 'checked="checked"';
780  }
781  else
782  {
783    $checked = '';
784  }
785
786  $template->assign_block_vars(
787    'user',
788    array(
789      'CLASS' => ($num % 2 == 1) ? 'row2' : 'row1',
790      'ID' => $local_user['id'],
791      'CHECKED' => $checked,
792      'U_PROFILE' => $profile_url.$local_user['id'],
793      'U_PERM' => $perm_url.$local_user['id'],
794      'USERNAME' => $local_user['username']
795        .($local_user['id'] == $conf['guest_id']
796          ? '<BR />['.l10n('is_the_guest').']' : '')
797        .($local_user['id'] == $conf['default_user_id']
798          ? '<BR />['.l10n('is_the_default').']' : ''),
799      'STATUS' => $lang['user_status_'.
800        $local_user['status']].(($local_user['adviser'] == 'true')
801        ? '<BR />['.l10n('adviser').']' : ''),
802      'EMAIL' => get_email_address_as_display_text($local_user['email']),
803      'GROUPS' => $groups_string,
804      'PROPERTIES' => 
805        (isset($local_user['enabled_high']) and ($local_user['enabled_high'] == 'true'))
806        ? $lang['is_high_enabled'] : $lang['is_high_disabled']
807      )
808    );
809}
810
811// +-----------------------------------------------------------------------+
812// |                           html code display                           |
813// +-----------------------------------------------------------------------+
814
815$template->assign_var_from_handle('ADMIN_CONTENT', 'user_list');
816?>
Note: See TracBrowser for help on using the repository browser.