streams/Zotlabs/Module/Register.php

309 lines
9.6 KiB
PHP
Raw Normal View History

2016-04-19 03:38:38 +00:00
<?php
namespace Zotlabs\Module;
2019-05-11 01:34:27 +00:00
use Zotlabs\Web\Controller;
2019-11-21 10:32:22 +00:00
use Zotlabs\Access\PermissionRoles;
2016-04-19 03:38:38 +00:00
2019-05-11 01:34:27 +00:00
require_once('include/security.php');
2016-04-19 03:38:38 +00:00
2019-05-11 01:34:27 +00:00
class Register extends Controller {
2016-04-19 03:38:38 +00:00
function init() {
$result = null;
$cmd = ((argc() > 1) ? argv(1) : '');
// Provide a stored request for somebody desiring a connection
// when they first need to register someplace. Once they've
// created a channel, we'll try to revive the connection request
// and process it.
2019-11-21 10:32:22 +00:00
if ($_REQUEST['connect']) {
2016-04-19 03:38:38 +00:00
$_SESSION['connect'] = $_REQUEST['connect'];
2019-11-21 10:32:22 +00:00
}
2016-04-19 03:38:38 +00:00
2019-11-21 10:32:22 +00:00
switch ($cmd) {
2016-04-19 03:38:38 +00:00
case 'invite_check.json':
$result = check_account_invite($_REQUEST['invite_code']);
break;
case 'email_check.json':
$result = check_account_email($_REQUEST['email']);
break;
case 'password_check.json':
$result = check_account_password($_REQUEST['password1']);
2016-04-19 03:38:38 +00:00
break;
default:
break;
}
2019-11-21 10:32:22 +00:00
if ($result) {
2016-04-19 03:38:38 +00:00
json_return_and_die($result);
}
}
2016-08-28 23:17:58 +00:00
function post() {
2019-05-11 01:34:27 +00:00
check_form_security_token_redirectOnErr('/register', 'register');
2016-04-19 03:38:38 +00:00
$max_dailies = intval(get_config('system','max_daily_registrations'));
2019-11-21 10:32:22 +00:00
if ($max_dailies) {
2016-04-19 03:38:38 +00:00
$r = q("select count(account_id) as total from account where account_created > %s - INTERVAL %s",
db_utcnow(), db_quoteinterval('1 day')
);
2019-11-21 10:32:22 +00:00
if ($r && intval($r[0]['total']) >= $max_dailies) {
2016-04-19 03:38:38 +00:00
notice( t('Maximum daily site registrations exceeded. Please try again tomorrow.') . EOL);
return;
}
}
2019-11-21 10:32:22 +00:00
if (! x($_POST,'tos')) {
2016-04-19 03:38:38 +00:00
notice( t('Please indicate acceptance of the Terms of Service. Registration failed.') . EOL);
return;
}
$policy = get_config('system','register_policy');
$email_verify = get_config('system','verify_email');
2019-11-21 10:32:22 +00:00
switch ($policy) {
2016-04-19 03:38:38 +00:00
case REGISTER_OPEN:
$flags = ACCOUNT_OK;
break;
case REGISTER_APPROVE:
$flags = ACCOUNT_BLOCKED | ACCOUNT_PENDING;
break;
default:
case REGISTER_CLOSED:
2019-11-21 10:32:22 +00:00
if (! is_site_admin()) {
2016-04-19 03:38:38 +00:00
notice( t('Permission denied.') . EOL );
return;
}
$flags = ACCOUNT_BLOCKED;
break;
}
2019-11-21 10:32:22 +00:00
if ($email_verify && $policy == REGISTER_OPEN) {
2016-04-19 03:38:38 +00:00
$flags = $flags | ACCOUNT_UNVERIFIED;
2019-11-21 10:32:22 +00:00
}
2016-04-19 03:38:38 +00:00
2019-11-21 10:32:22 +00:00
if ((! $_POST['password']) || ($_POST['password'] !== $_POST['password2'])) {
2016-04-19 03:38:38 +00:00
notice( t('Passwords do not match.') . EOL);
return;
}
$arr = $_POST;
$arr['account_flags'] = $flags;
$result = create_account($arr);
2019-11-21 10:32:22 +00:00
if (! $result['success']) {
2016-04-19 03:38:38 +00:00
notice($result['message']);
return;
}
2019-11-21 10:32:22 +00:00
2016-04-19 03:38:38 +00:00
require_once('include/security.php');
2019-11-21 10:32:22 +00:00
if ($_REQUEST['name']) {
2016-04-19 03:38:38 +00:00
set_aconfig($result['account']['account_id'],'register','channel_name',$_REQUEST['name']);
2019-11-21 10:32:22 +00:00
}
if ($_REQUEST['nickname']) {
2016-04-19 03:38:38 +00:00
set_aconfig($result['account']['account_id'],'register','channel_address',$_REQUEST['nickname']);
2019-11-21 10:32:22 +00:00
}
if ($_REQUEST['permissions_role']) {
2016-04-19 03:38:38 +00:00
set_aconfig($result['account']['account_id'],'register','permissions_role',$_REQUEST['permissions_role']);
2019-11-21 10:32:22 +00:00
}
2016-04-19 03:38:38 +00:00
$using_invites = intval(get_config('system','invitation_only'));
$num_invites = intval(get_config('system','number_invites'));
$invite_code = ((x($_POST,'invite_code')) ? notags(trim($_POST['invite_code'])) : '');
2019-11-21 10:32:22 +00:00
if ($using_invites && $invite_code) {
2020-05-28 01:56:51 +00:00
q("delete from register where hash = '%s'", dbesc($invite_code));
2016-04-19 03:38:38 +00:00
// @FIXME - this also needs to be considered when using 'invites_remaining' in mod/invite.php
set_aconfig($result['account']['account_id'],'system','invites_remaining',$num_invites);
}
2019-11-21 10:32:22 +00:00
if ($policy == REGISTER_OPEN ) {
if ($email_verify) {
2016-04-19 03:38:38 +00:00
$res = verify_email_address($result);
}
else {
$res = send_register_success_email($result['email'],$result['password']);
}
2019-11-21 10:32:22 +00:00
if ($res) {
if ($invite_code) {
info( t('Registration successful. Continue to create your first channel...') . EOL ) ;
}
else {
info( t('Registration successful. Please check your email for validation instructions.') . EOL ) ;
}
2016-04-19 03:38:38 +00:00
}
}
2019-11-21 10:32:22 +00:00
elseif ($policy == REGISTER_APPROVE) {
2016-04-19 03:38:38 +00:00
$res = send_reg_approval_email($result);
2019-11-21 10:32:22 +00:00
if ($res) {
2016-04-19 03:38:38 +00:00
info( t('Your registration is pending approval by the site owner.') . EOL ) ;
}
else {
notice( t('Your registration can not be processed.') . EOL);
}
goaway(z_root());
}
2019-11-21 10:32:22 +00:00
if ($email_verify) {
goaway(z_root() . '/email_validation/' . bin2hex($result['email']));
2016-04-19 03:38:38 +00:00
}
// fall through and authenticate if no approvals or verifications were required.
authenticate_success($result['account'],null,true,false,true);
2016-04-19 03:38:38 +00:00
$new_channel = false;
$next_page = 'new_channel';
2019-11-21 10:32:22 +00:00
if (get_config('system','auto_channel_create')) {
2016-04-19 03:38:38 +00:00
$new_channel = auto_channel_create($result['account']['account_id']);
2019-11-21 10:32:22 +00:00
if ($new_channel['success']) {
2016-04-19 03:38:38 +00:00
$channel_id = $new_channel['channel']['channel_id'];
change_channel($channel_id);
$next_page = '~';
}
2019-11-21 10:32:22 +00:00
else {
2016-04-19 03:38:38 +00:00
$new_channel = false;
2019-11-21 10:32:22 +00:00
}
2016-04-19 03:38:38 +00:00
}
$x = get_config('system','workflow_register_next');
2019-11-21 10:32:22 +00:00
if ($x) {
2016-04-19 03:38:38 +00:00
$next_page = $x;
$_SESSION['workflow'] = true;
}
unset($_SESSION['login_return_url']);
2016-04-19 03:38:38 +00:00
goaway(z_root() . '/' . $next_page);
}
function get() {
2016-04-19 03:38:38 +00:00
2020-08-31 04:08:39 +00:00
$registration_is = EMPTY_STR;
$other_sites = false;
2016-04-19 03:38:38 +00:00
2019-11-21 10:32:22 +00:00
if (intval(get_config('system','register_policy')) === REGISTER_CLOSED) {
2020-08-31 04:08:39 +00:00
notice( t('Registration on this website is disabled.') . EOL);
2019-11-21 10:32:22 +00:00
if (intval(get_config('system','directory_mode')) === DIRECTORY_MODE_STANDALONE) {
2020-08-31 04:08:39 +00:00
return EMPTY_STR;
}
else {
$other_sites = true;
2016-04-19 03:38:38 +00:00
}
}
2019-11-21 10:32:22 +00:00
if (intval(get_config('system','register_policy')) == REGISTER_APPROVE) {
2020-08-31 04:08:39 +00:00
$registration_is = t('Registration on this website is by approval only.');
$other_sites = true;
2016-04-19 03:38:38 +00:00
}
$invitations = false;
2019-11-21 10:32:22 +00:00
if (intval(get_config('system','invitation_only'))) {
$invitations = true;
2020-08-31 04:08:39 +00:00
$registration_is = t('Registration on this site is by invitation only.');
$other_sites = true;
}
2016-04-19 03:38:38 +00:00
$max_dailies = intval(get_config('system','max_daily_registrations'));
2019-11-21 10:32:22 +00:00
if ($max_dailies) {
2016-04-19 03:38:38 +00:00
$r = q("select count(account_id) as total from account where account_created > %s - INTERVAL %s",
db_utcnow(), db_quoteinterval('1 day')
);
2019-11-21 10:32:22 +00:00
if ($r && $r[0]['total'] >= $max_dailies) {
2016-04-19 03:38:38 +00:00
logger('max daily registrations exceeded.');
notice( t('This site has exceeded the number of allowed daily account registrations. Please try again tomorrow.') . EOL);
return;
}
}
$privacy_role = ((x($_REQUEST,'permissions_role')) ? $_REQUEST['permissions_role'] : "");
2019-11-21 10:32:22 +00:00
$perm_roles = PermissionRoles::roles();
2016-04-19 03:38:38 +00:00
// Configurable terms of service link
$tosurl = get_config('system','tos_url');
2019-11-21 10:32:22 +00:00
if (! $tosurl) {
2016-04-19 03:38:38 +00:00
$tosurl = z_root() . '/help/TermsOfService';
2019-11-21 10:32:22 +00:00
}
2016-04-19 03:38:38 +00:00
$toslink = '<a href="' . $tosurl . '" target="_blank">' . t('Terms of Service') . '</a>';
// Configurable whether to restrict age or not - default is based on international legal requirements
// This can be relaxed if you are on a restricted server that does not share with public servers
2019-11-21 10:32:22 +00:00
if (get_config('system','no_age_restriction')) {
2016-04-19 03:38:38 +00:00
$label_tos = sprintf( t('I accept the %s for this website'), $toslink);
}
else {
$age = get_config('system','minimum_age');
2020-08-31 04:08:39 +00:00
if (! $age) {
$age = 13;
}
$label_tos = sprintf( t('I am over %s years of age and accept the %s for this website'), $age, $toslink);
}
2020-08-31 04:08:39 +00:00
$enable_tos = 1 - intval(get_config('system','no_termsofservice'));
$email = [ 'email', t('Your email address'), ((x($_REQUEST,'email')) ? strip_tags(trim($_REQUEST['email'])) : "")];
$password = [ 'password', t('Choose a password'), '' ];
$password2 = [ 'password2', t('Please re-enter your password'), '' ];
$invite_code = [ 'invite_code', t('Please enter your invitation code'), ((x($_REQUEST,'invite_code')) ? strip_tags(trim($_REQUEST['invite_code'])) : "")];
$name = [ 'name', t('Your Name'), ((x($_REQUEST,'name')) ? $_REQUEST['name'] : ''), t('Real names are preferred.') ];
$nickhub = '@' . str_replace(array('http://','https://','/'), '', get_config('system','baseurl'));
$nickname = [ 'nickname', t('Choose a short nickname'), ((x($_REQUEST,'nickname')) ? $_REQUEST['nickname'] : ''), sprintf( t('Your nickname will be used to create an easy to remember channel address e.g. nickname%s'), $nickhub)];
$role = ['permissions_role' , t('Channel role and privacy'), ($privacy_role) ? $privacy_role : 'social', t('Select a channel permission role for your usage needs and privacy requirements.'),$perm_roles];
$tos = [ 'tos', $label_tos, '', '', [ t('no'), t('yes') ] ];
2017-05-29 20:50:02 +00:00
$auto_create = (get_config('system','auto_channel_create') ? true : false);
$default_role = get_config('system','default_permissions_role');
$email_verify = get_config('system','verify_email');
2016-04-19 03:38:38 +00:00
2020-08-31 04:08:39 +00:00
$o = replace_macros(get_markup_template('register.tpl'), [
2019-05-11 01:34:27 +00:00
'$form_security_token' => get_form_security_token("register"),
2016-04-19 03:38:38 +00:00
'$title' => t('Registration'),
'$reg_is' => $registration_is,
'$registertext' => bbcode(get_config('system','register_text')),
2020-08-31 04:08:39 +00:00
'$other_sites' => (($other_sites) ? t('<a href="sites">Show affiliated sites - some of which may allow registration.</a>') : EMPTY_STR),
'$invitations' => $invitations,
2016-04-19 03:38:38 +00:00
'$invite_code' => $invite_code,
'$auto_create' => $auto_create,
'$name' => $name,
'$role' => $role,
2016-04-19 03:38:38 +00:00
'$default_role' => $default_role,
'$nickname' => $nickname,
'$enable_tos' => $enable_tos,
'$tos' => $tos,
2016-04-19 03:38:38 +00:00
'$email' => $email,
'$pass1' => $password,
'$pass2' => $password2,
'$submit' => t('Register'),
'$verify_note' => (($email_verify) ? t('This site requires email verification. After completing this form, please check your email for further instructions.') : ''),
2020-08-31 04:08:39 +00:00
] );
2016-04-19 03:38:38 +00:00
return $o;
}
}