UserNotLoggedIn.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. */
  20. /**
  21. * Redirect a user to the login page
  22. *
  23. * This is essentially an ErrorPageError exception which by default uses the
  24. * 'exception-nologin' as a title and 'exception-nologin-text' for the message.
  25. *
  26. * @note In order for this exception to redirect, the error message passed to the
  27. * constructor has to be explicitly added to LoginHelper::validErrorMessages or with
  28. * the LoginFormValidErrorMessages hook. Otherwise, the user will just be shown the message
  29. * rather than redirected.
  30. *
  31. * @par Example:
  32. * @code
  33. * if( $user->isAnon() ) {
  34. * throw new UserNotLoggedIn();
  35. * }
  36. * @endcode
  37. *
  38. * Note the parameter order differs from ErrorPageError, this allows you to
  39. * simply specify a reason without overriding the default title.
  40. *
  41. * @par Example:
  42. * @code
  43. * if( $user->isAnon() ) {
  44. * throw new UserNotLoggedIn( 'action-require-loggedin' );
  45. * }
  46. * @endcode
  47. *
  48. * @see T39627
  49. * @since 1.20
  50. * @ingroup Exception
  51. */
  52. class UserNotLoggedIn extends ErrorPageError {
  53. /**
  54. * @note The value of the $reasonMsg parameter must be set with the LoginFormValidErrorMessages
  55. * hook if you want the user to be automatically redirected to the login form.
  56. *
  57. * @param string $reasonMsg A message key containing the reason for the error.
  58. * Optional, default: 'exception-nologin-text'
  59. * @param string $titleMsg A message key to set the page title.
  60. * Optional, default: 'exception-nologin'
  61. * @param array $params Parameters to wfMessage().
  62. * Optional, default: []
  63. */
  64. public function __construct(
  65. $reasonMsg = 'exception-nologin-text',
  66. $titleMsg = 'exception-nologin',
  67. $params = []
  68. ) {
  69. parent::__construct( $titleMsg, $reasonMsg, $params );
  70. }
  71. /**
  72. * Redirect to Special:Userlogin if the specified message is compatible. Otherwise,
  73. * show an error page as usual.
  74. */
  75. public function report( $action = self::SEND_OUTPUT ) {
  76. // If an unsupported message is used, don't try redirecting to Special:Userlogin,
  77. // since the message may not be compatible.
  78. if ( !in_array( $this->msg, LoginHelper::getValidErrorMessages() ) ) {
  79. parent::report( $action );
  80. return;
  81. }
  82. // Message is valid. Redirect to Special:Userlogin
  83. $context = RequestContext::getMain();
  84. $output = $context->getOutput();
  85. $query = $context->getRequest()->getValues();
  86. // Title will be overridden by returnto
  87. unset( $query['title'] );
  88. // Redirect to Special:Userlogin
  89. $output->redirect( SpecialPage::getTitleFor( 'Userlogin' )->getFullURL( [
  90. // Return to this page when the user logs in
  91. 'returnto' => $context->getTitle()->getFullText(),
  92. 'returntoquery' => wfArrayToCgi( $query ),
  93. 'warning' => $this->msg,
  94. ] ) );
  95. if ( $action === self::SEND_OUTPUT ) {
  96. $output->output();
  97. }
  98. }
  99. }