register.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. set_include_path(dirname(__FILE__) ."/include" . PATH_SEPARATOR .
  7. get_include_path());
  8. require_once "autoload.php";
  9. require_once "functions.php";
  10. require_once "sessions.php";
  11. require_once "sanity_check.php";
  12. require_once "config.php";
  13. require_once "db.php";
  14. startup_gettext();
  15. $action = $_REQUEST["action"];
  16. if (!init_plugins()) return;
  17. if ($_REQUEST["format"] == "feed") {
  18. header("Content-Type: text/xml");
  19. print '<?xml version="1.0" encoding="utf-8"?>';
  20. print "<feed xmlns=\"http://www.w3.org/2005/Atom\">
  21. <id>".htmlspecialchars(SELF_URL_PATH . "/register.php")."</id>
  22. <title>Tiny Tiny RSS registration slots</title>
  23. <link rel=\"self\" href=\"".htmlspecialchars(SELF_URL_PATH . "/register.php?format=feed")."\"/>
  24. <link rel=\"alternate\" href=\"".htmlspecialchars(SELF_URL_PATH)."\"/>";
  25. if (ENABLE_REGISTRATION) {
  26. $result = db_query( "SELECT COUNT(*) AS cu FROM ttrss_users");
  27. $num_users = db_fetch_result($result, 0, "cu");
  28. $num_users = REG_MAX_USERS - $num_users;
  29. if ($num_users < 0) $num_users = 0;
  30. $reg_suffix = "enabled";
  31. } else {
  32. $num_users = 0;
  33. $reg_suffix = "disabled";
  34. }
  35. print "<entry>
  36. <id>".htmlspecialchars(SELF_URL_PATH)."/register.php?$num_users"."</id>
  37. <link rel=\"alternate\" href=\"".htmlspecialchars(SELF_URL_PATH . "/register.php")."\"/>";
  38. print "<title>$num_users slots are currently available, registration $reg_suffix</title>";
  39. print "<summary>$num_users slots are currently available, registration $reg_suffix</summary>";
  40. print "</entry>";
  41. print "</feed>";
  42. return;
  43. }
  44. /* Remove users which didn't login after receiving their registration information */
  45. if (DB_TYPE == "pgsql") {
  46. db_query( "DELETE FROM ttrss_users WHERE last_login IS NULL
  47. AND created < NOW() - INTERVAL '1 day' AND access_level = 0");
  48. } else {
  49. db_query( "DELETE FROM ttrss_users WHERE last_login IS NULL
  50. AND created < DATE_SUB(NOW(), INTERVAL 1 DAY) AND access_level = 0");
  51. }
  52. if (file_exists("register_expire_do.php")) {
  53. require_once "register_expire_do.php";
  54. }
  55. if ($action == "check") {
  56. header("Content-Type: application/xml");
  57. $login = trim(db_escape_string( $_REQUEST['login']));
  58. $result = db_query( "SELECT id FROM ttrss_users WHERE
  59. LOWER(login) = LOWER('$login')");
  60. $is_registered = db_num_rows($result) > 0;
  61. print "<result>";
  62. printf("%d", $is_registered);
  63. print "</result>";
  64. return;
  65. }
  66. ?>
  67. <html>
  68. <head>
  69. <title>Create new account</title>
  70. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  71. <?php echo stylesheet_tag("css/default.css") ?>
  72. <?php echo javascript_tag("js/common.js") ?>
  73. <?php echo javascript_tag("lib/prototype.js") ?>
  74. <?php echo javascript_tag("lib/scriptaculous/scriptaculous.js?load=effects,controls") ?>
  75. </head>
  76. <script type="text/javascript">
  77. function checkUsername() {
  78. try {
  79. var f = document.forms['register_form'];
  80. var login = f.login.value;
  81. if (login == "") {
  82. new Effect.Highlight(f.login);
  83. f.sub_btn.disabled = true;
  84. return false;
  85. }
  86. var query = "register.php?action=check&login=" +
  87. encodeURIComponent(login);
  88. new Ajax.Request(query, {
  89. onComplete: function(transport) {
  90. try {
  91. var reply = transport.responseXML;
  92. var result = reply.getElementsByTagName('result')[0];
  93. var result_code = result.firstChild.nodeValue;
  94. if (result_code == 0) {
  95. new Effect.Highlight(f.login, {startcolor : '#00ff00'});
  96. f.sub_btn.disabled = false;
  97. } else {
  98. new Effect.Highlight(f.login, {startcolor : '#ff0000'});
  99. f.sub_btn.disabled = true;
  100. }
  101. } catch (e) {
  102. App.Error.report(e);
  103. }
  104. } });
  105. } catch (e) {
  106. App.Error.report(e);
  107. }
  108. return false;
  109. }
  110. function validateRegForm() {
  111. try {
  112. var f = document.forms['register_form'];
  113. if (f.login.value.length == 0) {
  114. new Effect.Highlight(f.login);
  115. return false;
  116. }
  117. if (f.email.value.length == 0) {
  118. new Effect.Highlight(f.email);
  119. return false;
  120. }
  121. if (f.turing_test.value.length == 0) {
  122. new Effect.Highlight(f.turing_test);
  123. return false;
  124. }
  125. return true;
  126. } catch (e) {
  127. alert(e.stack);
  128. return false;
  129. }
  130. }
  131. </script>
  132. <body class="claro ttrss_utility">
  133. <div class="floatingLogo"><img src="images/logo_small.png"></div>
  134. <h1><?php echo __("Create new account") ?></h1>
  135. <div class="content">
  136. <?php
  137. if (!ENABLE_REGISTRATION) {
  138. print_error(__("New user registrations are administratively disabled."));
  139. print "<p><form method=\"GET\" action=\"backend.php\">
  140. <input type=\"hidden\" name=\"op\" value=\"logout\">
  141. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  142. </form>";
  143. return;
  144. }
  145. ?>
  146. <?php if (REG_MAX_USERS > 0) {
  147. $result = db_query( "SELECT COUNT(*) AS cu FROM ttrss_users");
  148. $num_users = db_fetch_result($result, 0, "cu");
  149. } ?>
  150. <?php if (!REG_MAX_USERS || $num_users < REG_MAX_USERS) { ?>
  151. <!-- If you have any rules or ToS you'd like to display, enter them here -->
  152. <?php if (file_exists("templates/register_notice.txt")) {
  153. require_once "templates/register_notice.txt";
  154. } ?>
  155. <?php if (!$action) { ?>
  156. <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>
  157. <form action="register.php" method="POST" name="register_form">
  158. <input type="hidden" name="action" value="do_register">
  159. <table>
  160. <tr>
  161. <td><?php echo __('Desired login:') ?></td><td>
  162. <input name="login" required>
  163. </td><td>
  164. <input type="submit" value="<?php echo __('Check availability') ?>" onclick='return checkUsername()'>
  165. </td></tr>
  166. <tr><td><?php echo __('Email:') ?></td><td>
  167. <input name="email" type="email" required>
  168. </td></tr>
  169. <tr><td><?php echo __('How much is two plus two:') ?></td><td>
  170. <input name="turing_test" required></td></tr>
  171. <tr><td colspan="2" align="right">
  172. <input type="submit" name="sub_btn" value="<?php echo __('Submit registration') ?>"
  173. disabled="disabled" onclick='return validateRegForm()'>
  174. </td></tr>
  175. </table>
  176. </form>
  177. <?php print "<p><form method=\"GET\" action=\"index.php\">
  178. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  179. </form>"; ?>
  180. <?php } else if ($action == "do_register") { ?>
  181. <?php
  182. $login = mb_strtolower(trim(db_escape_string( $_REQUEST["login"])));
  183. $email = trim(db_escape_string( $_REQUEST["email"]));
  184. $test = trim(db_escape_string( $_REQUEST["turing_test"]));
  185. if (!$login || !$email || !$test) {
  186. print_error(__("Your registration information is incomplete."));
  187. print "<p><form method=\"GET\" action=\"index.php\">
  188. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  189. </form>";
  190. return;
  191. }
  192. if ($test == "four" || $test == "4") {
  193. $result = db_query( "SELECT id FROM ttrss_users WHERE
  194. login = '$login'");
  195. $is_registered = db_num_rows($result) > 0;
  196. if ($is_registered) {
  197. print_error(__('Sorry, this username is already taken.'));
  198. print "<p><form method=\"GET\" action=\"index.php\">
  199. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  200. </form>";
  201. } else {
  202. $password = make_password();
  203. $salt = substr(bin2hex(get_random_bytes(125)), 0, 250);
  204. $pwd_hash = encrypt_password($password, $salt, true);
  205. db_query( "INSERT INTO ttrss_users
  206. (login,pwd_hash,access_level,last_login, email, created, salt)
  207. VALUES ('$login', '$pwd_hash', 0, null, '$email', NOW(), '$salt')");
  208. $result = db_query( "SELECT id FROM ttrss_users WHERE
  209. login = '$login' AND pwd_hash = '$pwd_hash'");
  210. if (db_num_rows($result) != 1) {
  211. print_error(__('Registration failed.'));
  212. print "<p><form method=\"GET\" action=\"index.php\">
  213. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  214. </form>";
  215. } else {
  216. $new_uid = db_fetch_result($result, 0, "id");
  217. initialize_user( $new_uid);
  218. $reg_text = "Hi!\n".
  219. "\n".
  220. "You are receiving this message, because you (or somebody else) have opened\n".
  221. "an account at Tiny Tiny RSS.\n".
  222. "\n".
  223. "Your login information is as follows:\n".
  224. "\n".
  225. "Login: $login\n".
  226. "Password: $password\n".
  227. "\n".
  228. "Don't forget to login at least once to your new account, otherwise\n".
  229. "it will be deleted in 24 hours.\n".
  230. "\n".
  231. "If that wasn't you, just ignore this message. Thanks.";
  232. $mailer = new Mailer();
  233. $rc = $mailer->mail(["to_address" => $email,
  234. "subject" => "Registration information for Tiny Tiny RSS",
  235. "message" => $reg_text]);
  236. if (!$rc) print_error($mailer->error());
  237. $reg_text = "Hi!\n".
  238. "\n".
  239. "New user had registered at your Tiny Tiny RSS installation.\n".
  240. "\n".
  241. "Login: $login\n".
  242. "Email: $email\n";
  243. $mailer = new Mailer();
  244. $rc = $mailer->mail(["to_address" => REG_NOTIFY_ADDRESS,
  245. "subject" => "Registration notice for Tiny Tiny RSS",
  246. "message" => $reg_text]);
  247. if (!$rc) print_error($mailer->error());
  248. print_notice(__("Account created successfully."));
  249. print "<p><form method=\"GET\" action=\"index.php\">
  250. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  251. </form>";
  252. }
  253. }
  254. } else {
  255. print_error('Plese check the form again, you have failed the robot test.');
  256. print "<p><form method=\"GET\" action=\"index.php\">
  257. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  258. </form>";
  259. }
  260. }
  261. ?>
  262. <?php } else { ?>
  263. <?php print_notice(__('New user registrations are currently closed.')) ?>
  264. <?php print "<p><form method=\"GET\" action=\"index.php\">
  265. <input type=\"submit\" value=\"".__("Return to Tiny Tiny RSS")."\">
  266. </form>"; ?>
  267. <?php } ?>
  268. </div>
  269. </body>
  270. </html>