PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/web/auth/register.php

https://bitbucket.org/yoander/mtrack
PHP | 205 lines | 184 code | 17 blank | 4 comment | 17 complexity | d18ae85f9456af06d85e9e61ba3248b4 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. <?php # vim:ts=2:sw=2:et:
  2. /* For copyright and licensing terms, see the file named LICENSE */
  3. include '../../inc/common.php';
  4. if (MTrackAuth::whoami() != 'anonymous') {
  5. header("Location: $ABSWEB");
  6. exit;
  7. }
  8. function validate_input()
  9. {
  10. $cap = MTrackCaptcha::check('');
  11. if (is_array($cap) && $cap[0] === false) {
  12. return "Captcha validation failed: " . $cap[1];
  13. }
  14. if (empty($_POST['id']) || empty($_POST['email'])
  15. || empty($_POST['fullname']))
  16. {
  17. return 'You must complete all of the fields';
  18. }
  19. if (isset($_POST['password']) || isset($_POST['password2'])) {
  20. if ($_POST['password'] != $_POST['password2']) {
  21. return "Passwords don't match";
  22. }
  23. if (!strlen($_POST['password'])) {
  24. return "Password must not be empty";
  25. }
  26. }
  27. return null;
  28. }
  29. $message = null;
  30. if ($_SERVER['REQUEST_METHOD'] == 'POST' &&
  31. MTrackConfig::get('core', 'allow_self_registration'))
  32. {
  33. $message = validate_input();
  34. if ($message === null) {
  35. $userid = $_POST['id'];
  36. $email = $_POST['email'];
  37. $name = $_POST['fullname'];
  38. /* is the requested id available? */
  39. $user = MTrackUser::loadUser($userid);
  40. if ($user) {
  41. $message = "Your selected user ID is not available";
  42. } else {
  43. $user = new MTrackUser;
  44. $user->userid = $userid;
  45. $user->email = $email;
  46. $user->fullname = $name;
  47. $user->active = true;
  48. $reg = $_SESSION['mtrack.auth.register'];
  49. if (isset($reg['alias'])) {
  50. // verify that alias doesn't already exist!
  51. $alias = MTrackUser::loadUser($reg['alias'], true);
  52. if (!$alias) {
  53. // We need to do this manually, as the User object save
  54. // method checks our rights, and we don't have any right now.
  55. MTrackDB::q('insert into useraliases (userid, alias) values (?, ?)',
  56. $userid, $reg['alias']);
  57. }
  58. }
  59. $CS = MTrackChangeset::begin("user:$user->userid", "registered");
  60. $user->save($CS);
  61. if (isset($_POST['password']) && strlen($_POST['password'])) {
  62. $user->setPassword($_POST['password']);
  63. }
  64. $CS->commit();
  65. /* now; we are logged in as this user; gate into mtrack auth */
  66. $_SESSION['auth.mtrack'] = $user->userid;
  67. unset($_SESSION['mtrack.auth.register']);
  68. header("Location: {$ABSWEB}user.php?user=$userid&edit=1");
  69. exit;
  70. }
  71. }
  72. }
  73. if (!MTrackConfig::get('core', 'allow_self_registration')) {
  74. mtrack_head("Registration Denied");
  75. echo <<<HTML
  76. <h1>Registration Denied</h1>
  77. <p>
  78. Thanks for visiting, but the settings at this site don't allow
  79. the public to register for access. If you believe this result
  80. to be in error, contact the site administrator.
  81. </p>
  82. HTML;
  83. mtrack_foot();
  84. exit;
  85. }
  86. mtrack_head('Register');
  87. if (isset($_POST['id'])) {
  88. $userid = htmlentities($_POST['id'], ENT_QUOTES, 'utf-8');
  89. } else {
  90. $userid = null;
  91. }
  92. if (isset($_POST['email'])) {
  93. $email = htmlentities($_POST['email'], ENT_QUOTES, 'utf-8');
  94. } else {
  95. $email = null;
  96. }
  97. if (isset($_POST['fullname'])) {
  98. $fullname = htmlentities($_POST['fullname'], ENT_QUOTES, 'utf-8');
  99. } else {
  100. $fullname = null;
  101. }
  102. if (isset($_SESSION['mtrack.auth.register'])) {
  103. $reg = $_SESSION['mtrack.auth.register'];
  104. if (!$userid) {
  105. $userid = htmlentities($reg['login'], ENT_QUOTES, 'utf-8');
  106. }
  107. if (!$email) {
  108. $email = htmlentities($reg['email'], ENT_QUOTES, 'utf-8');
  109. }
  110. if (!$fullname) {
  111. $fullname = htmlentities($reg['name'], ENT_QUOTES, 'utf-8');
  112. }
  113. } else {
  114. $reg = null;
  115. }
  116. if ($message) {
  117. $message = htmlentities($message, ENT_QUOTES, 'utf-8');
  118. echo <<<HTML
  119. <div class='alert alert-danger'>
  120. <a class='close' data-dismiss='alert'>&times;</a>
  121. $message
  122. </div>
  123. HTML;
  124. }
  125. echo <<<HTML
  126. <h1>Register your local account</h1>
  127. <p>
  128. Please fill out this short form so that we can complete your
  129. login. The User ID and Full Name you select below will be how your name
  130. appears on the site, and the email address will be used to
  131. send you notifications.
  132. </p>
  133. <br>
  134. <form method='post'>
  135. <table>
  136. <tr>
  137. <td>User ID</td>
  138. <td><input type='text' name='id' value='$userid'>
  139. <em>Once selected, it cannot be changed; choose wisely!</em>
  140. </td>
  141. </tr>
  142. <tr>
  143. <td>Full Name</td>
  144. <td><input type='text' name='fullname' value='$fullname'></td>
  145. </tr>
  146. <tr>
  147. <td>Email</td>
  148. <td><input type='text' name='email' value='$email'></td>
  149. </tr>
  150. HTML;
  151. // We only strictly need to show the password box for users
  152. // that didn't come in via an external authentication mechanism
  153. if (!$reg) {
  154. echo <<<HTML
  155. <tr>
  156. <td>Password</td>
  157. <td><input type='password' name='password'
  158. placeholder="Choose a password"><br>
  159. <input type='password' name='password2'
  160. placeholder="Confirm that password">
  161. </td>
  162. </tr>
  163. HTML;
  164. }
  165. echo "</table>";
  166. echo MTrackCaptcha::emit('');
  167. echo <<<HTML
  168. <button type='submit' class='btn btn-primary'>Save</button>
  169. </form>
  170. HTML;
  171. mtrack_foot();