ApiTokens.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Copyright © 2011 John Du Hart john@johnduhart.me
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * @deprecated since 1.24
  24. * @ingroup API
  25. */
  26. class ApiTokens extends ApiBase {
  27. public function execute() {
  28. $this->addDeprecation(
  29. [ 'apiwarn-deprecation-withreplacement', 'action=tokens', 'action=query&meta=tokens' ],
  30. 'action=tokens'
  31. );
  32. $params = $this->extractRequestParams();
  33. $res = [
  34. ApiResult::META_TYPE => 'assoc',
  35. ];
  36. $types = $this->getTokenTypes();
  37. foreach ( $params['type'] as $type ) {
  38. $val = call_user_func( $types[$type], null, null );
  39. if ( $val === false ) {
  40. $this->addWarning( [ 'apiwarn-tokennotallowed', $type ] );
  41. } else {
  42. $res[$type . 'token'] = $val;
  43. }
  44. }
  45. $this->getResult()->addValue( null, $this->getModuleName(), $res );
  46. }
  47. private function getTokenTypes() {
  48. // If we're in a mode that breaks the same-origin policy, no tokens can
  49. // be obtained
  50. if ( $this->lacksSameOriginSecurity() ) {
  51. return [];
  52. }
  53. static $types = null;
  54. if ( $types ) {
  55. return $types;
  56. }
  57. $types = [ 'patrol' => [ ApiQueryRecentChanges::class, 'getPatrolToken' ] ];
  58. $names = [ 'edit', 'delete', 'protect', 'move', 'block', 'unblock',
  59. 'email', 'import', 'watch', 'options' ];
  60. foreach ( $names as $name ) {
  61. $types[$name] = [ ApiQueryInfo::class, 'get' . ucfirst( $name ) . 'Token' ];
  62. }
  63. Hooks::run( 'ApiTokensGetTokenTypes', [ &$types ] );
  64. // For forwards-compat, copy any token types from ApiQueryTokens that
  65. // we don't already have something for.
  66. $user = $this->getUser();
  67. $request = $this->getRequest();
  68. foreach ( ApiQueryTokens::getTokenTypeSalts() as $name => $salt ) {
  69. if ( !isset( $types[$name] ) ) {
  70. $types[$name] = function () use ( $salt, $user, $request ) {
  71. return ApiQueryTokens::getToken( $user, $request->getSession(), $salt )->toString();
  72. };
  73. }
  74. }
  75. ksort( $types );
  76. return $types;
  77. }
  78. public function isDeprecated() {
  79. return true;
  80. }
  81. public function getAllowedParams() {
  82. return [
  83. 'type' => [
  84. ApiBase::PARAM_DFLT => 'edit',
  85. ApiBase::PARAM_ISMULTI => true,
  86. ApiBase::PARAM_TYPE => array_keys( $this->getTokenTypes() ),
  87. ],
  88. ];
  89. }
  90. protected function getExamplesMessages() {
  91. return [
  92. 'action=tokens'
  93. => 'apihelp-tokens-example-edit',
  94. 'action=tokens&type=email|move'
  95. => 'apihelp-tokens-example-emailmove',
  96. ];
  97. }
  98. }