]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiaccountregister.php
Fix OpenID discovery in pages using uppercase <HEAD> tag
[quix0rs-gnu-social.git] / actions / apiaccountregister.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Register account
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  API
23  * @package   GNUsocial
24  * @author    Hannes Mannerheim <h@nnesmannerhe.im>
25  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
26  * @link      http://www.gnu.org/software/social/
27  */
28
29 if (!defined('GNUSOCIAL')) { exit(1); }
30
31 class ApiAccountRegisterAction extends ApiAction
32 {
33
34     /**
35      * Has there been an error?
36      */
37     var $error = null;
38
39     /**
40      * Have we registered?
41      */
42     var $registered = false;
43
44     protected $needPost = true;
45
46     protected $code   = null; // invite code
47     protected $invite = null; // invite to-be-stored
48
49     /**
50      * Take arguments for running
51      *
52      * @param array $args $_REQUEST args
53      *
54      * @return boolean success flag
55      */
56     protected function prepare(array $args=array())
57     {
58         parent::prepare($args);
59
60         if ($this->format !== 'json') {
61             $this->clientError('This method currently only serves JSON.', 415);
62         }
63
64         $this->code = $this->trimmed('code');
65
66         return true;
67     }
68
69     /**
70      * Handle the request
71      *
72      * @param array $args $_REQUEST data (unused)
73      *
74      * @return void
75      */
76     protected function handle()
77     {
78         parent::handle();
79
80         $nickname = $this->trimmed('nickname');
81         $email    = $this->trimmed('email');
82         $fullname = $this->trimmed('fullname');
83         $homepage = $this->trimmed('homepage');
84         $bio      = $this->trimmed('bio');
85         $location = $this->trimmed('location');
86
87         // We don't trim these... whitespace is OK in a password!
88         $password = $this->arg('password');
89         $confirm  = $this->arg('confirm');
90
91         if (empty($this->code)) {
92             common_ensure_session();
93             if (array_key_exists('invitecode', $_SESSION)) {
94                 $this->code = $_SESSION['invitecode'];
95             }
96         }
97
98         if (common_config('site', 'inviteonly') && empty($this->code)) {
99             // TRANS: Client error displayed when trying to register to an invite-only site without an invitation.
100             $this->clientError(_('Sorry, only invited people can register.'), 401);
101         }
102
103         if (!empty($this->code)) {
104             $this->invite = Invitation::getKV('code', $this->code);
105             if (empty($this->invite)) {
106             // TRANS: Client error displayed when trying to register to an invite-only site without a valid invitation.
107                     $this->clientError(_('Sorry, invalid invitation code.'), 401);
108             }
109             // Store this in case we need it
110             common_ensure_session();
111             $_SESSION['invitecode'] = $this->code;
112         }
113
114         // Input scrubbing
115         try {
116             $nickname = Nickname::normalize($nickname, true);
117         } catch (NicknameException $e) {
118             // clientError handles Api exceptions with various formats and stuff
119                 $this->clientError($e->getMessage(), $e->getCode());
120         }
121
122         $email = common_canonical_email($email);
123
124             if ($email && !Validate::email($email, common_config('email', 'check_domain'))) {
125             // TRANS: Form validation error displayed when trying to register without a valid e-mail address.
126                 $this->clientError(_('Not a valid email address.'), 400);
127         } else if ($this->emailExists($email)) {
128             // TRANS: Form validation error displayed when trying to register with an already registered e-mail address.
129                 $this->clientError(_('Email address already exists.'), 400);
130         } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
131                    !common_valid_http_url($homepage)) {
132             // TRANS: Form validation error displayed when trying to register with an invalid homepage URL.
133                 $this->clientError(_('Homepage is not a valid URL.'), 400);
134         } else if (Profile::bioTooLong($bio)) {
135             // TRANS: Form validation error on registration page when providing too long a bio text.
136             // TRANS: %d is the maximum number of characters for bio; used for plural.
137                 $this->clientError(sprintf(_m('Bio is too long (maximum %d character).',
138                                        'Bio is too long (maximum %d characters).',
139                                        Profile::maxBio()),
140                                        Profile::maxBio()), 400);
141         } else if (strlen($password) < 6) {
142             // TRANS: Form validation error displayed when trying to register with too short a password.
143                 $this->clientError(_('Password must be 6 or more characters.'), 400);
144         } else if ($password != $confirm) {
145             // TRANS: Form validation error displayed when trying to register with non-matching passwords.
146                 $this->clientError(_('Passwords do not match.'), 400);
147         } else {
148
149             // annoy spammers
150             sleep(7);
151             
152                         if (Event::handle('APIStartRegistrationTry', array($this))) { 
153                                 try {
154                                         $user = User::register(array('nickname' => $nickname,
155                                                                                                                 'password' => $password,
156                                                                                                                 'email' => $email,
157                                                                                                                 'fullname' => $fullname,
158                                                                                                                 'homepage' => $homepage,
159                                                                                                                 'bio' => $bio,
160                                                                                                                 'location' => $location,
161                                                                                                                 'code' => $this->code));
162                                         Event::handle('EndRegistrationTry', array($this));
163
164                                         $this->initDocument('json');
165                                         $this->showJsonObjects($this->twitterUserArray($user->getProfile()));
166                                         $this->endDocument('json');
167
168                                 } catch (Exception $e) {
169                                         $this->clientError($e->getMessage(), 400);
170                                 }                       
171                         }
172         }
173     }
174
175     /**
176      * Does the given email address already exist?
177      *
178      * Checks a canonical email address against the database.
179      *
180      * @param string $email email address to check
181      *
182      * @return boolean true if the address already exists
183      */
184     function emailExists($email)
185     {
186         $email = common_canonical_email($email);
187         if (!$email || strlen($email) == 0) {
188             return false;
189         }
190         $user = User::getKV('email', $email);
191         return is_object($user);
192     }
193
194 }