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