2016-09-25 18:50:08 +02:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
2020-02-09 16:34:23 +01:00
|
|
|
|
* @copyright Copyright (C) 2020, Friendica
|
|
|
|
|
*
|
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
2016-09-25 18:50:08 +02:00
|
|
|
|
* Friendica implementation of statusnet/twitter API
|
|
|
|
|
*
|
2017-12-24 03:20:50 +01:00
|
|
|
|
* @file include/api.php
|
2016-09-25 18:50:08 +02:00
|
|
|
|
* @todo Automatically detect if incoming data is HTML or BBCode
|
|
|
|
|
*/
|
2018-01-25 03:08:45 +01:00
|
|
|
|
|
2017-04-30 06:07:00 +02:00
|
|
|
|
use Friendica\App;
|
2018-01-10 04:42:04 +01:00
|
|
|
|
use Friendica\Content\ContactSelector;
|
2018-01-27 02:01:32 +01:00
|
|
|
|
use Friendica\Content\Text\BBCode;
|
2018-03-07 22:24:13 +01:00
|
|
|
|
use Friendica\Content\Text\HTML;
|
2018-12-26 07:06:24 +01:00
|
|
|
|
use Friendica\Core\Hook;
|
2018-10-29 22:20:46 +01:00
|
|
|
|
use Friendica\Core\Logger;
|
2018-07-01 20:42:38 +02:00
|
|
|
|
use Friendica\Core\Protocol;
|
2019-05-13 06:55:26 +02:00
|
|
|
|
use Friendica\Core\Session;
|
2018-01-25 03:08:45 +01:00
|
|
|
|
use Friendica\Core\System;
|
2017-11-05 13:15:53 +01:00
|
|
|
|
use Friendica\Core\Worker;
|
2018-07-20 14:19:26 +02:00
|
|
|
|
use Friendica\Database\DBA;
|
2019-12-15 23:28:01 +01:00
|
|
|
|
use Friendica\DI;
|
2017-12-07 15:04:24 +01:00
|
|
|
|
use Friendica\Model\Contact;
|
2017-12-09 19:45:17 +01:00
|
|
|
|
use Friendica\Model\Group;
|
2018-01-25 03:08:45 +01:00
|
|
|
|
use Friendica\Model\Item;
|
2018-01-15 18:14:09 +01:00
|
|
|
|
use Friendica\Model\Mail;
|
2020-01-26 20:30:24 +01:00
|
|
|
|
use Friendica\Model\Notify;
|
2017-12-07 14:56:11 +01:00
|
|
|
|
use Friendica\Model\Photo;
|
2021-01-16 05:11:28 +01:00
|
|
|
|
use Friendica\Model\Post;
|
2017-11-26 20:46:08 +01:00
|
|
|
|
use Friendica\Model\User;
|
2020-01-05 20:32:39 +01:00
|
|
|
|
use Friendica\Model\UserItem;
|
2020-05-26 07:18:50 +02:00
|
|
|
|
use Friendica\Model\Verb;
|
2020-09-30 16:49:16 +02:00
|
|
|
|
use Friendica\Security\FKOAuth1;
|
2017-11-24 05:48:15 +01:00
|
|
|
|
use Friendica\Network\HTTPException;
|
|
|
|
|
use Friendica\Network\HTTPException\BadRequestException;
|
2019-01-07 17:25:00 +01:00
|
|
|
|
use Friendica\Network\HTTPException\ExpectationFailedException;
|
2017-11-24 05:48:15 +01:00
|
|
|
|
use Friendica\Network\HTTPException\ForbiddenException;
|
|
|
|
|
use Friendica\Network\HTTPException\InternalServerErrorException;
|
|
|
|
|
use Friendica\Network\HTTPException\MethodNotAllowedException;
|
|
|
|
|
use Friendica\Network\HTTPException\NotFoundException;
|
|
|
|
|
use Friendica\Network\HTTPException\NotImplementedException;
|
|
|
|
|
use Friendica\Network\HTTPException\TooManyRequestsException;
|
2018-01-25 03:08:45 +01:00
|
|
|
|
use Friendica\Network\HTTPException\UnauthorizedException;
|
2017-12-07 14:56:11 +01:00
|
|
|
|
use Friendica\Object\Image;
|
2019-10-24 00:25:43 +02:00
|
|
|
|
use Friendica\Protocol\Activity;
|
2017-11-08 14:34:48 +01:00
|
|
|
|
use Friendica\Protocol\Diaspora;
|
2020-09-30 11:21:58 +02:00
|
|
|
|
use Friendica\Security\OAuth1\OAuthRequest;
|
|
|
|
|
use Friendica\Security\OAuth1\OAuthUtil;
|
2018-01-27 03:38:34 +01:00
|
|
|
|
use Friendica\Util\DateTimeFormat;
|
2019-10-18 03:26:15 +02:00
|
|
|
|
use Friendica\Util\Images;
|
2018-01-27 05:09:48 +01:00
|
|
|
|
use Friendica\Util\Network;
|
2018-07-31 04:06:22 +02:00
|
|
|
|
use Friendica\Util\Proxy as ProxyUtils;
|
2018-11-08 14:45:46 +01:00
|
|
|
|
use Friendica\Util\Strings;
|
2017-11-10 13:45:33 +01:00
|
|
|
|
use Friendica\Util\XML;
|
2017-01-17 20:21:46 +01:00
|
|
|
|
|
2019-09-16 14:47:49 +02:00
|
|
|
|
require_once __DIR__ . '/../mod/item.php';
|
|
|
|
|
require_once __DIR__ . '/../mod/wall_upload.php';
|
2017-04-05 22:07:55 +02:00
|
|
|
|
|
|
|
|
|
define('API_METHOD_ANY', '*');
|
|
|
|
|
define('API_METHOD_GET', 'GET');
|
|
|
|
|
define('API_METHOD_POST', 'POST,PUT');
|
|
|
|
|
define('API_METHOD_DELETE', 'POST,DELETE');
|
|
|
|
|
|
2018-12-30 21:42:56 +01:00
|
|
|
|
define('API_LOG_PREFIX', 'API {action} - ');
|
|
|
|
|
|
2018-01-15 14:05:12 +01:00
|
|
|
|
$API = [];
|
2018-04-24 16:16:57 +02:00
|
|
|
|
$called_api = [];
|
2017-04-05 22:07:55 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
|
* Auth API user
|
|
|
|
|
*
|
2017-11-10 06:00:50 +01:00
|
|
|
|
* It is not sufficient to use local_user() to check whether someone is allowed to use the API,
|
|
|
|
|
* because this will open CSRF holes (just embed an image with src=friendicasite.com/api/statuses/update?status=CSRF
|
|
|
|
|
* into a page, and visitors will post something without noticing it).
|
|
|
|
|
*/
|
|
|
|
|
function api_user()
|
|
|
|
|
{
|
2018-11-30 15:06:22 +01:00
|
|
|
|
if (!empty($_SESSION['allow_api'])) {
|
2017-11-10 06:00:50 +01:00
|
|
|
|
return local_user();
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
|
* Get source name from API client
|
|
|
|
|
*
|
2017-11-10 06:00:50 +01:00
|
|
|
|
* Clients can send 'source' parameter to be show in post metadata
|
|
|
|
|
* as "sent via <source>".
|
|
|
|
|
* Some clients doesn't send a source param, we support ones we know
|
|
|
|
|
* (only Twidere, atm)
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
2019-01-07 18:24:01 +01:00
|
|
|
|
* Client source name, default to "api" if unset/unknown
|
|
|
|
|
* @throws Exception
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*/
|
|
|
|
|
function api_source()
|
|
|
|
|
{
|
|
|
|
|
if (requestdata('source')) {
|
|
|
|
|
return requestdata('source');
|
2016-09-25 18:50:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
// Support for known clients that doesn't send a source name
|
2018-07-01 06:15:11 +02:00
|
|
|
|
if (!empty($_SERVER['HTTP_USER_AGENT'])) {
|
|
|
|
|
if(strpos($_SERVER['HTTP_USER_AGENT'], "Twidere") !== false) {
|
|
|
|
|
return "Twidere";
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2018-12-30 21:42:56 +01:00
|
|
|
|
Logger::info(API_LOG_PREFIX . 'Unrecognized user-agent', ['module' => 'api', 'action' => 'source', 'http_user_agent' => $_SERVER['HTTP_USER_AGENT']]);
|
2018-07-04 23:33:09 +02:00
|
|
|
|
} else {
|
2018-12-30 21:42:56 +01:00
|
|
|
|
Logger::info(API_LOG_PREFIX . 'Empty user-agent', ['module' => 'api', 'action' => 'source']);
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
return "api";
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
|
* Format date for API
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*
|
|
|
|
|
* @param string $str Source date, as UTC
|
|
|
|
|
* @return string Date in UTC formatted as "D M d H:i:s +0000 Y"
|
2019-01-07 18:24:01 +01:00
|
|
|
|
* @throws Exception
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*/
|
|
|
|
|
function api_date($str)
|
|
|
|
|
{
|
|
|
|
|
// Wed May 23 06:01:13 +0000 2007
|
2018-01-27 03:38:34 +01:00
|
|
|
|
return DateTimeFormat::utc($str, "D M d H:i:s +0000 Y");
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2017-04-05 22:07:55 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/**
|
2017-12-24 03:20:50 +01:00
|
|
|
|
* Register a function to be the endpoint for defined API path.
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*
|
2019-12-30 23:00:08 +01:00
|
|
|
|
* @param string $path API URL path, relative to DI::baseUrl()
|
2017-11-10 06:00:50 +01:00
|
|
|
|
* @param string $func Function name to call on path request
|
|
|
|
|
* @param bool $auth API need logged user
|
|
|
|
|
* @param string $method HTTP method reqiured to call this endpoint.
|
|
|
|
|
* One of API_METHOD_ANY, API_METHOD_GET, API_METHOD_POST.
|
|
|
|
|
* Default to API_METHOD_ANY
|
|
|
|
|
*/
|
|
|
|
|
function api_register_func($path, $func, $auth = false, $method = API_METHOD_ANY)
|
|
|
|
|
{
|
|
|
|
|
global $API;
|
|
|
|
|
|
2018-01-15 14:05:12 +01:00
|
|
|
|
$API[$path] = [
|
2017-11-10 06:00:50 +01:00
|
|
|
|
'func' => $func,
|
|
|
|
|
'auth' => $auth,
|
|
|
|
|
'method' => $method,
|
2018-01-15 14:05:12 +01:00
|
|
|
|
];
|
2017-11-10 06:00:50 +01:00
|
|
|
|
|
|
|
|
|
// Workaround for hotot
|
|
|
|
|
$path = str_replace("api/", "api/1.1/", $path);
|
|
|
|
|
|
2018-01-15 14:05:12 +01:00
|
|
|
|
$API[$path] = [
|
2017-11-10 06:00:50 +01:00
|
|
|
|
'func' => $func,
|
|
|
|
|
'auth' => $auth,
|
|
|
|
|
'method' => $method,
|
2018-01-15 14:05:12 +01:00
|
|
|
|
];
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/**
|
|
|
|
|
* Log in user via OAuth1 or Simple HTTP Auth.
|
|
|
|
|
* Simple Auth allow username in form of <pre>user@server</pre>, ignoring server part
|
|
|
|
|
*
|
2019-01-07 18:24:01 +01:00
|
|
|
|
* @param App $a App
|
2019-10-06 17:17:30 +02:00
|
|
|
|
* @throws ForbiddenException
|
2019-01-07 18:24:01 +01:00
|
|
|
|
* @throws InternalServerErrorException
|
|
|
|
|
* @throws UnauthorizedException
|
|
|
|
|
* @hook 'authenticate'
|
|
|
|
|
* array $addon_auth
|
|
|
|
|
* 'username' => username from login form
|
|
|
|
|
* 'password' => password from login form
|
|
|
|
|
* 'authenticated' => return status,
|
|
|
|
|
* 'user_record' => return authenticated user record
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*/
|
|
|
|
|
function api_login(App $a)
|
|
|
|
|
{
|
|
|
|
|
// workaround for HTTP-auth in CGI mode
|
2018-11-30 15:06:22 +01:00
|
|
|
|
if (!empty($_SERVER['REDIRECT_REMOTE_USER'])) {
|
|
|
|
|
$userpass = base64_decode(substr($_SERVER["REDIRECT_REMOTE_USER"], 6));
|
2017-11-10 06:00:50 +01:00
|
|
|
|
if (strlen($userpass)) {
|
|
|
|
|
list($name, $password) = explode(':', $userpass);
|
|
|
|
|
$_SERVER['PHP_AUTH_USER'] = $name;
|
|
|
|
|
$_SERVER['PHP_AUTH_PW'] = $password;
|
2016-09-25 18:50:08 +02:00
|
|
|
|
}
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2018-11-30 15:06:22 +01:00
|
|
|
|
if (empty($_SERVER['PHP_AUTH_USER'])) {
|
2020-03-08 09:30:19 +01:00
|
|
|
|
// Try OAuth when no user is provided
|
|
|
|
|
$oauth1 = new FKOAuth1();
|
|
|
|
|
// login with oauth
|
|
|
|
|
try {
|
|
|
|
|
$request = OAuthRequest::from_request();
|
|
|
|
|
list($consumer, $token) = $oauth1->verify_request($request);
|
|
|
|
|
if (!is_null($token)) {
|
|
|
|
|
$oauth1->loginUser($token->uid);
|
|
|
|
|
Session::set('allow_api', true);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
echo __FILE__.__LINE__.__FUNCTION__ . "<pre>";
|
|
|
|
|
var_dump($consumer, $token);
|
|
|
|
|
die();
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
Logger::warning(API_LOG_PREFIX . 'OAuth error', ['module' => 'api', 'action' => 'login', 'exception' => $e->getMessage()]);
|
|
|
|
|
}
|
|
|
|
|
|
2018-12-30 21:42:56 +01:00
|
|
|
|
Logger::debug(API_LOG_PREFIX . 'failed', ['module' => 'api', 'action' => 'login', 'parameters' => $_SERVER]);
|
2017-11-10 06:00:50 +01:00
|
|
|
|
header('WWW-Authenticate: Basic realm="Friendica"');
|
|
|
|
|
throw new UnauthorizedException("This API requires login");
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2019-10-13 04:01:34 +02:00
|
|
|
|
$user = $_SERVER['PHP_AUTH_USER'] ?? '';
|
|
|
|
|
$password = $_SERVER['PHP_AUTH_PW'] ?? '';
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
// allow "user@server" login (but ignore 'server' part)
|
|
|
|
|
$at = strstr($user, "@", true);
|
|
|
|
|
if ($at) {
|
|
|
|
|
$user = $at;
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
// next code from mod/auth.php. needs better solution
|
|
|
|
|
$record = null;
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2018-01-15 14:05:12 +01:00
|
|
|
|
$addon_auth = [
|
2017-11-10 06:00:50 +01:00
|
|
|
|
'username' => trim($user),
|
|
|
|
|
'password' => trim($password),
|
|
|
|
|
'authenticated' => 0,
|
|
|
|
|
'user_record' => null,
|
2018-01-15 14:05:12 +01:00
|
|
|
|
];
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/*
|
2018-01-17 20:22:38 +01:00
|
|
|
|
* An addon indicates successful login by setting 'authenticated' to non-zero value and returning a user record
|
|
|
|
|
* Addons should never set 'authenticated' except to indicate success - as hooks may be chained
|
|
|
|
|
* and later addons should not interfere with an earlier one that succeeded.
|
|
|
|
|
*/
|
2018-12-26 07:06:24 +01:00
|
|
|
|
Hook::callAll('authenticate', $addon_auth);
|
2017-11-10 06:00:50 +01:00
|
|
|
|
|
2017-12-23 00:10:32 +01:00
|
|
|
|
if ($addon_auth['authenticated'] && count($addon_auth['user_record'])) {
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$record = $addon_auth['user_record'];
|
|
|
|
|
} else {
|
2019-07-22 13:56:36 +02:00
|
|
|
|
$user_id = User::authenticate(trim($user), trim($password), true);
|
2018-04-09 21:34:53 +02:00
|
|
|
|
if ($user_id !== false) {
|
2018-07-20 14:19:26 +02:00
|
|
|
|
$record = DBA::selectFirst('user', [], ['uid' => $user_id]);
|
2016-09-25 18:50:08 +02:00
|
|
|
|
}
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2018-07-21 14:46:04 +02:00
|
|
|
|
if (!DBA::isResult($record)) {
|
2018-12-30 21:42:56 +01:00
|
|
|
|
Logger::debug(API_LOG_PREFIX . 'failed', ['module' => 'api', 'action' => 'login', 'parameters' => $_SERVER]);
|
2017-11-10 06:00:50 +01:00
|
|
|
|
header('WWW-Authenticate: Basic realm="Friendica"');
|
|
|
|
|
//header('HTTP/1.0 401 Unauthorized');
|
|
|
|
|
//die('This api requires login');
|
|
|
|
|
throw new UnauthorizedException("This API requires login");
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2020-06-23 16:15:39 +02:00
|
|
|
|
// Don't refresh the login date more often than twice a day to spare database writes
|
|
|
|
|
$login_refresh = strcmp(DateTimeFormat::utc('now - 12 hours'), $record['login_date']) > 0;
|
|
|
|
|
|
|
|
|
|
DI::auth()->setForUser($a, $record, false, false, $login_refresh);
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$_SESSION["allow_api"] = true;
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2018-12-26 07:06:24 +01:00
|
|
|
|
Hook::callAll('logged_in', $a->user);
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
|
* Check HTTP method of called API
|
|
|
|
|
*
|
2017-11-10 06:00:50 +01:00
|
|
|
|
* API endpoints can define which HTTP method to accept when called.
|
|
|
|
|
* This function check the current HTTP method agains endpoint
|
|
|
|
|
* registered method.
|
|
|
|
|
*
|
|
|
|
|
* @param string $method Required methods, uppercase, separated by comma
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
function api_check_method($method)
|
|
|
|
|
{
|
|
|
|
|
if ($method == "*") {
|
|
|
|
|
return true;
|
2016-09-25 18:50:08 +02:00
|
|
|
|
}
|
2019-10-13 04:01:34 +02:00
|
|
|
|
return (stripos($method, $_SERVER['REQUEST_METHOD'] ?? 'GET') !== false);
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
|
* Main API entry point
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*
|
2020-01-19 07:05:23 +01:00
|
|
|
|
* Authenticate user, call registered API function, set HTTP headers
|
2017-12-24 03:20:50 +01:00
|
|
|
|
*
|
2019-01-07 18:24:01 +01:00
|
|
|
|
* @param App $a App
|
2019-12-16 01:35:26 +01:00
|
|
|
|
* @param App\Arguments $args The app arguments (optional, will retrieved by the DI-Container in case of missing)
|
2018-04-09 19:34:02 +02:00
|
|
|
|
* @return string|array API call result
|
2019-01-07 18:24:01 +01:00
|
|
|
|
* @throws Exception
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*/
|
2019-12-16 01:35:26 +01:00
|
|
|
|
function api_call(App $a, App\Arguments $args = null)
|
2017-11-10 06:00:50 +01:00
|
|
|
|
{
|
|
|
|
|
global $API, $called_api;
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2019-12-16 01:35:26 +01:00
|
|
|
|
if ($args == null) {
|
|
|
|
|
$args = DI::args();
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$type = "json";
|
2020-09-09 06:15:25 +02:00
|
|
|
|
if (strpos($args->getCommand(), ".xml") > 0) {
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$type = "xml";
|
|
|
|
|
}
|
2020-09-09 06:15:25 +02:00
|
|
|
|
if (strpos($args->getCommand(), ".json") > 0) {
|
2017-04-05 22:07:55 +02:00
|
|
|
|
$type = "json";
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2020-09-09 06:15:25 +02:00
|
|
|
|
if (strpos($args->getCommand(), ".rss") > 0) {
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$type = "rss";
|
|
|
|
|
}
|
2020-09-09 06:15:25 +02:00
|
|
|
|
if (strpos($args->getCommand(), ".atom") > 0) {
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$type = "atom";
|
|
|
|
|
}
|
2017-04-05 22:07:55 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
try {
|
|
|
|
|
foreach ($API as $p => $info) {
|
2020-09-09 06:15:25 +02:00
|
|
|
|
if (strpos($args->getCommand(), $p) === 0) {
|
2017-11-10 06:00:50 +01:00
|
|
|
|
if (!api_check_method($info['method'])) {
|
|
|
|
|
throw new MethodNotAllowedException();
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$called_api = explode("/", $p);
|
2017-04-05 22:17:15 +02:00
|
|
|
|
|
2018-07-01 06:15:11 +02:00
|
|
|
|
if (!empty($info['auth']) && api_user() === false) {
|
2017-11-10 06:00:50 +01:00
|
|
|
|
api_login($a);
|
2020-07-09 21:03:48 +02:00
|
|
|
|
Logger::info(API_LOG_PREFIX . 'username {username}', ['module' => 'api', 'action' => 'call', 'username' => $a->user['username']]);
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2018-12-30 21:42:56 +01:00
|
|
|
|
Logger::debug(API_LOG_PREFIX . 'parameters', ['module' => 'api', 'action' => 'call', 'parameters' => $_REQUEST]);
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$stamp = microtime(true);
|
2018-01-04 02:54:35 +01:00
|
|
|
|
$return = call_user_func($info['func'], $type);
|
2019-10-16 14:43:59 +02:00
|
|
|
|
$duration = floatval(microtime(true) - $stamp);
|
2018-12-30 21:42:56 +01:00
|
|
|
|
|
2020-07-09 21:03:48 +02:00
|
|
|
|
Logger::info(API_LOG_PREFIX . 'duration {duration}', ['module' => 'api', 'action' => 'call', 'duration' => round($duration, 2)]);
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2019-12-15 23:50:35 +01:00
|
|
|
|
DI::profiler()->saveLog(DI::logger(), API_LOG_PREFIX . 'performance');
|
2016-11-04 23:45:20 +01:00
|
|
|
|
|
2018-01-04 02:54:35 +01:00
|
|
|
|
if (false === $return) {
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/*
|
|
|
|
|
* api function returned false withour throw an
|
|
|
|
|
* exception. This should not happend, throw a 500
|
|
|
|
|
*/
|
|
|
|
|
throw new InternalServerErrorException();
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
switch ($type) {
|
|
|
|
|
case "xml":
|
|
|
|
|
header("Content-Type: text/xml");
|
|
|
|
|
break;
|
|
|
|
|
case "json":
|
|
|
|
|
header("Content-Type: application/json");
|
2019-08-02 18:43:36 +02:00
|
|
|
|
if (!empty($return)) {
|
|
|
|
|
$json = json_encode(end($return));
|
|
|
|
|
if (!empty($_GET['callback'])) {
|
|
|
|
|
$json = $_GET['callback'] . "(" . $json . ")";
|
|
|
|
|
}
|
|
|
|
|
$return = $json;
|
2017-12-24 00:27:45 +01:00
|
|
|
|
}
|
2017-11-10 06:00:50 +01:00
|
|
|
|
break;
|
|
|
|
|
case "rss":
|
|
|
|
|
header("Content-Type: application/rss+xml");
|
2018-01-04 02:54:35 +01:00
|
|
|
|
$return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
|
2017-11-10 06:00:50 +01:00
|
|
|
|
break;
|
|
|
|
|
case "atom":
|
|
|
|
|
header("Content-Type: application/atom+xml");
|
2018-01-04 02:54:35 +01:00
|
|
|
|
$return = '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $return;
|
2017-11-10 06:00:50 +01:00
|
|
|
|
break;
|
2016-09-25 18:50:08 +02:00
|
|
|
|
}
|
2018-01-04 02:54:35 +01:00
|
|
|
|
return $return;
|
2016-09-25 18:50:08 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-10 06:00:50 +01:00
|
|
|
|
|
2019-12-16 01:30:34 +01:00
|
|
|
|
Logger::warning(API_LOG_PREFIX . 'not implemented', ['module' => 'api', 'action' => 'call', 'query' => DI::args()->getQueryString()]);
|
2020-11-11 21:28:36 +01:00
|
|
|
|
throw new NotFoundException();
|
2017-11-10 06:00:50 +01:00
|
|
|
|
} catch (HTTPException $e) {
|
2019-05-02 19:08:06 +02:00
|
|
|
|
header("HTTP/1.1 {$e->getCode()} {$e->httpdesc}");
|
2019-12-16 01:35:26 +01:00
|
|
|
|
return api_error($type, $e, $args);
|
2016-09-25 18:50:08 +02:00
|
|
|
|
}
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
|
* Format API error string
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*
|
|
|
|
|
* @param string $type Return type (xml, json, rss, as)
|
|
|
|
|
* @param object $e HTTPException Error object
|
2019-12-16 01:35:26 +01:00
|
|
|
|
* @param App\Arguments $args The App arguments
|
2018-04-09 19:34:02 +02:00
|
|
|
|
* @return string|array error message formatted as $type
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*/
|
2019-12-16 01:35:26 +01:00
|
|
|
|
function api_error($type, $e, App\Arguments $args)
|
2017-11-10 06:00:50 +01:00
|
|
|
|
{
|
|
|
|
|
$error = ($e->getMessage() !== "" ? $e->getMessage() : $e->httpdesc);
|
|
|
|
|
/// @TODO: https://dev.twitter.com/overview/api/response-codes
|
|
|
|
|
|
2018-01-15 14:05:12 +01:00
|
|
|
|
$error = ["error" => $error,
|
2019-05-02 19:08:06 +02:00
|
|
|
|
"code" => $e->getCode() . " " . $e->httpdesc,
|
2019-12-16 01:35:26 +01:00
|
|
|
|
"request" => $args->getQueryString()];
|
2017-11-10 06:00:50 +01:00
|
|
|
|
|
2018-01-15 14:05:12 +01:00
|
|
|
|
$return = api_format_data('status', $type, ['status' => $error]);
|
2017-11-10 06:00:50 +01:00
|
|
|
|
|
|
|
|
|
switch ($type) {
|
|
|
|
|
case "xml":
|
|
|
|
|
header("Content-Type: text/xml");
|
|
|
|
|
break;
|
|
|
|
|
case "json":
|
|
|
|
|
header("Content-Type: application/json");
|
2018-01-04 02:54:35 +01:00
|
|
|
|
$return = json_encode($return);
|
2017-11-10 06:00:50 +01:00
|
|
|
|
break;
|
|
|
|
|
case "rss":
|
|
|
|
|
header("Content-Type: application/rss+xml");
|
|
|
|
|
break;
|
|
|
|
|
case "atom":
|
|
|
|
|
header("Content-Type: application/atom+xml");
|
|
|
|
|
break;
|
|
|
|
|
}
|
2018-01-04 02:54:35 +01:00
|
|
|
|
|
|
|
|
|
return $return;
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
|
* Set values for RSS template
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*
|
2019-01-07 18:24:01 +01:00
|
|
|
|
* @param App $a
|
2017-11-10 06:00:50 +01:00
|
|
|
|
* @param array $arr Array to be passed to template
|
|
|
|
|
* @param array $user_info User info
|
|
|
|
|
* @return array
|
2019-01-07 18:24:01 +01:00
|
|
|
|
* @throws BadRequestException
|
|
|
|
|
* @throws ImagickException
|
|
|
|
|
* @throws InternalServerErrorException
|
|
|
|
|
* @throws UnauthorizedException
|
|
|
|
|
* @todo find proper type-hints
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*/
|
|
|
|
|
function api_rss_extra(App $a, $arr, $user_info)
|
|
|
|
|
{
|
|
|
|
|
if (is_null($user_info)) {
|
|
|
|
|
$user_info = api_get_user($a);
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$arr['$user'] = $user_info;
|
2018-01-15 14:05:12 +01:00
|
|
|
|
$arr['$rss'] = [
|
2017-11-10 06:00:50 +01:00
|
|
|
|
'alternate' => $user_info['url'],
|
2019-12-30 23:00:08 +01:00
|
|
|
|
'self' => DI::baseUrl() . "/" . DI::args()->getQueryString(),
|
|
|
|
|
'base' => DI::baseUrl(),
|
2017-11-10 06:00:50 +01:00
|
|
|
|
'updated' => api_date(null),
|
2018-01-27 03:38:34 +01:00
|
|
|
|
'atom_updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
|
2018-07-04 23:33:09 +02:00
|
|
|
|
'language' => $user_info['lang'],
|
2019-12-30 23:00:08 +01:00
|
|
|
|
'logo' => DI::baseUrl() . "/images/friendica-32.png",
|
2018-01-15 14:05:12 +01:00
|
|
|
|
];
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
return $arr;
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
|
* Unique contact to contact url.
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*
|
|
|
|
|
* @param int $id Contact id
|
|
|
|
|
* @return bool|string
|
2019-01-07 18:24:01 +01:00
|
|
|
|
* Contact url or False if contact id is unknown
|
|
|
|
|
* @throws Exception
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*/
|
2017-12-17 12:11:28 +01:00
|
|
|
|
function api_unique_id_to_nurl($id)
|
2017-11-10 06:00:50 +01:00
|
|
|
|
{
|
2018-07-20 14:19:26 +02:00
|
|
|
|
$r = DBA::selectFirst('contact', ['nurl'], ['id' => $id]);
|
2017-11-10 06:00:50 +01:00
|
|
|
|
|
2018-07-21 14:46:04 +02:00
|
|
|
|
if (DBA::isResult($r)) {
|
2017-12-17 12:11:28 +01:00
|
|
|
|
return $r["nurl"];
|
2017-11-10 06:00:50 +01:00
|
|
|
|
} else {
|
|
|
|
|
return false;
|
2016-09-25 18:50:08 +02:00
|
|
|
|
}
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
|
* Get user info array.
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*
|
2019-01-07 18:24:01 +01:00
|
|
|
|
* @param App $a App
|
2017-11-10 06:00:50 +01:00
|
|
|
|
* @param int|string $contact_id Contact ID or URL
|
2019-01-21 17:36:01 +01:00
|
|
|
|
* @return array|bool
|
2019-01-07 18:24:01 +01:00
|
|
|
|
* @throws BadRequestException
|
|
|
|
|
* @throws ImagickException
|
|
|
|
|
* @throws InternalServerErrorException
|
|
|
|
|
* @throws UnauthorizedException
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*/
|
2017-12-16 16:16:25 +01:00
|
|
|
|
function api_get_user(App $a, $contact_id = null)
|
2017-11-10 06:00:50 +01:00
|
|
|
|
{
|
|
|
|
|
global $called_api;
|
2017-04-05 22:07:55 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$user = null;
|
|
|
|
|
$extra_query = "";
|
|
|
|
|
$url = "";
|
|
|
|
|
|
2018-12-30 21:42:56 +01:00
|
|
|
|
Logger::info(API_LOG_PREFIX . 'Fetching data for user {user}', ['module' => 'api', 'action' => 'get_user', 'user' => $contact_id]);
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
// Searching for contact URL
|
|
|
|
|
if (!is_null($contact_id) && (intval($contact_id) == 0)) {
|
2018-11-08 17:28:29 +01:00
|
|
|
|
$user = DBA::escape(Strings::normaliseLink($contact_id));
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$url = $user;
|
|
|
|
|
$extra_query = "AND `contact`.`nurl` = '%s' ";
|
|
|
|
|
if (api_user() !== false) {
|
|
|
|
|
$extra_query .= "AND `contact`.`uid`=" . intval(api_user());
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
// Searching for contact id with uid = 0
|
|
|
|
|
if (!is_null($contact_id) && (intval($contact_id) != 0)) {
|
2018-07-21 15:10:13 +02:00
|
|
|
|
$user = DBA::escape(api_unique_id_to_nurl(intval($contact_id)));
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
if ($user == "") {
|
2018-06-03 11:55:41 +02:00
|
|
|
|
throw new BadRequestException("User ID ".$contact_id." not found.");
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2017-04-05 22:07:55 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$url = $user;
|
|
|
|
|
$extra_query = "AND `contact`.`nurl` = '%s' ";
|
|
|
|
|
if (api_user() !== false) {
|
|
|
|
|
$extra_query .= "AND `contact`.`uid`=" . intval(api_user());
|
2017-05-15 22:11:33 +02:00
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-30 15:06:22 +01:00
|
|
|
|
if (is_null($user) && !empty($_GET['user_id'])) {
|
2018-07-21 15:10:13 +02:00
|
|
|
|
$user = DBA::escape(api_unique_id_to_nurl($_GET['user_id']));
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
if ($user == "") {
|
2018-06-03 11:55:41 +02:00
|
|
|
|
throw new BadRequestException("User ID ".$_GET['user_id']." not found.");
|
2016-09-25 18:50:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$url = $user;
|
|
|
|
|
$extra_query = "AND `contact`.`nurl` = '%s' ";
|
|
|
|
|
if (api_user() !== false) {
|
|
|
|
|
$extra_query .= "AND `contact`.`uid`=" . intval(api_user());
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-30 15:06:22 +01:00
|
|
|
|
if (is_null($user) && !empty($_GET['screen_name'])) {
|
2018-07-21 15:10:13 +02:00
|
|
|
|
$user = DBA::escape($_GET['screen_name']);
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$extra_query = "AND `contact`.`nick` = '%s' ";
|
|
|
|
|
if (api_user() !== false) {
|
|
|
|
|
$extra_query .= "AND `contact`.`uid`=".intval(api_user());
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2018-11-30 15:06:22 +01:00
|
|
|
|
if (is_null($user) && !empty($_GET['profileurl'])) {
|
2018-11-08 17:28:29 +01:00
|
|
|
|
$user = DBA::escape(Strings::normaliseLink($_GET['profileurl']));
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$extra_query = "AND `contact`.`nurl` = '%s' ";
|
|
|
|
|
if (api_user() !== false) {
|
|
|
|
|
$extra_query .= "AND `contact`.`uid`=".intval(api_user());
|
2016-09-25 18:50:08 +02:00
|
|
|
|
}
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2018-07-01 06:16:32 +02:00
|
|
|
|
// $called_api is the API path exploded on / and is expected to have at least 2 elements
|
2017-11-10 06:00:50 +01:00
|
|
|
|
if (is_null($user) && ($a->argc > (count($called_api) - 1)) && (count($called_api) > 0)) {
|
|
|
|
|
$argid = count($called_api);
|
2018-07-08 11:37:05 +02:00
|
|
|
|
if (!empty($a->argv[$argid])) {
|
2018-09-02 10:01:13 +02:00
|
|
|
|
$data = explode(".", $a->argv[$argid]);
|
|
|
|