. /** * An action that requires an API key * * @category DomainStatusNetwork * @package GNUsocial * @author Evan Prodromou * @copyright 2011 StatusNet, Inc. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ defined('GNUSOCIAL') || die(); /** * An action that requires an API key * * @category General * @package GNUsocial * @author Evan Prodromou * @copyright 2011 StatusNet, Inc. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ class GlobalApiAction extends Action { public $email; /** * Check for an API key, and throw an exception if it's not set * * @param array $args URL and POST params * * @return boolean continuation flag */ public function prepare(array $args = []) { GNUsocial::setApi(true); // reduce exception reports to aid in debugging parent::prepare($args); if (!common_config('globalapi', 'enabled')) { throw new ClientException(_('Global API not enabled.'), 403); } $apikey = $this->trimmed('apikey'); if (empty($apikey)) { throw new ClientException(_('No API key.'), 403); } $expected = common_config('globalapi', 'key'); if ($expected != $apikey) { // FIXME: increment a counter by IP address to prevent brute-force // attacks on the key. throw new ClientException(_('Bad API key.'), 403); } $email = common_canonical_email($this->trimmed('email')); if (empty($email)) { throw new ClientException(_('No email address.')); } if (!Validate::email($email, common_config('email', 'check_domain'))) { throw new ClientException(_('Invalid email address.')); } $this->email = $email; return true; } public function showError($message, $code = 400) { $this->showOutput(array('error' => $message), $code); } public function showSuccess($values = null, $code = 200) { if (empty($values)) { $values = array(); } $values['success'] = 1; $this->showOutput($values, $code); } public function showOutput($values, $code) { if ( !array_key_exists($code, ClientErrorAction::$status) && !array_key_exists($code, ServerErrorAction::$status) ) { // bad code! $code = 500; } http_response_code($code); header('Content-Type: application/json; charset=utf-8'); print(json_encode($values)); print("\n"); } }