source: trunk/admin/user_list.php @ 1753

Last change on this file since 1753 was 1753, checked in by rvelices, 17 years ago
  • user profiles available from admin page
  • user creation from admin page with email (bug 514)
  • some language cleanup
  • small template enhancements
  • php syntax corrections (my mistake)
  • Property svn:eol-style set to native
  • 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-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2007-01-25 02:18:56 +0000 (Thu, 25 Jan 2007) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1753 $
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                ui.adviser,
101                ui.enabled_high
102  FROM '.USERS_TABLE.' AS u
103    INNER JOIN '.USER_INFOS_TABLE.' AS ui
104      ON u.'.$conf['user_fields']['id'].' = ui.user_id
105    LEFT JOIN '.USER_GROUP_TABLE.' AS ug
106      ON u.'.$conf['user_fields']['id'].' = ug.user_id
107  WHERE u.'.$conf['user_fields']['id'].' != '.$conf['guest_id'];
108  if (isset($filter['username']))
109  {
110    $query.= '
111  AND u.'.$conf['user_fields']['username'].' LIKE \''.$filter['username'].'\'';
112  }
113  if (isset($filter['group']))
114  {
115    $query.= '
116    AND ug.group_id = '.$filter['group'];
117  }
118  if (isset($filter['status']))
119  {
120    $query.= '
121    AND ui.status = \''.$filter['status']."'";
122  }
123  $query.= '
124  ORDER BY '.$order_by.' '.$direction.'
125;';
126
127  $result = pwg_query($query);
128  while ($row = mysql_fetch_array($result))
129  {
130    $user = $row;
131    $user['groups'] = array();
132
133    array_push($users, $user);
134  }
135
136  // add group lists
137  $user_ids = array();
138  foreach ($users as $i => $user)
139  {
140    $user_ids[$i] = $user['id'];
141  }
142  $user_nums = array_flip($user_ids);
143
144  if (count($user_ids) > 0)
145  {
146    $query = '
147SELECT user_id, group_id
148  FROM '.USER_GROUP_TABLE.'
149  WHERE user_id IN ('.implode(',', $user_ids).')
150;';
151    $result = pwg_query($query);
152    while ($row = mysql_fetch_array($result))
153    {
154      array_push(
155        $users[$user_nums[$row['user_id']]]['groups'],
156        $row['group_id']
157        );
158    }
159  }
160
161  return $users;
162}
163
164// +-----------------------------------------------------------------------+
165// |                           initialization                              |
166// +-----------------------------------------------------------------------+
167
168if (!defined('PHPWG_ROOT_PATH'))
169{
170  die('Hacking attempt!');
171}
172
173include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
174
175// +-----------------------------------------------------------------------+
176// | Check Access and exit when user status is not ok                      |
177// +-----------------------------------------------------------------------+
178check_status(ACCESS_ADMINISTRATOR);
179
180$page['order_by_items'] = array(
181  'id' => $lang['registration_date'],
182  'username' => $lang['Username']
183  );
184
185$page['direction_items'] = array(
186  'asc' => $lang['ascending'],
187  'desc' => $lang['descending']
188  );
189
190// +-----------------------------------------------------------------------+
191// |                              add a user                               |
192// +-----------------------------------------------------------------------+
193
194if (isset($_POST['submit_add']))
195{
196  $page['errors'] = register_user(
197    $_POST['login'], $_POST['password'], $_POST['email']);
198
199  if (count($page['errors']) == 0)
200  {
201    array_push(
202      $page['infos'],
203      sprintf(
204        l10n('user "%s" added'),
205        $_POST['login']
206        )
207      );
208  }
209}
210
211// +-----------------------------------------------------------------------+
212// |                               user list                               |
213// +-----------------------------------------------------------------------+
214
215$page['filtered_users'] = get_filtered_user_list();
216
217// +-----------------------------------------------------------------------+
218// |                            selected users                             |
219// +-----------------------------------------------------------------------+
220
221if (isset($_POST['delete']) or isset($_POST['pref_submit']))
222{
223  $collection = array();
224
225  switch ($_POST['target'])
226  {
227    case 'all' :
228    {
229      foreach($page['filtered_users'] as $local_user)
230      {
231        array_push($collection, $local_user['id']);
232      }
233      break;
234    }
235    case 'selection' :
236    {
237      if (isset($_POST['selection']))
238      {
239        $collection = $_POST['selection'];
240      }
241      break;
242    }
243  }
244
245  if (count($collection) == 0)
246  {
247    array_push($page['errors'], l10n('Select at least one user'));
248  }
249}
250
251// +-----------------------------------------------------------------------+
252// |                             delete users                              |
253// +-----------------------------------------------------------------------+
254if (isset($_POST['delete']) and count($collection) > 0)
255{
256  if (in_array($conf['webmaster_id'], $collection))
257  {
258    array_push($page['errors'], l10n('Webmaster cannot be deleted'));
259  }
260  elseif (in_array($user['id'], $collection))
261  {
262    array_push($page['errors'], l10n('You cannot delete your account'));
263  }
264  else
265  {
266    if (isset($_POST['confirm_deletion']) and 1 == $_POST['confirm_deletion'])
267    {
268      foreach ($collection as $user_id)
269      {
270        delete_user($user_id);
271      }
272      array_push(
273        $page['infos'],
274        sprintf(
275          l10n('%d users deleted'),
276          count($collection)
277          )
278        );
279      foreach ($page['filtered_users'] as $filter_key => $filter_user)
280      {
281        if (in_array($filter_user['id'], $collection))
282        {
283          unset($page['filtered_users'][$filter_key]);
284        }
285      }
286    }
287    else
288    {
289      array_push($page['errors'], l10n('You need to confirm deletion'));
290    }
291  }
292}
293
294// +-----------------------------------------------------------------------+
295// |                       preferences form submission                     |
296// +-----------------------------------------------------------------------+
297
298if (isset($_POST['pref_submit']) and count($collection) > 0)
299{
300  if (-1 != $_POST['associate'])
301  {
302    $datas = array();
303
304    $query = '
305SELECT user_id
306  FROM '.USER_GROUP_TABLE.'
307  WHERE group_id = '.$_POST['associate'].'
308;';
309    $associated = array_from_query($query, 'user_id');
310
311    $associable = array_diff($collection, $associated);
312
313    if (count($associable) > 0)
314    {
315      foreach ($associable as $item)
316      {
317        array_push($datas,
318                   array('group_id'=>$_POST['associate'],
319                         'user_id'=>$item));
320      }
321
322      mass_inserts(USER_GROUP_TABLE,
323                   array('group_id', 'user_id'),
324                   $datas);
325    }
326  }
327
328  if (-1 != $_POST['dissociate'])
329  {
330    $query = '
331DELETE FROM '.USER_GROUP_TABLE.'
332  WHERE group_id = '.$_POST['dissociate'].'
333  AND user_id IN ('.implode(',', $collection).')
334';
335    pwg_query($query);
336  }
337
338  // properties to set for the collection (a user list)
339  $datas = array();
340  $dbfields = array('primary' => array('user_id'), 'update' => array());
341
342  $formfields =
343    array('nb_image_line', 'nb_line_page', 'template', 'language',
344          'recent_period', 'maxwidth', 'expand', 'show_nb_comments',
345          'maxheight', 'status', 'enabled_high');
346
347  $true_false_fields = array('expand', 'show_nb_comments', '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
549// ---
550//   $user['template'] = $conf['default_template'];
551//   $user['nb_image_line'] = $conf['nb_image_line'];
552//   $user['nb_line_page'] = $conf['nb_line_page'];
553//   $user['language'] = $conf['default_language'];
554//   $user['maxwidth'] = $conf['default_maxwidth'];
555//   $user['maxheight'] = $conf['default_maxheight'];
556//   $user['recent_period'] = $conf['recent_period'];
557//   $user['expand'] = $conf['auto_expand'];
558//   $user['show_nb_comments'] = $conf['show_nb_comments'];
559// ---
560
561if (isset($_POST['pref_submit']))
562{
563//  echo '<pre>'; print_r($_POST); echo '</pre>';
564  $template->assign_vars(
565    array(
566      'ADVISER_YES' => 'true' == (isset($_POST['adviser']) and $_POST['adviser']) ? 'checked="checked"' : '',
567      'ADVISER_NO' => 'false' == (isset($_POST['adviser']) and $_POST['adviser']) ? 'checked="checked"' : '',
568      'NB_IMAGE_LINE' => $_POST['nb_image_line'],
569      'NB_LINE_PAGE' => $_POST['nb_line_page'],
570      'MAXWIDTH' => $_POST['maxwidth'],
571      'MAXHEIGHT' => $_POST['maxheight'],
572      'RECENT_PERIOD' => $_POST['recent_period'],
573      'EXPAND_YES' => 'true' == $_POST['expand'] ? 'checked="checked"' : '',
574      'EXPAND_NO' => 'false' == $_POST['expand'] ? 'checked="checked"' : '',
575      'SHOW_NB_COMMENTS_YES' =>
576        'true' == $_POST['show_nb_comments'] ? 'checked="checked"' : '',
577      'SHOW_NB_COMMENTS_NO' =>
578        'false' == $_POST['show_nb_comments'] ? 'checked="checked"' : '',
579      'ENABLED_HIGH_YES' => 'true' == $_POST['enabled_high'] ? 'checked="checked"' : '',
580      'ENABLED_HIGH_NO' => 'false' == $_POST['enabled_high'] ? 'checked="checked"' : '',
581      ));
582}
583else
584{
585  $template->assign_vars(
586    array(
587      'NB_IMAGE_LINE' => $conf['nb_image_line'],
588      'NB_LINE_PAGE' => $conf['nb_line_page'],
589      'MAXWIDTH' => @$conf['default_maxwidth'],
590      'MAXHEIGHT' => @$conf['default_maxheight'],
591      'RECENT_PERIOD' => $conf['recent_period'],
592      ));
593}
594
595$blockname = 'template_option';
596
597foreach (get_pwg_themes() as $pwg_template)
598{
599  if (isset($_POST['pref_submit']))
600  {
601    $selected = $_POST['template']==$pwg_template ? 'selected="selected"' : '';
602  }
603  else if ($conf['default_template'] == $pwg_template)
604  {
605    $selected = 'selected="selected"';
606  }
607  else
608  {
609    $selected = '';
610  }
611
612  $template->assign_block_vars(
613    $blockname,
614    array(
615      'VALUE'=> $pwg_template,
616      'CONTENT' => $pwg_template,
617      'SELECTED' => $selected
618      ));
619}
620
621$blockname = 'language_option';
622
623foreach (get_languages() as $language_code => $language_name)
624{
625  if (isset($_POST['pref_submit']))
626  {
627    $selected = $_POST['language']==$language_code ? 'selected="selected"':'';
628  }
629  else if ($conf['default_language'] == $language_code)
630  {
631    $selected = 'selected="selected"';
632  }
633  else
634  {
635    $selected = '';
636  }
637
638  $template->assign_block_vars(
639    $blockname,
640    array(
641      'VALUE'=> $language_code,
642      'CONTENT' => $language_name,
643      'SELECTED' => $selected
644      ));
645}
646
647$blockname = 'pref_status_option';
648
649foreach (get_enums(USER_INFOS_TABLE, 'status') as $status)
650{
651  if (isset($_POST['pref_submit']))
652  {
653    $selected = $_POST['status'] == $status ? 'selected="selected"' : '';
654  }
655  else if ('normal' == $status)
656  {
657    $selected = 'selected="selected"';
658  }
659  else
660  {
661    $selected = '';
662  }
663
664  // Only status <= can be assign
665  if (is_autorize_status(get_access_type_status($status)))
666  {
667    $template->assign_block_vars(
668      $blockname,
669      array(
670        'VALUE' => $status,
671        'CONTENT' => $lang['user_status_'.$status],
672        'SELECTED' => $selected
673        ));
674  }
675}
676
677// associate
678$blockname = 'associate_option';
679
680$template->assign_block_vars(
681  $blockname,
682  array(
683    'VALUE'=> -1,
684    'CONTENT' => '------------',
685    'SELECTED' => ''
686    ));
687
688foreach ($groups as $group_id => $group_name)
689{
690  if (isset($_POST['pref_submit']))
691  {
692    $selected = $_POST['associate'] == $group_id ? 'selected="selected"' : '';
693  }
694  else
695  {
696    $selected = '';
697  }
698
699  $template->assign_block_vars(
700    $blockname,
701    array(
702      'VALUE' => $group_id,
703      'CONTENT' => $group_name,
704      'SELECTED' => $selected
705      ));
706}
707
708// dissociate
709$blockname = 'dissociate_option';
710
711$template->assign_block_vars(
712  $blockname,
713  array(
714    'VALUE'=> -1,
715    'CONTENT' => '------------',
716    'SELECTED' => ''
717    ));
718
719foreach ($groups as $group_id => $group_name)
720{
721  if (isset($_POST['pref_submit']))
722  {
723    $selected = $_POST['dissociate'] == $group_id ? 'selected="selected"' : '';
724  }
725  else
726  {
727    $selected = '';
728  }
729
730  $template->assign_block_vars(
731    $blockname,
732    array(
733      'VALUE' => $group_id,
734      'CONTENT' => $group_name,
735      'SELECTED' => $selected
736      ));
737}
738
739// +-----------------------------------------------------------------------+
740// |                            navigation bar                             |
741// +-----------------------------------------------------------------------+
742
743$url = PHPWG_ROOT_PATH.'admin.php'.get_query_string_diff(array('start'));
744
745$navbar = create_navigation_bar(
746  $url,
747  count($page['filtered_users']),
748  $start,
749  $conf['users_page']
750  );
751
752$template->assign_vars(array('NAVBAR' => $navbar));
753
754// +-----------------------------------------------------------------------+
755// |                               user list                               |
756// +-----------------------------------------------------------------------+
757
758$profile_url = get_root_url().'admin.php?page=profile&amp;user_id=';
759$perm_url = get_root_url().'admin.php?page=user_perm&amp;user_id=';
760
761foreach ($page['filtered_users'] as $num => $local_user)
762{
763  // simulate LIMIT $start, $conf['users_page']
764  if ($num < $start)
765  {
766    continue;
767  }
768  if ($num >= $start + $conf['users_page'])
769  {
770    break;
771  }
772
773  $groups_string = preg_replace(
774    '/(\d+)/e',
775    "\$groups['$1']",
776    implode(
777      ', ',
778      $local_user['groups']
779      )
780    );
781
782  if (isset($_POST['pref_submit'])
783      and isset($_POST['selection'])
784      and in_array($local_user['id'], $_POST['selection']))
785  {
786    $checked = 'checked="checked"';
787  }
788  else
789  {
790    $checked = '';
791  }
792
793  $template->assign_block_vars(
794    'user',
795    array(
796      'CLASS' => ($num % 2 == 1) ? 'row2' : 'row1',
797      'ID' => $local_user['id'],
798      'CHECKED' => $checked,
799      'U_PROFILE' => $profile_url.$local_user['id'],
800      'U_PERM' => $perm_url.$local_user['id'],
801      'USERNAME' => $local_user['username'],
802      'STATUS' => $lang['user_status_'.$local_user['status']].(($local_user['adviser'] == 'true') ? ' ['.$lang['adviser'].']' : ''),
803      'EMAIL' => get_email_address_as_display_text($local_user['email']),
804      'GROUPS' => $groups_string,
805      'PROPERTIES' => (isset($local_user['enabled_high']) and ($local_user['enabled_high'] == 'true')) ? $lang['is_high_enabled'] : $lang['is_high_disabled']
806      )
807    );
808}
809
810// +-----------------------------------------------------------------------+
811// |                           html code display                           |
812// +-----------------------------------------------------------------------+
813
814$template->assign_var_from_handle('ADMIN_CONTENT', 'user_list');
815?>
Note: See TracBrowser for help on using the repository browser.