| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Provides an easy way to view all information about users.
|
| 7 |
*
|
| 8 |
* Allows Site Adminstrators (Users with the "Administer Users"
|
| 9 |
* permission view lots of information about registered users.
|
| 10 |
*/
|
| 11 |
|
| 12 |
/**
|
| 13 |
* Implementation of hook_menu().
|
| 14 |
*
|
| 15 |
* @return Array of menu items
|
| 16 |
*/
|
| 17 |
function userinfo_menu($may_cache) {
|
| 18 |
$items = array();
|
| 19 |
if (!$may_cache) {
|
| 20 |
/**
|
| 21 |
* Actual Info View
|
| 22 |
*/
|
| 23 |
$items[] = array(
|
| 24 |
'path' => 'admin/user/userinfo',
|
| 25 |
'title' => t('User Info'),
|
| 26 |
'description' => t('Show info on all users'),
|
| 27 |
'callback' => 'userinfo_admin_users',
|
| 28 |
'access' => user_access('administer users'),
|
| 29 |
);
|
| 30 |
/**
|
| 31 |
* CSV output
|
| 32 |
*/
|
| 33 |
$items[] = array(
|
| 34 |
'path' => 'admin/user/userinfo/csv',
|
| 35 |
'title' => t('CSV'),
|
| 36 |
'description' => t('Create a CSV file with info on all users'),
|
| 37 |
'callback' => 'userinfo_generate_csv_file',
|
| 38 |
'access' => user_access('administer users'),
|
| 39 |
'type' => MENU_CALLBACK,
|
| 40 |
);
|
| 41 |
/**
|
| 42 |
* Configuration
|
| 43 |
*/
|
| 44 |
$items[] = array(
|
| 45 |
'path' => 'admin/settings/userinfo',
|
| 46 |
'title' => t('User Info'),
|
| 47 |
'description' => t('Configure how much User Info is displayed'),
|
| 48 |
'callback' => 'drupal_get_form',
|
| 49 |
'callback arguments' => array('userinfo_admin_settings'),
|
| 50 |
'access' => user_access('administer site configuration'),
|
| 51 |
);
|
| 52 |
}
|
| 53 |
return $items;
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Define the settings form.
|
| 58 |
*
|
| 59 |
* @return The form's settings to be rendered by Drupal
|
| 60 |
*
|
| 61 |
* @see hook_forms()
|
| 62 |
*/
|
| 63 |
function userinfo_admin_settings() {
|
| 64 |
$form['userinfo_display'] = array(
|
| 65 |
'#type' => 'checkboxes',
|
| 66 |
'#title' => t('Info to display'),
|
| 67 |
'#options' => array(
|
| 68 |
'uid' => 'UID',
|
| 69 |
'name' => 'Username',
|
| 70 |
'mail' => 'Email address',
|
| 71 |
'theme' => 'Chosen theme',
|
| 72 |
'signature' => 'Signature',
|
| 73 |
'created' => 'Date created',
|
| 74 |
'access' => 'Date last accessed',
|
| 75 |
'login' => 'Date of last login',
|
| 76 |
'status' => 'Blocked/Unblocked',
|
| 77 |
'timezone' => 'Timezone',
|
| 78 |
'language' => 'Language',
|
| 79 |
'picture' => 'Picture',
|
| 80 |
'init' => 'Signup email address',
|
| 81 |
),
|
| 82 |
'#default_value' => variable_get('userinfo_display', array('uid', 'name', 'mail', 'status',)),
|
| 83 |
);
|
| 84 |
/**
|
| 85 |
* Get custom profile fields and append them to the list
|
| 86 |
*/
|
| 87 |
$sql = 'SELECT DISTINCT `title`, `name` FROM `{profile_fields}`';
|
| 88 |
$sql_result = db_query($sql);
|
| 89 |
for ($i = 1; $i <= db_num_rows($sql_result); $i++) {
|
| 90 |
$result = db_fetch_array($sql_result);
|
| 91 |
$form['userinfo_display']['#options'][$result['name']] = $result['title'];
|
| 92 |
}
|
| 93 |
return system_settings_form($form);
|
| 94 |
}
|
| 95 |
|
| 96 |
/**
|
| 97 |
* Define the info page
|
| 98 |
*
|
| 99 |
* @return Info on users based on the settings form
|
| 100 |
*
|
| 101 |
* @see userinfo_admin_settings()
|
| 102 |
*/
|
| 103 |
function userinfo_admin_users() {
|
| 104 |
/**
|
| 105 |
* Reduce Database overhead by getting settings all at once
|
| 106 |
*/
|
| 107 |
$settings = variable_get('userinfo_display', array('uid', 'name', 'mail', 'status',));
|
| 108 |
|
| 109 |
/**
|
| 110 |
* Number of users
|
| 111 |
*/
|
| 112 |
$num_users = db_num_rows(db_query('SELECT * FROM `{users}`'));
|
| 113 |
|
| 114 |
$output = '<table><thead>';
|
| 115 |
$customfields = array();
|
| 116 |
$user_info = array();
|
| 117 |
|
| 118 |
/**
|
| 119 |
* Get custom profile fields, if the profile module is enabled
|
| 120 |
*/
|
| 121 |
if (in_array('profile', module_list())) {
|
| 122 |
$sql = 'SELECT DISTINCT `title`, `name`, `type` FROM `{profile_fields}`';
|
| 123 |
$sql_result = db_query($sql);
|
| 124 |
// for ($i = 1; $i <= $num_users; $i++) {
|
| 125 |
for ($i = 1; $i <= $num_users; $i++) {
|
| 126 |
$result = db_fetch_array($sql_result);
|
| 127 |
$customfields[$result['name']] = array(
|
| 128 |
'name' => $result['name'],
|
| 129 |
'title' => $result['title'],
|
| 130 |
'type' => $result['type'],
|
| 131 |
);
|
| 132 |
}
|
| 133 |
}
|
| 134 |
/**
|
| 135 |
* Default theme
|
| 136 |
*/
|
| 137 |
$default_theme = variable_get('theme_default', 'garland');
|
| 138 |
/**
|
| 139 |
* Get user info
|
| 140 |
*/
|
| 141 |
$sql = 'SELECT * FROM `{users}` WHERE `uid` >= 1';
|
| 142 |
$sql_result = db_query($sql);
|
| 143 |
// for ($i = 1; $i <= db_num_rows($sql_result); $i++) {
|
| 144 |
for ($i = 1; $i <= $num_users; $i++) {
|
| 145 |
$result = db_fetch_array($sql_result);
|
| 146 |
$user_info[$result['uid']] = $result;
|
| 147 |
}
|
| 148 |
/**
|
| 149 |
* Get data for custom profile fields
|
| 150 |
*/
|
| 151 |
if (in_array('profile', module_list())) {
|
| 152 |
$sql = 'SELECT `name` , `uid` , `value` FROM {profile_fields} INNER JOIN {profile_values} ON {profile_fields}.fid = {profile_values}.fid';
|
| 153 |
$sql_result = db_query($sql);
|
| 154 |
// for ($i = 1; $i <= db_num_rows($sql_result); $i++) {
|
| 155 |
for ($i = 1; $i <= $num_users; $i++) {
|
| 156 |
$result = db_fetch_array($sql_result);
|
| 157 |
$user_info[$result['uid']][$result['name']] = $result['value'];
|
| 158 |
}
|
| 159 |
}
|
| 160 |
/**
|
| 161 |
* Table Header
|
| 162 |
*
|
| 163 |
* Generated by going through each item in the settings page
|
| 164 |
* and checking to se if it is enabled. If it is, then output
|
| 165 |
* the value of the item and move on.
|
| 166 |
*
|
| 167 |
* @see userinfo_admin_settings()
|
| 168 |
*/
|
| 169 |
foreach ($settings as $item) {
|
| 170 |
if ($item) {
|
| 171 |
if ($item == 'uid') {
|
| 172 |
$output .= '<th>UID</th>';
|
| 173 |
}
|
| 174 |
else if ($item == 'name') {
|
| 175 |
$output .= '<th>Username</th>';
|
| 176 |
}
|
| 177 |
else if ($item == 'mail') {
|
| 178 |
$output .= '<th>Email</th>';
|
| 179 |
}
|
| 180 |
else if ($item == 'theme') {
|
| 181 |
$output .= '<th>Theme</th>';
|
| 182 |
}
|
| 183 |
else if ($item == 'signature') {
|
| 184 |
$output .= '<th>Signature</th>';
|
| 185 |
}
|
| 186 |
else if ($item == 'created') {
|
| 187 |
$output .= '<th>Created</th>';
|
| 188 |
}
|
| 189 |
else if ($item == 'access') {
|
| 190 |
$output .= '<th>Last accessed</th>';
|
| 191 |
}
|
| 192 |
else if ($item == 'login') {
|
| 193 |
$output .= '<th>Last login</th>';
|
| 194 |
}
|
| 195 |
else if ($item == 'status') {
|
| 196 |
$output .= '<th>Status</th>';
|
| 197 |
}
|
| 198 |
else if ($item == 'timezone') {
|
| 199 |
$output .= '<th>Timezone</th>';
|
| 200 |
}
|
| 201 |
else if ($item == 'language') {
|
| 202 |
$output .= '<th>Language</th>';
|
| 203 |
}
|
| 204 |
else if ($item == 'picture') {
|
| 205 |
$output .= '<th>Picture</th>';
|
| 206 |
}
|
| 207 |
else if ($item == 'init') {
|
| 208 |
$output .= '<th>Sign-up email</th>';
|
| 209 |
}
|
| 210 |
}
|
| 211 |
}
|
| 212 |
foreach ($customfields as $item) {
|
| 213 |
if ($settings[$item['name']]) {
|
| 214 |
$output .= '<th>' . $item['title'] . '</th>';
|
| 215 |
}
|
| 216 |
}
|
| 217 |
$output .= '</thead><tbody>';
|
| 218 |
/**
|
| 219 |
* Actual Info
|
| 220 |
*/
|
| 221 |
foreach ($user_info as $info) {
|
| 222 |
if ($info) {
|
| 223 |
$output .= '<tr>';
|
| 224 |
foreach ($settings as $item) {
|
| 225 |
if ($item) {
|
| 226 |
/**
|
| 227 |
* Special things need to be done with date, status, picture, url, and e-mail fields
|
| 228 |
*/
|
| 229 |
if ($item != 'created' && $item != 'access' && $item != 'login' && $item != 'status' && $item != 'picture' && $customfields[$item]['type'] != 'url' && $item != 'mail') {
|
| 230 |
$output .= '<td>' . $info[$item] . '</td>';
|
| 231 |
}
|
| 232 |
else if ($item == 'created' || $item == 'access' || $item == 'login') {
|
| 233 |
$output .= '<td>' . format_date($info[$item], 'small') . '</td>';
|
| 234 |
}
|
| 235 |
else if ($item == 'status') {
|
| 236 |
if ($info[$item]) {
|
| 237 |
$output .= '<td>Active</td>';
|
| 238 |
}
|
| 239 |
else {
|
| 240 |
$output .= '<td>Blocked</td>';
|
| 241 |
}
|
| 242 |
}
|
| 243 |
else if ($item == 'picture') {
|
| 244 |
if ($info[$item]) {
|
| 245 |
$output .= '<td><img src="/' . $info[$item] . '" /></td>';
|
| 246 |
}
|
| 247 |
else {
|
| 248 |
$output .= '<td></td>';
|
| 249 |
}
|
| 250 |
}
|
| 251 |
else if ($customfields[$item]['type'] == 'url') {
|
| 252 |
$output .= '<td><a href="' . $info[$item] . '">' . $info[$item] . '</a>';
|
| 253 |
}
|
| 254 |
else if ($item == 'mail') {
|
| 255 |
$output .= '<td><a href="mailto:' . $info[$item] . '">' . $info[$item] . '</a>';
|
| 256 |
}
|
| 257 |
}
|
| 258 |
}
|
| 259 |
$output .= '</tr>';
|
| 260 |
}
|
| 261 |
}
|
| 262 |
$output .= '</tbody></table>';
|
| 263 |
|
| 264 |
$output .= l('Export as CSV', 'admin/user/userinfo/csv');
|
| 265 |
|
| 266 |
return $output;
|
| 267 |
}
|
| 268 |
|
| 269 |
/**
|
| 270 |
* Generate a csv-formatted string with uerinfo in it. This string will contain info selected in the settings page.
|
| 271 |
*
|
| 272 |
* @see userinfo_admin_settings()
|
| 273 |
* @return A csv file with formatted userinfo
|
| 274 |
*/
|
| 275 |
function userinfo_generate_csv() {
|
| 276 |
/**
|
| 277 |
* Reduce Database overhead by getting settings all at once
|
| 278 |
*/
|
| 279 |
$settings = variable_get('userinfo_display', array('uid', 'name', 'mail', 'status',));
|
| 280 |
|
| 281 |
/**
|
| 282 |
* Number of users
|
| 283 |
*/
|
| 284 |
$num_users = db_num_rows(db_query('SELECT * FROM `{users}`'));
|
| 285 |
|
| 286 |
$output = '<table><thead>';
|
| 287 |
$customfields = array();
|
| 288 |
$user_info = array();
|
| 289 |
|
| 290 |
/**
|
| 291 |
* Get custom profile fields, if the profile module is enabled
|
| 292 |
*/
|
| 293 |
if (in_array('profile', module_list())) {
|
| 294 |
$sql = 'SELECT DISTINCT `title`, `name`, `type` FROM `{profile_fields}`';
|
| 295 |
$sql_result = db_query($sql);
|
| 296 |
// for ($i = 1; $i <= $num_users; $i++) {
|
| 297 |
for ($i = 1; $i <= $num_users; $i++) {
|
| 298 |
$result = db_fetch_array($sql_result);
|
| 299 |
$customfields[$result['name']] = array(
|
| 300 |
'name' => $result['name'],
|
| 301 |
'title' => $result['title'],
|
| 302 |
'type' => $result['type'],
|
| 303 |
);
|
| 304 |
}
|
| 305 |
}
|
| 306 |
/**
|
| 307 |
* Default theme
|
| 308 |
*/
|
| 309 |
$default_theme = variable_get('theme_default', 'garland');
|
| 310 |
/**
|
| 311 |
* Get user info
|
| 312 |
*/
|
| 313 |
$sql = 'SELECT * FROM `{users}` WHERE `uid` >= 1';
|
| 314 |
$sql_result = db_query($sql);
|
| 315 |
// for ($i = 1; $i <= db_num_rows($sql_result); $i++) {
|
| 316 |
for ($i = 1; $i <= $num_users; $i++) {
|
| 317 |
$result = db_fetch_array($sql_result);
|
| 318 |
$user_info[$result['uid']] = $result;
|
| 319 |
}
|
| 320 |
/**
|
| 321 |
* Get data for custom profile fields
|
| 322 |
*/
|
| 323 |
if (in_array('profile', module_list())) {
|
| 324 |
$sql = 'SELECT `name` , `uid` , `value` FROM {profile_fields} INNER JOIN {profile_values} ON {profile_fields}.fid = {profile_values}.fid';
|
| 325 |
$sql_result = db_query($sql);
|
| 326 |
// for ($i = 1; $i <= db_num_rows($sql_result); $i++) {
|
| 327 |
for ($i = 1; $i <= $num_users; $i++) {
|
| 328 |
$result = db_fetch_array($sql_result);
|
| 329 |
$user_info[$result['uid']][$result['name']] = $result['value'];
|
| 330 |
}
|
| 331 |
}
|
| 332 |
|
| 333 |
/**
|
| 334 |
* The csv output
|
| 335 |
*/
|
| 336 |
$output = '';
|
| 337 |
|
| 338 |
/**
|
| 339 |
* Table Header
|
| 340 |
*/
|
| 341 |
$header = '';
|
| 342 |
|
| 343 |
/**
|
| 344 |
* Table Header
|
| 345 |
*
|
| 346 |
* Generated by going through each item in the settings page
|
| 347 |
* and checking to se if it is enabled. If it is, then output
|
| 348 |
* the value of the item and move on.
|
| 349 |
*
|
| 350 |
* @see userinfo_admin_settings()
|
| 351 |
*/
|
| 352 |
foreach ($settings as $item) {
|
| 353 |
if ($item) {
|
| 354 |
if ($item == 'uid') {
|
| 355 |
$header .= 'UID,';
|
| 356 |
}
|
| 357 |
else if ($item == 'name') {
|
| 358 |
$header .= 'Username,';
|
| 359 |
}
|
| 360 |
else if ($item == 'mail') {
|
| 361 |
$header .= 'Email,';
|
| 362 |
}
|
| 363 |
else if ($item == 'theme') {
|
| 364 |
$header .= 'Theme,';
|
| 365 |
}
|
| 366 |
else if ($item == 'signature') {
|
| 367 |
$header .= 'Signature,';
|
| 368 |
}
|
| 369 |
else if ($item == 'created') {
|
| 370 |
$header .= 'Created,';
|
| 371 |
}
|
| 372 |
else if ($item == 'access') {
|
| 373 |
$header .= 'Last accessed,';
|
| 374 |
}
|
| 375 |
else if ($item == 'login') {
|
| 376 |
$header .= 'Last login,';
|
| 377 |
}
|
| 378 |
else if ($item == 'status') {
|
| 379 |
$header .= 'Status,';
|
| 380 |
}
|
| 381 |
else if ($item == 'timezone') {
|
| 382 |
$header .= 'Timezone,';
|
| 383 |
}
|
| 384 |
else if ($item == 'language') {
|
| 385 |
$header .= 'Language,';
|
| 386 |
}
|
| 387 |
else if ($item == 'picture') {
|
| 388 |
$header .= 'Picture,';
|
| 389 |
}
|
| 390 |
else if ($item == 'init') {
|
| 391 |
$header .= 'Sign-up email,';
|
| 392 |
}
|
| 393 |
}
|
| 394 |
}
|
| 395 |
foreach ($customfields as $item) {
|
| 396 |
if ($settings[$item['name']]) {
|
| 397 |
$header .= $item['title'] . ',';
|
| 398 |
}
|
| 399 |
}
|
| 400 |
|
| 401 |
// Remove the comma to prevent unnecessary columns from being created
|
| 402 |
$header[strlen($header)-1] = '';
|
| 403 |
|
| 404 |
$output .= $header . "\n";
|
| 405 |
|
| 406 |
foreach ($user_info as $info) {
|
| 407 |
$line = '';
|
| 408 |
if ($info) {
|
| 409 |
foreach ($settings as $item) {
|
| 410 |
if ($item) {
|
| 411 |
/**
|
| 412 |
* Special things need to be done with date, status, and picture fields
|
| 413 |
*/
|
| 414 |
if ($item != 'created' && $item != 'access' && $item != 'login' && $item != 'status' && $item != 'picture') {
|
| 415 |
$line .= '"' . $info[$item] . '",';
|
| 416 |
}
|
| 417 |
else if ($item == 'created' || $item == 'access' || $item == 'login') {
|
| 418 |
$line .= '"' . format_date($info[$item], 'small') . '",';
|
| 419 |
}
|
| 420 |
else if ($item == 'status') {
|
| 421 |
if ($info[$item]) {
|
| 422 |
$line .= '"Active",';
|
| 423 |
}
|
| 424 |
else {
|
| 425 |
$line .= '"Blocked",';
|
| 426 |
}
|
| 427 |
}
|
| 428 |
else if ($item == 'picture') {
|
| 429 |
if ($info[$item]) {
|
| 430 |
$line .= '"Yes",';
|
| 431 |
}
|
| 432 |
else {
|
| 433 |
$line .= '"No",';
|
| 434 |
}
|
| 435 |
}
|
| 436 |
}
|
| 437 |
}
|
| 438 |
// Remove the comma to prevent unnecessary columns from being created
|
| 439 |
$line[strlen($line)-1] = '';
|
| 440 |
|
| 441 |
$output .= $line . "\n";
|
| 442 |
}
|
| 443 |
}
|
| 444 |
|
| 445 |
return $output;
|
| 446 |
}
|
| 447 |
|
| 448 |
/**
|
| 449 |
* Create a file with userinfo in it
|
| 450 |
*
|
| 451 |
* @see userinfo_generate_csv
|
| 452 |
*/
|
| 453 |
function userinfo_generate_csv_file() {
|
| 454 |
drupal_set_header('Content-Type: text/csv; charset=utf-8');
|
| 455 |
echo userinfo_generate_csv();
|
| 456 |
}
|