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