LoginController.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. class LoginController extends Zend_Controller_Action
  3. {
  4. public function init()
  5. {
  6. }
  7. public function indexAction()
  8. {
  9. $CC_CONFIG = Config::getConfig();
  10. $request = $this->getRequest();
  11. $stationLocale = Application_Model_Preference::GetDefaultLocale();
  12. Application_Model_Locale::configureLocalization($request->getcookie('airtime_locale', $stationLocale));
  13. $auth = Zend_Auth::getInstance();
  14. if ($auth->hasIdentity()) {
  15. $this->_redirect('Showbuilder');
  16. }
  17. //uses separate layout without a navigation.
  18. $this->_helper->layout->setLayout('login');
  19. $error = false;
  20. $baseUrl = Application_Common_OsPath::getBaseDir();
  21. $this->view->headScript()->appendFile($baseUrl.'js/airtime/login/login.js?'.$CC_CONFIG['airtime_version'],'text/javascript');
  22. $form = new Application_Form_Login();
  23. $message = _("Please enter your user name and password");
  24. if ($request->isPost()) {
  25. // if the post contains recaptcha field, which means form had recaptcha field.
  26. // Hence add the element for validation.
  27. if (array_key_exists('recaptcha_response_field', $request->getPost())) {
  28. $form->addRecaptcha();
  29. }
  30. if ($form->isValid($request->getPost())) {
  31. //get the username and password from the form
  32. $username = $form->getValue('username');
  33. $password = $form->getValue('password');
  34. $locale = $form->getValue('locale');
  35. if (Application_Model_Subjects::getLoginAttempts($username) >= 3 && $form->getElement('captcha') == NULL) {
  36. $form->addRecaptcha();
  37. } else {
  38. $authAdapter = Application_Model_Auth::getAuthAdapter();
  39. //pass to the adapter the submitted username and password
  40. $authAdapter->setIdentity($username)
  41. ->setCredential($password);
  42. $result = $auth->authenticate($authAdapter);
  43. if ($result->isValid()) {
  44. // Regenerate session id on login to prevent session fixation.
  45. Zend_Session::regenerateId();
  46. //all info about this user from the login table omit only the password
  47. $userInfo = $authAdapter->getResultRowObject(null, 'password');
  48. //the default storage is a session with namespace Zend_Auth
  49. $authStorage = $auth->getStorage();
  50. $authStorage->write($userInfo);
  51. Application_Model_LoginAttempts::resetAttempts($_SERVER['REMOTE_ADDR']);
  52. Application_Model_Subjects::resetLoginAttempts($username);
  53. //set the user locale in case user changed it in when logging in
  54. Application_Model_Preference::SetUserLocale($locale);
  55. $this->_redirect('Showbuilder');
  56. } else {
  57. $message = _("Wrong username or password provided. Please try again.");
  58. Application_Model_Subjects::increaseLoginAttempts($username);
  59. Application_Model_LoginAttempts::increaseAttempts($_SERVER['REMOTE_ADDR']);
  60. $form = new Application_Form_Login();
  61. $error = true;
  62. }
  63. }
  64. }
  65. }
  66. $this->view->message = $message;
  67. $this->view->error = $error;
  68. $this->view->form = $form;
  69. $this->view->airtimeVersion = Application_Model_Preference::GetAirtimeVersion();
  70. $this->view->airtimeCopyright = AIRTIME_COPYRIGHT_DATE;
  71. if (isset($CC_CONFIG['demo'])) {
  72. $this->view->demo = $CC_CONFIG['demo'];
  73. }
  74. }
  75. public function logoutAction()
  76. {
  77. $auth = Zend_Auth::getInstance();
  78. $auth->clearIdentity();
  79. $this->_redirect('showbuilder/index');
  80. }
  81. public function passwordRestoreAction()
  82. {
  83. $CC_CONFIG = Config::getConfig();
  84. $baseUrl = Application_Common_OsPath::getBaseDir();
  85. $this->view->headScript()->appendFile($baseUrl.'js/airtime/login/password-restore.js?'.$CC_CONFIG['airtime_version'],'text/javascript');
  86. $request = $this->getRequest();
  87. $stationLocale = Application_Model_Preference::GetDefaultLocale();
  88. Application_Model_Locale::configureLocalization($request->getcookie('airtime_locale', $stationLocale));
  89. if (!Application_Model_Preference::GetEnableSystemEmail()) {
  90. $this->_redirect('login');
  91. } else {
  92. //uses separate layout without a navigation.
  93. $this->_helper->layout->setLayout('login');
  94. $form = new Application_Form_PasswordRestore();
  95. $request = $this->getRequest();
  96. if ($request->isPost() && $form->isValid($request->getPost())) {
  97. if (is_null($form->username->getValue()) || $form->username->getValue() == '') {
  98. $user = CcSubjsQuery::create()
  99. ->filterByDbEmail($form->email->getValue())
  100. ->findOne();
  101. } else {
  102. $user = CcSubjsQuery::create()
  103. ->filterByDbEmail($form->email->getValue())
  104. ->filterByDbLogin($form->username->getValue())
  105. ->findOne();
  106. }
  107. if (!empty($user)) {
  108. $auth = new Application_Model_Auth();
  109. $success = $auth->sendPasswordRestoreLink($user, $this->view);
  110. if ($success) {
  111. $this->_helper->redirector('password-restore-after', 'login');
  112. } else {
  113. $form->email->addError($this->view->translate(_("Email could not be sent. Check your mail server settings and ensure it has been configured properly.")));
  114. }
  115. } else {
  116. $form->email->addError($this->view->translate(_("Given email not found.")));
  117. }
  118. }
  119. $this->view->form = $form;
  120. }
  121. }
  122. public function passwordRestoreAfterAction()
  123. {
  124. $request = $this->getRequest();
  125. $stationLocale = Application_Model_Preference::GetDefaultLocale();
  126. Application_Model_Locale::configureLocalization($request->getcookie('airtime_locale', $stationLocale));
  127. //uses separate layout without a navigation.
  128. $this->_helper->layout->setLayout('login');
  129. }
  130. public function passwordChangeAction()
  131. {
  132. //uses separate layout without a navigation.
  133. $this->_helper->layout->setLayout('login');
  134. $request = $this->getRequest();
  135. $token = $request->getParam("token", false);
  136. $user_id = $request->getParam("user_id", 0);
  137. $form = new Application_Form_PasswordChange();
  138. $auth = new Application_Model_Auth();
  139. $user = CcSubjsQuery::create()->findPK($user_id);
  140. $stationLocale = Application_Model_Preference::GetDefaultLocale();
  141. Application_Model_Locale::configureLocalization($request->getcookie('airtime_locale', $stationLocale));
  142. //check validity of token
  143. if (!$auth->checkToken($user_id, $token, 'password.restore')) {
  144. Logging::debug("token not valid");
  145. $this->_helper->redirector('index', 'login');
  146. }
  147. if ($request->isPost() && $form->isValid($request->getPost())) {
  148. $user->setDbPass(md5($form->password->getValue()));
  149. $user->save();
  150. $auth->invalidateTokens($user, 'password.restore');
  151. $zend_auth = Zend_Auth::getInstance();
  152. $zend_auth->clearIdentity();
  153. $authAdapter = Application_Model_Auth::getAuthAdapter();
  154. $authAdapter->setIdentity($user->getDbLogin())
  155. ->setCredential($form->password->getValue());
  156. $zend_auth->authenticate($authAdapter);
  157. //all info about this user from the login table omit only the password
  158. $userInfo = $authAdapter->getResultRowObject(null, 'password');
  159. //the default storage is a session with namespace Zend_Auth
  160. $authStorage = $zend_auth->getStorage();
  161. $authStorage->write($userInfo);
  162. $this->_helper->redirector('index', 'showbuilder');
  163. }
  164. $this->view->form = $form;
  165. }
  166. }