123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- <?php
- if (!defined('STATUSNET')) {
-
-
- exit(1);
- }
- class GlobalApiAction extends Action
- {
- var $email;
-
- function prepare($args)
- {
- StatusNet::setApi(true);
- 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) {
-
-
- 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;
- }
- function showError($message, $code=400)
- {
- $this->showOutput(array('error' => $message), $code);
- }
- function showSuccess($values=null, $code=200)
- {
- if (empty($values)) {
- $values = array();
- }
- $values['success'] = 1;
- $this->showOutput($values, $code);
- }
- function showOutput($values, $code)
- {
- if (array_key_exists($code, ClientErrorAction::$status)) {
- $status_string = ClientErrorAction::$status[$code];
- } else if (array_key_exists($code, ServerErrorAction::$status)) {
- $status_string = ServerErrorAction::$status[$code];
- } else {
-
- $code = 500;
- $status_string = ServerErrorAction::$status[$code];
- }
- header('HTTP/1.1 '.$code.' '.$status_string);
- header('Content-Type: application/json; charset=utf-8');
- print(json_encode($values));
- print("\n");
- }
- }
|