user.module 44.6 KB
Newer Older
Dries's avatar
   
Dries committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

session_set_save_handler("sess_open", "sess_close", "sess_read", "sess_write", "sess_destroy", "sess_gc");
session_start();

/*** Session functions *****************************************************/

function sess_open($save_path, $session_name) {
  return 1;
}

function sess_close() {
  return 1;
}

function sess_read($key) {
  global $user;
  $user = user_load(array("session" => $key, "status" => 1));
  return $user;
}

function sess_write($key, $value) {
23
  global $HTTP_SERVER_VARS;
Dries's avatar
   
Dries committed
24

Dries's avatar
   
Dries committed
25
  db_query("UPDATE users SET hostname = '". check_input($HTTP_SERVER_VARS[REMOTE_ADDR]) ."', timestamp = '". time() ."' WHERE session = '$key'");
Dries's avatar
   
Dries committed
26
27
28
}

function sess_destroy($key) {
29
  global $HTTP_SERVER_VARS;
Dries's avatar
   
Dries committed
30

Dries's avatar
   
Dries committed
31
  db_query("UPDATE users SET hostname = '". check_input($HTTP_SERVER_VARS[REMOTE_ADDR]) ."', timestamp = '". time() ."', session = '' WHERE session = '$key'");
Dries's avatar
   
Dries committed
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
}

function sess_gc($lifetime) {
  return 1;
}

/*** Common functions ******************************************************/

function user_load($array = array()) {

  /*
  ** Dynamically compose a SQL query:
  */

  foreach ($array as $key => $value) {
    if ($key == "pass") {
Dries's avatar
   
Dries committed
48
      $query .= "u.$key = '" . md5($value) . "' AND ";
Dries's avatar
   
Dries committed
49
50
51
52
53
    }
    else {
      $query .= "u.$key = '". addslashes($value) ."' AND ";
    }
  }
Dries's avatar
   
Dries committed
54
  $result = db_query("SELECT u.*, r.perm FROM users u LEFT JOIN role r ON u.role = r.name WHERE $query u.status < 3");
Dries's avatar
   
Dries committed
55

Dries's avatar
   
Dries committed
56
57
58
  $user = db_fetch_object($result);

  return $user;
Dries's avatar
   
Dries committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74


}

function user_save($account, $array = array()) {

  /*
  ** Dynamically compose a SQL query:
  */


  /*
  ** Update existing or insert new user account:
  */

  if ($account->uid) {
Dries's avatar
   
Dries committed
75
76
77
78
79
80
81
82
83
   foreach ($array as $key => $value) {
      if ($key == "pass") {
        $query .= "$key = '". md5($value) ."', ";
      }
      else {
        $query .= "$key = '". addslashes($value) ."', ";
      }
    }
    db_query("UPDATE users SET $query timestamp = '". time() ."' WHERE uid = '$account->uid'");
Dries's avatar
   
Dries committed
84
85
86
    return user_load(array("uid" => $account->uid));
  }
  else {
Dries's avatar
   
Dries committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    $fields = "(";
    $values = "(";
    $num = 0;

    foreach ($array as $key => $value) {
      $fields .= ($num ? ", " : "") . $key;
      $values .= ($num ? ", " : "") . (($key == "pass") ? "'" . md5 ($value) . "'" : "'" . addslashes ($value) . "'");
      $num = 1;
    }

    $fields .= ($num ? ", " : "") . "timestamp";
    $values .= ($num ? ", " : "") . "'" . time() ."'";
    $fields .= ")";
    $values .= ")";

    db_query("INSERT INTO users $fields VALUES $values");
Dries's avatar
   
Dries committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
    return user_load(array("name" => $array["name"]));
  }

}

function user_set($account, $key, $value) {
  $account->data[$key] = $value;
  return $account;
}

function user_get($account, $key) {
  return $account->data[$key];
}

function user_validate_name($name) {

  /*
  ** Verify the syntax of the given name:
  */

  if (!$name) return t("You must enter a name.");
  if (eregi("^ ", $name)) return t("The name can not begin with a space.");
  if (eregi(" \$", $name)) return t("The name can not end with a space.");
  if (eregi("  ", $name)) return t("The name can not contain multiple spaces in a row.");
  if (eregi("[^a-zA-Z0-9 ]", $name)) return t("The name contains an illegal character.");
  if (strlen($name) > 32) return t("The name '$name' is too long: it must be less than 32 characters.");
}

function user_validate_mail($mail) {

  /*
  ** Verify the syntax of the given e-mail address:
  */

  if (!$mail) return t("Your must enter an e-mail address.");
  if (!eregi("^[_+\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$", $mail)) return t("The e-mail address '$mail' is not valid.");
}

function user_password($min_length = 6) {

  /*
  ** Generate a human-readable password:
  */

  mt_srand((double)microtime() * 1000000);
  $words = explode(",", variable_get("user_password", "foo,bar,guy,neo,tux,moo,sun,asm,dot,god,axe,geek,nerd,fish,hack,star,mice,warp,moon,hero,cola,girl,fish,java,perl,boss,dark,sith,jedi,drop,mojo"));
  while (strlen($password) < $min_length) $password .= trim($words[mt_rand(0, count($words))]);
  return $password;
}

function user_access($perm) {
  global $user;

  if ($user->uid == 1) {
    return 1;
  }
  else if ($user->perm) {
    return strstr($user->perm, $perm);
  }
  else {
    return db_fetch_object(db_query("SELECT * FROM role WHERE name = 'anonymous user' AND perm LIKE '%$perm%'"));
  }
}

Dries's avatar
   
Dries committed
167
function user_mail($mail, $subject, $message, $header) {
Dries's avatar
   
Dries committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
  // print "<pre>subject: $subject<hr />header: $header<hr />$message</pre>";
  mail($mail, $subject, $message, $header);
}

function user_deny($type, $mask) {
  $allow = db_fetch_object(db_query("SELECT * FROM access WHERE status = '1' AND type = '$type' AND LOWER('$mask') LIKE LOWER(mask)"));

  $deny = db_fetch_object(db_query("SELECT * FROM access WHERE status = '0' AND type = '$type' AND LOWER('$mask') LIKE LOWER(mask)"));

  if ($deny && !$allow) {
    return 1;
  }
  else {
    return 0;
  }
}

/*** Module hooks **********************************************************/

function user_help() {
 ?>

  <p>The user system is responsible for maintaining the user database.  It automatically handles tasks like registration, authentication, access control, password retrieval, user settings and much more.</p>

  <h3>User management</h3>
  <p>No participant can use his own name or handle to post comments until they sign up and submit their e-mail address.  Those who do not may participate as anonymous users, but they will suffer numerous disadvantages, for example their posts beginning at a lower score.</p>
  <p>In contrast, those with a user account can use their own name or handle and are granted various privileges: the most important are probably the ability to post content, to rate comments and to fine-tune the site to their personal liking.</p>
  <p>Registered users need to authenticate by supplying a username and password, or alternatively, by supplying a valid Drupal ID or Jabber ID.  The username and password are kept in the database, where the password is hashed so that no one can read nor use it.  When a username and password needs to be checked the system goes down the list of registered users till it finds a matching username, and then hashes the password that was supplied and compares it to the listed value.  If they match then that means the username and password supplied were correct.</p>
  <p>Once a user authenticated a session is started and until that session is over they won't have to re-authenticate.  To keep track of the individual sessions, drupal relies on PHP's session support.  A visitor accessing your web site is assigned an unique ID, the so-called session ID, which is stored in a cookie.  For security's sake, the cookie does not contain personal information but acts as a key to retrieve the information stored on your server's side.  When a visitor accesses your site, drupal will check whether a specific session ID has been sent with the request.  If this is the case, the prior saved environment is recreated.</p>
  <p>Drupal allows you to control who is allowed to get authenticated and who is not.  To accomplish this, you can ban certain hostnames, IPs, IP-ranges, e-mail address and usernames.  Any user that matches any of the given ban criteria will not be able to authenticate or to register as a new user.</p>
  <p>Authenticated users can themselves select entirely different appearances for the site, utilizing their own preferences for how the pages are structured, how navigation lists and other page components are presented and much more.</p>
  <p>An important feature of drupal is that any user can be granted administrator rights.  The ability to share maintenance responsibility with volunteers from across the globe can be considered valuable for most community-based projects.</p>

  <h3>Access rules</h3>
  <p>The access rules allows you to describe which e-mail addresses or usernames will or will not be granted access using a flexible wild-card system.  Examples are given below.  If no access rules are provided, access control is turned off and everybody will be able to access your website.  For each category, zero or more access rules can be specified.  The 'allow' rules are processed prior to the 'deny' rules and are thus considered to be stronger.</p>
  <p>To do describe access rules you can use the following wild-card characters:</p>
  <ul>
   <li>&nbsp;% : matches any number of characters, including zero characters.</li>
   <li>&nbsp;_ : matches exactly one character.</li>
  </UL>
  <p><u>Examples:</u></p>
  <ul>
   <li>E-mail address bans <code>%@hotmail.com</code>, <code>%@altavista.%</code>, <code>%@usa.net</code>, etc.  Used to prevent users from using free email accounts, which might be used to cause trouble.</li>
   <li>Username bans <code>root</code>, <code>webmaster</code>, <code>admin%</code>, etc.  Used to prevent administrator impersonators.</li>
  </ul>

  <h3>User roles</h3>
  <p>Users have roles that define what kinds of actions they can take.  Roles define classes of users such as <i>anonymous user</i>, <i>authenticated user</I>, <I>moderator</i>, <i>administrator</i> and so on.  Every user can have one role.</p>
  <p>Roles make it easier for you to manage security.  Instead of defining what every single user can do, you can simply set a couple different permissions for different user roles.</p>
  <p>Drupal comes with three built-in roles:</p>
  <ul>
   <li>Anonymous user: this role is used for users that don't have a user account or that are not authenticated.</li>
   <li>Registered user: this role is assigned automatically to authenticated users.  Most users will belong to this user role unless specified otherwise.</li>
  </uL>
  <p>For basic Drupal sites you can get by with <i>anonymous user</i> and <i>authenticated user</i> but for more complex sites where you want other users to be able to perform maintainance or administrative duties, you may want to create your own roles to classify your users into different groups.</p>

  <h3>User permissions</h3>
  <p>Each Drupal's permission describes a fine-grained logical operation such as <i>access administration pages</i> or <i>add and modify user accounts</i>.  You could say a permission represents access granted to a user to perform a set of operations.</p>
  <p>Roles tie users to permissions.  The combination of roles and permissions represent a way to tie user authorization to the performance of actions, which is how Drupal can determine what users can do.</p>

 <?php
}

Dries's avatar
   
Dries committed
231
function user_perm() {
Dries's avatar
   
Dries committed
232
233
234
235
236
  return array("administer users");
}

function user_search($keys) {
  global $PHP_SELF;
Dries's avatar
   
Dries committed
237
  $result = db_query("SELECT * FROM users WHERE name LIKE '%$keys%' LIMIT 20");
Dries's avatar
   
Dries committed
238
  while ($account = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
239
    $find[$i++] = array("title" => $account->name, "link" => (strstr($PHP_SELF, "admin.php") ? "admin.php?mod=user&op=edit&id=$account->uid" : "module.php?mod=user&op=view&id=$account->uid"), "user" => $account->name);
Dries's avatar
   
Dries committed
240
241
242
243
  }
  return $find;
}

Dries's avatar
   
Dries committed
244
245
246
247
248
249
function user_link($type) {
  if ($type == "page") {
    $links[] = "<a href=\"module.php?mod=user\">". t("user account") ."</a>";
  }

  if ($type == "menu") {
Dries's avatar
   
Dries committed
250
    $links[] = "<a href=\"module.php?mod=user&op=view\">". t("account settings") ."</a>";
Dries's avatar
   
Dries committed
251
252
253
  }

  if ($type == "admin") {
Dries's avatar
   
Dries committed
254
    $links[] = "<a href=\"admin.php?mod=user\">user management</a>";
Dries's avatar
   
Dries committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
  }

  return $links ? $links : array();
}

function drupal_login($arguments) {
  $argument = $arguments->getparam(0);
  $username = $argument->scalarval();
  $argument = $arguments->getparam(1);
  $password = $argument->scalarval();

  if ($user = user_load(array(name => "$username", "pass" => $password, "status" => 1))) {
    return new xmlrpcresp(new xmlrpcval($user->uid, "int"));
  }
  else {
    return new xmlrpcresp(new xmlrpcval(0, "int"));
  }
}


function user_xmlrpc() {
   return array("drupal.login" => array("function" => "drupal_login"));
}

/*** Authentication methods ************************************************/

function startElement($parser, $name, $attributes) {
  global $jabber;

  if ($attributes["ID"]) {
    $jabber["jid"] = $attributes["ID"];
  }

  if (stristr($name, "error") || ($attributes["ERROR"])){
    $jabber["error"] = true;
  }
}

function endElement($parser, $name) {
}

function characterData($parser, $data) {
  global $jabber;

  $jabber["data"] = $data;
}

function jabber_send($session, $message) {

  // print "SEND: ". htmlentities($message) ."<br />";

  fwrite($session, $message, strlen($message));
}

function jabber_recv($session, $timout = 50) {

  /*
  ** Returns a chunk of data, read from the socket descriptor '$session'.
  ** If the call fails, or if no data is read after the specified timout,
  ** false will be returned.
  */

  while ($count < $timout) {
    $data = fread($session, 1);
    if ($data) {
      $message .= $data;
    }
    else {
      usleep(100);
      $count = $count + 1;
    }
  }

  if ($message) {

    // print "RECV: ". htmlentities($message) ."<br />";

    return $message;
  }
  else {
    return false;
  }
}

function jabber_auth($username, $password, $server, $port = 5222) {
  global $jabber;

  // print "username = $username, pasword = $password, server = $server<br />";

  $session = fsockopen($server, $port, &$errno, &$errstr, 5);

  if ($session) {

    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");

    /*
    ** Switch the given socket descriptor '$session' to non-blocking mode:
    */

    set_socket_blocking($session, false);

    /*
    ** A jabber session consists of two parallel XML streams, one from
    ** the client to the server and one from the server to the client.
    ** On connecting to a Jabber server, a Jabber client initiates the
    ** client-to-server XML stream and the server responds by initiating
    ** the server-to-client XML stream:
    */

    jabber_send($session, "<?xml version='1.0'?>");

    jabber_send($session, "<stream:stream to='$server' xmlns='jabber:client' xmlns:stream='http://etherx.jabber.org/streams'>");

    $data = jabber_recv($session);

    if (!xml_parse($xml_parser, $data, 0)) {
      watchdog("error", "XML error: '". xml_error_string(xml_get_error_code($xml_parser)) ."' at line ". xml_get_current_line_number($xml_parser));
      return 0;
    }

    if ($jabber["error"]) {
      watchdog("error", "protocol error: ". $jabber["data"]);
      return 0;
    }

Dries's avatar
Dries committed
382
383
384
385
    /*
    ** Hash the password:
    */

Dries's avatar
Dries committed
386
    jabber_send($session, "<iq type='set' id='". $jabber["jid"] ."'><query xmlns='jabber:iq:auth'><username>$username</username><password>$password</password><resource>drupal</resource></query></iq>");
Dries's avatar
   
Dries committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430

    $data = jabber_recv($session);

    if (!xml_parse($xml_parser, $data, 0)) {
       watchdog("error", "XML error: '". xml_error_string(xml_get_error_code($xml_parser)) ."' at line ". xml_get_current_line_number($xml_parser));
    }

    if ($jabber["error"]) {
      watchdog("error", "protocol error: ". $jabber["data"]);
      return 0;
    }

    xml_parser_free($xml_parser);

    return 1;
  }
  else {
    watchdog("error", "failed to open socket to jabber server:\n  $errno, $errstr");
    return 0;
  }
}

function drupal_auth($username, $password, $server) {

  $message = new xmlrpcmsg("drupal.login", array(new xmlrpcval($username, "string"), new xmlrpcval($password, "string")));

  // print "username = $username, pasword = $password, server = $server<br />";
  // print "<pre>" . htmlentities($message->serialize()) . "</pre>";

  $client = new xmlrpc_client("/xmlrpc.php", $server, 80);

  $result = $client->send($message);

  if ($result && !$result->faultCode()) {
    $value = $result->value();
    $login = $value->scalarval();
  }

  return $login;
}

/*** User features *********************************************************/

function user_login($edit = array()) {
Dries's avatar
   
Dries committed
431
  global $user, $HTTP_REFERER;
Dries's avatar
   
Dries committed
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463

  if (user_deny("user", $edit["name"])) {
    $error = sprintf(t("The name '%s' has been denied access."), $edit["name"]);
  }
  else if ($edit["name"] && $edit["pass"]) {

    /*
    ** Strip name and server from ID:
    */

    if ($pos = strpos($edit["name"], "@")) {
      $server = check_input(substr($edit["name"], $pos + 1, strlen($edit["name"])));
      $name = check_input(substr($edit["name"], 0, $pos));
      $pass = check_input($edit["pass"]);
    }
    else {
      $name = check_input($edit["name"]);
      $pass = check_input($edit["pass"]);
    }

    /*
    ** Try to log on the user locally:
    */

    if (!$user) {
      $user = user_load(array("name" => $name, "pass" => $pass, "status" => 1));
    }

    /*
    ** Try to log on the user through Drupal:
    */

Dries's avatar
   
Dries committed
464
    if (!$user && $server && variable_get("user_drupal", 1) == 1 && $id = drupal_auth($name, $pass, $server)) {
Dries's avatar
   
Dries committed
465
466
467
468
469
470
471
472
473
474
475
476
      $user = user_load(array("drupal" => "$id@$server", "status" => 1));

      if (!$user && variable_get("user_register", 1) == 1 && !user_load(array("name" => $name))) {
        watchdog("user", "new user: '$name' &lt;$name@$server&gt; (Drupal ID)");
        $user = user_save("", array("name" => $name, "pass" => user_password(), "init" => "$id@$server", "drupal" => "$id@$server", "role" => "authenticated user", "status" => 1));
      }
    }

    /*
    ** Try to log on the user through Jabber:
    */

Dries's avatar
   
Dries committed
477
    if (!$user && $server && variable_get("user_jabber", 1) == 1 && jabber_auth($name, $pass, $server)) {
Dries's avatar
   
Dries committed
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
      $user = user_load(array("jabber" => "$name@$server", "status" => 1));
      if (!$user && variable_get("user_register", 1) == 1 && !user_load(array("name" => "$name"))) {
        watchdog("user", "new user: '$name' &lt;$name@$server&gt; (Jabber ID)");
        $user = user_save("", array("name" => $name, "pass" => user_password(), "init" => "$name@$server", "jabber" => "$name@$server", "role" => "authenticated user", "status" => 1));
      }
    }

    if ($user->uid) {

      watchdog("user", "session opened for '$user->name'");

      /*
      ** Write session ID to database:
      */

      user_save($user, array("session" => session_id()));

      /*
Dries's avatar
   
Dries committed
496
497
      ** Redirect the user to the page he logged on from or to his personal
      ** information page if we can detect the referer page:
Dries's avatar
   
Dries committed
498
499
      */

Dries's avatar
   
Dries committed
500
501
      $url = $HTTP_REFERER ? $HTTP_REFERER : "module.php?mod=user&op=view";

Dries's avatar
   
Dries committed
502
      drupal_goto($url);
503
      return;
Dries's avatar
   
Dries committed
504

Dries's avatar
   
Dries committed
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
    }
    else {
      watchdog("user", "failed to login for '". $name ."': invalid password");

      $error = t("Authentication failed.");
    }
  }

  /*
  ** Display error message (if any):
  */

  if ($error) {
    $output .= "<p><font color=\"red\">". check_output($error) ."</font></p>";
  }

  /*
  ** Display login form:
  */

Dries's avatar
   
Dries committed
525
  $output .= form_textfield(t("Username"), "name", $edit["name"], 20, 64, t("Enter your local username, a Drupal ID or a Jabber ID."));
Dries's avatar
   
Dries committed
526
527
528
  $output .= form_password(t("Password"), "pass", $pass, 20, 64, t("Enter the password that accompanies your username."));
  $output .= form_submit(t("Log in"));

Dries's avatar
   
Dries committed
529
  return form($output);
Dries's avatar
   
Dries committed
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549

}

function user_logout() {
  global $user;

  if ($user->uid) {
    watchdog("user", "session closed for user '$user->name'");

    /*
    ** Destroy the current session:
    */

    session_destroy();
    unset($user);

    /*
    ** Redirect the user to his personal information page:
    */

Dries's avatar
   
Dries committed
550
    drupal_goto("index.php");
Dries's avatar
   
Dries committed
551
552
553
554
555
556
  }
}

function user_pass($edit = array()) {

  if ($edit["name"] && $edit["mail"]) {
Dries's avatar
   
Dries committed
557
    if ($account = db_fetch_object(db_query("SELECT uid FROM users WHERE name = '". check_input($edit["name"]) ."' AND mail = '". check_input($edit["mail"]) ."'"))) {
Dries's avatar
   
Dries committed
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593

      $from = variable_get("site_mail", "root@localhost");
      $pass = user_password();

      /*
      ** Save new password:
      */

      user_save($account, array("pass" => $pass));

      /*
      ** Mail new passowrd:
      */

      user_mail($edit["mail"], t("user account details"), sprintf(t("%s,\n\nyou requested us to e-mail you a new password for your account at %s.  You can now login using the following username and password:\n\n  username: %s\n  password: %s\n\n\n-- %s team"), $edit["name"], variable_get("site_name", "drupal"), $edit["name"], $pass, variable_get("site_name", "drupal")), "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");

      watchdog("user", "mail password: '". $edit["name"] ."' &lt;". $edit["mail"] ."&gt;");

      return t("Your password and further instructions have been sent to your e-mail address.");
    }
    else {
      watchdog("user", "mail password: '". $edit["name"] ."' and &lt;". $edit["mail"] ."&gt; do not match");

      return t("Could not sent password: no match for the specified username and e-mail address.");
    }
  }
  else {

    /*
    ** Display form:
    */

    $output .= form_textfield(t("Username"), "name", $edit["name"], 30, 64);
    $output .= form_textfield(t("E-mail address"), "mail", $edit["mail"], 30, 64);
    $output .= form_submit(t("E-mail new password"));

Dries's avatar
   
Dries committed
594
    return form($output);
Dries's avatar
   
Dries committed
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
  }
}

function user_register($edit = array()) {

  if ($edit["name"] && $edit["mail"]) {
    if ($error = user_validate_name($edit["name"])) {
      // do nothing
    }
    else if ($error = user_validate_mail($edit["mail"])) {
      // do nothing
    }
    else if (user_deny("user", $edit["name"])) {
      $error = sprintf(t("The name '%s' has been denied access."), $edit["name"]);
    }
    else if (user_deny("mail", $edit["mail"])) {
      $error = sprintf(t("The e-mail address '%s' has been denied access."), $edit["mail"]);
    }
Dries's avatar
   
Dries committed
613
    else if (db_num_rows(db_query("SELECT name FROM users WHERE LOWER(name) = LOWER('". $edit["name"] ."')")) > 0) {
Dries's avatar
   
Dries committed
614
615
      $error = sprintf(t("The name '%s' is already taken."), $edit["name"]);
    }
Dries's avatar
   
Dries committed
616
    else if (db_num_rows(db_query("SELECT mail FROM users WHERE LOWER(mail) = LOWER('". $edit["mail"] ."')")) > 0) {
Dries's avatar
   
Dries committed
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
      $error = sprintf(t("The e-mail address '%s' is already taken."), $edit["mail"]);
    }
    else if (variable_get("user_register", 1) == 0) {
      $error = t("Public registrations have been disabled by the site administrator.");
    }
    else {
      $success = 1;
    }
  }

  if ($success) {

    watchdog("user", "new user: '". $edit["name"] ."' &lt;". $edit["mail"] ."&gt;");

    $from = variable_get("site_mail", "root@localhost");
    $pass = user_password();

    if (variable_get("user_register", 1) == 1) {
      /*
      ** Create new user account, no administrator approval required:
      */

Dries's avatar
   
Dries committed
639
      user_save("", array("name" => $edit["name"], "pass" => $pass, "init" => $edit["mail"], "mail" => $edit["mail"], "role" => "authenticated user", "status" => 1));
Dries's avatar
   
Dries committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664

      user_mail($edit["mail"], t("user account details"), sprintf(t("%s,\n\nsomoneone signed up for a user account on %s and supplied this e-mail address as their contact.  If it wasn't you, just ignore this mail but if it was you, you can now login using the following username and password:\n\n  username: %s\n  password: %s\n\n\n-- %s team"), $edit["name"], variable_get("site_name", "drupal"), $edit["name"], $pass, variable_get("site_name", "drupal")), "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
    }
    else {
      /*
      ** Create new user account, administrator approval required:
      */

      user_save("", array("name" => $edit["name"], "pass" => $pass, "init" => $edit["mail"], "mail" => $edit["mail"], "role" => "authenticated user", "status" => 0));

      user_mail($edit["mail"], t("user account details"), sprintf(t("%s,\n\nsomoneone signed up for a user account on %s and supplied this e-mail address as their contact.  If it wasn't you, just ignore this mail but if it was you, you can login as soon a site administrator approved your request using the following username and password:\n\n  username: %s\n  password: %s\n\n\n-- %s team"), $edit["name"], variable_get("site_name", "drupal"), $edit["name"], $pass, variable_get("site_name", "drupal")), "From: $from\nReply-to: $from\nX-Mailer: Drupal\nReturn-path: $from\nErrors-to: $from");
    }

    return t("Your password and further instructions have been sent to your e-mail address.");
  }
  else {

    if ($error) {
      $output .= "<p><font color=\"red\">". check_output($error) ."</font></p>";
    }

    $output .= form_textfield(t("Username"), "name", $edit["name"], 30, 64, t("Your full name or your prefered username: only letters, numbers and spaces are allowed."));
    $output .= form_textfield(t("E-mail address"), "mail", $edit["mail"], 30, 64, t("Your e-mail address: a password and instructions will be sent to this e-mail address so make sure it is accurate."));
    $output .= form_submit(t("Create new account"));

Dries's avatar
   
Dries committed
665
    return form($output);
Dries's avatar
   
Dries committed
666
667
668
669
  }
}

function user_edit($edit = array()) {
Dries's avatar
   
Dries committed
670
  global $HTTP_HOST, $themes, $user, $languages;
Dries's avatar
   
Dries committed
671
672
673
674
675
676
677
678
679

  if ($user->uid) {
    if ($edit["name"]) {
      if ($error = user_validate_name($edit["name"])) {
        // do nothing
      }
      else if ($error = user_validate_mail($edit["mail"])) {
        // do nothing
      }
Dries's avatar
   
Dries committed
680
      else if (db_num_rows(db_query("SELECT uid FROM users WHERE uid != '$user->uid' AND LOWER(name) = LOWER('". $edit["name"] ."')")) > 0) {
Dries's avatar
   
Dries committed
681
682
        $error = sprintf(t("The name '%s' is already taken."), $edit["name"]);
      }
Dries's avatar
   
Dries committed
683
      else if ($edit["mail"] && db_num_rows(db_query("SELECT uid FROM users WHERE uid != '$user->uid' AND LOWER(mail) = LOWER('". $edit["mail"] ."')")) > 0) {
Dries's avatar
   
Dries committed
684
685
        $error = sprintf(t("The e-mail address '%s' is already taken."), $edit["mail"]);
      }
Dries's avatar
   
Dries committed
686
      else if ($edit["jabber"] && db_num_rows(db_query("SELECT uid FROM users WHERE uid != '$user->uid' AND LOWER(jabber) = LOWER('". $edit["jabber"] ."')")) > 0) {
Dries's avatar
   
Dries committed
687
688
689
690
691
692
693
694
        $error = sprintf(t("The Jabber ID '%s' is already taken."), $edit["jabber"]);
      }
      else if ($user->uid) {

        /*
        ** Save user information:
        */

Dries's avatar
   
Dries committed
695
        $user = user_save($user, array("name" => $edit["name"], "mail" => $edit["mail"], "jabber" => $edit["jabber"], "theme" => $edit["theme"], "timezone" => $edit["timezone"], "homepage" => $edit["homepage"], "signature" => $edit["signature"], "language" => $edit["language"]));
Dries's avatar
   
Dries committed
696
697
698
699
700
701
702
703
704
705
706
707
708

        /*
        ** Save new password (if required):
        */

        if ($edit[pass1] && $edit[pass1] == $edit[pass2]) {
          $user = user_save($user, array("pass" => $edit[pass1]));
        }

        /*
        ** Redirect the user to his personal information page:
        */

Dries's avatar
   
Dries committed
709
        drupal_goto("module.php?mod=user&op=view");
Dries's avatar
   
Dries committed
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
      }
    }

    if ($error) {
      $output .= "<p><font color=\"red\">". check_output($error) ."</font></p>";
    }

    $output .= form_textfield(t("Username"), "name", $user->name, 30, 55, t("Your full name or your prefered username: only letters, numbers and spaces are allowed."));
    $output .= form_textfield(t("E-mail address"), "mail", $user->mail, 30, 55, t("Insert a valid e-mail address.  All emails from the system will be sent to this address. The email address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by email."));
    $output .= form_textfield(t("Jabber ID"), "jabber", $user->jabber, 30, 55, t("Insert a valid Jabber ID.  If you are using your Jabber ID to log in,  it must be correct.  Your Jabber ID is not made public and is only used to log in or to authenticate for affilliate services."));
    $output .= form_textfield(t("Homepage"), "homepage", $user->homepage, 30, 55, t("Optional") .". ". t("Make sure you enter a fully qualified URL: remember to include \"http://\"."));
    foreach ($themes as $key=>$value) $options .= "<OPTION VALUE=\"$key\"". (($user->theme == $key) ? " SELECTED" : "") .">$key - $value[1]</OPTION>\n";
    $output .= form_item(t("Theme"), "<SELECT NAME=\"edit[theme]\">$options</SELECT>", t("Selecting a different theme will change the look and feel of the site."));
    for ($zone = -43200; $zone <= 46800; $zone += 3600) $zones[$zone] = date("l, F dS, Y - h:i A", time() - date("Z") + $zone) ." (GMT ". $zone / 3600 .")";
    $output .= form_select(t("Timezone"), "timezone", $user->timezone, $zones, t("Select what time you currently have and your timezone settings will be set appropriate."));
    $output .= form_select(t("Language"), "language", $user->language, $languages, t("Selecting a different language will change the language of the site."));
    $output .= form_textarea(t("Signature"), "signature", $user->signature, 70, 3, t("Your signature will be publicly displayed at the end of your comments.") ."<br />". t("Allowed HTML tags") .": ". htmlspecialchars(variable_get("allowed_html", "")));
    $output .= form_item(t("Password"), "<input type=\"password\" name=\"edit[pass1]\" size=\"12\" maxlength=\"24\"> <input type=\"password\" name=\"edit[pass2]\" size=\"12\" maxlength=\"24\">", t("Enter your new password twice if you want to change your current password or leave it blank if you are happy with your current password."));
    $output .= form_submit(t("Save user information"));
Dries's avatar
   
Dries committed
729
730

    $output = form($output);
Dries's avatar
   
Dries committed
731
732
  }

Dries's avatar
   
Dries committed
733
  return $output;
Dries's avatar
   
Dries committed
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
}

function user_menu() {
  $links[] = "<a href=\"module.php?mod=user&op=view\">view user information</a>";
  $links[] = "<a href=\"module.php?mod=user&op=edit\">edit user information</a>";
  $links[] = "<a href=\"module.php?mod=user&op=logout\">logout</a>";

  return "<div align=\"center\">". implode(" &middot; ", $links) ."</div>";
}

function user_view($uid = 0) {
  global $theme, $user, $HTTP_HOST;

  if (!$uid) {
    $uid = $user->uid;
  }

  if ($user->uid && $user->uid == $uid) {
    $output .= form_item(t("Name"), check_output("$user->name ($user->init)"));
    $output .= form_item(t("E-mail address"), check_output($user->mail));
    $output .= form_item(t("Drupal ID"), strtolower(urlencode($user->name) ."@$HTTP_HOST"));
    $output .= form_item(t("Jabber ID"), check_output($user->jabber));
    $output .= form_item(t("Homepage"), format_url($user->homepage));
    $output .= form_item(t("Signature"), check_output($user->signature, 1));

    $theme->header();
    $theme->box(t("User account"), user_menu());
    $theme->box(t("View user information"), $output);
    $theme->footer();
  }
  else if ($uid && $account = user_load(array("uid" => $uid, "status" => 1))) {
    $output .= form_item(t("Name"), check_output($account->name));
    $output .= form_item(t("Homepage"), format_url($account->homepage));

    $theme->header();
    $theme->box(t("View user information"), $output);
    $theme->footer();
  }
  else {
    $theme->header();
    $theme->box(t("Log in"), user_login());
    $theme->box(t("Create new user account"), user_register());
    $theme->box(t("E-mail new password"), user_pass());
    $theme->footer();
  }
}

function user_page() {
  global $edit, $op, $id, $theme;

  switch ($op) {
    case t("E-mail new password");
    case "password":
      $theme->header();
      $theme->box(t("E-mail new password"), user_pass($edit));
      $theme->footer();
      break;
    case t("Create new account"):
    case "register":
      $theme->header();
      $theme->box(t("Create new account"), user_register($edit));
      $theme->footer();
      break;
    case t("Log in"):
    case "login":
      $output = user_login($edit);
      $theme->header();
      $theme->box(t("Log in"), $output);
      $theme->footer();
      break;
    case t("Save user information"):
    case "edit":
      $output = user_edit($edit);
      $theme->header();
      $theme->box(t("User account"), user_menu());
      $theme->box(t("Edit user information"), $output);
      $theme->footer();
      break;
    case "view":
      user_view($id);
      break;
    case t("Logout"):
    case "logout":
      print user_logout();
      break;
    default:
      print user_view();
  }

}

/*** Administrative features ***********************************************/

function user_conf_options() {
  $output .= form_select("Allow authentication with Drupal IDs", "user_drupal", variable_get("user_drupal", 1), array("Disabled", "Enabled"), "Allow people to authenticate with their Drupal ID and password from other Drupal sites.");
  $output .= form_select("Allow authentication with Jabber IDs", "user_jabber", variable_get("user_jabber", 1), array("Disabled", "Enabled"), "Allow people to authenticate with their Jabber ID.");
Dries's avatar
   
Dries committed
830
  $output .= form_select("Public registrations", "user_register", variable_get("user_register", 1), array("Only site administrators can create new user accounts.", "Visitors can create accounts and no administrator approval is required.", "Visitors can create accounts but administrator approval is required."));
Dries's avatar
   
Dries committed
831
832
833
834
835
836
  $output .= form_textfield("Password words", "user_password", variable_get("user_password", "foo,bar,guy,neo,tux,moo,sun,asm,dot,god,axe,geek,nerd,fish,hack,star,mice,warp,moon,hero,cola,girl,fish,java,perl,boss,dark,sith,jedi,drop,mojo"), 55, 256, "A comma separated list of short words that can be concatenated to generate human-readable passwords.");

  return $output;
}

function user_admin_settings($edit = array()) {
Dries's avatar
   
Dries committed
837
  global $op;
Dries's avatar
   
Dries committed
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858

  if ($op == "Save configuration") {
    /*
    ** Save the configuration options:
    */

    foreach ($edit as $name => $value) variable_set($name, $value);
  }

  if ($op == "Reset to defaults") {
    /*
    ** Reset the configuration options to their default value:
    */

    foreach ($edit as $name=>$value) variable_del($name);
  }

  $output .= user_conf_options();
  $output .= form_submit("Save configuration");
  $output .= form_submit("Reset to defaults");

Dries's avatar
   
Dries committed
859
  return form($output);
Dries's avatar
   
Dries committed
860
861
862
863
864
865
866
867
868
869
870
871

}

function user_admin_create($edit = array()) {

  if ($edit["name"] || $edit["mail"]) {
    if ($error = user_validate_name($edit["name"])) {
      // do nothing
    }
    else if ($error = user_validate_mail($edit["mail"])) {
      // do nothing
    }
Dries's avatar
   
Dries committed
872
    else if (db_num_rows(db_query("SELECT name FROM users WHERE LOWER(name) = LOWER('". $edit["name"] ."')")) > 0) {
Dries's avatar
   
Dries committed
873
874
      $error = sprintf(t("The name '%s' is already taken."), $edit["name"]);
    }
Dries's avatar
   
Dries committed
875
    else if (db_num_rows(db_query("SELECT mail FROM users WHERE LOWER(mail) = LOWER('". $edit["mail"] ."')")) > 0) {
Dries's avatar
   
Dries committed
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
      $error = sprintf(t("The e-mail address '%s' is already taken."), $edit["mail"]);
    }
    else {
      $success = 1;
    }
  }

  if ($success) {

    watchdog("user", "new user: '". $edit["name"] ."' &lt;". $edit["mail"] ."&gt;");

    user_save("", array("name" => $edit["name"], "pass" => $edit["pass"], "init" => $edit["mail"], "mail" => $edit["mail"], "role" => "authenticated user", "status" => 1));

    return "Created a new user '". $edit["name"] ."'.  No e-mail has been sent.";
  }
  else {

    if ($error) {
      $output .= "<p><font color=\"red\">". check_output($error) ."</font></p>";
    }

    $output .= form_textfield("Username", "name", $edit["name"], 30, 55);
    $output .= form_textfield("E-mail address", "mail", $edit["mail"], 30, 55);
    $output .= form_textfield("Password", "pass", $edit["pass"], 30, 55);
    $output .= form_submit("Create account");

Dries's avatar
   
Dries committed
902
    return form($output);
Dries's avatar
   
Dries committed
903
904
905
906
  }
}

function user_admin_access($edit = array()) {
Dries's avatar
   
Dries committed
907
  global $op, $id, $type;
Dries's avatar
   
Dries committed
908

Dries's avatar
   
Dries committed
909
  $output .= "<small><a href=\"admin.php?mod=user&op=access&type=mail\">e-mail rules</a> :: <a href=\"admin.php?mod=user&op=access&type=user\">username rules</a></small><hr />";
Dries's avatar
   
Dries committed
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961

  if ($type != "user") {
    $output .= "<h3>E-mail rules</h3>";
    $type = "mail";
  }
  else {
    $output .= "<h3>Username rules</h3>";
  }

  if ($op == "Add rule") {
    db_query("INSERT INTO access (mask, type, status) VALUES ('". check_input($edit["mask"]) ."', '". check_input($type) ."', '". check_input($edit["status"]) ."')");
  }
  else if ($op == "Check") {
    if (user_deny($type, $edit["test"])) {
      $message = "<b>'". $edit["test"] ."' is not allowed.</b><p />";
    }
    else {
      $message = "<b>'". $edit["test"] ."' is allowed.</b><p />";
    }
  }
  else if ($id) {
    db_query("DELETE FROM access WHERE aid = '$id'");
  }

  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
  $output .= " <tr><th>type</th><th>mask</th><th>operations</th></tr>";

  $result = db_query("SELECT * FROM access WHERE type = '". check_input($type) ."' AND status = '1' ORDER BY mask");

  while ($rule = db_fetch_object($result)) {
    $output .= "<tr><td align=\"center\">allow</td><td>". check_output($rule->mask) ."</td><td><a href=\"admin.php?mod=user&op=access&type=$type&id=$rule->aid\">delete rule</a></td></tr>";
  }

  $result = db_query("SELECT * FROM access WHERE type = '". check_input($type) ."' AND status = '0' ORDER BY mask");

  while ($rule = db_fetch_object($result)) {
    $output .= "<tr><td align=\"center\">deny</td><td>". check_output($rule->mask) ."</td><td><a href=\"admin.php?mod=user&op=access&type=$type&id=$rule->aid\">delete rule</a></td></tr>";
  }

  $output .= " <tr><td><select name=\"edit[status]\"><option value=\"1\">allow</option><option value=\"0\">deny</option></select></td><td><input size=\"32\" maxlength=\"64\" name=\"edit[mask]\" /></td><td><input type=\"submit\" name=\"op\" value=\"Add rule\" /></td></tr>";
  $output .= "</table>";
  $output .= "<p><small>%: matches any number of characters, even zero characters.<br />_: matches exactly one character.</small></p>";

  if ($type != "user") {
    $output .= "<h3>Check e-mail address</h3>";
  }
  else {
    $output .= "<h3>Check username</h3>";
  }

  $output .= "$message<input size=\"32\" maxlength=\"64\" name=\"edit[test]\" value=\"". $edit["test"] ."\" /><input type=\"submit\" name=\"op\" value=\"Check\" />";

Dries's avatar
   
Dries committed
962
  return form($output);
Dries's avatar
   
Dries committed
963
964
965

}

Dries's avatar
   
Dries committed
966
967
968
969
970
971
972
973
function user_roles() {
  $result = db_query("SELECT * FROM role ORDER BY name");
  while ($role = db_fetch_object($result)) {
    $roles[$role->name] = $role->name;
  }
  return $roles;
}

Dries's avatar
   
Dries committed
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
function user_admin_perm($edit = array()) {

  if ($edit) {

    /*
    ** Save permissions:
    */

    $result = db_query("SELECT * FROM role");
    while ($role = db_fetch_object($result)) {
      $perm = $edit[$role->name] ? implode(", ", array_keys($edit[$role->name])) : "";
      db_query("UPDATE role SET perm = '$perm' WHERE name = '$role->name'");
    }
  }

  /*
  ** Compile permission array:
  */

  foreach (module_list() as $name) {
    if (module_hook($name, "perm")) {
      $perms = array_merge($perms, module_invoke($name, "perm"));
    }
  }
  asort($perms);

  /*
  ** Compile role array:
  */

  $result = db_query("SELECT * FROM role ORDER BY name");
Dries's avatar
   
Dries committed
1005
  $roles = array ();
Dries's avatar
   
Dries committed
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
  while ($role = db_fetch_object($result)) {
    $roles[$role->name] = $role->perm;
  }

  /*
  ** Render roles / permission overview:
  */

  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
  $output .= " <tr><th>&nbsp;</th><th>". implode("</th><th>", array_keys($roles)) ."</th></tr>";
  foreach ($perms as $perm) {
    $output .= " <tr>";
    $output .= "  <td>". check_output($perm) ."</td>";
    foreach ($roles as $name => $value) {
      $output .= "  <td align=\"center\"><input type=\"checkbox\" name=\"edit[$name][$perm]\"". (strstr($value, $perm) ? " checked=\"checked\"" : "") ."></td>";
    }
    $output .= " </tr>";
  }
  $output .= "</table>";
  $output .= form_submit("Save permissions");

Dries's avatar
   
Dries committed
1027
  return form($output);
Dries's avatar
   
Dries committed
1028
1029
1030
1031

}

function user_admin_role($edit = array()) {
Dries's avatar
   
Dries committed
1032
  global $op, $id;
Dries's avatar
   
Dries committed
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054

  if ($op == "Save role") {
    db_query("UPDATE role SET name = '". $edit["name"] ."' WHERE rid = '$id'");
  }
  else if ($op == "Delete role") {
    db_query("DELETE FROM role WHERE rid = '$id'");
  }
  else if ($op == "Add role") {
    db_query("INSERT INTO role (name) VALUES ('". $edit["name"] ."')");
  }
  else if ($id) {

    /*
    ** Display role form:
    */

    $role = db_fetch_object(db_query("SELECT * FROM role WHERE rid = '$id'"));

    $output .= form_textfield("Role name", "name", $role->name, 32, 64, "The name for this role.  Example: 'moderator', 'editorial board', 'site architect'.");
    $output .= form_submit("Save role");
    $output .= form_submit("Delete role");

Dries's avatar
   
Dries committed
1055
    $output = form($output);
Dries's avatar
   
Dries committed
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
  }

  if (!$output) {
    /*
    ** Render role overview:
    */

    $result = db_query("SELECT * FROM role ORDER BY name");

    $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
    $output .= " <tr><th>name</th><th>operations</th></tr>";
    while ($role = db_fetch_object($result)) {
      $output .= "<tr><td>". check_output($role->name) ."</td><td><a href=\"admin.php?mod=user&op=role&id=$role->rid\">edit role</a></td></tr>";
    }
    $output .= " <tr><td><input size=\"32\" maxlength=\"64\" name=\"edit[name]\" /></td><td><input type=\"submit\" name=\"op\" value=\"Add role\" /></td></tr>";
    $output .= "</table>";
Dries's avatar
   
Dries committed
1072
1073

    $output = form($output);
Dries's avatar
   
Dries committed
1074
1075
1076
1077
1078
1079
  }

  return $output;
}

function user_admin_edit($edit = array()) {
Dries's avatar
   
Dries committed
1080
  global $op, $id, $HTTP_HOST;
Dries's avatar
   
Dries committed
1081
1082
1083
1084
1085
1086
1087

  if ($account = user_load(array("uid" => $id))) {
    if ($op == "Save account") {
      $account = user_save($account, $edit);
    }
    else if ($op == "Delete account") {
      if ($edit["status"] == 0) {
Dries's avatar
   
Dries committed
1088
        db_query("DELETE FROM users WHERE uid = '$account->uid'");
Dries's avatar
   
Dries committed
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
        $output .= "The account has been deleted.";
      }
      else {
        $output .= "Failed to delete account: the account has to be blocked first.";
      }
    }

    if (!$output) {

      /*
      ** Display user form:
      */

      $output .= form_item("User ID", check_output($account->uid));
      $output .= form_item(t("Name"), check_output("$account->name ($account->init)"));
      $output .= form_item(t("E-mail address"), format_email($account->mail));
      $output .= form_item(t("Drupal ID"), strtolower(urlencode($account->name) ."@$HTTP_HOST"));
      $output .= form_item(t("Jabber ID"), check_output($account->jabber));
Dries's avatar
   
Dries committed
1107
      $output .= form_item(t("Theme"), check_output("$account->theme"));
Dries's avatar
   
Dries committed
1108
      $output .= form_select("Status", "status", $account->status, array("blocked", "active"));
Dries's avatar
   
Dries committed
1109
      $output .= form_select("Role", "role", $account->role, user_roles());
Dries's avatar
   
Dries committed
1110
1111
1112
1113

      $output .= form_submit("Save account");
      $output .= form_submit("Delete account");

Dries's avatar
   
Dries committed
1114
      $output = form($output);
Dries's avatar
   
Dries committed
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
    }
  }
  else {
    $output = "no such user";
  }

  return $output;
}

function user_admin_account() {
  global $query;

  $queries = array(array("ORDER BY timestamp DESC", "active users"), array("ORDER BY uid DESC", "new users"), array("WHERE status = 0 ORDER BY uid DESC", "blocked users"), array("WHERE role != 'authenticated user' ORDER BY uid DESC", "special users"));

Dries's avatar
   
Dries committed
1129
  $result = db_query("SELECT uid, name, timestamp FROM users ". $queries[$query ? $query : 0][0] ." LIMIT 50");
Dries's avatar
   
Dries committed
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157

  foreach ($queries as $key => $value) {
    $links[] = "<a href=\"admin.php?mod=user&op=account&query=$key\">$value[1]</a>";
  }

  $output .= "<small>". implode(" :: ", $links) ."</small><hr />";

  $output .= "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\">";
  $output .= " <tr><th>username</th><th>last access</th><th>operations</th></tr>";
  while ($account = db_fetch_object($result)) {
    $output .= " <tr><td>". format_name($account) ."</td><td>". format_date($account->timestamp, "small") ."</td><td align=\"center\"><a href=\"admin.php?mod=user&op=edit&id=$account->uid\">edit account</a></td></tr>";
  }
  $output .= "</table>";

  return $output;

}

function admin_access_init() {
  $role = db_fetch_object(db_query("SELECT * FROM role WHERE name = 'anonymous user'"));
  if (!$role) db_query("INSERT INTO role (name) VALUES ('anonymous user')");

  $role = db_fetch_object(db_query("SELECT * FROM role WHERE name = 'authenticated user'"));
  if (!$role) db_query("INSERT INTO role (name) VALUES ('authenticated user')");
}


function user_admin() {
Dries's avatar
   
Dries committed
1158
  global $edit, $id, $op, $user;
Dries's avatar
   
Dries committed
1159

Dries's avatar
   
Dries committed
1160
  if (user_access("administer users")) {
Dries's avatar
   
Dries committed
1161

Dries's avatar
   
Dries committed
1162
1163
1164
    /*
    ** Initialize all the roles and permissions:
    */
Dries's avatar
   
Dries committed
1165

Dries's avatar
   
Dries committed
1166
    admin_access_init();
Dries's avatar
   
Dries committed
1167

Dries's avatar
   
Dries committed
1168
1169
1170
    /*
    ** Compile all the administrative links:
    */
Dries's avatar
   
Dries committed
1171

Dries's avatar
   
Dries committed
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
    $links[] = "<a href=\"admin.php?mod=user&op=create\">add new user</a>";
    $links[] = "<a href=\"admin.php?mod=user&op=access\">access rules</a>";
    $links[] = "<a href=\"admin.php?mod=user&op=account\">user accounts</a>";
    $links[] = "<a href=\"admin.php?mod=user&op=role\">user roles</a>";
    $links[] = "<a href=\"admin.php?mod=user&op=permission\">user permissions</a>";
    $links[] = "<a href=\"admin.php?mod=user&op=search\">search account</a>";
    $links[] = "<a href=\"admin.php?mod=user&op=settings\">settings</a>";
    $links[] = "<a href=\"admin.php?mod=user&op=help\">help</a>";

    print "<small>". implode(" &middot; ", $links) ."</small><hr />";

    switch ($op) {
      case "help":
        print user_help();
        break;
      case "search":
Dries's avatar
   
Dries committed
1188
        print search_type("user", "admin.php?mod=user&op=search");
Dries's avatar
   
Dries committed
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
        break;
      case "Save configuration":
      case "Reset to defaults":
      case "settings":
        print user_admin_settings($edit);
        break;
      case "Add rule":
      case "Check":
      case "access":
        print user_admin_access($edit);
        break;
      case "Save permissions":
      case "permission":
        print user_admin_perm($edit);
        break;
      case "Create account":
      case "create":
        print user_admin_create($edit);
        break;
      case "Add role":
      case "Delete role":
      case "Save role":
      case "role":
        print user_admin_role($edit);
        break;
      case "Delete account":
      case "Save account":
      case "edit":
        print user_admin_edit($edit);
        break;
      default:
        print user_admin_account();
    }
Dries's avatar
   
Dries committed
1222
1223
1224
  }
}

Dries's avatar
   
Dries committed
1225
?>