source: extensions/LCAS/branch/2.3/include/admin_search.inc.php

Last change on this file was 27127, checked in by LucMorizur, 10 years ago

Add branch 2.3

  • Property svn:eol-style set to LF
File size: 8.3 KB
Line 
1<?php
2// Keep file coded in UTF-8 without BOM: é
3
4if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
5
6global $template, $conf, $page;
7
8$conf_LCAS = unserialize($conf['LoginCaseAccentsSensitivity']);
9
10/**
11 * Add users and manage users list according to LCAS plugin
12 * ( http://piwigo.org/ext/extension_view.php?eid=513 )
13 */
14
15// +-----------------------------------------------------------------------+
16// |                              functions                                |
17// +-----------------------------------------------------------------------+
18
19/**
20 * returns a list of users depending on page filters (in $_GET)
21 *
22 * Each user comes with his related informations : id, username, mail
23 * address, list of groups.
24 *
25 * @return array
26 */
27function LCAS_get_filtered_user_list()
28{
29  global $conf, $page, $conf_LCAS;
30
31  $conf_LCAS= unserialize($conf['LoginCaseAccentsSensitivity']);
32
33  $users = array();
34  $users_t1 = array();
35
36  // filter
37  $filter = array();
38
39    // Build a PHP regex both complying to user's request,
40    // and to LCAS current rule
41    $username=  preg_replace(array('/^\*/', '/\*$/'), '%', $_GET['username']);
42    if (preg_match('/%$/', $username))
43     $username = preg_replace('/%$/', '/', $username);
44    else
45     $username.= '$/';
46    if (preg_match('/^%/', $username))
47     $username = preg_replace('/^%/', '/', $username);
48    else
49     $username = '/^'.$username;
50    $filter['username'] =
51     pwg_db_real_escape_string(LCAS_change_case($username, $conf_LCAS[0]));
52
53  if (isset($_GET['group'])
54      and -1 != $_GET['group']
55      and is_numeric($_GET['group']))
56  {
57    $filter['group'] = $_GET['group'];
58  }
59
60  if (isset($_GET['status'])
61      and in_array($_GET['status'], get_enums(USER_INFOS_TABLE, 'status')))
62  {
63    $filter['status'] = $_GET['status'];
64  }
65
66  // how to order the list?
67  $order_by = 'id';
68  if (isset($_GET['order_by'])
69      and in_array($_GET['order_by'], array_keys($page['order_by_items'])))
70  {
71    $order_by = $_GET['order_by'];
72  }
73
74  $direction = 'ASC';
75  if (isset($_GET['direction'])
76      and in_array($_GET['direction'], array_keys($page['direction_items'])))
77  {
78    $direction = strtoupper($_GET['direction']);
79  }
80
81  // search users depending on filters and order
82  $query = '
83SELECT DISTINCT u.'.$conf['user_fields']['id'].' AS id,
84                u.'.$conf['user_fields']['username'].' AS username,
85                u.'.$conf['user_fields']['email'].' AS email,
86                ui.status,
87                ui.enabled_high,
88                ui.level
89  FROM '.USERS_TABLE.' AS u
90    INNER JOIN '.USER_INFOS_TABLE.' AS ui
91      ON u.'.$conf['user_fields']['id'].' = ui.user_id
92    LEFT JOIN '.USER_GROUP_TABLE.' AS ug
93      ON u.'.$conf['user_fields']['id'].' = ug.user_id
94  WHERE u.'.$conf['user_fields']['id'].' > 0';
95
96  if (isset($filter['group']))
97  {
98    $query.= '
99    AND ug.group_id = '.$filter['group'];
100  }
101  if (isset($filter['status']))
102  {
103    $query.= '
104    AND ui.status = \''.$filter['status']."'";
105  }
106  $query.= '
107  ORDER BY '.$order_by.' '.$direction.'
108;';
109
110  $result = pwg_query($query);
111  while ($row = pwg_db_fetch_assoc($result))
112  {
113    $user = $row;
114    $user['groups'] = array();
115
116    array_push($users, $user);
117  }
118
119  // add group lists
120  $user_ids = array();
121  foreach ($users as $i => $user)
122  {
123    $user_ids[$i] = $user['id'];
124  }
125  $user_nums = array_flip($user_ids);
126
127  if (count($user_ids) > 0)
128  {
129    $query = '
130SELECT user_id, group_id
131  FROM '.USER_GROUP_TABLE.'
132  WHERE user_id IN ('.implode(',', $user_ids).')
133;';
134    $result = pwg_query($query);
135    while ($row = pwg_db_fetch_assoc($result))
136    {
137      array_push(
138        $users[$user_nums[$row['user_id']]]['groups'],
139        $row['group_id']
140        );
141    }
142  }
143 
144  // $users is now all usernames matching all filters but the username
145  // filter
146  // Username filter applied now: convert all found usernames with current
147  // LCAS rule, and compare to LCAS-converted filter on username
148  foreach ($users as $user) if (preg_match(
149   $filter['username'], LCAS_change_case($user['username'], $conf_LCAS[0])
150   )) $users_t1[] = $user;
151 
152  return $users_t1;
153}
154
155// +-----------------------------------------------------------------------+
156// |                           initialization                              |
157// +-----------------------------------------------------------------------+
158
159if (!defined('PHPWG_ROOT_PATH'))
160{
161  die('Hacking attempt!');
162}
163
164include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
165
166// +-----------------------------------------------------------------------+
167// |                               user list                               |
168// +-----------------------------------------------------------------------+
169
170$page['filtered_users'] = LCAS_get_filtered_user_list();
171
172// +-----------------------------------------------------------------------+
173// |                              groups list                              |
174// +-----------------------------------------------------------------------+
175
176$groups[-1] = '------------';
177
178$query = '
179SELECT id, name
180  FROM '.GROUPS_TABLE.'
181  ORDER BY name ASC
182;';
183$result = pwg_query($query);
184
185while ($row = pwg_db_fetch_assoc($result))
186{
187  $groups[$row['id']] = $row['name'];
188}
189
190// +-----------------------------------------------------------------------+
191// |                             template init                             |
192// +-----------------------------------------------------------------------+
193
194$base_url = PHPWG_ROOT_PATH.'admin.php?page=user_list';
195
196if (isset($_GET['start']) and is_numeric($_GET['start']))
197{
198  $start = $_GET['start'];
199}
200else
201{
202  $start = 0;
203}
204
205$template->assign(
206  'F_USERNAME', @htmlentities($_GET['username'], ENT_COMPAT, 'UTF-8')
207);
208
209$profile_url = get_root_url().'admin.php?page=profile&amp;user_id=';
210$perm_url = get_root_url().'admin.php?page=user_perm&amp;user_id=';
211
212$visible_user_list = array();
213foreach ($page['filtered_users'] as $num => $local_user)
214{
215  // simulate LIMIT $start, $conf['users_page']
216  if ($num < $start)
217  {
218    continue;
219  }
220  if ($num >= $start + $conf['users_page'])
221  {
222    break;
223  }
224
225  $visible_user_list[] = $local_user;
226}
227
228// Reset TPL variables
229$template->smarty->_tpl_vars['users'] = array();
230$template->smarty->_tpl_vars['plugin_columns'] = array();
231$template->smarty->_tpl_vars['plugin_actions'] = array();
232
233// allow plugins to fill template var plugin_user_list_column_titles and
234// plugin_columns/plugin_actions for each user in the list
235$visible_user_list =
236 trigger_event('loc_visible_user_list', $visible_user_list);
237
238foreach ($visible_user_list as $local_user)
239{
240  $groups_string = preg_replace(
241    '/(\d+)/e',
242    "\$groups['$1']",
243    implode(
244      ', ',
245      $local_user['groups']
246      )
247    );
248
249  if (isset($_POST['pref_submit'])
250      and isset($_POST['selection'])
251      and in_array($local_user['id'], $_POST['selection']))
252  {
253    $checked = 'checked="checked"';
254  }
255  else
256  {
257    $checked = '';
258  }
259
260  $properties = array();
261  if ( $local_user['level'] != 0 )
262  {
263    $properties[] = l10n( sprintf('Level %d', $local_user['level']) );
264  }
265  $properties[] = (
266    isset($local_user['enabled_high']) and
267    ($local_user['enabled_high'] == 'true')
268  ) ? l10n('High definition') : l10n('');
269
270  $template->append(
271    'users',
272    array(
273      'ID' => $local_user['id'],
274      'CHECKED' => $checked,
275      'U_PROFILE' => $profile_url.$local_user['id'],
276      'U_PERM' => $perm_url.$local_user['id'],
277      'USERNAME' => stripslashes($local_user['username'])
278        .($local_user['id'] == $conf['guest_id']
279          ? '<br>['.l10n('guest').']' : '')
280        .($local_user['id'] == $conf['default_user_id']
281          ? '<br>['.l10n('default values').']' : ''),
282      'STATUS' => l10n('user_status_'.$local_user['status']),
283      'EMAIL' => $local_user['email'],
284      'GROUPS' => $groups_string,
285      'PROPERTIES' => implode( ', ', $properties),
286      'plugin_columns' => isset($local_user['plugin_columns']) ?
287       $local_user['plugin_columns'] : array(),
288      'plugin_actions' => isset($local_user['plugin_actions']) ?
289       $local_user['plugin_actions'] : array(),
290      )
291    );
292}
293
294// +-----------------------------------------------------------------------+
295// |                           html code display                           |
296// +-----------------------------------------------------------------------+
297
298$template->assign_var_from_handle('ADMIN_CONTENT', 'user_list');
299?>
Note: See TracBrowser for help on using the repository browser.