/
register.php
143 lines (120 loc) · 3.55 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
<?php
/*
Registeration form. Part of My Stuff -project.
Copyright (C) 2009 Aleksi Räsänen <aleksi.rasanen@runosydan.net>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
session_start();
require 'general_functions.php';
function check_post_values()
{
if(! isset( $_POST['ms_username'] ) )
return false;
// Remove dots and / characters so the user cannot try
// to set username to ../../pwnd or similar.
$_POST['ms_username'] = preg_replace( '/\./', '',
$_POST['ms_username'] );
$_POST['ms_username'] = preg_replace( '/\//', '',
$_POST['ms_username'] );
// Is username empty?
if( isset( $_POST['ms_username'] )
&& empty( $_POST['ms_username'] ) )
{
$_SESSION['errorMsg'] = 'Username is not set.';
return false;
}
// If users exists
if( file_exists( 'users/' . $_POST['ms_username'] . '.txt' ) )
{
$_SESSION['errorMsg'] = 'Username is already used.';
return false;
}
// Are there two different passwords?
if( isset( $_POST['password'] )
&& isset( $_POST['password_again'] ) )
{
if( $_POST['password'] != $_POST['password_again'] )
{
$_SESSION['errorMsg'] = 'Passwords did not match.';
return false;
}
}
// Check that the 'users/' directory exist.
if(! file_exists( 'users/' ) )
{
if(! mkdir( 'users/', 0700 ) )
{
$_SESSION['errorMsg'] = 'Can\'t create folder users/';
return false;
}
}
$fh = @fopen( 'users/' . $_POST['ms_username'] . '.txt', 'w' );
if(! $fh )
{
$_SESSION['errorMsg'] = 'Permission denied.';
return false;
}
fwrite( $fh, 'password=' . sha1( $_POST['password'] ) . "\n" );
fclose( $fh );
$_SESSION['errorMsg'] = 'User ' . $_POST['ms_username']
. ' is now registered!';
return true;
}
function create_register_form()
{
echo '<div id="register">';
echo '<h2>Register</h2>';
echo '<form action="register.php" method="post">';
echo '<table>';
echo '<tr>';
echo '<td>Username</td>';
echo '<td><input type="text" name="ms_username"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Password</td>';
echo '<td><input type="password" name="password"></td>';
echo '</tr>';
echo '<tr>';
echo '<td>Password again</td>';
echo '<td><input type="password" name="password_again"></td>';
echo '</tr>';
echo '<tr>';
echo '<td colspan="2"><input type="submit" value="Register"></td>';
echo '</tr>';
echo '</table>';
echo '<br />';
echo '<a href="index.php">Back to main page</a>';
echo '</form>';
echo '</div>';
}
function main()
{
create_html_start();
// If check_post_values returns true, then new
// user is succesfully created (or at least it should be...)
// Note that show_error_msg shows message where is
// message that user is registered succesfully!
if( check_post_values() )
{
show_error_msg();
echo '<br />';
echo '<a href="index.php">Back to main page</a>';
}
else
{
show_error_msg();
create_register_form();
}
create_html_end();
}
main();
?>