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