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