registeruser.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
  21. $shortoptions = 'n:w:f:e:';
  22. $longoptions = array('nickname=', 'password=', 'fullname=', 'email=');
  23. $helptext = <<<END_OF_REGISTERUSER_HELP
  24. registeruser.php [options]
  25. registers a user in the database
  26. -n --nickname nickname of the new user
  27. -w --password password of the new user
  28. -f --fullname full name of the new user (optional)
  29. -e --email email address of the new user (optional)
  30. END_OF_REGISTERUSER_HELP;
  31. require_once INSTALLDIR.'/scripts/commandline.inc';
  32. $nickname = get_option_value('n', 'nickname');
  33. $password = get_option_value('w', 'password');
  34. $fullname = get_option_value('f', 'fullname');
  35. $email = get_option_value('e', 'email');
  36. if (empty($nickname) || empty($password)) {
  37. print "Must provide a username and password.\n";
  38. exit(1);
  39. }
  40. try {
  41. $user = User::getKV('nickname', $nickname);
  42. if (!empty($user)) {
  43. throw new Exception("A user named '$nickname' already exists.");
  44. }
  45. $user = User::register(array('nickname' => $nickname,
  46. 'password' => $password,
  47. 'fullname' => $fullname));
  48. if (empty($user)) {
  49. throw new Exception("Can't register user '$nickname' with password '$password' and fullname '$fullname'.");
  50. }
  51. if (!empty($email)) {
  52. $orig = clone($user);
  53. $user->email = $email;
  54. // Throws exception on failure.
  55. $user->updateWithKeys($orig);
  56. }
  57. } catch (Exception $e) {
  58. print $e->getMessage() . "\n";
  59. exit(1);
  60. }