register.php 10.0 KB

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