source: trunk/admin/user_list.php @ 805

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