]> git.mxchange.org Git - friendica.git/blob - mod/register.php
7aaf747925216a14777f81a3fc3ec7c20ab28aef
[friendica.git] / mod / register.php
1 <?php
2
3 if(! function_exists('register_post')) {
4 function register_post(&$a) {
5
6         global $lang;
7
8         $verified = 0;
9         $blocked  = 1;
10
11         switch($a->config['register_policy']) {
12
13         
14         case REGISTER_OPEN:
15                 $blocked = 0;
16                 $verified = 1;
17                 break;
18
19         case REGISTER_APPROVE:
20                 $blocked = 1;
21                 $verified = 0;
22                 break;
23
24         default:
25         case REGISTER_CLOSED:
26                 if((! x($_SESSION,'authenticated') && (! x($_SESSION,'administrator')))) {
27                         notice( t('Permission denied.') . EOL );
28                         return;
29                 }
30                 $blocked = 1;
31                 $verified = 0;
32                 break;
33         }
34
35
36         $username   = ((x($_POST,'username'))   ? notags(trim($_POST['username']))   : '');
37         $nickname   = ((x($_POST,'nickname'))   ? notags(trim($_POST['nickname']))   : '');
38         $email      = ((x($_POST,'email'))      ? notags(trim($_POST['email']))      : '');
39         $openid_url = ((x($_POST,'openid_url')) ? notags(trim($_POST['openid_url'])) : '');
40         $photo      = ((x($_POST,'photo'))      ? notags(trim($_POST['photo']))      : '');
41         $publish    = ((x($_POST,'profile_publish_reg') && intval($_POST['profile_publish_reg'])) ? 1 : 0);
42
43         $netpublish = ((strlen(get_config('system','directory_submit_url'))) ? $publish : 0);
44                 
45         $tmp_str = $openid_url;
46         if((! x($username)) || (! x($email)) || (! x($nickname))) {
47                 if($openid_url) {
48                         if(! validate_url($tmp_str)) {
49                                 notice( t('Invalid OpenID url') . EOL);
50                                 return;
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                 $err .= t('Please use a shorter name.') . EOL;
79         if(mb_strlen($username) < 3)
80                 $err .= 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                         $err .= t("That doesn't appear to be your full \x28First Last\x29 name.") . EOL;
96         }
97
98
99         if(! allowed_email($email))
100                         $err .= t('Your email domain is not among those allowed on this site.') . EOL;
101
102         if((! valid_email($email)) || (! validate_email($email)))
103                 $err .= 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.
107
108         if((x($a->config,'admin_email')) && (strcasecmp($email,$a->config['admin_email']) == 0) && strlen($openid_url))
109                 $err .= t('Cannot use that email.') . EOL;
110
111         $nickname = $_POST['nickname'] = strtolower($nickname);
112
113         if(! preg_match("/^[a-z][a-z0-9\-\_]*$/",$nickname))
114                 $err .= t('Your "nickname" can only contain "a-z", "0-9", "-", and "_", and must also begin with a letter.') . EOL;
115         $r = q("SELECT `uid` FROM `user`
116                 WHERE `nickname` = '%s' LIMIT 1",
117                 dbesc($nickname)
118         );
119         if(count($r))
120                 $err .= t('Nickname is already registered. Please choose another.') . EOL;
121
122         if(strlen($err)) {
123                 notice( $err );
124                 return;
125         }
126
127
128         $new_password = autoname(6) . mt_rand(100,9999);
129         $new_password_encoded = hash('whirlpool',$new_password);
130
131         $res=openssl_pkey_new(array(
132                 'digest_alg' => 'sha1',
133                 'private_key_bits' => 4096,
134                 'encrypt_key' => false ));
135
136         // Get private key
137
138         if(empty($res)) {
139                 notice( t('SERIOUS ERROR: Generation of security keys failed.') . EOL);
140                 return;
141         }
142
143         $prvkey = '';
144
145         openssl_pkey_export($res, $prvkey);
146
147         // Get public key
148
149         $pkey = openssl_pkey_get_details($res);
150         $pubkey = $pkey["key"];
151
152         /**
153          *
154          * Create another keypair for signing/verifying
155          * salmon protocol messages. We have to use a slightly
156          * less robust key because this won't be using openssl
157          * but the phpseclib. Since it is PHP interpreted code
158          * it is not nearly as efficient, and the larger keys
159          * will take several minutes each to process.
160          *
161          */
162         
163         $sres=openssl_pkey_new(array(
164                 'digest_alg' => 'sha1',
165                 'private_key_bits' => 512,
166                 'encrypt_key' => false ));
167
168         // Get private key
169
170         $sprvkey = '';
171
172         openssl_pkey_export($sres, $sprvkey);
173
174         // Get public key
175
176         $spkey = openssl_pkey_get_details($sres);
177         $spubkey = $spkey["key"];
178
179         $r = q("INSERT INTO `user` ( `username`, `password`, `email`, `openid`, `nickname`,
180                 `pubkey`, `prvkey`, `spubkey`, `sprvkey`, `register_date`, `verified`, `blocked` )
181                 VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d )",
182                 dbesc($username),
183                 dbesc($new_password_encoded),
184                 dbesc($email),
185                 dbesc($openid_url),
186                 dbesc($nickname),
187                 dbesc($pubkey),
188                 dbesc($prvkey),
189                 dbesc($spubkey),
190                 dbesc($sprvkey),
191                 dbesc(datetime_convert()),
192                 intval($verified),
193                 intval($blocked)
194                 );
195
196         if($r) {
197                 $r = q("SELECT `uid` FROM `user` 
198                         WHERE `username` = '%s' AND `password` = '%s' LIMIT 1",
199                         dbesc($username),
200                         dbesc($new_password_encoded)
201                         );
202                 if($r !== false && count($r))
203                         $newuid = intval($r[0]['uid']);
204         }
205         else {
206                 notice( t('An error occurred during registration. Please try again.') . EOL );
207                 return;
208         }               
209
210         /**
211          * if somebody clicked submit twice very quickly, they could end up with two accounts 
212          * due to race condition. Remove this one.
213          */
214
215         $r = q("SELECT `uid` FROM `user`
216                 WHERE `nickname` = '%s' ",
217                 dbesc($nickname)
218         );
219         if((count($r) > 1) && $newuid) {
220                 $err .= t('Nickname is already registered. Please choose another.') . EOL;
221                 q("DELETE FROM `user` WHERE `uid` = %d LIMIT 1",
222                         intval($newuid)
223                 );
224                 notice ($err);
225                 return;
226         }
227
228         if(x($newuid) !== false) {
229                 $r = q("INSERT INTO `profile` ( `uid`, `profile-name`, `is-default`, `name`, `photo`, `thumb`, `publish`, `net-publish` )
230                         VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, %d ) ",
231                         intval($newuid),
232                         'default',
233                         1,
234                         dbesc($username),
235                         dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
236                         dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg"),
237                         intval($publish),
238                         intval($netpublish)
239
240                 );
241                 if($r === false) {
242                         notice( t('An error occurred creating your default profile. Please try again.') . EOL );
243                         // Start fresh next time.
244                         $r = q("DELETE FROM `user` WHERE `uid` = %d",
245                                 intval($newuid));
246                         return;
247                 }
248                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `self`, `name`, `nick`, `photo`, `thumb`, `micro`, `blocked`, `pending`, `url`,
249                         `request`, `notify`, `poll`, `confirm`, `name-date`, `uri-date`, `avatar-date` )
250                         VALUES ( %d, '%s', 1, '%s', '%s', '%s', '%s', '%s', 0, 0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
251                         intval($newuid),
252                         datetime_convert(),
253                         dbesc($username),
254                         dbesc($nickname),
255                         dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
256                         dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg"),
257                         dbesc($a->get_baseurl() . "/photo/micro/{$newuid}.jpg"),
258                         dbesc($a->get_baseurl() . "/profile/$nickname"),
259                         dbesc($a->get_baseurl() . "/dfrn_request/$nickname"),
260                         dbesc($a->get_baseurl() . "/dfrn_notify/$nickname"),
261                         dbesc($a->get_baseurl() . "/dfrn_poll/$nickname"),
262                         dbesc($a->get_baseurl() . "/dfrn_confirm/$nickname"),
263                         dbesc(datetime_convert()),
264                         dbesc(datetime_convert()),
265                         dbesc(datetime_convert())
266                 );
267
268
269         }
270
271         $use_gravatar = ((get_config('system','no_gravatar')) ? false : true);
272
273         // if we have an openid photo use it. 
274         // otherwise unless it is disabled, use gravatar
275
276         if($use_gravatar || strlen($photo)) {
277
278                 require_once('include/Photo.php');
279
280                 if(($use_gravatar) && (! strlen($photo))) 
281                         $photo = gravatar_img($email);
282                 $photo_failure = false;
283
284                 $filename = basename($photo);
285                 $img_str = fetch_url($photo,true);
286                 $img = new Photo($img_str);
287                 if($img->is_valid()) {
288
289                         $img->scaleImageSquare(175);
290                                         
291                         $hash = photo_new_resource();
292
293                         $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 4 );
294
295                         if($r === false)
296                                 $photo_failure = true;
297
298                         $img->scaleImage(80);
299
300                         $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 5 );
301
302                         if($r === false)
303                                 $photo_failure = true;
304
305                         $img->scaleImage(48);
306
307                         $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 6 );
308
309                         if($r === false)
310                                 $photo_failure = true;
311
312                         if(! $photo_failure) {
313                                 q("UPDATE `photo` SET `profile` = 1 WHERE `resource-id` = '%s' ",
314                                         dbesc($hash)
315                                 );
316                         }
317                 }
318         }
319
320         if($netpublish && $a->config['register_policy'] != REGISTER_APPROVE) {
321                 $url = $a->get_baseurl() . "/profile/$nickname";
322                 proc_run('php',"include/directory.php","$url");
323         }
324
325
326         if( $a->config['register_policy'] == REGISTER_OPEN ) {
327                 $email_tpl = get_intltext_template("register_open_eml.tpl");
328                 $email_tpl = replace_macros($email_tpl, array(
329                                 '$sitename' => $a->config['sitename'],
330                                 '$siteurl' =>  $a->get_baseurl(),
331                                 '$username' => $username,
332                                 '$email' => $email,
333                                 '$password' => $new_password,
334                                 '$uid' => $newuid ));
335
336                 $res = mail($email, sprintf(t('Registration details for %s'), $a->config['sitename']),
337                         $email_tpl, 
338                                 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
339                                 . 'Content-type: text/plain; charset=UTF-8' . "\n"
340                                 . 'Content-transfer-encoding: 8bit' );
341
342
343                 if($res) {
344                         info( t('Registration successful. Please check your email for further instructions.') . EOL ) ;
345                         goaway($a->get_baseurl());
346                 }
347                 else {
348                         notice( t('Failed to send email message. Here is the message that failed.') . $email_tpl . EOL );
349                 }
350         }
351         elseif($a->config['register_policy'] == REGISTER_APPROVE) {
352                 if(! strlen($a->config['admin_email'])) {
353                         notice( t('Your registration can not be processed.') . EOL);
354                         goaway($a->get_baseurl());
355                 }
356
357                 $hash = random_string();
358                 $r = q("INSERT INTO `register` ( `hash`, `created`, `uid`, `password`, `language` ) VALUES ( '%s', '%s', %d, '%s', '%s' ) ",
359                         dbesc($hash),
360                         dbesc(datetime_convert()),
361                         intval($newuid),
362                         dbesc($new_password),
363                         dbesc($lang)
364                 );
365
366                 $r = q("SELECT `language` FROM `user` WHERE `email` = '%s' LIMIT 1",
367                         dbesc($a->config['admin_email'])
368                 );
369                 if(count($r))
370                         push_lang($r[0]['language']);
371                 else
372                         push_lang('en');
373
374
375                 $email_tpl = get_intltext_template("register_verify_eml.tpl");
376                 $email_tpl = replace_macros($email_tpl, array(
377                                 '$sitename' => $a->config['sitename'],
378                                 '$siteurl' =>  $a->get_baseurl(),
379                                 '$username' => $username,
380                                 '$email' => $email,
381                                 '$password' => $new_password,
382                                 '$uid' => $newuid,
383                                 '$hash' => $hash
384                  ));
385
386                 $res = mail($a->config['admin_email'], sprintf(t('Registration request at %s'), $a->config['sitename']),
387                         $email_tpl,
388                                 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
389                                 . 'Content-type: text/plain; charset=UTF-8' . "\n"
390                                 . 'Content-transfer-encoding: 8bit' );
391
392                 pop_lang();
393
394                 if($res) {
395                         info( t('Your registration is pending approval by the site owner.') . EOL ) ;
396                         goaway($a->get_baseurl());
397                 }
398
399         }
400
401         return;
402 }}
403
404
405
406
407
408
409 if(! function_exists('register_content')) {
410 function register_content(&$a) {
411
412         // logged in users can register others (people/pages/groups)
413         // even with closed registrations, unless specifically prohibited by site policy.
414         // 'block_extended_register' blocks all registrations, period.
415
416         $block = get_config('system','block_extended_register');
417
418         if((($a->config['register_policy'] == REGISTER_CLOSED) && (! local_user())) || ($block)) {
419                 notice("Permission denied." . EOL);
420                 return;
421         }
422
423         if(x($_SESSION,'theme'))
424                 unset($_SESSION['theme']);
425
426
427         $username     = ((x($_POST,'username'))     ? $_POST['username']     : ((x($_GET,'username'))     ? $_GET['username']              : ''));
428         $email        = ((x($_POST,'email'))        ? $_POST['email']        : ((x($_GET,'email'))        ? $_GET['email']                 : ''));
429         $openid_url   = ((x($_POST,'openid_url'))   ? $_POST['openid_url']   : ((x($_GET,'openid_url'))   ? $_GET['openid_url']            : ''));
430         $nickname     = ((x($_POST,'nickname'))     ? $_POST['nickname']     : ((x($_GET,'nickname'))     ? $_GET['nickname']              : ''));
431         $photo        = ((x($_POST,'photo'))        ? $_POST['photo']        : ((x($_GET,'photo'))        ? hex2bin($_GET['photo'])        : ''));
432
433         $noid = get_config('system','no_openid');
434
435         if($noid) {
436                 $oidhtml = '';
437                 $fillwith = '';
438                 $fillext = '';
439                 $oidlabel = '';
440         }
441         else {
442                 $oidhtml = '<label for="register-openid" id="label-register-openid" >$oidlabel</label><input type="text" maxlength="60" size="32" name="openid_url" class="openid" id="register-openid" value="$openid" >';
443                 $fillwith = t("You may \x28optionally\x29 fill in this form via OpenID by supplying your OpenID and clicking 'Register'.");
444                 $fillext =  t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.');
445                 $oidlabel = t("Your OpenID \x28optional\x29: ");
446         }
447
448         // I set this and got even more fake names than before...
449
450         $realpeople = ''; // t('Members of this network prefer to communicate with real people who use their real names.');
451
452         if(get_config('system','publish_all')) {
453                 $profile_publish_reg = '<input type="hidden" name="profile_publish_reg" value="1" />';
454         }
455         else {
456                 $publish_tpl = get_markup_template("profile_publish.tpl");
457                 $profile_publish = replace_macros($publish_tpl,array(
458                         '$instance'     => 'reg',
459                         '$pubdesc'      => t('Include your profile in member directory?'),
460                         '$yes_selected' => ' checked="checked" ',
461                         '$no_selected'  => '',
462                         '$str_yes'      => t('Yes'),
463                         '$str_no'       => t('No')
464                 ));
465         }
466
467
468         $license = t('Shared content is covered by the <a href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0</a> license.');
469
470
471         $o = get_markup_template("register.tpl");
472         $o = replace_macros($o, array(
473                 '$oidhtml' => $oidhtml,
474                 '$realpeople' => $realpeople,
475                 '$regtitle'  => t('Registration'),
476                 '$registertext' =>((x($a->config,'register_text'))
477                         ? '<div class="error-message">' . $a->config['register_text'] . '</div>'
478                         : "" ),
479                 '$fillwith'  => $fillwith,
480                 '$fillext'   => $fillext,
481                 '$oidlabel'  => $oidlabel,
482                 '$openid'    => $openid_url,
483                 '$namelabel' => t('Your Full Name ' . "\x28" . 'e.g. Joe Smith' . "\x29" . ': '),
484                 '$addrlabel' => t('Your Email Address: '),
485                 '$nickdesc'  => t('Choose a profile nickname. This must begin with a text character. Your profile address on this site will then be \'<strong>nickname@$sitename</strong>\'.'),
486                 '$nicklabel' => t('Choose a nickname: '),
487                 '$photo'     => $photo,
488                 '$publish'   => $profile_publish,
489                 '$regbutt'   => t('Register'),
490                 '$username'  => $username,
491                 '$email'     => $email,
492                 '$nickname'  => $nickname,
493                 '$license'   => $license,
494                 '$sitename'  => $a->get_hostname()
495         ));
496         return $o;
497
498 }}
499