]> git.mxchange.org Git - friendica.git/blob - mod/register.php
highlight any messages on page newer than 12 hours
[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         $sres=openssl_pkey_new(array(
127                 'encrypt_key' => false ));
128
129         // Get private key
130
131         $sprvkey = '';
132
133         openssl_pkey_export($sres, $sprvkey);
134
135         // Get public key
136
137         $spkey = openssl_pkey_get_details($sres);
138         $spubkey = $spkey["key"];
139
140         $r = q("INSERT INTO `user` ( `username`, `password`, `email`, `openid`, `nickname`,
141                 `pubkey`, `prvkey`, `spubkey`, `sprvkey`, `verified`, `blocked` )
142                 VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d )",
143                 dbesc($username),
144                 dbesc($new_password_encoded),
145                 dbesc($email),
146                 dbesc($openid_url),
147                 dbesc($nickname),
148                 dbesc($pubkey),
149                 dbesc($prvkey),
150                 dbesc($spubkey),
151                 dbesc($sprvkey),
152                 intval($verified),
153                 intval($blocked)
154                 );
155
156         if($r) {
157                 $r = q("SELECT `uid` FROM `user` 
158                         WHERE `username` = '%s' AND `password` = '%s' LIMIT 1",
159                         dbesc($username),
160                         dbesc($new_password_encoded)
161                         );
162                 if($r !== false && count($r))
163                         $newuid = intval($r[0]['uid']);
164         }
165         else {
166                 notice( t('An error occurred during registration. Please try again.') . EOL );
167                 return;
168         }               
169
170         if(x($newuid) !== false) {
171                 $r = q("INSERT INTO `profile` ( `uid`, `profile-name`, `is-default`, `name`, `photo`, `thumb` )
172                         VALUES ( %d, '%s', %d, '%s', '%s', '%s' ) ",
173                         intval($newuid),
174                         'default',
175                         1,
176                         dbesc($username),
177                         dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
178                         dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg")
179
180                 );
181                 if($r === false) {
182                         notice( t('An error occurred creating your default profile. Please try again.') . EOL );
183                         // Start fresh next time.
184                         $r = q("DELETE FROM `user` WHERE `uid` = %d",
185                                 intval($newuid));
186                         return;
187                 }
188                 $r = q("INSERT INTO `contact` ( `uid`, `created`, `self`, `name`, `nick`, `photo`, `thumb`, `micro`, `blocked`, `pending`, `url`,
189                         `request`, `notify`, `poll`, `confirm`, `name-date`, `uri-date`, `avatar-date` )
190                         VALUES ( %d, '%s', 1, '%s', '%s', '%s', '%s', '%s', 0, 0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
191                         intval($newuid),
192                         datetime_convert(),
193                         dbesc($username),
194                         dbesc($nickname),
195                         dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
196                         dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg"),
197                         dbesc($a->get_baseurl() . "/photo/micro/{$newuid}.jpg"),
198                         dbesc($a->get_baseurl() . "/profile/$nickname"),
199                         dbesc($a->get_baseurl() . "/dfrn_request/$nickname"),
200                         dbesc($a->get_baseurl() . "/dfrn_notify/$nickname"),
201                         dbesc($a->get_baseurl() . "/dfrn_poll/$nickname"),
202                         dbesc($a->get_baseurl() . "/dfrn_confirm/$nickname"),
203                         dbesc(datetime_convert()),
204                         dbesc(datetime_convert()),
205                         dbesc(datetime_convert())
206                 );
207
208
209         }
210
211         $use_gravatar = ((get_config('system','no_gravatar')) ? false : true);
212
213         // if we have an openid photo use it. 
214         // otherwise unless it is disabled, use gravatar
215
216         if($use_gravatar || strlen($photo)) {
217
218                 require_once('include/Photo.php');
219
220                 if(($use_gravatar) && (! strlen($photo))) 
221                         $photo = gravatar_img($email);
222                 $photo_failure = false;
223
224                 $filename = basename($photo);
225                 $img_str = fetch_url($photo,true);
226                 $img = new Photo($img_str);
227                 if($img->is_valid()) {
228
229                         $img->scaleImageSquare(175);
230                                         
231                         $hash = photo_new_resource();
232
233                         $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 4 );
234
235                         if($r === false)
236                                 $photo_failure = true;
237
238                         $img->scaleImage(80);
239
240                         $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 5 );
241
242                         if($r === false)
243                                 $photo_failure = true;
244
245                         $img->scaleImage(48);
246
247                         $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 6 );
248
249                         if($r === false)
250                                 $photo_failure = true;
251
252                         if(! $photo_failure) {
253                                 q("UPDATE `photo` SET `profile` = 1 WHERE `resource-id` = '%s' ",
254                                         dbesc($hash)
255                                 );
256                         }
257                 }
258         }
259
260         if( $a->config['register_policy'] == REGISTER_OPEN ) {
261                 $email_tpl = load_view_file("view/register_open_eml.tpl");
262                 $email_tpl = replace_macros($email_tpl, array(
263                                 '$sitename' => $a->config['sitename'],
264                                 '$siteurl' =>  $a->get_baseurl(),
265                                 '$username' => $username,
266                                 '$email' => $email,
267                                 '$password' => $new_password,
268                                 '$uid' => $newuid ));
269
270                 $res = mail($email, t('Registration details for ') . $a->config['sitename'],
271                         $email_tpl, 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME']);
272
273
274                 if($res) {
275                         notice( t('Registration successful. Please check your email for further instructions.') . EOL ) ;
276                         goaway($a->get_baseurl());
277                 }
278                 else {
279                         notice( t('Failed to send email message. Here is the message that failed.') . $email_tpl . EOL );
280                 }
281         }
282         elseif($a->config['register_policy'] == REGISTER_APPROVE) {
283                 if(! strlen($a->config['admin_email'])) {
284                         notice( t('Your registration can not be processed.') . EOL);
285                         goaway($a->get_baseurl());
286                 }
287
288                 $hash = random_string();
289                 $r = q("INSERT INTO `register` ( `hash`, `created`, `uid`, `password` ) VALUES ( '%s', '%s', %d, '%s' ) ",
290                         dbesc($hash),
291                         dbesc(datetime_convert()),
292                         intval($newuid),
293                         dbesc($new_password)
294                 );
295
296                 $email_tpl = load_view_file("view/register_verify_eml.tpl");
297                 $email_tpl = replace_macros($email_tpl, array(
298                                 '$sitename' => $a->config['sitename'],
299                                 '$siteurl' =>  $a->get_baseurl(),
300                                 '$username' => $username,
301                                 '$email' => $email,
302                                 '$password' => $new_password,
303                                 '$uid' => $newuid,
304                                 '$hash' => $hash
305                  ));
306
307                 $res = mail($a->config['admin_email'], t('Registration request at ') . $a->config['sitename'],
308                         $email_tpl,'From: ' .  t('Administrator') . '@' . $_SERVER['SERVER_NAME']);
309
310                 if($res) {
311                         notice( t('Your registration is pending approval by the site owner.') . EOL ) ;
312                         goaway($a->get_baseurl());
313                 }
314
315         }
316         return;
317 }}
318
319
320
321
322
323
324 if(! function_exists('register_content')) {
325 function register_content(&$a) {
326
327         // logged in users can register others (people/pages/groups)
328         // even with closed registrations, unless specifically prohibited by site policy.
329         // 'block_extended_register' blocks all registrations, period.
330
331         $block = get_config('system','block_extended_register');
332
333         if((($a->config['register_policy'] == REGISTER_CLOSED) && (! getuid())) || ($block)) {
334                 notice("Permission denied." . EOL);
335                 return;
336         }
337
338         $username     = ((x($_POST,'username'))     ? $_POST['username']     : ((x($_GET,'username'))     ? $_GET['username']              : ''));
339         $email        = ((x($_POST,'email'))        ? $_POST['email']        : ((x($_GET,'email'))        ? $_GET['email']                 : ''));
340         $openid_url   = ((x($_POST,'openid_url'))   ? $_POST['openid_url']   : ((x($_GET,'openid_url'))   ? $_GET['openid_url']            : ''));
341         $nickname     = ((x($_POST,'nickname'))     ? $_POST['nickname']     : ((x($_GET,'nickname'))     ? $_GET['nickname']              : ''));
342         $photo        = ((x($_POST,'photo'))        ? $_POST['photo']        : ((x($_GET,'photo'))        ? hex2bin($_GET['photo'])        : ''));
343
344         $noid = get_config('system','no_openid');
345
346         if($noid) {
347                 $oidhtml = '';
348                 $fillwith = '';
349                 $fillext = '';
350                 $oidlabel = '';
351         }
352         else {
353                 $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" >';
354                 $fillwith = t("You may \x28optionally\x29 fill in this form via OpenID by supplying your OpenID and clicking 'Register'.");
355                 $fillext =  t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.');
356                 $oidlabel = t("Your OpenID \x28optional\x29: ");
357         }
358
359         $o = load_view_file("view/register.tpl");
360         $o = replace_macros($o, array(
361                 '$oidhtml' => $oidhtml,
362                 '$regtitle'  => t('Registration'),
363                 '$registertext' =>((x($a->config,'register_text'))
364                         ? '<div class="error-message">' . $a->config['register_text'] . '</div>'
365                         : "" ),
366                 '$fillwith'  => $fillwith,
367                 '$fillext'   => $fillext,
368                 '$oidlabel'  => $oidlabel,
369                 '$openid'    => $openid_url,
370                 '$namelabel' => t('Your Full Name ' . "\x28" . 'e.g. Joe Smith' . "\x29" . ': '),
371                 '$addrlabel' => t('Your Email Address: '),
372                 '$nickdesc'  => t('Choose a profile nickname. This must begin with a text character. Your global profile locator will then be \'<strong>nickname@$sitename</strong>\'.'),
373                 '$nicklabel' => t('Choose a nickname: '),
374                 '$photo'     => $photo,
375                 '$regbutt'   => t('Register'),
376                 '$username'  => $username,
377                 '$email'     => $email,
378                 '$nickname'  => $nickname,
379                 '$sitename'  => $a->get_hostname()
380         ));
381         return $o;
382
383 }}
384