PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/blog/app/controllers/user.php

http://skeleton.googlecode.com/
PHP | 254 lines | 186 code | 45 blank | 23 comment | 23 complexity | 8bb4d63e28419bbd75c4d8c08b8ced39 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. #include_once 'A/User/Session.php';
  3. #include_once 'A/Model/Form.php';
  4. class user extends A_Controller_Action {
  5. public function login($locator) {
  6. $session = $locator->get('Session');
  7. $user = $locator->get('UserSession');
  8. $session->start(); // controller and view use session
  9. $session->set('foo', 'bar');
  10. $form = new A_Model_Form();
  11. $field = new A_Model_Form_Field('username');
  12. $field->addRule(new A_Rule_Notnull('username', 'Username required'));
  13. $form->addField($field);
  14. $field = new A_Model_Form_Field('password');
  15. $field->addRule(new A_Rule_Notnull('password', 'Password required'));
  16. $form->addField($field);
  17. $errmsg = '';
  18. // If username and password valid and isPost
  19. if($form->isValid($this->request)){
  20. $model = $this->_load('app')->model('users');
  21. $userdata = $model->login($form->get('username'), $form->get('password'), $locator->get('Config')->get('SITESALT'));
  22. if ($userdata) { // user record matching userid and password found
  23. unset($userdata['password']); // don't save passwords in the session
  24. $user->login($userdata);
  25. $this->_redirect($locator->get('Config')->get('BASE') . 'user/login/'); // build redirect URL back to this page
  26. } else {
  27. $errmsg = $model->loginErrorMsg();
  28. }
  29. } elseif($form->isSubmitted()){ // submitted form has errors
  30. $errmsg = $form->getErrorMsg(', ');
  31. }
  32. $template = $this->_load()->template('user/login');
  33. $template->set('errmsg', $errmsg);
  34. $template->set('username', $form->get('username'));
  35. $template->set('user', $user);
  36. $this->response->set('maincontent', $template);
  37. }
  38. public function logout($locator) {
  39. $session = $locator->get('Session');
  40. $user = $locator->get('UserSession');
  41. $session->start();
  42. if ($user->isLoggedIn()) { // user record matching userid and password found
  43. $user->logout();
  44. }
  45. $this->_redirect($locator->get('Config')->get('BASE') . 'user/login/'); // build redirect URL back to this page
  46. }
  47. public function register($locator){
  48. $session = $locator->get('Session');
  49. $user = $locator->get('UserSession');
  50. $session->start();
  51. $request = $this->request;
  52. $messages = array();
  53. if($request->isPost()){
  54. $usermodel = $this->_load('app')->model('users');
  55. $usermodel->addRule(new A_Rule_Match('passwordagain', 'password', 'Fields password and passwordagain do not match'));
  56. $usermodel->addRule(new A_Rule_Regexp('/agree/', 'tos', 'Dont agree with the terms of service?'), 'tos');
  57. // Inlcude only rules for these fields
  58. $usermodel->includeRules(array('username', 'password', 'passwordagain', 'email', 'tos'));
  59. if(!$usermodel->isValid($request))
  60. {
  61. $messages[] = $usermodel->getErrorMsg("</li>\n<li>");
  62. $this->response->setPartial('maincontent', 'user/register/registerForm', array('messages' => $messages));
  63. }
  64. else
  65. {
  66. if($usermodel->isUsernameAvailable($request->get('username')))
  67. {
  68. if($usermodel->isEmailAvailable($request->get('email')))
  69. {
  70. // Create activation key
  71. $actkey = $usermodel->createActivationkey();
  72. // Create a random user salt
  73. $usersalt = uniqid(mt_rand().time(),true);
  74. // Insert user data in db
  75. $usermodel->insertUser( $request->get('username'),
  76. $request->get('password'),
  77. $request->get('email'),
  78. $actkey,
  79. $usersalt,
  80. $locator->get('Config')->get('SITESALT')
  81. );
  82. // Send confirmation email
  83. $activationlink = $locator->get('Config')->get('BASE') . 'user/activate?id=' . $actkey;
  84. $this->mailActivationMessage($request->get('email'), $activationlink);
  85. // Get Template SuccesfulRegistration
  86. $this->response->setPartial('maincontent', 'user/register/success', array( 'email'=>$request->get('email')));
  87. }
  88. else
  89. {
  90. // Another account for this email adress exists, get Template email adress already in database
  91. $this->response->setPartial('maincontent', 'user/register/emailTakenForm');
  92. }
  93. }
  94. else
  95. {
  96. if($usermodel->usernameMatchesEmail($request->get('username'), $request->get('email')))
  97. {
  98. if($usermodel->isAccountActivated($request->get('username'), $request->get('email')))
  99. {
  100. if($usermodel->isPasswordCorrect($request->get('username'), $request->get('password'), $locator->get('Config')->get('SITESALT')))
  101. {
  102. // Login the user
  103. $usermodel->login($request->get('username'), $request->get('password'));
  104. // Get Template you have been logged in
  105. $this->response->setPartial('maincontent', 'user/register/signedin');
  106. }
  107. else
  108. {
  109. // Password was wrong. Get Template LoginForm
  110. $this->response->setPartial('maincontent', 'user/register/loginForm');
  111. }
  112. }
  113. else
  114. {
  115. // Get Template AccountNotYetActivated
  116. $this->response->setPartial('maincontent', 'user/register/activate');
  117. }
  118. }
  119. else
  120. {
  121. // Get Template username already taken
  122. $this->response->setPartial('maincontent', 'user/register/usernameUnavailable',array('username'=> $request->get('username')));
  123. }
  124. }
  125. }
  126. }
  127. else
  128. {
  129. // Show registration form
  130. $this->response->setPartial('maincontent', 'user/register/registerForm');
  131. }
  132. }
  133. private function mailActivationMessage($email, $activationlink){
  134. $subject = 'Registration at this app';
  135. $message = 'Thanks for registering, ' . "\n\r";
  136. $message = 'Please click the following link to activate your account' . "\n\r";
  137. $message .= 'Click this: ' . $activationlink . "\n\r";
  138. $message .= 'Thanks.';
  139. $from = 'From: skeleton blog';
  140. mail($email, $subject, $message, $from);
  141. }
  142. public function activate($locator){
  143. // get the activation key
  144. $activationkey = $this->request->get('id');
  145. $model = $this->_load('app')->model('users');
  146. $model->activate($activationkey);
  147. $this->response->setPartial('maincontent', 'user/activate', array('errmsg' => $model->getErrorMsg(' ')));
  148. }
  149. public function password($locator){
  150. $session = $locator->get('Session');
  151. $user = $locator->get('UserSession');
  152. $session->start();
  153. $errmsg = '';
  154. $form = new A_Model_Form();
  155. $field = new A_Model_Form_Field('username');
  156. $field->addRule(new A_Rule_Notnull('username', 'username required'));
  157. $form->addField($field);
  158. // @todo: should we check in db if filled in username even exists
  159. $model = $this->_load('app')->model('users');
  160. // If password forgot form is posted and is valid
  161. if($form->isValid($this->request)){
  162. // @todo: retrieve email+password from user model and send email with pw
  163. } elseif($form->isSubmitted()){ // submitted form has errors
  164. $errmsg = $form->getErrorMsg(', ');
  165. }
  166. // Show password forgot page and form
  167. $template = $this->_load()->template('user/password');
  168. $template->set('errmsg', $errmsg);
  169. $template->set('user', $user);
  170. $this->response->set('maincontent', $template);
  171. }
  172. public function profile($locator){
  173. $session = $locator->get('Session');
  174. $user = $locator->get('UserSession');
  175. $session->start();
  176. $messages = array();
  177. $request = $this->request;
  178. // If user is not signed in don't show profile page but redirect to login?
  179. if (!$user->isLoggedIn()) {
  180. $this->_redirect($locator->get('Config')->get('BASE') . 'user/login/'); // build redirect URL back to this page
  181. }
  182. // To show the profile we need the model
  183. $model = $this->_load('app')->model('users');
  184. $userdata = $model->find($user->get('id'));
  185. if($request->isPost()){
  186. $model->includeRules(array('firstname', 'lastname', 'email'));
  187. if(!$model->isValid($request)){
  188. $messages[] = $model->getErrorMsg("</li>\n<li>");
  189. $data = array(
  190. 'firstname' => $request->get('firstname'),
  191. 'lastname' => $request->get('lastname'),
  192. 'email' => $request->get('email')
  193. );
  194. $this->response->setPartial('maincontent', 'user/profile', array( 'messages' => $messages, 'data'=>$data ));
  195. } else {
  196. $data = array(
  197. 'firstname' => $model->get('firstname'),
  198. 'lastname' => $model->get('lastname'),
  199. 'email' => $model->get('email')
  200. );
  201. $user_id = $user->get('id');
  202. $model->updateUser($data, $user_id);
  203. $this->response->setPartial('maincontent', 'user/profile', array( 'messages' => $messages, 'data'=>$data ));
  204. }
  205. } else {
  206. $this->response->setPartial('maincontent', 'user/profile', array( 'messages' => $messages, 'data' => $userdata ));
  207. }
  208. }
  209. }