apichecknickname.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Check nickname
  6. *
  7. * Returns 1 if nickname is available on this instance, 0 if not. Error if site is private.
  8. *
  9. * PHP version 5
  10. *
  11. * LICENCE: This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. * @category API
  25. * @package GNUsocial
  26. * @author Hannes Mannerheim <h@nnesmannerhe.im>
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  28. * @link http://www.gnu.org/software/social/
  29. */
  30. if (!defined('GNUSOCIAL')) { exit(1); }
  31. class ApiCheckNicknameAction extends ApiAction
  32. {
  33. protected function prepare(array $args=array())
  34. {
  35. parent::prepare($args);
  36. if (common_config('site', 'private')) {
  37. $this->clientError(_('This site is private.'), 403);
  38. }
  39. if ($this->format !== 'json') {
  40. $this->clientError('This method currently only serves JSON.', 415);
  41. }
  42. return true;
  43. }
  44. protected function handle()
  45. {
  46. parent::handle();
  47. $nickname = $this->trimmed('nickname');
  48. try {
  49. Nickname::normalize($nickname, true);
  50. $nickname_ok = 1;
  51. } catch (NicknameException $e) {
  52. $nickname_ok = 0;
  53. }
  54. $this->initDocument('json');
  55. $this->showJsonObjects($nickname_ok);
  56. $this->endDocument('json');
  57. }
  58. }