]> git.mxchange.org Git - friendica.git/blob - include/user.php
Merge branch 'master', remote-tracking branch 'remotes/upstream/master'
[friendica.git] / include / user.php
1 <?php
2
3 require_once('include/config.php');
4 require_once('include/network.php');
5 require_once('include/plugin.php');
6 require_once('include/text.php');
7 require_once('include/pgettext.php');
8 require_once('include/datetime.php');
9
10 function create_user($arr) {
11
12         // Required: { username, nickname, email } or { openid_url }
13
14         $a = get_app();
15         $result = array('success' => false, 'user' => null, 'password' => '', 'message' => '');
16
17         $using_invites = get_config('system','invitation_only');
18         $num_invites   = get_config('system','number_invites');
19
20
21         $invite_id  = ((x($arr,'invite_id'))  ? notags(trim($arr['invite_id']))  : '');
22         $username   = ((x($arr,'username'))   ? notags(trim($arr['username']))   : '');
23         $nickname   = ((x($arr,'nickname'))   ? notags(trim($arr['nickname']))   : '');
24         $email      = ((x($arr,'email'))      ? notags(trim($arr['email']))      : '');
25         $openid_url = ((x($arr,'openid_url')) ? notags(trim($arr['openid_url'])) : '');
26         $photo      = ((x($arr,'photo'))      ? notags(trim($arr['photo']))      : '');
27         $publish    = ((x($arr,'profile_publish_reg') && intval($arr['profile_publish_reg'])) ? 1 : 0);
28         $password   = ((x($arr,'password'))   ? trim($arr['password'])           : '');
29
30         $netpublish = ((strlen(get_config('system','directory_submit_url'))) ? $publish : 0);
31                 
32         $tmp_str = $openid_url;
33
34         if($using_invites) {
35                 if(! $invite_id) {
36                         $result['message'] .= t('An invitation is required.') . EOL;
37                         return $result;
38                 }
39                 $r = q("select * from register where `hash` = '%s' limit 1", dbesc($invite_id));
40                 if(! results($r)) {
41                         $result['message'] .= t('Invitation could not be verified.') . EOL;
42                         return $result;
43                 }
44         } 
45
46         if((! x($username)) || (! x($email)) || (! x($nickname))) {
47                 if($openid_url) {
48                         if(! validate_url($tmp_str)) {
49                                 $result['message'] .= t('Invalid OpenID url') . EOL;
50                                 return $result;
51                         }
52                         $_SESSION['register'] = 1;
53                         $_SESSION['openid'] = $openid_url;
54                         require_once('library/openid.php');
55                         $openid = new LightOpenID;
56                         $openid->identity = $openid_url;
57                         $openid->returnUrl = $a->get_baseurl() . '/openid'; 
58                         $openid->required = array('namePerson/friendly', 'contact/email', 'namePerson');
59                         $openid->optional = array('namePerson/first','media/image/aspect11','media/image/default');
60                         goaway($openid->authUrl());
61                         // NOTREACHED   
62                 }
63
64                 notice( t('Please enter the required information.') . EOL );
65                 return;
66         }
67
68         if(! validate_url($tmp_str))
69                 $openid_url = '';
70
71
72         $err = '';
73
74         // collapse multiple spaces in name
75         $username = preg_replace('/ +/',' ',$username);
76
77         if(mb_strlen($username) > 48)
78                 $result['message'] .= t('Please use a shorter name.') . EOL;
79         if(mb_strlen($username) < 3)
80                 $result['message'] .= t('Name too short.') . EOL;
81
82         // I don't really like having this rule, but it cuts down
83         // on the number of auto-registrations by Russian spammers
84         
85         //  Using preg_match was completely unreliable, due to mixed UTF-8 regex support
86         //      $no_utf = get_config('system','no_utf');
87         //      $pat = (($no_utf) ? '/^[a-zA-Z]* [a-zA-Z]*$/' : '/^\p{L}* \p{L}*$/u' ); 
88
89         // So now we are just looking for a space in the full name. 
90         
91         $loose_reg = get_config('system','no_regfullname');
92         if(! $loose_reg) {
93                 $username = mb_convert_case($username,MB_CASE_TITLE,'UTF-8');
94                 if(! strpos($username,' '))
95                         $result['message'] .= t("That doesn't appear to be your full \x28First Last\x29 name.") . EOL;
96         }
97
98
99         if(! allowed_email($email))
100                         $result['message'] .= t('Your email domain is not among those allowed on this site.') . EOL;
101
102         if((! valid_email($email)) || (! validate_email($email)))
103                 $result['message'] .= t('Not a valid email address.') . EOL;
104
105         // Disallow somebody creating an account using openid that uses the admin email address,
106         // since openid bypasses email verification. We'll allow it if there is not yet an admin account.
107
108         if((x($a->config,'admin_email')) && (strcasecmp($email,$a->config['admin_email']) == 0) && strlen($openid_url)) {
109                 $r = q("SELECT * FROM `user` WHERE `email` = '%s' LIMIT 1",
110                         dbesc($email)
111                 );
112                 if(count($r))
113                         $result['message'] .= t('Cannot use that email.') . EOL;
114         }
115
116         $nickname = $arr['nickname'] = strtolower($nickname);
117
118         if(! preg_match("/^[a-z][a-z0-9\-\_]*$/",$nickname))
119                 $result['message'] .= t('Your "nickname" can only contain "a-z", "0-9", "-", and "_", and must also begin with a letter.') . EOL;
120         $r = q("SELECT `uid` FROM `user`
121                 WHERE `nickname` = '%s' LIMIT 1",
122                 dbesc($nickname)
123         );
124         if(count($r))
125                 $result['message'] .= t('Nickname is already registered. Please choose another.') . EOL;
126
127         // Check deleted accounts that had this nickname. Doesn't matter to us,
128         // but could be a security issue for federated platforms.
129
130         $r = q("SELECT * FROM `userd`
131                 WHERE `username` = '%s' LIMIT 1",
132                 dbesc($nickname)
133         );
134         if(count($r))
135                 $result['message'] .= t('Nickname was once registered here and may not be re-used. Please choose another.') . EOL;
136
137         if(strlen($result['message'])) {
138                 return $result;
139         }
140
141         $new_password = ((strlen($password)) ? $password : autoname(6) . mt_rand(100,9999));
142         $new_password_encoded = hash('whirlpool',$new_password);
143
144         $result['password'] = $new_password;
145
146         require_once('include/crypto.php');
147
148         $keys = new_keypair(1024);
149
150         if($keys === false) {
151                 $result['message'] .= t('SERIOUS ERROR: Generation of security keys failed.') . EOL;
152                 return $result;
153         }
154
155         $prvkey = $keys['prvkey'];
156         $pubkey = $keys['pubkey'];
157
158         /**
159          *
160          * Create another keypair for signing/verifying
161          * salmon protocol messages. We have to use a slightly
162          * less robust key because this won't be using openssl
163          * but the phpseclib. Since it is PHP interpreted code
164          * it is not nearly as efficient, and the larger keys
165          * will take several minutes each to process.
166          *
167          */
168         
169         $sres    = new_keypair(512);
170         $sprvkey = $sres['prvkey'];
171         $spubkey = $sres['pubkey'];
172
173         $r = q("INSERT INTO `user` ( `guid`, `username`, `password`, `email`, `openid`, `nickname`,
174                 `pubkey`, `prvkey`, `spubkey`, `sprvkey`, `register_date`, `verified`, `blocked`, `timezone` )
175                 VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, 'UTC' )",
176                 dbesc(generate_user_guid()),
177                 dbesc($username),
178                 dbesc($new_password_encoded),
179                 dbesc($email),
180                 dbesc($openid_url),
181                 dbesc($nickname),
182                 dbesc($pubkey),
183                 dbesc($prvkey),
184                 dbesc($spubkey),
185                 dbesc($sprvkey),
186                 dbesc(datetime_convert()),
187                 intval($verified),
188                 intval($blocked)
189         );
190
191         if($r) {
192                 $r = q("SELECT * FROM `user` 
193                         WHERE `username` = '%s' AND `password` = '%s' LIMIT 1",
194                         dbesc($username),
195                         dbesc($new_password_encoded)
196                 );
197                 if($r !== false && count($r)) {
198                         $u = $r[0];
199                         $newuid = intval($r[0]['uid']);
200                 }
201         }
202         else {
203                 $result['message'] .=  t('An error occurred during registration. Please try again.') . EOL ;
204                 return $result;
205         }               
206
207         /**
208          * if somebody clicked submit twice very quickly, they could end up with two accounts 
209          * due to race condition. Remove this one.
210          */
211
212         $r = q("SELECT `uid` FROM `user`
213                 WHERE `nickname` = '%s' ",
214                 dbesc($nickname)
215         );
216         if((count($r) > 1) && $newuid) {
217                 $result['message'] .= t('Nickname is already registered. Please choose another.') . EOL;
218                 q("DELETE FROM `user` WHERE `uid` = %d LIMIT 1",
219                         intval($newuid)
220                 );
221                 return $result;
222         }
223
224         if(x($newuid) !== false) {
225                 $r = q("INSERT INTO `profile` ( `uid`, `profile-name`, `is-default`, `name`, `photo`, `thumb`, `publish`, `net-publish` )
226                         VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, %d ) ",
227                         intval($newuid),
228                         t('default'),
229                         1,
230                         dbesc($username),
231                         dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
232                         dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg"),
233                         intval($publish),
234                         intval($netpublish)
235
236                 );
237                 if($r === false) {
238                         $result['message'] .=  t('An error occurred creating your default profile. Please try again.') . EOL;
239                         // Start fresh next time.
240                         $r = q("DELETE FROM `user` WHERE `uid` = %d",
241                                 intval($newuid));
242                         return $result;
243                 }
244                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `self`, `name`, `nick`, `photo`, `thumb`, `micro`, `blocked`, `pending`, `url`, `nurl`,
245                         `request`, `notify`, `poll`, `confirm`, `poco`, `name-date`, `uri-date`, `avatar-date`, `closeness` )
246                         VALUES ( %d, '%s', 1, '%s', '%s', '%s', '%s', '%s', 0, 0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', 0 ) ",
247                         intval($newuid),
248                         datetime_convert(),
249                         dbesc($username),
250                         dbesc($nickname),
251                         dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
252                         dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg"),
253                         dbesc($a->get_baseurl() . "/photo/micro/{$newuid}.jpg"),
254                         dbesc($a->get_baseurl() . "/profile/$nickname"),
255                         dbesc(normalise_link($a->get_baseurl() . "/profile/$nickname")),
256                         dbesc($a->get_baseurl() . "/dfrn_request/$nickname"),
257                         dbesc($a->get_baseurl() . "/dfrn_notify/$nickname"),
258                         dbesc($a->get_baseurl() . "/dfrn_poll/$nickname"),
259                         dbesc($a->get_baseurl() . "/dfrn_confirm/$nickname"),
260                         dbesc($a->get_baseurl() . "/poco/$nickname"),
261                         dbesc(datetime_convert()),
262                         dbesc(datetime_convert()),
263                         dbesc(datetime_convert())
264                 );
265
266                 // Create a group with no members. This allows somebody to use it 
267                 // right away as a default group for new contacts. 
268
269                 require_once('include/group.php');
270                 group_add($newuid, t('Friends'));
271
272         }
273
274         // if we have no OpenID photo try to look up an avatar
275         if(! strlen($photo))
276                 $photo = avatar_img($email);
277
278         // unless there is no avatar-plugin loaded
279         if(strlen($photo)) {
280                 require_once('include/Photo.php');
281                 $photo_failure = false;
282
283                 $filename = basename($photo);
284                 $img_str = fetch_url($photo,true);
285                 $img = new Photo($img_str);
286                 if($img->is_valid()) {
287
288                         $img->scaleImageSquare(175);
289
290                         $hash = photo_new_resource();
291
292                         $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 4 );
293
294                         if($r === false)
295                                 $photo_failure = true;
296
297                         $img->scaleImage(80);
298
299                         $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 5 );
300
301                         if($r === false)
302                                 $photo_failure = true;
303
304                         $img->scaleImage(48);
305
306                         $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 6 );
307
308                         if($r === false)
309                                 $photo_failure = true;
310
311                         if(! $photo_failure) {
312                                 q("UPDATE `photo` SET `profile` = 1 WHERE `resource-id` = '%s' ",
313                                         dbesc($hash)
314                                 );
315                         }
316                 }
317         }
318
319         call_hooks('register_account', $newuid);
320
321         $result['success'] = true;
322         $result['user'] = $u;
323         return $result;
324
325 }