Passed
Push — master ( 6b7476...095fed )
by Cody
03:39
created

register.php (1 issue)

1
<?php
2
    // This file uses two additional include files:
3
    //
4
    // 1) templates/register_notice.txt - displayed above the registration form
5
    // 2) register_expire_do.php - contains user expiration queries when necessary
6
7
    set_include_path(dirname(__FILE__)."/include".PATH_SEPARATOR.
8
        get_include_path());
9
10
    require_once "autoload.php";
11
    require_once "functions.php";
12
    require_once "sessions.php";
13
    require_once "sanity_check.php";
14
    require_once "config.php";
15
    require_once "db.php";
16
17
    startup_gettext();
0 ignored issues
show
Deprecated Code introduced by
The function startup_gettext() has been deprecated: Loaded in bootstrap ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

17
    /** @scrutinizer ignore-deprecated */ startup_gettext();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
18
19
    $action = $_REQUEST["action"];
20
21
    if (!init_plugins()) {
22
        return;
23
    }
24
25
    if ($_REQUEST["format"] == "feed") {
26
        header("Content-Type: text/xml");
27
28
        print '<?xml version="1.0" encoding="utf-8"?>';
29
        print "<feed xmlns=\"http://www.w3.org/2005/Atom\">
30
			<id>".htmlspecialchars(SELF_URL_PATH."/register.php")."</id>
31
			<title>Tiny Tiny RSS registration slots</title>
32
			<link rel=\"self\" href=\"".htmlspecialchars(SELF_URL_PATH."/register.php?format=feed")."\"/>
33
			<link rel=\"alternate\" href=\"".htmlspecialchars(SELF_URL_PATH)."\"/>";
34
35
        if (ENABLE_REGISTRATION) {
36
            $result = db_query("SELECT COUNT(*) AS cu FROM ttrss_users");
37
            $num_users = db_fetch_result($result, 0, "cu");
38
39
            $num_users = REG_MAX_USERS - $num_users;
40
            if ($num_users < 0) {
41
                $num_users = 0;
42
            }
43
            $reg_suffix = "enabled";
44
        } else {
45
            $num_users = 0;
46
            $reg_suffix = "disabled";
47
        }
48
49
        print "<entry>
50
			<id>".htmlspecialchars(SELF_URL_PATH)."/register.php?$num_users"."</id>
51
			<link rel=\"alternate\" href=\"".htmlspecialchars(SELF_URL_PATH."/register.php")."\"/>";
52
53
        print "<title>$num_users slots are currently available, registration $reg_suffix</title>";
54
        print "<summary>$num_users slots are currently available, registration $reg_suffix</summary>";
55
56
        print "</entry>";
57
58
        print "</feed>";
59
60
        return;
61
    }
62
63
    /* Remove users which didn't login after receiving their registration information */
64
65
    if (DB_TYPE == "pgsql") {
66
        db_query("DELETE FROM ttrss_users WHERE last_login IS NULL
67
				AND created < NOW() - INTERVAL '1 day' AND access_level = 0");
68
    } else {
69
        db_query("DELETE FROM ttrss_users WHERE last_login IS NULL
70
				AND created < DATE_SUB(NOW(), INTERVAL 1 DAY) AND access_level = 0");
71
    }
72
73
    if (file_exists("register_expire_do.php")) {
74
        require_once "register_expire_do.php";
75
    }
76
77
    if ($action == "check") {
78
        header("Content-Type: application/xml");
79
80
        $login = trim(db_escape_string($_REQUEST['login']));
81
82
        $result = db_query("SELECT id FROM ttrss_users WHERE
83
			LOWER(login) = LOWER('$login')");
84
85
        $is_registered = db_num_rows($result) > 0;
86
87
        print "<result>";
88
89
        printf("%d", $is_registered);
90
91
        print "</result>";
92
93
        return;
94
    }
95
?>
96
<!DOCTYPE html>
97
<html>
98
<head>
99
<title>Create new account</title>
100
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
101
<?php echo stylesheet_tag("css/default.css") ?>
102
<?php echo javascript_tag("js/common.js") ?>
103
<?php echo javascript_tag("lib/prototype.js") ?>
104
<?php echo javascript_tag("lib/scriptaculous/scriptaculous.js?load=effects,controls") ?>
105
</head>
106
107
<script type="text/javascript">
108
109
	public function checkUsername() {
110
111
		try {
112
			var f = document.forms['register_form'];
113
			var login = f.login.value;
114
115
			if (login == "") {
116
				new Effect.Highlight(f.login);
117
				f.sub_btn.disabled = true;
118
				return false;
119
			}
120
121
			var query = "register.php?action=check&login=" +
122
					encodeURIComponent(login);
123
124
			new Ajax.Request(query, {
125
				onComplete: function(transport) {
126
127
					try {
128
129
						var reply = transport.responseXML;
130
131
						var result = reply.getElementsByTagName('result')[0];
132
						var result_code = result.firstChild.nodeValue;
133
134
						if (result_code == 0) {
135
							new Effect.Highlight(f.login, {startcolor : '#00ff00'});
136
							f.sub_btn.disabled = false;
137
						} else {
138
							new Effect.Highlight(f.login, {startcolor : '#ff0000'});
139
							f.sub_btn.disabled = true;
140
						}
141
					} catch (e) {
142
						App.Error.report(e);
143
					}
144
145
				} });
146
147
		} catch (e) {
148
			App.Error.report(e);
149
		}
150
151
		return false;
152
153
	}
154
155
	public function validateRegForm() {
156
		try {
157
158
			var f = document.forms['register_form'];
159
160
			if (f.login.value.length == 0) {
161
				new Effect.Highlight(f.login);
162
				return false;
163
			}
164
165
			if (f.email.value.length == 0) {
166
				new Effect.Highlight(f.email);
167
				return false;
168
			}
169
170
			if (f.turing_test.value.length == 0) {
171
				new Effect.Highlight(f.turing_test);
172
				return false;
173
			}
174
175
			return true;
176
177
		} catch (e) {
178
			alert(e.stack);
179
			return false;
180
		}
181
	}
182
183
</script>
184
185
<body class="claro ttrss_utility">
186
187
<h1><?php echo __("Create new account") ?></h1>
188
189
<div class="content">
190
191
<?php
192
        if (!ENABLE_REGISTRATION) {
193
            print_error(__("New user registrations are administratively disabled."));
194
195
            print "<p><form method=\"GET\" action=\"backend.php\">
196
				<input type=\"hidden\" name=\"op\" value=\"logout\">
197
				<input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
198
				</form>";
199
            return;
200
        }
201
?>
202
203
<?php if (REG_MAX_USERS > 0) {
204
        $result = db_query("SELECT COUNT(*) AS cu FROM ttrss_users");
205
        $num_users = db_fetch_result($result, 0, "cu");
206
} ?>
207
208
<?php if (!REG_MAX_USERS || $num_users < REG_MAX_USERS) { ?>
209
210
	<!-- If you have any rules or ToS you'd like to display, enter them here -->
211
212
	<?php	if (file_exists("templates/register_notice.txt")) {
213
            require_once "templates/register_notice.txt";
214
    } ?>
215
216
	<?php if (!$action) { ?>
217
218
	<p><?php echo __('Your temporary password will be sent to the specified email. Accounts, which were not logged in once, are erased automatically 24 hours after temporary password is sent.') ?></p>
219
220
	<form action="register.php" method="POST" name="register_form">
221
	<input type="hidden" name="action" value="do_register">
222
	<table>
223
	<tr>
224
	<td><?php echo __('Desired login:') ?></td><td>
225
		<input name="login" required>
226
	</td><td>
227
		<input type="submit" value="<?php echo __('Check availability') ?>" onclick='return checkUsername()'>
228
	</td></tr>
229
	<tr><td><?php echo __('Email:') ?></td><td>
230
		<input name="email" type="email" required>
231
	</td></tr>
232
	<tr><td><?php echo __('How much is two plus two:') ?></td><td>
233
		<input name="turing_test" required></td></tr>
234
	<tr><td colspan="2" align="right">
235
	<input type="submit" name="sub_btn" value="<?php echo __('Submit registration') ?>"
236
			disabled="disabled" onclick='return validateRegForm()'>
237
	</td></tr>
238
	</table>
239
	</form>
240
241
	<?php print "<p><form method=\"GET\" action=\"index.php\">
242
				<input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
243
				</form>"; ?>
244
245
	<?php } else if ($action == "do_register") { ?>
246
247
	<?php
248
        $login = mb_strtolower(trim(db_escape_string($_REQUEST["login"])));
249
        $email = trim(db_escape_string($_REQUEST["email"]));
250
        $test = trim(db_escape_string($_REQUEST["turing_test"]));
251
252
        if (!$login || !$email || !$test) {
253
            print_error(__("Your registration information is incomplete."));
254
            print "<p><form method=\"GET\" action=\"index.php\">
255
				<input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
256
				</form>";
257
            return;
258
        }
259
260
        if ($test == "four" || $test == "4") {
261
262
            $result = db_query("SELECT id FROM ttrss_users WHERE
263
				login = '$login'");
264
265
            $is_registered = db_num_rows($result) > 0;
266
267
            if ($is_registered) {
268
                print_error(__('Sorry, this username is already taken.'));
269
                print "<p><form method=\"GET\" action=\"index.php\">
270
				<input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
271
				</form>";
272
            } else {
273
274
                $password = make_password();
275
276
                $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
277
                $pwd_hash = encrypt_password($password, $salt, true);
278
279
                db_query("INSERT INTO ttrss_users
280
					(login,pwd_hash,access_level,last_login, email, created, salt)
281
					VALUES ('$login', '$pwd_hash', 0, null, '$email', NOW(), '$salt')");
282
283
                $result = db_query("SELECT id FROM ttrss_users WHERE
284
					login = '$login' AND pwd_hash = '$pwd_hash'");
285
286
                if (db_num_rows($result) != 1) {
287
                    print_error(__('Registration failed.'));
288
                    print "<p><form method=\"GET\" action=\"index.php\">
289
					<input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
290
					</form>";
291
                } else {
292
293
                    $new_uid = db_fetch_result($result, 0, "id");
294
295
                    initialize_user($new_uid);
296
297
                    $reg_text = "Hi!\n".
298
                        "\n".
299
                        "You are receiving this message, because you (or somebody else) have opened\n".
300
                        "an account at Tiny Tiny RSS.\n".
301
                        "\n".
302
                        "Your login information is as follows:\n".
303
                        "\n".
304
                        "Login: $login\n".
305
                        "Password: $password\n".
306
                        "\n".
307
                        "Don't forget to login at least once to your new account, otherwise\n".
308
                        "it will be deleted in 24 hours.\n".
309
                        "\n".
310
                        "If that wasn't you, just ignore this message. Thanks.";
311
312
                    $mailer = new Mailer();
313
                    $rc = $mailer->mail(["to_address" => $email,
314
                        "subject" => "Registration information for Tiny Tiny RSS",
315
                        "message" => $reg_text]);
316
317
                    if (!$rc) {
318
                        print_error($mailer->error());
319
                    }
320
321
                    $reg_text = "Hi!\n".
322
                        "\n".
323
                        "New user had registered at your Tiny Tiny RSS installation.\n".
324
                        "\n".
325
                        "Login: $login\n".
326
                        "Email: $email\n";
327
328
                    $mailer = new Mailer();
329
                    $rc = $mailer->mail(["to_address" => REG_NOTIFY_ADDRESS,
330
                        "subject" => "Registration notice for Tiny Tiny RSS",
331
                        "message" => $reg_text]);
332
333
                    if (!$rc) {
334
                        print_error($mailer->error());
335
                    }
336
337
                    print_notice(__("Account created successfully."));
338
339
                    print "<p><form method=\"GET\" action=\"index.php\">
340
					<input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
341
					</form>";
342
343
                }
344
345
            }
346
347
            } else {
348
                print_error('Plese check the form again, you have failed the robot test.');
349
                print "<p><form method=\"GET\" action=\"index.php\">
350
				<input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
351
				</form>";
352
353
            }
354
        }
355
    ?>
356
357
<?php } else { ?>
358
359
	<?php print_notice(__('New user registrations are currently closed.')) ?>
360
361
	<?php print "<p><form method=\"GET\" action=\"index.php\">
362
				<input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
363
				</form>"; ?>
364
365
<?php } ?>
366
367
	</div>
368
369
</body>
370
</html>
371