PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/user_controller.php

https://github.com/Fusion/lenses
PHP | 188 lines | 149 code | 28 blank | 11 comment | 30 complexity | e012242eb7b5687f9d66813aee5c1fc3 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * @package Lenses
  4. * @copyright (c) Chris F. Ravenscroft
  5. * @license See 'license.txt'
  6. */
  7. class UserController extends ApplicationController
  8. {
  9. function index()
  10. {
  11. // Piss off
  12. }
  13. function register()
  14. {
  15. global $validator, $db;
  16. import('com.voilaweb.core.security');
  17. $skipOptions = false;
  18. if(isset($_POST['submitform']))
  19. {
  20. $msg = '';
  21. if(!Security::checkCaptcha())
  22. $msg .= 'You did not enter the correct verification words.<br />';
  23. if(empty($_POST['username']))
  24. $msg .= 'You must enter a username<br />';
  25. if(empty($_POST['password']))
  26. $msg .= 'You must enter your password<br />';
  27. if($_POST['password'] != $_POST['confirmpassword'])
  28. $msg .= 'Your confirmation password does not match your password<br />';
  29. if(empty($_POST['email']) || !$validator->validee($_POST['email'])->checkemail()->isok())
  30. $msg .= 'Please enter a valid email address<br />';
  31. if(Config::$settings['users.registration.requireinvite']=='Yes')
  32. {
  33. if(empty($_POST['invite']) || !$validator->validee($_POST['invite'])->checkinvite()->isok())
  34. $msg .= 'Sorry, you need to enter a valid invitation code<br />';
  35. }
  36. try
  37. {
  38. if(!empty($msg))
  39. displayMessage(MESSAGE_ERROR, $msg);
  40. $username = $_POST['username'];
  41. $password = $_POST['password'];
  42. $email = $_POST['email'];
  43. $invite = $_POST['invite'];
  44. $member = new Member();
  45. if($member->find(FIRST, "username='$username'"))
  46. displayMessage(MESSAGE_ERROR, "User $username already exists!");
  47. $member->username = $username;
  48. $member->password = Member::encodePassword($password);
  49. $member->email = $email;
  50. if(!$member->save() || !$member->find(FIRST, "username='$username'"))
  51. displayMessage(MESSAGE_ERROR, "Sorry, there was an unexpected problem while trying to create your account :(");
  52. if(!empty($invite))
  53. $db->query("UPDATE invites SET member_id='{$member->id}',used=1 WHERE code='{$invite}'");
  54. $skipOptions = true;
  55. // We exist, therefore we should be logged in
  56. import('com.voilaweb.core.login');
  57. Login::authenticate($username, $password);
  58. //
  59. redirect('main/index', "Welcome! You should now visit your control panel...");
  60. }
  61. catch(GoodException $e) {}
  62. }
  63. if(!$skipOptions)
  64. {
  65. $this->options['header'] = 'partial_header';
  66. $this->options['footer'] = 'partial_footer';
  67. $this->options['view'] = 'register';
  68. $this->input = array(
  69. 'username' => empty($_POST['username']) ? '' : $_POST['username'],
  70. 'email' => empty($_POST['email']) ? '' : $_POST['email'],
  71. 'invite' => empty($_POST['invite']) ? '' : $_POST['invite'],
  72. );
  73. $this->captcha = Security::presentCaptcha();
  74. }
  75. }
  76. function inviteme()
  77. {
  78. global $validator, $db;
  79. if(isset($_POST['submitform']))
  80. {
  81. try
  82. {
  83. $msg = '';
  84. if(empty($_POST['email']) || !$validator->validee($_POST['email'])->checkemail()->isok())
  85. displayMessage(MESSAGE_ERROR, 'Please enter a valid email address');
  86. $email = $_POST['email'];
  87. $inviteme = new Inviteme();
  88. if(!$inviteme->find(FIRST, "email='$email'"))
  89. displayMessage(MESSAGE_ERROR, "You already requested an invitation. Do not worry, we did not forget!");
  90. $inviteme->email = $email;
  91. $inviteme->save();
  92. $this->options['view'] = 'invited';
  93. }
  94. catch(GoodException $e) {}
  95. }
  96. if(empty($this->options['view']))
  97. {
  98. $this->input = array(
  99. 'email' => empty($_POST['email']) ? '' : $_POST['email'],
  100. );
  101. $this->options['view'] = 'inviteme';
  102. }
  103. }
  104. function login()
  105. {
  106. global $validator, $db;
  107. $skipOptions = false;
  108. if(isset($_POST['submitform']))
  109. {
  110. $msg = '';
  111. if(empty($_POST['username']))
  112. $msg .= 'You must enter a username<br />';
  113. if(empty($_POST['password']))
  114. $msg .= 'You must enter your password<br />';
  115. try
  116. {
  117. if(!empty($msg))
  118. displayMessage(MESSAGE_ERROR, $msg);
  119. import('com.voilaweb.core.login');
  120. if(!Login::authenticate($_POST['username'], $_POST['password']))
  121. displayMessage(MESSAGE_ERROR, "Sorry, this username and password combination is invalid");
  122. $skipOptions = true;
  123. redirect(
  124. (empty($_POST['fwc']) ? 'main' : $_POST['fwc']) .
  125. '/' .
  126. (empty($_POST['fwa']) ? 'index' : $_POST['fwa']),
  127. "Welcome back {$_POST['username']}!");
  128. }
  129. catch(GoodException $e) {}
  130. }
  131. if(!$skipOptions)
  132. {
  133. $this->options['header'] = 'partial_header';
  134. $this->options['footer'] = 'partial_footer';
  135. $this->options['view'] = 'login';
  136. $this->input = array(
  137. 'username' => empty($_POST['username']) ? '' : $_POST['username'],
  138. );
  139. }
  140. }
  141. function logout()
  142. {
  143. import('com.voilaweb.core.login');
  144. Login::logout();
  145. redirect('main/index', "You logged out. Bummer!");
  146. }
  147. //-----------------------------------------------------------------------------
  148. // AJAX Calls
  149. //-----------------------------------------------------------------------------
  150. static function checkUsernameAvailable($username)
  151. {
  152. global $db;
  153. $r = $db->query("SELECT id FROM members WHERE username='$username'");
  154. if($r->numrows()>0)
  155. return false;
  156. return true;
  157. }
  158. }
  159. ajaxExport(UserController, checkUsernameAvailable);
  160. ?>