login.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Login form
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Login
  23. * @package GNUsocial
  24. * @author Evan Prodromou <evan@status.net>
  25. * @author Sarven Capadisli <csarven@status.net>
  26. * @author Mikael Nordfeldth <mmn@hethane.se>
  27. * @copyright 2008-2009 StatusNet, Inc.
  28. * @copyright 2013 Free Software Foundation, Inc.
  29. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  30. * @link http://www.gnu.org/software/social/
  31. */
  32. if (!defined('GNUSOCIAL')) { exit(1); }
  33. class LoginAction extends FormAction
  34. {
  35. protected $needLogin = false;
  36. /**
  37. * Prepare page to run
  38. *
  39. *
  40. * @param $args
  41. * @return string title
  42. */
  43. protected function prepare(array $args=array())
  44. {
  45. // @todo this check should really be in index.php for all sensitive actions
  46. $ssl = common_config('site', 'ssl');
  47. if (empty($_SERVER['HTTPS']) && ($ssl == 'always' || $ssl == 'sometimes')) {
  48. common_redirect(common_local_url('login'));
  49. }
  50. return parent::prepare($args);
  51. }
  52. /**
  53. * Handle input, produce output
  54. *
  55. * Switches on request method; either shows the form or handles its input.
  56. *
  57. * @return void
  58. */
  59. protected function handle()
  60. {
  61. if (common_is_real_login()) {
  62. common_redirect(common_local_url('all', array('nickname' => $this->scoped->nickname)), 307);
  63. }
  64. return parent::handle();
  65. }
  66. /**
  67. * Check the login data
  68. *
  69. * Determines if the login data is valid. If so, logs the user
  70. * in, and redirects to the 'with friends' page, or to the stored
  71. * return-to URL.
  72. *
  73. * @return void
  74. */
  75. protected function handlePost()
  76. {
  77. parent::handlePost();
  78. // XXX: login throttle
  79. $nickname = $this->trimmed('nickname');
  80. $password = $this->arg('password');
  81. $user = common_check_user($nickname, $password);
  82. if (!$user instanceof User) {
  83. // TRANS: Form validation error displayed when trying to log in with incorrect credentials.
  84. throw new ServerException(_('Incorrect username or password.'));
  85. }
  86. // success!
  87. if (!common_set_user($user)) {
  88. // TRANS: Server error displayed when during login a server error occurs.
  89. throw new ServerException(_('Error setting user. You are probably not authorized.'));
  90. }
  91. common_real_login(true);
  92. $this->updateScopedProfile();
  93. if ($this->boolean('rememberme')) {
  94. common_rememberme($user);
  95. }
  96. $url = common_get_returnto();
  97. if ($url) {
  98. // We don't have to return to it again
  99. common_set_returnto(null);
  100. $url = common_inject_session($url);
  101. } else {
  102. $url = common_local_url('all',
  103. array('nickname' => $this->scoped->nickname));
  104. }
  105. common_redirect($url, 303);
  106. }
  107. /**
  108. * Store an error and show the page
  109. *
  110. * This used to show the whole page; now, it's just a wrapper
  111. * that stores the error in an attribute.
  112. *
  113. * @param string $error error, if any.
  114. *
  115. * @return void
  116. */
  117. public function showForm($msg=null, $success=false)
  118. {
  119. common_ensure_session();
  120. return parent::showForm($msg, $success);
  121. }
  122. function showScripts()
  123. {
  124. parent::showScripts();
  125. $this->autofocus('nickname');
  126. }
  127. /**
  128. * Title of the page
  129. *
  130. * @return string title of the page
  131. */
  132. function title()
  133. {
  134. // TRANS: Page title for login page.
  135. return _('Login');
  136. }
  137. /**
  138. * Core of the display code
  139. *
  140. * Shows the login form.
  141. *
  142. * @return void
  143. */
  144. function showContent()
  145. {
  146. $this->elementStart('form', array('method' => 'post',
  147. 'id' => 'form_login',
  148. 'class' => 'form_settings',
  149. 'action' => common_local_url('login')));
  150. $this->elementStart('fieldset');
  151. // TRANS: Form legend on login page.
  152. $this->element('legend', null, _('Login to site'));
  153. $this->elementStart('ul', 'form_data');
  154. $this->elementStart('li');
  155. // TRANS: Field label on login page.
  156. $this->input('nickname', _('Username or email address'));
  157. $this->elementEnd('li');
  158. $this->elementStart('li');
  159. // TRANS: Field label on login page.
  160. $this->password('password', _('Password'));
  161. $this->elementEnd('li');
  162. $this->elementStart('li');
  163. // TRANS: Checkbox label label on login page.
  164. $this->checkbox('rememberme', _('Remember me'), false,
  165. // TRANS: Checkbox title on login page.
  166. _('Automatically login in the future; ' .
  167. 'not for shared computers!'));
  168. $this->elementEnd('li');
  169. $this->elementEnd('ul');
  170. // TRANS: Button text for log in on login page.
  171. $this->submit('submit', _m('BUTTON','Login'));
  172. $this->hidden('token', common_session_token());
  173. $this->elementEnd('fieldset');
  174. $this->elementEnd('form');
  175. $this->elementStart('p');
  176. $this->element('a', array('href' => common_local_url('recoverpassword')),
  177. // TRANS: Link text for link to "reset password" on login page.
  178. _('Lost or forgotten password?'));
  179. $this->elementEnd('p');
  180. }
  181. /**
  182. * Instructions for using the form
  183. *
  184. * For "remembered" logins, we make the user re-login when they
  185. * try to change settings. Different instructions for this case.
  186. *
  187. * @return void
  188. */
  189. function getInstructions()
  190. {
  191. if (common_logged_in() && !common_is_real_login() &&
  192. common_get_returnto()) {
  193. // rememberme logins have to reauthenticate before
  194. // changing any profile settings (cookie-stealing protection)
  195. // TRANS: Form instructions on login page before being able to change user settings.
  196. return _('For security reasons, please re-enter your ' .
  197. 'user name and password ' .
  198. 'before changing your settings.');
  199. } else {
  200. // TRANS: Form instructions on login page.
  201. $prompt = _('Login with your username and password.');
  202. if (!common_config('site', 'closed') && !common_config('site', 'inviteonly')) {
  203. $prompt .= ' ';
  204. // TRANS: Form instructions on login page. This message contains Markdown links in the form [Link text](Link).
  205. // TRANS: %%action.register%% is a link to the registration page.
  206. $prompt .= _('Don\'t have a username yet? ' .
  207. '[Register](%%action.register%%) a new account.');
  208. }
  209. return $prompt;
  210. }
  211. }
  212. /**
  213. * A local menu
  214. *
  215. * Shows different login/register actions.
  216. *
  217. * @return void
  218. */
  219. function showLocalNav()
  220. {
  221. $nav = new LoginGroupNav($this);
  222. $nav->show();
  223. }
  224. function showNoticeForm()
  225. {
  226. }
  227. function showProfileBlock()
  228. {
  229. }
  230. }