]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - scripts/registeruser.php
Make attachment fit better in notice: drop text and link
[quix0rs-gnu-social.git] / scripts / registeruser.php
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', dirname(__DIR__));
22 define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
23
24 $shortoptions = 'n:w:f:e:';
25 $longoptions = array('nickname=', 'password=', 'fullname=', 'email=');
26
27 $helptext = <<<END_OF_REGISTERUSER_HELP
28 registeruser.php [options]
29 registers a user in the database
30
31   -n --nickname nickname of the new user
32   -w --password password of the new user
33   -f --fullname full name of the new user (optional)
34   -e --email    email address of the new user (optional)
35
36 END_OF_REGISTERUSER_HELP;
37
38 require_once INSTALLDIR.'/scripts/commandline.inc';
39
40 $nickname = get_option_value('n', 'nickname');
41 $password = get_option_value('w', 'password');
42 $fullname = get_option_value('f', 'fullname');
43
44 $email = get_option_value('e', 'email');
45
46 if (empty($nickname) || empty($password)) {
47     print "Must provide a username and password.\n";
48     exit(1);
49 }
50
51 try {
52
53     $user = User::getKV('nickname', $nickname);
54
55     if (!empty($user)) {
56         throw new Exception("A user named '$nickname' already exists.");
57     }
58
59     $user = User::register(array('nickname' => $nickname,
60                                  'password' => $password,
61                                  'fullname' => $fullname));
62
63     if (empty($user)) {
64         throw new Exception("Can't register user '$nickname' with password '$password' and fullname '$fullname'.");
65     }
66
67     if (!empty($email)) {
68
69         $orig = clone($user);
70
71         $user->email = $email;
72
73         // Throws exception on failure.
74         $user->updateWithKeys($orig);
75     }
76
77 } catch (Exception $e) {
78     print $e->getMessage() . "\n";
79     exit(1);
80 }