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