registeruser.php 2.4 KB

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