source: tags/release-1_5_0/admin/user_list.php @ 979

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