]> git.wh0rd.org - tt-rss.git/blame - register.php
add Public_Handler
[tt-rss.git] / register.php
CommitLineData
4f7956b3 1<?php
a53f3dc4 2 // This file uses two additional include files:
4f7956b3 3 //
a53f3dc4
AD
4 // 1) templates/register_notice.txt - displayed above the registration form
5 // 2) register_expire_do.php - contains user expiration queries when necessary
4f7956b3 6
107d0cf3
AD
7 set_include_path(get_include_path() . PATH_SEPARATOR . "include");
8
31303c6b
AD
9 require_once 'lib/phpmailer/class.phpmailer.php';
10
4f7956b3
AD
11 $action = $_REQUEST["action"];
12
fb074239 13 require_once "functions.php";
4f7956b3 14 require_once "sessions.php";
4f7956b3 15 require_once "sanity_check.php";
4f7956b3
AD
16 require_once "config.php";
17 require_once "db.php";
4f7956b3 18
133ab8c7
AD
19 $link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
20
5f0a3741 21 if (!init_connection($link)) return;
4f7956b3 22
870a70e1
AD
23 if ($_REQUEST["format"] == "feed") {
24 header("Content-Type: text/xml");
25
26 print '<?xml version="1.0" encoding="utf-8"?>';
27 print "<feed xmlns=\"http://www.w3.org/2005/Atom\">
28 <id>".htmlspecialchars(SELF_URL_PATH . "/register.php")."</id>
29 <title>Tiny Tiny RSS registration slots</title>
30 <link rel=\"self\" href=\"".htmlspecialchars(SELF_URL_PATH . "/register.php?format=feed")."\"/>
31 <link rel=\"alternate\" href=\"".htmlspecialchars(SELF_URL_PATH)."\"/>";
32
33 if (ENABLE_REGISTRATION) {
34 $result = db_query($link, "SELECT COUNT(*) AS cu FROM ttrss_users");
35 $num_users = db_fetch_result($result, 0, "cu");
36
410c0b3a 37 $num_users = REG_MAX_USERS - $num_users;
870a70e1
AD
38 if ($num_users < 0) $num_users = 0;
39 $reg_suffix = "enabled";
40 } else {
41 $num_users = 0;
42 $reg_suffix = "disabled";
43 }
44
45 print "<entry>
46 <id>".htmlspecialchars(SELF_URL_PATH)."/register.php?$num_users"."</id>
47 <link rel=\"alternate\" href=\"".htmlspecialchars(SELF_URL_PATH . "/register.php")."\"/>";
48
49 print "<title>$num_users slots are currently available, registration $reg_suffix</title>";
50 print "<summary>$num_users slots are currently available, registration $reg_suffix</summary>";
51
52 print "</entry>";
53
54 print "</feed>";
55
56 return;
57 }
58
4f7956b3
AD
59 /* Remove users which didn't login after receiving their registration information */
60
61 if (DB_TYPE == "pgsql") {
133ab8c7 62 db_query($link, "DELETE FROM ttrss_users WHERE last_login IS NULL
4f7956b3
AD
63 AND created < NOW() - INTERVAL '1 day' AND access_level = 0");
64 } else {
133ab8c7 65 db_query($link, "DELETE FROM ttrss_users WHERE last_login IS NULL
4f7956b3
AD
66 AND created < DATE_SUB(NOW(), INTERVAL 1 DAY) AND access_level = 0");
67 }
68
a53f3dc4
AD
69 if (file_exists("register_expire_do.php")) {
70 require_once "register_expire_do.php";
71 }
72
4f7956b3
AD
73 if ($action == "check") {
74 header("Content-Type: application/xml");
75
76 $login = trim(db_escape_string($_REQUEST['login']));
77
78 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
79 LOWER(login) = LOWER('$login')");
133ab8c7 80
4f7956b3
AD
81 $is_registered = db_num_rows($result) > 0;
82
83 print "<result>";
84
85 printf("%d", $is_registered);
86
87 print "</result>";
88
89 return;
90 }
91?>
92
93<html>
94<head>
95<title>Create new account</title>
96<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
97<link rel="stylesheet" type="text/css" href="utility.css">
107d0cf3 98<script type="text/javascript" src="js/functions.js"></script>
fe7537b5 99<script type="text/javascript" src="lib/prototype.js"></script>
bd40e88e 100<script type="text/javascript" src="lib/scriptaculous/scriptaculous.js?load=effects,dragdrop,controls"></script>
4f7956b3
AD
101</head>
102
103<script type="text/javascript">
104
105 function checkUsername() {
106
107 try {
108 var f = document.forms['register_form'];
109 var login = f.login.value;
110
111 if (login == "") {
112 new Effect.Highlight(f.login);
113 f.sub_btn.disabled = true;
114 return false;
115 }
116
133ab8c7 117 var query = "register.php?action=check&login=" +
4f7956b3
AD
118 param_escape(login);
119
120 new Ajax.Request(query, {
133ab8c7 121 onComplete: function(transport) {
4f7956b3
AD
122
123 try {
124
125 var reply = transport.responseXML;
126
127 var result = reply.getElementsByTagName('result')[0];
128 var result_code = result.firstChild.nodeValue;
129
130 if (result_code == 0) {
131 new Effect.Highlight(f.login, {startcolor : '#00ff00'});
132 f.sub_btn.disabled = false;
133 } else {
134 new Effect.Highlight(f.login, {startcolor : '#ff0000'});
135 f.sub_btn.disabled = true;
133ab8c7 136 }
4f7956b3
AD
137 } catch (e) {
138 exception_error("checkUsername_callback", e);
139 }
140
141 } });
142
143 } catch (e) {
144 exception_error("checkUsername", e);
145 }
146
147 return false;
148
149 }
150
151 function validateRegForm() {
152 try {
153
154 var f = document.forms['register_form'];
155
156 if (f.login.value.length == 0) {
157 new Effect.Highlight(f.login);
158 return false;
159 }
160
161 if (f.email.value.length == 0) {
162 new Effect.Highlight(f.email);
163 return false;
164 }
165
166 if (f.turing_test.value.length == 0) {
167 new Effect.Highlight(f.turing_test);
168 return false;
169 }
170
171 return true;
172
173 } catch (e) {
174 exception_error("validateRegForm", e);
175 return false;
176 }
177 }
178
179</script>
180
181<body>
182
0ae2bb2a 183<div class="floatingLogo"><img src="images/logo_wide.png"></div>
4f7956b3
AD
184
185<h1><?php echo __("Create new account") ?></h1>
186
187<?php
188 if (!ENABLE_REGISTRATION) {
189 print_error(__("New user registrations are administratively disabled."));
190
2376ad49
AD
191 print "<p><form method=\"GET\" action=\"backend.php\">
192 <input type=\"hidden\" name=\"op\" value=\"logout\">
4f7956b3
AD
193 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
194 </form>";
195 return;
196 }
197?>
198
4f7956b3
AD
199<?php if (REG_MAX_USERS > 0) {
200 $result = db_query($link, "SELECT COUNT(*) AS cu FROM ttrss_users");
201 $num_users = db_fetch_result($result, 0, "cu");
202} ?>
203
0844d6a9 204<?php if (!REG_MAX_USERS || $num_users < REG_MAX_USERS) { ?>
4f7956b3 205
4ca3d8c4
AD
206 <!-- If you have any rules or ToS you'd like to display, enter them here -->
207
208 <?php if (file_exists("templates/register_notice.txt")) {
209 require_once "templates/register_notice.txt";
210 } ?>
211
0844d6a9 212 <?php if (!$action) { ?>
133ab8c7
AD
213
214 <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>
215
4f7956b3
AD
216 <form action="register.php" method="POST" name="register_form">
217 <input type="hidden" name="action" value="do_register">
218 <table>
219 <tr>
220 <td><?php echo __('Desired login:') ?></td><td>
221 <input name="login">
222 </td><td>
223 <input type="submit" value="<?php echo __('Check availability') ?>" onclick='return checkUsername()'>
224 </td></tr>
e331188f 225 <tr><td><?php echo __('Email:') ?></td><td>
4f7956b3
AD
226 <input name="email">
227 </td></tr>
e331188f 228 <tr><td><?php echo __('How much is two plus two:') ?></td><td>
4f7956b3
AD
229 <input name="turing_test"></td></tr>
230 <tr><td colspan="2" align="right">
2a52d96f 231 <input type="submit" name="sub_btn" value="<?php echo __('Submit registration') ?>"
a2998811 232 disabled="disabled" onclick='return validateRegForm()'>
4f7956b3
AD
233 </td></tr>
234 </table>
235 </form>
1da195e2 236
107d0cf3 237 <?php print "<p><form method=\"GET\" action=\"index.php\">
1da195e2
AD
238 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
239 </form>"; ?>
240
0844d6a9 241 <?php } else if ($action == "do_register") { ?>
133ab8c7 242
0844d6a9 243 <?php
4f7956b3
AD
244 $login = mb_strtolower(trim(db_escape_string($_REQUEST["login"])));
245 $email = trim(db_escape_string($_REQUEST["email"]));
246 $test = trim(db_escape_string($_REQUEST["turing_test"]));
133ab8c7 247
4f7956b3 248 if (!$login || !$email || !$test) {
c46a4a05 249 print_error(__("Your registration information is incomplete."));
107d0cf3 250 print "<p><form method=\"GET\" action=\"index.php\">
c46a4a05
AD
251 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
252 </form>";
4f7956b3
AD
253 return;
254 }
133ab8c7 255
4f7956b3 256 if ($test == "four" || $test == "4") {
133ab8c7 257
4f7956b3
AD
258 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
259 login = '$login'");
133ab8c7 260
4f7956b3 261 $is_registered = db_num_rows($result) > 0;
133ab8c7 262
4f7956b3
AD
263 if ($is_registered) {
264 print_error(__('Sorry, this username is already taken.'));
107d0cf3 265 print "<p><form method=\"GET\" action=\"index.php\">
4f7956b3
AD
266 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
267 </form>";
268 } else {
133ab8c7 269
4f7956b3 270 $password = make_password();
133ab8c7 271
4f7956b3 272 $pwd_hash = encrypt_password($password, $login);
133ab8c7
AD
273
274 db_query($link, "INSERT INTO ttrss_users
4f7956b3
AD
275 (login,pwd_hash,access_level,last_login, email, created)
276 VALUES ('$login', '$pwd_hash', 0, null, '$email', NOW())");
133ab8c7
AD
277
278 $result = db_query($link, "SELECT id FROM ttrss_users WHERE
4f7956b3 279 login = '$login' AND pwd_hash = '$pwd_hash'");
133ab8c7 280
4f7956b3
AD
281 if (db_num_rows($result) != 1) {
282 print_error(__('Registration failed.'));
107d0cf3 283 print "<p><form method=\"GET\" action=\"index.php\">
4f7956b3
AD
284 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
285 </form>";
286 } else {
133ab8c7 287
4f7956b3 288 $new_uid = db_fetch_result($result, 0, "id");
133ab8c7 289
4f7956b3 290 initialize_user($link, $new_uid);
133ab8c7 291
4f7956b3
AD
292 $reg_text = "Hi!\n".
293 "\n".
294 "You are receiving this message, because you (or somebody else) have opened\n".
295 "an account at Tiny Tiny RSS.\n".
296 "\n".
297 "Your login information is as follows:\n".
298 "\n".
299 "Login: $login\n".
300 "Password: $password\n".
301 "\n".
302 "Don't forget to login at least once to your new account, otherwise\n".
303 "it will be deleted in 24 hours.\n".
304 "\n".
305 "If that wasn't you, just ignore this message. Thanks.";
133ab8c7 306
4f7956b3 307 $mail = new PHPMailer();
133ab8c7 308
d134e3a3
AD
309 $mail->PluginDir = "lib/phpmailer/";
310 $mail->SetLanguage("en", "lib/phpmailer/language/");
133ab8c7 311
4f7956b3 312 $mail->CharSet = "UTF-8";
133ab8c7 313
4f7956b3
AD
314 $mail->From = DIGEST_FROM_ADDRESS;
315 $mail->FromName = DIGEST_FROM_NAME;
316 $mail->AddAddress($email);
133ab8c7 317
4f7956b3
AD
318 if (DIGEST_SMTP_HOST) {
319 $mail->Host = DIGEST_SMTP_HOST;
320 $mail->Mailer = "smtp";
321 $mail->Username = DIGEST_SMTP_LOGIN;
322 $mail->Password = DIGEST_SMTP_PASSWORD;
323 }
133ab8c7 324
4f7956b3
AD
325 // $mail->IsHTML(true);
326 $mail->Subject = "Registration information for Tiny Tiny RSS";
327 $mail->Body = $reg_text;
328 // $mail->AltBody = $digest_text;
133ab8c7 329
4f7956b3 330 $rc = $mail->Send();
133ab8c7 331
4f7956b3 332 if (!$rc) print_error($mail->ErrorInfo);
133ab8c7 333
4f7956b3
AD
334 $reg_text = "Hi!\n".
335 "\n".
336 "New user had registered at your Tiny Tiny RSS installation.\n".
337 "\n".
338 "Login: $login\n".
339 "Email: $email\n";
133ab8c7 340
4f7956b3 341 $mail = new PHPMailer();
133ab8c7 342
d134e3a3
AD
343 $mail->PluginDir = "lib/phpmailer/";
344 $mail->SetLanguage("en", "lib/phpmailer/language/");
133ab8c7 345
4f7956b3 346 $mail->CharSet = "UTF-8";
133ab8c7 347
4f7956b3
AD
348 $mail->From = DIGEST_FROM_ADDRESS;
349 $mail->FromName = DIGEST_FROM_NAME;
350 $mail->AddAddress(REG_NOTIFY_ADDRESS);
133ab8c7 351
4f7956b3
AD
352 if (DIGEST_SMTP_HOST) {
353 $mail->Host = DIGEST_SMTP_HOST;
354 $mail->Mailer = "smtp";
355 $mail->Username = DIGEST_SMTP_LOGIN;
356 $mail->Password = DIGEST_SMTP_PASSWORD;
357 }
133ab8c7 358
4f7956b3
AD
359 // $mail->IsHTML(true);
360 $mail->Subject = "Registration notice for Tiny Tiny RSS";
361 $mail->Body = $reg_text;
362 // $mail->AltBody = $digest_text;
133ab8c7 363
4f7956b3 364 $rc = $mail->Send();
133ab8c7 365
4f7956b3 366 print_notice(__("Account created successfully."));
133ab8c7 367
107d0cf3 368 print "<p><form method=\"GET\" action=\"index.php\">
4f7956b3
AD
369 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
370 </form>";
133ab8c7 371
4f7956b3 372 }
133ab8c7 373
4f7956b3 374 }
133ab8c7 375
4f7956b3
AD
376 } else {
377 print_error('Plese check the form again, you have failed the robot test.');
107d0cf3 378 print "<p><form method=\"GET\" action=\"index.php\">
4f7956b3
AD
379 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
380 </form>";
133ab8c7 381
4f7956b3
AD
382 }
383 }
384 ?>
385
0844d6a9 386<?php } else { ?>
4f7956b3
AD
387
388 <?php print_notice(__('New user registrations are currently closed.')) ?>
389
107d0cf3 390 <?php print "<p><form method=\"GET\" action=\"index.php\">
4f7956b3
AD
391 <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
392 </form>"; ?>
393
0844d6a9 394<?php } ?>
4f7956b3
AD
395
396</body>
397</html>
398