2018-06-01 17:10:45 +01:00
< ? php
2020-01-10 12:01:31 +00:00
set_include_path ( " .: " . __DIR__ . " /../includes/ " );
include_once " web_functions.inc.php " ;
include_once " ldap_functions.inc.php " ;
include_once " module_functions.inc.php " ;
2018-06-01 17:10:45 +01:00
2019-02-08 11:28:11 +00:00
if ( $_POST [ 'setup_admin_account' ] ) {
$admin_setup = TRUE ;
2020-11-28 18:00:01 +00:00
2019-02-08 11:28:11 +00:00
validate_setup_cookie ();
set_page_access ( " setup " );
2020-11-28 18:00:01 +00:00
2019-02-08 11:28:11 +00:00
$completed_action = " /log_in " ;
$page_title = " New administrator account " ;
render_header ( " Setup administrator account " , FALSE );
}
else {
set_page_access ( " admin " );
$completed_action = " / $THIS_MODULE_PATH / " ;
$page_title = " New account " ;
render_header ();
render_submenu ();
}
2018-06-01 17:10:45 +01:00
2018-06-04 15:20:53 +01:00
$invalid_password = FALSE ;
$mismatched_passwords = FALSE ;
$invalid_username = FALSE ;
$weak_password = FALSE ;
2019-02-08 11:28:11 +00:00
$invalid_email = FALSE ;
2020-12-24 18:24:41 +00:00
$disabled_email_tickbox = TRUE ;
2018-06-01 17:10:45 +01:00
2020-12-24 18:24:41 +00:00
if ( isset ( $_GET [ 'account_request' ])) {
2020-11-28 18:00:01 +00:00
2020-12-24 18:24:41 +00:00
$first_name = filter_var ( $_GET [ 'first_name' ], FILTER_SANITIZE_STRING );
$last_name = filter_var ( $_GET [ 'last_name' ], FILTER_SANITIZE_STRING );
$email = filter_var ( $_GET [ 'email' ], FILTER_SANITIZE_EMAIL );
$username = generate_username ( $first_name , $last_name );
if ( $email == " " ) {
if ( isset ( $EMAIL_DOMAIN )) {
$email = $username . " @ " . $EMAIL_DOMAIN ;
$disabled_email_tickbox = FALSE ;
}
}
else {
$disabled_email_tickbox = FALSE ;
}
}
2020-11-28 18:00:01 +00:00
2018-06-01 17:10:45 +01:00
if ( isset ( $_POST [ 'create_account' ])) {
$ldap_connection = open_ldap_connection ();
$first_name = stripslashes ( $_POST [ 'first_name' ]);
$last_name = stripslashes ( $_POST [ 'last_name' ]);
$username = stripslashes ( $_POST [ 'username' ]);
$password = $_POST [ 'password' ];
2019-02-08 11:28:11 +00:00
2020-11-28 18:00:01 +00:00
if ( $_POST [ 'email' ]) { $email = stripslashes ( $_POST [ 'email' ]); }
2018-06-01 17:10:45 +01:00
2020-05-22 11:03:23 +01:00
if (( ! is_numeric ( $_POST [ 'pass_score' ]) or $_POST [ 'pass_score' ] < 3 ) and $ACCEPT_WEAK_PASSWORDS != TRUE ) { $weak_password = TRUE ; }
2019-02-08 11:28:11 +00:00
if ( isset ( $email ) and ! is_valid_email ( $email )) { $invalid_email = TRUE ; }
2018-06-04 15:20:53 +01:00
if ( preg_match ( " / \" |'/ " , $password )) { $invalid_password = TRUE ; }
if ( $_POST [ 'password' ] != $_POST [ 'password_match' ]) { $mismatched_passwords = TRUE ; }
if ( ! preg_match ( " / $USERNAME_REGEX / " , $username )) { $invalid_username = TRUE ; }
2020-12-24 18:24:41 +00:00
if ( isset ( $_POST [ 'send_email' ]) and isset ( $email ) and $EMAIL_SENDING_ENABLED == TRUE ) { $send_user_email = TRUE ; }
2020-11-28 18:00:01 +00:00
2018-06-01 17:10:45 +01:00
if ( isset ( $first_name )
and isset ( $last_name )
and isset ( $username )
and isset ( $password )
and ! $mismatched_passwords
and ! $weak_password
and ! $invalid_password
2019-02-08 11:28:11 +00:00
and ! $invalid_username
and ! $invalid_email ) {
2018-06-01 17:10:45 +01:00
$ldap_connection = open_ldap_connection ();
2019-02-08 11:28:11 +00:00
$new_account = ldap_new_account ( $ldap_connection , $first_name , $last_name , $username , $password , $email );
2018-06-01 17:10:45 +01:00
if ( $new_account ) {
2019-02-08 11:28:11 +00:00
2020-11-28 18:00:01 +00:00
$creation_message = " The account was created. " ;
if ( isset ( $send_user_email ) and $send_user_email == TRUE ) {
2020-11-30 16:14:53 +00:00
$mail_subject = " Your $ORGANISATION_NAME account has been created. " ;
2020-11-28 18:00:01 +00:00
$mail_body = <<< EoT
You ' ve been set up with an account for $ORGANISATION_NAME . Your credentials are :
Username : $username
Password : $password
2020-12-24 18:24:41 +00:00
You should change your password as soon as possible . Go to $ { SITE_PROTOCOL } $ { SERVER_HOSTNAME } / change_password and log in using your new credentials . This will take you to a page where you can change your password .
2020-11-28 18:00:01 +00:00
EoT ;
include_once " mail_functions.inc.php " ;
2020-11-30 16:14:53 +00:00
$sent_email = send_email ( $email , " $first_name $last_name " , $mail_subject , $mail_body );
$creation_message = " The account was created " ;
if ( $sent_email ) {
$creation_message .= " and an email sent to $email . " ;
}
else {
$creation_message .= " but unfortunately the email wasn't sent.<br>More information will be available in the logs. " ;
}
2020-11-28 18:00:01 +00:00
}
2019-02-08 11:28:11 +00:00
if ( $admin_setup == TRUE ) {
$member_add = ldap_add_member_to_group ( $ldap_connection , $LDAP [ 'admins_group' ], $username );
if ( ! $member_add ) { ?>
< div class = " alert alert-warning " >
2020-11-28 18:00:01 +00:00
< p class = " text-center " >< ? php print $creation_message ; ?> . Unfortunately adding it to the admin group failed.</p>
2019-02-08 11:28:11 +00:00
</ div >
< ? php
}
}
2018-06-01 17:10:45 +01:00
?>
< div class = " alert alert-success " >
2020-11-28 18:00:01 +00:00
< p class = " text-center " >< ? php print $creation_message ; ?> </p>
2018-06-01 17:10:45 +01:00
</ div >
2019-02-08 11:28:11 +00:00
< form action = '<?php print $completed_action; ?>' >
2018-06-01 17:10:45 +01:00
< p align = " center " >
< input type = 'submit' class = " btn btn-success " value = 'Finished' >
</ p >
</ form >
< ? php
render_footer ();
exit ( 0 );
}
else {
if ( ! $new_account ) { ?>
< div class = " alert alert-warning " >
2019-02-08 11:28:11 +00:00
< p class = " text-center " > Failed to create the account .</ p >
2018-06-01 17:10:45 +01:00
</ div >
2019-02-08 11:28:11 +00:00
< ? php
2018-06-01 17:10:45 +01:00
}
render_footer ();
exit ( 0 );
}
}
}
if ( $weak_password ) { ?>
< div class = " alert alert-warning " >
2019-02-08 11:28:11 +00:00
< p class = " text-center " > The password is too weak .</ p >
2018-06-01 17:10:45 +01:00
</ div >
< ? php }
if ( $invalid_password ) { ?>
< div class = " alert alert-warning " >
< p class = " text-center " > The password contained invalid characters .</ p >
</ div >
< ? php }
2019-02-08 11:28:11 +00:00
if ( $invalid_email ) { ?>
2018-06-01 17:10:45 +01:00
< div class = " alert alert-warning " >
2019-02-08 11:28:11 +00:00
< p class = " text-center " > The email address is invalid .</ p >
2018-06-01 17:10:45 +01:00
</ div >
< ? php }
2019-02-08 11:28:11 +00:00
if ( $mismatched_passwords ) { ?>
< div class = " alert alert-warning " >
< p class = " text-center " > The passwords are mismatched .</ p >
</ div >
< ? php }
2018-06-01 17:10:45 +01:00
if ( $invalid_username ) { ?>
< div class = " alert alert-warning " >
< p class = " text-center " > The username is invalid .</ p >
</ div >
< ? php }
render_js_username_generator ( 'first_name' , 'last_name' , 'username' , 'username_div' );
2019-02-08 11:28:11 +00:00
render_js_email_generator ( 'username' , 'email' );
2018-06-01 17:10:45 +01:00
?>
< script src = " //cdnjs.cloudflare.com/ajax/libs/zxcvbn/1.0/zxcvbn.min.js " ></ script >
< script type = " text/javascript " src = " /js/zxcvbn-bootstrap-strength-meter.js " ></ script >
< script type = " text/javascript " >
2019-02-08 11:28:11 +00:00
$ ( document ) . ready ( function (){
2018-06-01 17:10:45 +01:00
$ ( " #StrengthProgressBar " ) . zxcvbnProgressBar ({ passwordInput : " #password " });
});
</ script >
< script type = " text/javascript " src = " /js/generate_passphrase.js " ></ script >
< script type = " text/javascript " src = " /js/wordlist.js " ></ script >
< script >
2019-02-08 11:28:11 +00:00
2018-06-01 17:10:45 +01:00
function check_passwords_match () {
if ( document . getElementById ( 'password' ) . value != document . getElementById ( 'confirm' ) . value ) {
document . getElementById ( 'password_div' ) . classList . add ( " has-error " );
document . getElementById ( 'confirm_div' ) . classList . add ( " has-error " );
}
else {
document . getElementById ( 'password_div' ) . classList . remove ( " has-error " );
document . getElementById ( 'confirm_div' ) . classList . remove ( " has-error " );
}
}
function random_password () {
2019-02-08 11:28:11 +00:00
2018-06-01 17:10:45 +01:00
generatePassword ( 4 , '-' , 'password' , 'confirm' );
$ ( " #StrengthProgressBar " ) . zxcvbnProgressBar ({ passwordInput : " #password " });
}
2019-02-08 11:28:11 +00:00
2018-06-01 17:10:45 +01:00
function back_to_hidden ( passwordField , confirmField ) {
var passwordField = document . getElementById ( passwordField ) . type = 'password' ;
var confirmField = document . getElementById ( confirmField ) . type = 'password' ;
}
2019-02-08 11:28:11 +00:00
2018-06-01 17:10:45 +01:00
</ script >
2019-02-08 11:28:11 +00:00
2020-11-28 18:00:01 +00:00
< script >
function check_email_validity ( email ) {
2020-12-24 18:24:41 +00:00
var check_regex = < ? php print $JS_EMAIL_REGEX ; ?>
2020-11-28 18:00:01 +00:00
if ( ! check_regex . test ( email ) ) {
document . getElementById ( " email_div " ) . classList . add ( " has-error " );
2020-12-24 18:24:41 +00:00
< ? php if ( $EMAIL_SENDING_ENABLED == TRUE ) { ?> document.getElementById("send_email_checkbox").disabled = true;<?php } ?>
2020-11-28 18:00:01 +00:00
}
else {
document . getElementById ( " email_div " ) . classList . remove ( " has-error " );
2020-12-24 18:24:41 +00:00
< ? php if ( $EMAIL_SENDING_ENABLED == TRUE ) { ?> document.getElementById("send_email_checkbox").disabled = false;<?php } ?>
2020-11-28 18:00:01 +00:00
}
}
</ script >
2018-06-01 17:10:45 +01:00
< div class = " container " >
2020-01-10 12:01:31 +00:00
< div class = " col-sm-8 " >
2018-06-01 17:10:45 +01:00
< div class = " panel panel-default " >
2019-02-08 11:28:11 +00:00
< div class = " panel-heading text-center " >< ? php print $page_title ; ?> </div>
2018-06-01 17:10:45 +01:00
< div class = " panel-body text-center " >
< form class = " form-horizontal " action = " " method = " post " >
2019-02-08 11:28:11 +00:00
< ? php if ( $admin_setup == TRUE ) { ?> <input type="hidden" name="setup_admin_account" value="true"><?php } ?>
2018-06-01 17:10:45 +01:00
< input type = " hidden " name = " create_account " >
< input type = " hidden " id = " pass_score " value = " 0 " name = " pass_score " >
< div class = " form-group " >
2020-01-10 12:01:31 +00:00
< label for = " first_name " class = " col-sm-3 control-label " > First name </ label >
2018-06-01 17:10:45 +01:00
< div class = " col-sm-6 " >
2020-11-28 18:00:01 +00:00
< input tabindex = " 1 " type = " text " class = " form-control " id = " first_name " name = " first_name " < ? php if ( isset ( $first_name )){ print " value=' $first_name ' " ; } ?> onkeyup="update_username(); update_email(); check_email_validity(document.getElementById('email').value)">
2018-06-01 17:10:45 +01:00
</ div >
</ div >
< div class = " form-group " >
2020-01-10 12:01:31 +00:00
< label for = " last_name " class = " col-sm-3 control-label " > Last name </ label >
2018-06-01 17:10:45 +01:00
< div class = " col-sm-6 " >
2020-11-28 18:00:01 +00:00
< input tabindex = " 3 " type = " text " class = " form-control " id = " last_name " name = " last_name " < ? php if ( isset ( $last_name )){ print " value=' $last_name ' " ; } ?> onkeyup="update_username(); update_email(); check_email_validity(document.getElementById('email').value)">
2018-06-01 17:10:45 +01:00
</ div >
</ div >
< div class = " form-group " id = " username_div " >
2020-01-10 12:01:31 +00:00
< label for = " username " class = " col-sm-3 control-label " > Username </ label >
2018-06-01 17:10:45 +01:00
< div class = " col-sm-6 " >
2020-11-28 18:00:01 +00:00
< input tabindex = " 3 " type = " text " class = " form-control " id = " username " name = " username " < ? php if ( isset ( $username )){ print " value=' $username ' " ; } ?> onkeyup="check_entity_name_validity(document.getElementById('username').value,'username_div'); update_email(); check_email_validity(document.getElementById('email').value)">
2019-02-08 11:28:11 +00:00
</ div >
</ div >
< div class = " form-group " id = " email_div " >
2020-01-10 12:01:31 +00:00
< label for = " username " class = " col-sm-3 control-label " > Email </ label >
2019-02-08 11:28:11 +00:00
< div class = " col-sm-6 " >
2020-11-28 18:00:01 +00:00
< input tabindex = " 4 " type = " text " class = " form-control " id = " email " name = " email " < ? php if ( isset ( $email )){ print " value=' $email ' " ; } ?> onkeyup="auto_email_update = false; check_email_validity(document.getElementById('email').value)">
2018-06-01 17:10:45 +01:00
</ div >
</ div >
< div class = " form-group " id = " password_div " >
2020-01-10 12:01:31 +00:00
< label for = " password " class = " col-sm-3 control-label " > Password </ label >
2018-06-01 17:10:45 +01:00
< div class = " col-sm-6 " >
2020-05-06 17:19:20 +01:00
< input tabindex = " 5 " type = " text " class = " form-control " id = " password " name = " password " onkeyup = " back_to_hidden('password','confirm'); " >
2018-06-01 17:10:45 +01:00
</ div >
< div class = " col-sm-1 " >
2020-05-06 17:19:20 +01:00
< input tabindex = " 7 " type = " button " class = " btn btn-sm " id = " password_generator " onclick = " random_password(); " value = " Generate password " >
2018-06-01 17:10:45 +01:00
</ div >
</ div >
< div class = " form-group " id = " confirm_div " >
2020-01-10 12:01:31 +00:00
< label for = " confirm " class = " col-sm-3 control-label " > Confirm </ label >
2018-06-01 17:10:45 +01:00
< div class = " col-sm-6 " >
2020-05-06 17:19:20 +01:00
< input tabindex = " 6 " type = " password " class = " form-control " id = " confirm " name = " password_match " onkeyup = " check_passwords_match() " >
2018-06-01 17:10:45 +01:00
</ div >
</ div >
2020-12-24 18:24:41 +00:00
< ? php if ( $EMAIL_SENDING_ENABLED == TRUE and $admin_setup != TRUE ) { ?>
2020-11-28 18:00:01 +00:00
< div class = " form-group " id = " send_email_div " >
< label for = " send_email " class = " col-sm-3 control-label " > </ label >
< div class = " col-sm-6 " >
2020-12-24 18:24:41 +00:00
< input tabindex = " 8 " type = " checkbox " class = " form-check-input " id = " send_email_checkbox " name = " send_email " < ? php if ( $disabled_email_tickbox == TRUE ) { print " disabled " ; } ?> > Email these credentials to the user?
2020-11-28 18:00:01 +00:00
</ div >
</ div >
< ? php } ?>
2018-06-01 17:10:45 +01:00
< div class = " form-group " >
2020-11-28 18:00:01 +00:00
< button tabindex = " 9 " type = " submit " class = " btn btn-warning " > Create account </ button >
2018-06-01 17:10:45 +01:00
</ div >
</ form >
< div class = " progress " >
< div id = " StrengthProgressBar " class = " progress-bar " ></ div >
</ div >
</ div >
</ div >
</ div >
</ div >
< ? php
render_footer ();
?>