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