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