-
Notifications
You must be signed in to change notification settings - Fork 0
/
register.php
executable file
·221 lines (197 loc) · 7.21 KB
/
register.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
require_once (__SITE_ROOT__.'/classes/CSRFTokenHandler.php');
$lCSRFTokenHandler = new CSRFTokenHandler($_SESSION["security-level"], "register-user");
switch ($_SESSION["security-level"]){
case "0": // This code is insecure
// DO NOTHING: This is equivalent to using client side security
$lEnableJavaScriptValidation = FALSE;
$lEnableHTMLControls = FALSE;
$lProtectAgainstMethodTampering = FALSE;
$lEncodeOutput = FALSE;
break;
case "1": // This code is insecure
// DO NOTHING: This is equivalent to using client side security
$lEnableJavaScriptValidation = TRUE;
$lEnableHTMLControls = TRUE;
$lProtectAgainstMethodTampering = FALSE;
$lEncodeOutput = FALSE;
break;
case "2":
case "3":
case "4":
case "5": // This code is fairly secure
/*
* Concerning SQL Injection, use parameterized stored procedures. Parameterized
* queries is not good enough. You cannot use least privilege with queries.
*/
$lEnableJavaScriptValidation = TRUE;
$lEnableHTMLControls = TRUE;
$lProtectAgainstMethodTampering = TRUE;
$lEncodeOutput = TRUE;
break;
}// end switch
$lNewCSRFTokenForNextRequest = $lCSRFTokenHandler->generateCSRFToken();
$lFormSubmitted = isset($_REQUEST["register-php-submit-button"]);
?>
<div class="page-title">Register for an Account</div>
<?php include_once (__SITE_ROOT__.'/includes/back-button.inc');?>
<?php include_once (__SITE_ROOT__.'/includes/hints/hints-menu-wrapper.inc'); ?>
<?php
if ($lFormSubmitted){
try {
$lValidationFailed = false;
if ($lProtectAgainstMethodTampering) {
$lUsername = $_POST["username"];
$lPassword = $_POST["password"];
$lConfirmedPassword = $_POST["confirm_password"];
$lUserSignature = $_POST["my_signature"];
$lPostedCSRFToken = $_POST['csrf-token'];
}else{
$lUsername = $_REQUEST["username"];
$lPassword = $_REQUEST["password"];
$lConfirmedPassword = $_REQUEST["confirm_password"];
$lUserSignature = $_REQUEST["my_signature"];
$lPostedCSRFToken = $_REQUEST['csrf-token'];
}//end if
if ($lEncodeOutput){
$lUsernameText = $Encoder->encodeForHTML($lUsername);
}else{
//allow XSS by not encoding
$lUsernameText = $lUsername;
}//end if
$LogHandler->writeToLog("Attempting to add account for: " . $lUsername);
if (!$lCSRFTokenHandler->validateCSRFToken($lPostedCSRFToken)){
throw (new Exception("Security Violation: Cross Site Request Forgery attempt detected.", 500));
}// end if
if (strlen($lUsername) == 0) {
$lValidationFailed = TRUE;
echo '<h2 class="error-message">Username cannot be blank</h2>';
}// end if
if ($lPassword != $lConfirmedPassword ) {
$lValidationFailed = TRUE;
echo '<h2 class="error-message">Passwords do not match</h2>';
}// end if
if (!$lValidationFailed){
$lRowsAffected = $SQLQueryHandler->insertNewUserAccount($lUsername, $lPassword, $lUserSignature);
echo '<h2 class="success-message">Account created for ' . $lUsernameText .'. '.$lRowsAffected.' rows inserted.</h2>';
$LogHandler->writeToLog("Added account for: " . $lUsername);
}// end if (!$lValidationFailed)
} catch (Exception $e) {
echo $CustomErrorHandler->FormatError($e, "Failed to add account");
$LogHandler->writeToLog("Failed to add account for: " . $lUsername);
}// end try
}// end if $lFormSubmitted
?>
<script type="text/javascript">
<!--
<?php
if($lEnableJavaScriptValidation){
echo "var lValidateInput = \"TRUE\"" . PHP_EOL;
}else{
echo "var lValidateInput = \"FALSE\"" . PHP_EOL;
}// end if
?>
function onSubmitOfForm(/*HTMLFormElement*/ theForm){
try{
if(lValidateInput == "TRUE"){
var lUnsafeCharacters = /[`~!@#$%^&*()-_=+\[\]{}\\|;':",./<>?]/;
if (theForm.username.value.length > 15 ||
theForm.password.value.length > 15){
alert('Username too long. We dont want to allow too many characters.\n\nSomeone might have enough room to enter a hack attempt.');
return false;
};// end if
if (theForm.username.value.search(lUnsafeCharacters) > -1 ||
theForm.password.value.search(lUnsafeCharacters) > -1){
alert('Dangerous characters detected. We can\'t allow these. This all powerful blacklist will stop such attempts.\n\nMuch like padlocks, filtering cannot be defeated.\n\nBlacklisting is l33t like l33tspeak.');
return false;
};// end if
};// end if(lValidateInput)
return true;
}catch(e){
alert("Error: " + e.message);
};// end catch
};// end function onSubmitOfLoginForm(/*HTMLFormElement*/ theForm)
//-->
</script>
<span>
<a style="text-decoration: none; cursor: pointer;" href="./webservices/rest/ws-user-account.php">
<img style="vertical-align: middle;" src="./images/ajax_logo-75-79.jpg" height="75px" width="78px" />
<span style="font-weight:bold;">Switch to RESTful Web Service Version of this Page</span>
</a>
</span>
<div id="id-registration-form-div">
<form action="index.php?page=register.php" method="post" enctype="application/x-www-form-urlencoded"
onsubmit="return onSubmitOfForm(this);"
>
<input name="csrf-token" type="hidden" value="<?php echo $lNewCSRFTokenForNextRequest; ?>" />
<table>
<tr><td> </td></tr>
<tr>
<td colspan="2" class="form-header">Please choose your username, password and signature</td>
</tr>
<tr><td> </td></tr>
<tr>
<td class="label">Username</td>
<td>
<input type="text" name="username" size="15" autofocus="autofocus"
<?php
if ($lEnableHTMLControls) {
echo('minlength="1" maxlength="15" required="required"');
}// end if
?>
/>
</td>
</tr>
<tr>
<td class="label">Password</td>
<td>
<input type="password" name="password" size="15"
<?php
if ($lEnableHTMLControls) {
echo('minlength="1" maxlength="15" required="required"');
}// end if
?>
/>
<a href="index.php?page=password-generator.php&username=<?php echo $logged_in_user ?>" target="_blank">Password Generator</a>
</td>
</tr>
<tr>
<td class="label">Confirm Password</td>
<td>
<input type="password" name="confirm_password" size="15"
<?php
if ($lEnableHTMLControls) {
echo('minlength="1" maxlength="15" required="required"');
}// end if
?>
/>
</td>
</tr>
<tr>
<td class="label">Signature</td>
<td>
<textarea rows="3" cols="50" name="my_signature"
<?php
if ($lEnableHTMLControls) {
echo('minlength="1" maxlength="100" required="required"');
}// end if
?>
></textarea>
</td>
</tr>
<tr><td> </td></tr>
<tr>
<td colspan="2" style="text-align:center;">
<input name="register-php-submit-button" class="button" type="submit" value="Create Account" />
</td>
</tr>
<tr><td> </td></tr>
</table>
</form>
</div>
<?php
if ($lFormSubmitted) {
echo $lCSRFTokenHandler->generateCSRFHTMLReport();
}// end if
?>