]> git.mxchange.org Git - friendica.git/blob - mod/register.php
openid registration
[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         require_once('include/Photo.php');
209
210         $photo = gravatar_img($email);
211         $photo_failure = false;
212
213         $filename = basename($photo);
214         $img_str = fetch_url($photo,true);
215         $img = new Photo($img_str);
216         if($img->is_valid()) {
217
218                 $img->scaleImageSquare(175);
219                                         
220                 $hash = photo_new_resource();
221
222                 $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 4 );
223
224                 if($r === false)
225                         $photo_failure = true;
226
227                 $img->scaleImage(80);
228
229                 $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 5 );
230
231                 if($r === false)
232                         $photo_failure = true;
233
234                 $img->scaleImage(48);
235
236                 $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 6 );
237
238                 if($r === false)
239                         $photo_failure = true;
240
241                 if(! $photo_failure) {
242                         q("UPDATE `photo` SET `profile` = 1 WHERE `resource-id` = '%s' ",
243                                 dbesc($hash)
244                         );
245                 }
246         }
247
248         if( $a->config['register_policy'] == REGISTER_OPEN ) {
249                 $email_tpl = load_view_file("view/register_open_eml.tpl");
250                 $email_tpl = replace_macros($email_tpl, array(
251                                 '$sitename' => $a->config['sitename'],
252                                 '$siteurl' =>  $a->get_baseurl(),
253                                 '$username' => $username,
254                                 '$email' => $email,
255                                 '$password' => $new_password,
256                                 '$uid' => $newuid ));
257
258                 $res = mail($email, t('Registration details for ') . $a->config['sitename'],
259                         $email_tpl, 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME']);
260
261
262                 if($res) {
263                         notice( t('Registration successful. Please check your email for further instructions.') . EOL ) ;
264                         goaway($a->get_baseurl());
265                 }
266                 else {
267                         notice( t('Failed to send email message. Here is the message that failed.') . $email_tpl . EOL );
268                 }
269         }
270         elseif($a->config['register_policy'] == REGISTER_APPROVE) {
271                 if(! strlen($a->config['admin_email'])) {
272                         notice( t('Your registration can not be processed.') . EOL);
273                         goaway($a->get_baseurl());
274                 }
275
276                 $hash = random_string();
277                 $r = q("INSERT INTO `register` ( `hash`, `created`, `uid`, `password` ) VALUES ( '%s', '%s', %d, '%s' ) ",
278                         dbesc($hash),
279                         dbesc(datetime_convert()),
280                         intval($newuid),
281                         dbesc($new_password)
282                 );
283
284                 $email_tpl = load_view_file("view/register_verify_eml.tpl");
285                 $email_tpl = replace_macros($email_tpl, array(
286                                 '$sitename' => $a->config['sitename'],
287                                 '$siteurl' =>  $a->get_baseurl(),
288                                 '$username' => $username,
289                                 '$email' => $email,
290                                 '$password' => $new_password,
291                                 '$uid' => $newuid,
292                                 '$hash' => $hash
293                  ));
294
295                 $res = mail($a->config['admin_email'], t('Registration request at ') . $a->config['sitename'],
296                         $email_tpl,'From: ' .  t('Administrator') . '@' . $_SERVER['SERVER_NAME']);
297
298                 if($res) {
299                         notice( t('Your registration is pending approval by the site owner.') . EOL ) ;
300                         goaway($a->get_baseurl());
301                 }
302
303         }
304         
305         return;
306 }}
307
308
309
310
311
312
313 if(! function_exists('register_content')) {
314 function register_content(&$a) {
315
316         // logged in users can register others (people/pages/groups)
317         // even with closed registrations, unless specifically prohibited by site policy.
318         // 'block_extended_register' blocks all registrations, period.
319
320         $block = get_config('system','block_extended_register');
321
322         if((($a->config['register_policy'] == REGISTER_CLOSED) && (! getuid())) || ($block)) {
323                 notice("Permission denied." . EOL);
324                 return;
325         }
326
327         $username     = ((x($_POST,'username'))     ? $_POST['username']     : ((x($_GET,'username'))     ? $_GET['username'] : ''));
328         $email        = ((x($_POST,'email'))        ? $_POST['email']        : ((x($_GET,'email'))        ? $_GET['email']    : ''));
329         $openid_url   = ((x($_POST,'openid_url'))   ? $_POST['openid_url']   : ((x($_GET,'openid_url'))   ? $_GET['openid_url']   : ''));
330         $nickname     = ((x($_POST,'nickname'))     ? $_POST['nickname']     : ((x($_GET,'nickname'))     ? $_GET['nickname'] : ''));
331
332         $o = load_view_file("view/register.tpl");
333         $o = replace_macros($o, array(
334                 '$regtitle'  => t('Registration'),
335                 '$registertext' =>((x($a->config,'register_text'))
336                         ? '<div class="error-message">' . $a->config['register_text'] . '</div>'
337                         : "" ),
338                 '$fillwith'  => t('You may ' . "\x28" . 'optionally' . "\x29" . ' fill in this form via OpenID by supplying your OpenID and clicking ') . "'" . t('Register') . "'",
339                 '$fillext'   => t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.'),
340                 '$oidlabel'  => t('Your OpenID ' . "\x28" . 'optional' . "\x29" . ': '),
341                 '$openid'    => $openid_url,
342                 '$namelabel' => t('Your Full Name ' . "\x28" . 'e.g. Joe Smith' . "\x29" . ': '),
343                 '$addrlabel' => t('Your Email Address: '),
344                 '$nickdesc'  => t('Choose a profile nickname. This must begin with a text character. Your global profile locator will then be \'<strong>nickname@$sitename</strong>\'.'),
345                 '$nicklabel' => t('Choose a nickname: '),
346                 '$regbutt'   => t('Register'),
347                 '$username'  => $username,
348                 '$email'     => $email,
349                 '$nickname'  => $nickname,
350                 '$sitename'  => $a->get_hostname()
351         ));
352         return $o;
353
354 }}
355