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