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