3 if(! function_exists('register_post')) {
4 function register_post(&$a) {
11 $arr = array('post' => $_POST);
12 call_hooks('register_post', $arr);
14 $max_dailies = intval(get_config('system','max_daily_registrations'));
16 $r = q("select count(*) as total from user where register_date > UTC_TIMESTAMP - INTERVAL 1 day");
17 if($r && $r[0]['total'] >= $max_dailies) {
22 switch($a->config['register_policy']) {
30 case REGISTER_APPROVE:
37 if((! x($_SESSION,'authenticated') && (! x($_SESSION,'administrator')))) {
38 notice( t('Permission denied.') . EOL );
46 $using_invites = get_config('system','invitation_only');
47 $num_invites = get_config('system','number_invites');
50 $invite_id = ((x($_POST,'invite_id')) ? notags(trim($_POST['invite_id'])) : '');
51 $username = ((x($_POST,'username')) ? notags(trim($_POST['username'])) : '');
52 $nickname = ((x($_POST,'nickname')) ? notags(trim($_POST['nickname'])) : '');
53 $email = ((x($_POST,'email')) ? notags(trim($_POST['email'])) : '');
54 $openid_url = ((x($_POST,'openid_url')) ? notags(trim($_POST['openid_url'])) : '');
55 $photo = ((x($_POST,'photo')) ? notags(trim($_POST['photo'])) : '');
56 $publish = ((x($_POST,'profile_publish_reg') && intval($_POST['profile_publish_reg'])) ? 1 : 0);
58 $netpublish = ((strlen(get_config('system','directory_submit_url'))) ? $publish : 0);
60 $tmp_str = $openid_url;
64 notice( t('An invitation is required.') . EOL);
67 $r = q("select * from register where `hash` = '%s' limit 1", dbesc($invite_id));
69 notice( t('Invitation could not be verified.') . EOL);
74 if((! x($username)) || (! x($email)) || (! x($nickname))) {
76 if(! validate_url($tmp_str)) {
77 notice( t('Invalid OpenID url') . EOL);
80 $_SESSION['register'] = 1;
81 $_SESSION['openid'] = $openid_url;
82 require_once('library/openid.php');
83 $openid = new LightOpenID;
84 $openid->identity = $openid_url;
85 $openid->returnUrl = $a->get_baseurl() . '/openid';
86 $openid->required = array('namePerson/friendly', 'contact/email', 'namePerson');
87 $openid->optional = array('namePerson/first','media/image/aspect11','media/image/default');
88 goaway($openid->authUrl());
92 notice( t('Please enter the required information.') . EOL );
96 if(! validate_url($tmp_str))
102 // collapse multiple spaces in name
103 $username = preg_replace('/ +/',' ',$username);
105 if(mb_strlen($username) > 48)
106 $err .= t('Please use a shorter name.') . EOL;
107 if(mb_strlen($username) < 3)
108 $err .= t('Name too short.') . EOL;
110 // I don't really like having this rule, but it cuts down
111 // on the number of auto-registrations by Russian spammers
113 // Using preg_match was completely unreliable, due to mixed UTF-8 regex support
114 // $no_utf = get_config('system','no_utf');
115 // $pat = (($no_utf) ? '/^[a-zA-Z]* [a-zA-Z]*$/' : '/^\p{L}* \p{L}*$/u' );
117 // So now we are just looking for a space in the full name.
119 $loose_reg = get_config('system','no_regfullname');
121 $username = mb_convert_case($username,MB_CASE_TITLE,'UTF-8');
122 if(! strpos($username,' '))
123 $err .= t("That doesn't appear to be your full \x28First Last\x29 name.") . EOL;
127 if(! allowed_email($email))
128 $err .= t('Your email domain is not among those allowed on this site.') . EOL;
130 if((! valid_email($email)) || (! validate_email($email)))
131 $err .= t('Not a valid email address.') . EOL;
133 // Disallow somebody creating an account using openid that uses the admin email address,
134 // since openid bypasses email verification. We'll allow it if there is not yet an admin account.
136 if((x($a->config,'admin_email')) && (strcasecmp($email,$a->config['admin_email']) == 0) && strlen($openid_url)) {
137 $r = q("SELECT * FROM `user` WHERE `email` = '%s' LIMIT 1",
141 $err .= t('Cannot use that email.') . EOL;
144 $nickname = $_POST['nickname'] = strtolower($nickname);
146 if(! preg_match("/^[a-z][a-z0-9\-\_]*$/",$nickname))
147 $err .= t('Your "nickname" can only contain "a-z", "0-9", "-", and "_", and must also begin with a letter.') . EOL;
148 $r = q("SELECT `uid` FROM `user`
149 WHERE `nickname` = '%s' LIMIT 1",
153 $err .= t('Nickname is already registered. Please choose another.') . EOL;
155 // Check deleted accounts that had this nickname. Doesn't matter to us,
156 // but could be a security issue for federated platforms.
158 $r = q("SELECT * FROM `userd`
159 WHERE `username` = '%s' LIMIT 1",
163 $err .= t('Nickname was once registered here and may not be re-used. Please choose another.') . EOL;
171 $new_password = autoname(6) . mt_rand(100,9999);
172 $new_password_encoded = hash('whirlpool',$new_password);
174 $res=openssl_pkey_new(array(
175 'digest_alg' => 'sha1',
176 'private_key_bits' => 4096,
177 'encrypt_key' => false ));
182 notice( t('SERIOUS ERROR: Generation of security keys failed.') . EOL);
188 openssl_pkey_export($res, $prvkey);
192 $pkey = openssl_pkey_get_details($res);
193 $pubkey = $pkey["key"];
197 * Create another keypair for signing/verifying
198 * salmon protocol messages. We have to use a slightly
199 * less robust key because this won't be using openssl
200 * but the phpseclib. Since it is PHP interpreted code
201 * it is not nearly as efficient, and the larger keys
202 * will take several minutes each to process.
206 $sres=openssl_pkey_new(array(
207 'digest_alg' => 'sha1',
208 'private_key_bits' => 512,
209 'encrypt_key' => false ));
215 openssl_pkey_export($sres, $sprvkey);
219 $spkey = openssl_pkey_get_details($sres);
220 $spubkey = $spkey["key"];
222 $r = q("INSERT INTO `user` ( `guid`, `username`, `password`, `email`, `openid`, `nickname`,
223 `pubkey`, `prvkey`, `spubkey`, `sprvkey`, `register_date`, `verified`, `blocked`, `timezone` )
224 VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, 'UTC' )",
225 dbesc(generate_user_guid()),
227 dbesc($new_password_encoded),
235 dbesc(datetime_convert()),
241 $r = q("SELECT `uid` FROM `user`
242 WHERE `username` = '%s' AND `password` = '%s' LIMIT 1",
244 dbesc($new_password_encoded)
246 if($r !== false && count($r))
247 $newuid = intval($r[0]['uid']);
250 notice( t('An error occurred during registration. Please try again.') . EOL );
255 * if somebody clicked submit twice very quickly, they could end up with two accounts
256 * due to race condition. Remove this one.
259 $r = q("SELECT `uid` FROM `user`
260 WHERE `nickname` = '%s' ",
263 if((count($r) > 1) && $newuid) {
264 $err .= t('Nickname is already registered. Please choose another.') . EOL;
265 q("DELETE FROM `user` WHERE `uid` = %d LIMIT 1",
272 if(x($newuid) !== false) {
273 $r = q("INSERT INTO `profile` ( `uid`, `profile-name`, `is-default`, `name`, `photo`, `thumb`, `publish`, `net-publish` )
274 VALUES ( %d, '%s', %d, '%s', '%s', '%s', %d, %d ) ",
279 dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
280 dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg"),
286 notice( t('An error occurred creating your default profile. Please try again.') . EOL );
287 // Start fresh next time.
288 $r = q("DELETE FROM `user` WHERE `uid` = %d",
292 $r = q("INSERT INTO `contact` ( `uid`, `created`, `self`, `name`, `nick`, `photo`, `thumb`, `micro`, `blocked`, `pending`, `url`, `nurl`,
293 `request`, `notify`, `poll`, `confirm`, `poco`, `name-date`, `uri-date`, `avatar-date`, `closeness` )
294 VALUES ( %d, '%s', 1, '%s', '%s', '%s', '%s', '%s', 0, 0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', 0 ) ",
299 dbesc($a->get_baseurl() . "/photo/profile/{$newuid}.jpg"),
300 dbesc($a->get_baseurl() . "/photo/avatar/{$newuid}.jpg"),
301 dbesc($a->get_baseurl() . "/photo/micro/{$newuid}.jpg"),
302 dbesc($a->get_baseurl() . "/profile/$nickname"),
303 dbesc(normalise_link($a->get_baseurl() . "/profile/$nickname")),
304 dbesc($a->get_baseurl() . "/dfrn_request/$nickname"),
305 dbesc($a->get_baseurl() . "/dfrn_notify/$nickname"),
306 dbesc($a->get_baseurl() . "/dfrn_poll/$nickname"),
307 dbesc($a->get_baseurl() . "/dfrn_confirm/$nickname"),
308 dbesc($a->get_baseurl() . "/poco/$nickname"),
309 dbesc(datetime_convert()),
310 dbesc(datetime_convert()),
311 dbesc(datetime_convert())
317 // if we have no OpenID photo try to look up an avatar
319 $photo = avatar_img($email);
321 // unless there is no avatar-plugin loaded
323 require_once('include/Photo.php');
324 $photo_failure = false;
326 $filename = basename($photo);
327 $img_str = fetch_url($photo,true);
328 $img = new Photo($img_str);
329 if($img->is_valid()) {
331 $img->scaleImageSquare(175);
333 $hash = photo_new_resource();
335 $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 4 );
338 $photo_failure = true;
340 $img->scaleImage(80);
342 $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 5 );
345 $photo_failure = true;
347 $img->scaleImage(48);
349 $r = $img->store($newuid, 0, $hash, $filename, t('Profile Photos'), 6 );
352 $photo_failure = true;
354 if(! $photo_failure) {
355 q("UPDATE `photo` SET `profile` = 1 WHERE `resource-id` = '%s' ",
362 if($netpublish && $a->config['register_policy'] != REGISTER_APPROVE) {
363 $url = $a->get_baseurl() . "/profile/$nickname";
364 proc_run('php',"include/directory.php","$url");
368 call_hooks('register_account', $newuid);
370 if( $a->config['register_policy'] == REGISTER_OPEN ) {
372 if($using_invites && $invite_id) {
373 q("delete * from register where hash = '%s' limit 1", dbesc($invite_id));
374 set_pconfig($newuid,'system','invites_remaining',$num_invites);
377 $email_tpl = get_intltext_template("register_open_eml.tpl");
378 $email_tpl = replace_macros($email_tpl, array(
379 '$sitename' => $a->config['sitename'],
380 '$siteurl' => $a->get_baseurl(),
381 '$username' => $username,
383 '$password' => $new_password,
384 '$uid' => $newuid ));
386 $res = mail($email, sprintf(t('Registration details for %s'), $a->config['sitename']),
388 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
389 . 'Content-type: text/plain; charset=UTF-8' . "\n"
390 . 'Content-transfer-encoding: 8bit' );
394 info( t('Registration successful. Please check your email for further instructions.') . EOL ) ;
398 notice( t('Failed to send email message. Here is the message that failed.') . $email_tpl . EOL );
401 elseif($a->config['register_policy'] == REGISTER_APPROVE) {
402 if(! strlen($a->config['admin_email'])) {
403 notice( t('Your registration can not be processed.') . EOL);
407 $hash = random_string();
408 $r = q("INSERT INTO `register` ( `hash`, `created`, `uid`, `password`, `language` ) VALUES ( '%s', '%s', %d, '%s', '%s' ) ",
410 dbesc(datetime_convert()),
412 dbesc($new_password),
416 $r = q("SELECT `language` FROM `user` WHERE `email` = '%s' LIMIT 1",
417 dbesc($a->config['admin_email'])
420 push_lang($r[0]['language']);
424 if($using_invites && $invite_id) {
425 q("delete * from register where hash = '%s' limit 1", dbesc($invite_id));
426 set_pconfig($newuid,'system','invites_remaining',$num_invites);
429 $email_tpl = get_intltext_template("register_verify_eml.tpl");
430 $email_tpl = replace_macros($email_tpl, array(
431 '$sitename' => $a->config['sitename'],
432 '$siteurl' => $a->get_baseurl(),
433 '$username' => $username,
435 '$password' => $new_password,
440 $res = mail($a->config['admin_email'], sprintf(t('Registration request at %s'), $a->config['sitename']),
442 'From: ' . t('Administrator') . '@' . $_SERVER['SERVER_NAME'] . "\n"
443 . 'Content-type: text/plain; charset=UTF-8' . "\n"
444 . 'Content-transfer-encoding: 8bit' );
449 info( t('Your registration is pending approval by the site owner.') . EOL ) ;
463 if(! function_exists('register_content')) {
464 function register_content(&$a) {
466 // logged in users can register others (people/pages/groups)
467 // even with closed registrations, unless specifically prohibited by site policy.
468 // 'block_extended_register' blocks all registrations, period.
470 $block = get_config('system','block_extended_register');
472 if(local_user() && ($block)) {
473 notice("Permission denied." . EOL);
477 if((! local_user()) && ($a->config['register_policy'] == REGISTER_CLOSED)) {
478 notice("Permission denied." . EOL);
482 $max_dailies = intval(get_config('system','max_daily_registrations'));
484 $r = q("select count(*) as total from user where register_date > UTC_TIMESTAMP - INTERVAL 1 day");
485 if($r && $r[0]['total'] >= $max_dailies) {
486 logger('max daily registrations exceeded.');
487 notice( t('This site has exceeded the number of allowed daily account registrations. Please try again tomorrow.') . EOL);
492 if(x($_SESSION,'theme'))
493 unset($_SESSION['theme']);
496 $username = ((x($_POST,'username')) ? $_POST['username'] : ((x($_GET,'username')) ? $_GET['username'] : ''));
497 $email = ((x($_POST,'email')) ? $_POST['email'] : ((x($_GET,'email')) ? $_GET['email'] : ''));
498 $openid_url = ((x($_POST,'openid_url')) ? $_POST['openid_url'] : ((x($_GET,'openid_url')) ? $_GET['openid_url'] : ''));
499 $nickname = ((x($_POST,'nickname')) ? $_POST['nickname'] : ((x($_GET,'nickname')) ? $_GET['nickname'] : ''));
500 $photo = ((x($_POST,'photo')) ? $_POST['photo'] : ((x($_GET,'photo')) ? hex2bin($_GET['photo']) : ''));
501 $invite_id = ((x($_POST,'invite_id')) ? $_POST['invite_id'] : ((x($_GET,'invite_id')) ? $_GET['invite_id'] : ''));
503 $noid = get_config('system','no_openid');
512 $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" >';
513 $fillwith = t("You may \x28optionally\x29 fill in this form via OpenID by supplying your OpenID and clicking 'Register'.");
514 $fillext = t('If you are not familiar with OpenID, please leave that field blank and fill in the rest of the items.');
515 $oidlabel = t("Your OpenID \x28optional\x29: ");
518 // I set this and got even more fake names than before...
520 $realpeople = ''; // t('Members of this network prefer to communicate with real people who use their real names.');
522 if(get_config('system','publish_all')) {
523 $profile_publish_reg = '<input type="hidden" name="profile_publish_reg" value="1" />';
526 $publish_tpl = get_markup_template("profile_publish.tpl");
527 $profile_publish = replace_macros($publish_tpl,array(
528 '$instance' => 'reg',
529 '$pubdesc' => t('Include your profile in member directory?'),
530 '$yes_selected' => ' checked="checked" ',
531 '$no_selected' => '',
532 '$str_yes' => t('Yes'),
540 $o = get_markup_template("register.tpl");
542 $arr = array('template' => $o);
544 call_hooks('register_form',$arr);
546 $o = replace_macros($o, array(
547 '$oidhtml' => $oidhtml,
548 '$invitations' => get_config('system','invitation_only'),
549 '$invite_desc' => t('Membership on this site is by invitation only.'),
550 '$invite_label' => t('Your invitation ID: '),
551 '$invite_id' => $invite_id,
552 '$realpeople' => $realpeople,
553 '$regtitle' => t('Registration'),
554 '$registertext' =>((x($a->config,'register_text'))
555 ? '<div class="error-message">' . $a->config['register_text'] . '</div>'
557 '$fillwith' => $fillwith,
558 '$fillext' => $fillext,
559 '$oidlabel' => $oidlabel,
560 '$openid' => $openid_url,
561 '$namelabel' => t('Your Full Name ' . "\x28" . 'e.g. Joe Smith' . "\x29" . ': '),
562 '$addrlabel' => t('Your Email Address: '),
563 '$nickdesc' => t('Choose a profile nickname. This must begin with a text character. Your profile address on this site will then be \'<strong>nickname@$sitename</strong>\'.'),
564 '$nicklabel' => t('Choose a nickname: '),
566 '$publish' => $profile_publish,
567 '$regbutt' => t('Register'),
568 '$username' => $username,
570 '$nickname' => $nickname,
571 '$license' => $license,
572 '$sitename' => $a->get_hostname()