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