1
#!/usr/bin/env php
2
<?php
3
/*
4
 * StatusNet - a distributed open-source microblogging tool
5
 * Copyright (C) 2008, 2009, StatusNet, Inc.
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
22
23
$shortoptions = 'n:w:f:e:';
24
$longoptions = array('nickname=', 'password=', 'fullname=', 'email=');
25
26
$helptext = <<<END_OF_REGISTERUSER_HELP
27
registeruser.php [options]
28
registers a user in the database
29
30
  -n --nickname nickname of the new user
31
  -w --password password of the new user
32
  -f --fullname full name of the new user (optional)
33
  -e --email    email address of the new user (optional)
34
35
END_OF_REGISTERUSER_HELP;
36
37
require_once INSTALLDIR.'/scripts/commandline.inc';
38
39
$nickname = get_option_value('n', 'nickname');
40
$password = get_option_value('w', 'password');
41
$fullname = get_option_value('f', 'fullname');
42
43
$email = get_option_value('e', 'email');
44
45
if (empty($nickname) || empty($password)) {
46
    print "Must provide a username and password.\n";
47
    exit(1);
48
}
49
50
try {
51
52
    $user = User::staticGet('nickname', $nickname);
53
54
    if (!empty($user)) {
55
        throw new Exception("A user named '$nickname' already exists.");
56
    }
57
58
    $user = User::register(array('nickname' => $nickname,
59
                                 'password' => $password,
60
                                 'fullname' => $fullname));
61
62
    if (empty($user)) {
63
        throw new Exception("Can't register user '$nickname' with password '$password' and fullname '$fullname'.");
64
    }
65
66
    if (!empty($email)) {
67
68
        $orig = clone($user);
69
70
        $user->email = $email;
71
72
        if (!$user->updateKeys($orig)) {
73
            print "Failed!\n";
74
            throw new Exception("Can't update email address.");
75
        }
76
    }
77
78
} catch (Exception $e) {
79
    print $e->getMessage() . "\n";
80
    exit(1);
81
}