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