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