globalapi.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * An action that requires an API key
  18. *
  19. * @category DomainStatusNetwork
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @copyright 2011 StatusNet, Inc.
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * An action that requires an API key
  28. *
  29. * @category General
  30. * @package GNUsocial
  31. * @author Evan Prodromou <evan@status.net>
  32. * @copyright 2011 StatusNet, Inc.
  33. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  34. */
  35. class GlobalApiAction extends Action
  36. {
  37. public $email;
  38. /**
  39. * Check for an API key, and throw an exception if it's not set
  40. *
  41. * @param array $args URL and POST params
  42. *
  43. * @return boolean continuation flag
  44. */
  45. public function prepare(array $args = [])
  46. {
  47. GNUsocial::setApi(true); // reduce exception reports to aid in debugging
  48. parent::prepare($args);
  49. if (!common_config('globalapi', 'enabled')) {
  50. throw new ClientException(_('Global API not enabled.'), 403);
  51. }
  52. $apikey = $this->trimmed('apikey');
  53. if (empty($apikey)) {
  54. throw new ClientException(_('No API key.'), 403);
  55. }
  56. $expected = common_config('globalapi', 'key');
  57. if ($expected != $apikey) {
  58. // FIXME: increment a counter by IP address to prevent brute-force
  59. // attacks on the key.
  60. throw new ClientException(_('Bad API key.'), 403);
  61. }
  62. $email = common_canonical_email($this->trimmed('email'));
  63. if (empty($email)) {
  64. throw new ClientException(_('No email address.'));
  65. }
  66. if (!Validate::email($email, common_config('email', 'check_domain'))) {
  67. throw new ClientException(_('Invalid email address.'));
  68. }
  69. $this->email = $email;
  70. return true;
  71. }
  72. public function showError($message, $code = 400)
  73. {
  74. $this->showOutput(array('error' => $message), $code);
  75. }
  76. public function showSuccess($values = null, $code = 200)
  77. {
  78. if (empty($values)) {
  79. $values = array();
  80. }
  81. $values['success'] = 1;
  82. $this->showOutput($values, $code);
  83. }
  84. public function showOutput($values, $code)
  85. {
  86. if (
  87. !array_key_exists($code, ClientErrorAction::$status)
  88. && !array_key_exists($code, ServerErrorAction::$status)
  89. ) {
  90. // bad code!
  91. $code = 500;
  92. }
  93. http_response_code($code);
  94. header('Content-Type: application/json; charset=utf-8');
  95. print(json_encode($values));
  96. print("\n");
  97. }
  98. }