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