2016-09-25 18:50:08 +02:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* 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;
|
2017-12-04 15:04:36 +01:00
|
|
|
|
use Friendica\Content\Feature;
|
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-01-17 19:42:40 +01:00
|
|
|
|
use Friendica\Core\Addon;
|
2017-04-30 06:01:26 +02:00
|
|
|
|
use Friendica\Core\Config;
|
2018-01-21 17:38:01 +01:00
|
|
|
|
use Friendica\Core\L10n;
|
2018-01-25 03:08:45 +01:00
|
|
|
|
use Friendica\Core\NotificationsManager;
|
2017-12-18 20:39:35 +01:00
|
|
|
|
use Friendica\Core\PConfig;
|
2018-01-25 03:08:45 +01:00
|
|
|
|
use Friendica\Core\System;
|
2017-11-05 13:15:53 +01:00
|
|
|
|
use Friendica\Core\Worker;
|
2017-11-08 04:57:46 +01:00
|
|
|
|
use Friendica\Database\DBM;
|
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;
|
2017-12-07 14:56:11 +01:00
|
|
|
|
use Friendica\Model\Photo;
|
2017-11-26 20:46:08 +01:00
|
|
|
|
use Friendica\Model\User;
|
2017-12-05 00:30:18 +01:00
|
|
|
|
use Friendica\Network\FKOAuth1;
|
2017-11-24 05:48:15 +01:00
|
|
|
|
use Friendica\Network\HTTPException;
|
|
|
|
|
use Friendica\Network\HTTPException\BadRequestException;
|
|
|
|
|
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;
|
2017-11-08 14:34:48 +01:00
|
|
|
|
use Friendica\Protocol\Diaspora;
|
2018-01-27 03:38:34 +01:00
|
|
|
|
use Friendica\Util\DateTimeFormat;
|
2018-01-27 05:09:48 +01:00
|
|
|
|
use Friendica\Util\Network;
|
2017-11-10 13:45:33 +01:00
|
|
|
|
use Friendica\Util\XML;
|
2017-01-17 20:21:46 +01:00
|
|
|
|
|
2017-04-05 22:07:55 +02:00
|
|
|
|
require_once 'include/conversation.php';
|
|
|
|
|
require_once 'mod/share.php';
|
|
|
|
|
require_once 'mod/item.php';
|
|
|
|
|
require_once 'include/security.php';
|
|
|
|
|
require_once 'mod/wall_upload.php';
|
|
|
|
|
require_once 'mod/proxy.php';
|
|
|
|
|
|
|
|
|
|
define('API_METHOD_ANY', '*');
|
|
|
|
|
define('API_METHOD_GET', 'GET');
|
|
|
|
|
define('API_METHOD_POST', 'POST,PUT');
|
|
|
|
|
define('API_METHOD_DELETE', 'POST,DELETE');
|
|
|
|
|
|
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
|
|
|
|
/**
|
|
|
|
|
* 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).
|
2017-12-24 03:20:50 +01:00
|
|
|
|
*
|
|
|
|
|
* @brief Auth API user
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*/
|
|
|
|
|
function api_user()
|
|
|
|
|
{
|
|
|
|
|
if (x($_SESSION, 'allow_api')) {
|
|
|
|
|
return local_user();
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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)
|
|
|
|
|
*
|
2017-12-24 03:20:50 +01:00
|
|
|
|
* @brief Get source name from API client
|
|
|
|
|
*
|
2017-11-10 06:00:50 +01:00
|
|
|
|
* @return string
|
|
|
|
|
* Client source name, default to "api" if unset/unknown
|
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
|
if (strpos($_SERVER['HTTP_USER_AGENT'], "Twidere") !== false) {
|
|
|
|
|
return "Twidere";
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
logger("Unrecognized user-agent ".$_SERVER['HTTP_USER_AGENT'], LOGGER_DEBUG);
|
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
|
|
|
|
/**
|
|
|
|
|
* @brief Format date for API
|
|
|
|
|
*
|
|
|
|
|
* @param string $str Source date, as UTC
|
|
|
|
|
* @return string Date in UTC formatted as "D M d H:i:s +0000 Y"
|
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
*
|
2017-12-24 03:20:50 +01:00
|
|
|
|
* @brief Register API endpoint
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*
|
|
|
|
|
* @param string $path API URL path, relative to System::baseUrl()
|
|
|
|
|
* @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
|
|
|
|
|
*
|
2017-12-24 03:20:50 +01:00
|
|
|
|
* @brief Login API user
|
|
|
|
|
*
|
2017-11-10 06:00:50 +01:00
|
|
|
|
* @param object $a App
|
|
|
|
|
* @hook 'authenticate'
|
|
|
|
|
* array $addon_auth
|
|
|
|
|
* 'username' => username from login form
|
|
|
|
|
* 'password' => password from login form
|
|
|
|
|
* 'authenticated' => return status,
|
|
|
|
|
* 'user_record' => return authenticated user record
|
|
|
|
|
* @hook 'logged_in'
|
|
|
|
|
* array $user logged user record
|
|
|
|
|
*/
|
|
|
|
|
function api_login(App $a)
|
|
|
|
|
{
|
2017-12-05 03:10:54 +01:00
|
|
|
|
$oauth1 = new FKOAuth1();
|
2017-11-10 06:00:50 +01:00
|
|
|
|
// login with oauth
|
|
|
|
|
try {
|
2017-12-05 03:10:54 +01:00
|
|
|
|
list($consumer, $token) = $oauth1->verify_request(OAuthRequest::from_request());
|
2017-11-10 06:00:50 +01:00
|
|
|
|
if (!is_null($token)) {
|
2017-12-05 03:10:54 +01:00
|
|
|
|
$oauth1->loginUser($token->uid);
|
2018-01-17 19:42:40 +01:00
|
|
|
|
Addon::callHooks('logged_in', $a->user);
|
2017-11-10 06:00:50 +01:00
|
|
|
|
return;
|
2016-09-25 18:50:08 +02:00
|
|
|
|
}
|
2017-11-10 06:00:50 +01:00
|
|
|
|
echo __FILE__.__LINE__.__FUNCTION__ . "<pre>";
|
|
|
|
|
var_dump($consumer, $token);
|
|
|
|
|
die();
|
|
|
|
|
} catch (Exception $e) {
|
|
|
|
|
logger($e);
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
// workaround for HTTP-auth in CGI mode
|
|
|
|
|
if (x($_SERVER, 'REDIRECT_REMOTE_USER')) {
|
|
|
|
|
$userpass = base64_decode(substr($_SERVER["REDIRECT_REMOTE_USER"], 6)) ;
|
|
|
|
|
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
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
if (!x($_SERVER, 'PHP_AUTH_USER')) {
|
2017-12-24 00:27:45 +01:00
|
|
|
|
logger('API_login: ' . print_r($_SERVER, true), LOGGER_DEBUG);
|
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
|
|
|
|
|
2017-11-10 06:00:50 +01: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-01-17 19:42:40 +01:00
|
|
|
|
Addon::callHooks('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 {
|
2017-11-26 20:46:08 +01:00
|
|
|
|
$user_id = User::authenticate(trim($user), trim($password));
|
2018-04-09 21:34:53 +02:00
|
|
|
|
if ($user_id !== false) {
|
2018-01-10 14:36:02 +01: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-01-13 09:22:40 +01:00
|
|
|
|
if (!DBM::is_result($record)) {
|
2017-11-10 06:00:50 +01:00
|
|
|
|
logger('API_login failure: ' . print_r($_SERVER, true), LOGGER_DEBUG);
|
|
|
|
|
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
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
authenticate_success($record);
|
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-01-17 19:42:40 +01:00
|
|
|
|
Addon::callHooks('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
|
|
|
|
/**
|
|
|
|
|
* API endpoints can define which HTTP method to accept when called.
|
|
|
|
|
* This function check the current HTTP method agains endpoint
|
|
|
|
|
* registered method.
|
|
|
|
|
*
|
2017-12-24 03:20:50 +01:00
|
|
|
|
* @brief Check HTTP method of called API
|
|
|
|
|
*
|
2017-11-10 06:00:50 +01:00
|
|
|
|
* @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
|
|
|
|
}
|
2017-11-10 06:00:50 +01:00
|
|
|
|
return (strpos($method, $_SERVER['REQUEST_METHOD']) !== false);
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/**
|
|
|
|
|
* Authenticate user, call registered API function, set HTTP headers
|
|
|
|
|
*
|
2017-12-24 03:20:50 +01:00
|
|
|
|
* @brief Main API entry point
|
|
|
|
|
*
|
2017-11-10 06:00:50 +01:00
|
|
|
|
* @param object $a App
|
2018-04-09 19:34:02 +02:00
|
|
|
|
* @return string|array API call result
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*/
|
|
|
|
|
function api_call(App $a)
|
|
|
|
|
{
|
|
|
|
|
global $API, $called_api;
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$type = "json";
|
|
|
|
|
if (strpos($a->query_string, ".xml") > 0) {
|
|
|
|
|
$type = "xml";
|
|
|
|
|
}
|
|
|
|
|
if (strpos($a->query_string, ".json") > 0) {
|
2017-04-05 22:07:55 +02:00
|
|
|
|
$type = "json";
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
|
|
|
|
if (strpos($a->query_string, ".rss") > 0) {
|
|
|
|
|
$type = "rss";
|
|
|
|
|
}
|
|
|
|
|
if (strpos($a->query_string, ".atom") > 0) {
|
|
|
|
|
$type = "atom";
|
|
|
|
|
}
|
2017-04-05 22:07:55 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
try {
|
|
|
|
|
foreach ($API as $p => $info) {
|
|
|
|
|
if (strpos($a->query_string, $p) === 0) {
|
|
|
|
|
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);
|
|
|
|
|
//unset($_SERVER['PHP_AUTH_USER']);
|
2017-04-05 22:17:15 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/// @TODO should be "true ==[=] $info['auth']", if you miss only one = character, you assign a variable (only with ==). Let's make all this even.
|
|
|
|
|
if ($info['auth'] === true && api_user() === false) {
|
|
|
|
|
api_login($a);
|
|
|
|
|
}
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
logger('API call for ' . $a->user['username'] . ': ' . $a->query_string);
|
|
|
|
|
logger('API parameters: ' . print_r($_REQUEST, true));
|
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);
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$duration = (float) (microtime(true) - $stamp);
|
|
|
|
|
logger("API call duration: " . round($duration, 2) . "\t" . $a->query_string, LOGGER_DEBUG);
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
if (Config::get("system", "profiler")) {
|
|
|
|
|
$duration = microtime(true)-$a->performance["start"];
|
2016-11-12 14:17:28 +01:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
/// @TODO round() really everywhere?
|
|
|
|
|
logger(
|
|
|
|
|
parse_url($a->query_string, PHP_URL_PATH) . ": " . sprintf(
|
2018-03-01 07:25:39 +01:00
|
|
|
|
"Database: %s/%s, Cache %s/%s, Network: %s, I/O: %s, Other: %s, Total: %s",
|
2016-11-04 23:45:20 +01:00
|
|
|
|
round($a->performance["database"] - $a->performance["database_write"], 3),
|
|
|
|
|
round($a->performance["database_write"], 3),
|
2018-03-01 07:25:39 +01:00
|
|
|
|
round($a->performance["cache"], 3),
|
|
|
|
|
round($a->performance["cache_write"], 3),
|
2016-11-04 23:45:20 +01:00
|
|
|
|
round($a->performance["network"], 2),
|
|
|
|
|
round($a->performance["file"], 2),
|
2018-03-01 07:25:39 +01:00
|
|
|
|
round($duration - ($a->performance["database"]
|
|
|
|
|
+ $a->performance["cache"] + $a->performance["cache_write"]
|
|
|
|
|
+ $a->performance["network"] + $a->performance["file"]), 2),
|
2017-11-10 06:00:50 +01:00
|
|
|
|
round($duration, 2)
|
|
|
|
|
),
|
|
|
|
|
LOGGER_DEBUG
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (Config::get("rendertime", "callstack")) {
|
|
|
|
|
$o = "Database Read:\n";
|
|
|
|
|
foreach ($a->callstack["database"] as $func => $time) {
|
|
|
|
|
$time = round($time, 3);
|
|
|
|
|
if ($time > 0) {
|
|
|
|
|
$o .= $func . ": " . $time . "\n";
|
2016-11-04 23:45:20 +01:00
|
|
|
|
}
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
|
|
|
|
$o .= "\nDatabase Write:\n";
|
|
|
|
|
foreach ($a->callstack["database_write"] as $func => $time) {
|
|
|
|
|
$time = round($time, 3);
|
|
|
|
|
if ($time > 0) {
|
|
|
|
|
$o .= $func . ": " . $time . "\n";
|
2016-11-04 23:45:20 +01:00
|
|
|
|
}
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
2016-11-04 23:45:20 +01:00
|
|
|
|
|
2018-03-01 07:25:39 +01:00
|
|
|
|
$o = "Cache Read:\n";
|
|
|
|
|
foreach ($a->callstack["cache"] as $func => $time) {
|
|
|
|
|
$time = round($time, 3);
|
|
|
|
|
if ($time > 0) {
|
|
|
|
|
$o .= $func . ": " . $time . "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$o .= "\nCache Write:\n";
|
|
|
|
|
foreach ($a->callstack["cache_write"] as $func => $time) {
|
|
|
|
|
$time = round($time, 3);
|
|
|
|
|
if ($time > 0) {
|
|
|
|
|
$o .= $func . ": " . $time . "\n";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$o .= "\nNetwork:\n";
|
|
|
|
|
foreach ($a->callstack["network"] as $func => $time) {
|
|
|
|
|
$time = round($time, 3);
|
|
|
|
|
if ($time > 0) {
|
|
|
|
|
$o .= $func . ": " . $time . "\n";
|
2016-11-04 23:45:20 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-11-10 06:00:50 +01:00
|
|
|
|
logger($o, LOGGER_DEBUG);
|
2016-11-04 23:45:20 +01:00
|
|
|
|
}
|
2017-11-10 06:00:50 +01:00
|
|
|
|
}
|
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");
|
2018-04-09 21:34:53 +02:00
|
|
|
|
$json = json_encode(end($return));
|
2017-12-24 00:27:45 +01:00
|
|
|
|
if (x($_GET, 'callback')) {
|
|
|
|
|
$json = $_GET['callback'] . "(" . $json . ")";
|
|
|
|
|
}
|
2018-01-04 02:54:35 +01:00
|
|
|
|
$return = $json;
|
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
|
|
|
|
|
|
|
|
|
logger('API call not implemented: ' . $a->query_string);
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
} catch (HTTPException $e) {
|
|
|
|
|
header("HTTP/1.1 {$e->httpcode} {$e->httpdesc}");
|
|
|
|
|
return api_error($type, $e);
|
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
|
|
|
|
/**
|
|
|
|
|
* @brief Format API error string
|
|
|
|
|
*
|
|
|
|
|
* @param string $type Return type (xml, json, rss, as)
|
|
|
|
|
* @param object $e HTTPException Error object
|
2018-04-09 19:34:02 +02:00
|
|
|
|
* @return string|array error message formatted as $type
|
2017-11-10 06:00:50 +01:00
|
|
|
|
*/
|
|
|
|
|
function api_error($type, $e)
|
|
|
|
|
{
|
|
|
|
|
$a = get_app();
|
|
|
|
|
|
|
|
|
|
$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,
|
2017-11-10 06:00:50 +01:00
|
|
|
|
"code" => $e->httpcode . " " . $e->httpdesc,
|
2018-01-15 14:05:12 +01:00
|
|
|
|
"request" => $a->query_string];
|
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
|
|
|
|
/**
|
|
|
|
|
* @brief Set values for RSS template
|
|
|
|
|
*
|
|
|
|
|
* @param App $a
|
|
|
|
|
* @param array $arr Array to be passed to template
|
|
|
|
|
* @param array $user_info User info
|
|
|
|
|
* @return array
|
|
|
|
|
* @todo find proper type-hints
|
|
|
|
|
*/
|
|
|
|
|
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'],
|
|
|
|
|
'self' => System::baseUrl() . "/" . $a->query_string,
|
|
|
|
|
'base' => System::baseUrl(),
|
|
|
|
|
'updated' => api_date(null),
|
2018-01-27 03:38:34 +01:00
|
|
|
|
'atom_updated' => DateTimeFormat::utcNow(DateTimeFormat::ATOM),
|
2017-11-10 06:00:50 +01:00
|
|
|
|
'language' => $user_info['language'],
|
|
|
|
|
'logo' => System::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
|
|
|
|
/**
|
|
|
|
|
* @brief Unique contact to contact url.
|
|
|
|
|
*
|
|
|
|
|
* @param int $id Contact id
|
|
|
|
|
* @return bool|string
|
|
|
|
|
* Contact url or False if contact id is unknown
|
|
|
|
|
*/
|
2017-12-17 12:11:28 +01:00
|
|
|
|
function api_unique_id_to_nurl($id)
|
2017-11-10 06:00:50 +01:00
|
|
|
|
{
|
2018-01-15 14:05:12 +01:00
|
|
|
|
$r = dba::selectFirst('contact', ['nurl'], ['uid' => 0, 'id' => $id]);
|
2017-11-10 06:00:50 +01:00
|
|
|
|
|
|
|
|
|
if (DBM::is_result($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
|
|
|
|
/**
|
|
|
|
|
* @brief Get user info array.
|
|
|
|
|
*
|
|
|
|
|
* @param object $a App
|
|
|
|
|
* @param int|string $contact_id Contact ID or URL
|
|
|
|
|
*/
|
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 = "";
|
|
|
|
|
|
|
|
|
|
logger("api_get_user: Fetching user data for user ".$contact_id, LOGGER_DEBUG);
|
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)) {
|
|
|
|
|
$user = dbesc(normalise_link($contact_id));
|
|
|
|
|
$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-04-09 21:34:53 +02:00
|
|
|
|
$user = dbesc(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 == "") {
|
|
|
|
|
throw new BadRequestException("User not found.");
|
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
if (is_null($user) && x($_GET, 'user_id')) {
|
2017-12-17 12:11:28 +01:00
|
|
|
|
$user = dbesc(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 == "") {
|
|
|
|
|
throw new BadRequestException("User 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());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (is_null($user) && x($_GET, 'screen_name')) {
|
|
|
|
|
$user = dbesc($_GET['screen_name']);
|
|
|
|
|
$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
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
if (is_null($user) && x($_GET, 'profileurl')) {
|
|
|
|
|
$user = dbesc(normalise_link($_GET['profileurl']));
|
|
|
|
|
$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
|
|
|
|
|
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);
|
|
|
|
|
list($user, $null) = explode(".", $a->argv[$argid]);
|
|
|
|
|
if (is_numeric($user)) {
|
2018-04-09 21:34:53 +02:00
|
|
|
|
$user = dbesc(api_unique_id_to_nurl(intval($user)));
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2018-04-10 10:46:10 +02:00
|
|
|
|
if ($user != "") {
|
|
|
|
|
$url = $user;
|
|
|
|
|
$extra_query = "AND `contact`.`nurl` = '%s' ";
|
|
|
|
|
if (api_user() !== false) {
|
|
|
|
|
$extra_query .= "AND `contact`.`uid`=" . intval(api_user());
|
|
|
|
|
}
|
2017-04-05 22:07:55 +02:00
|
|
|
|
}
|
2017-11-10 06:00:50 +01:00
|
|
|
|
} else {
|
|
|
|
|
$user = dbesc($user);
|
2016-09-25 18:50:08 +02:00
|
|
|
|
$extra_query = "AND `contact`.`nick` = '%s' ";
|
2017-04-05 22:07:55 +02:00
|
|
|
|
if (api_user() !== false) {
|
2017-11-10 06:00:50 +01:00
|
|
|
|
$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
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
logger("api_get_user: user ".$user, LOGGER_DEBUG);
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
if (!$user) {
|
|
|
|
|
if (api_user() === false) {
|
|
|
|
|
api_login($a);
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
$user = $_SESSION['uid'];
|
|
|
|
|
$extra_query = "AND `contact`.`uid` = %d AND `contact`.`self` ";
|
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
|
|
|
|
logger('api_user: ' . $extra_query . ', user: ' . $user);
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
// user info
|
|
|
|
|
$uinfo = q(
|
|
|
|
|
"SELECT *, `contact`.`id` AS `cid` FROM `contact`
|
|
|
|
|
WHERE 1
|
|
|
|
|
$extra_query",
|
|
|
|
|
$user
|
|
|
|
|
);
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
// Selecting the id by priority, friendica first
|
2018-04-09 21:34:53 +02:00
|
|
|
|
if (is_array($uinfo)) {
|
|
|
|
|
api_best_nickname($uinfo);
|
|
|
|
|
}
|
2016-11-04 23:45:20 +01:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
// if the contact wasn't found, fetch it from the contacts with uid = 0
|
|
|
|
|
if (!DBM::is_result($uinfo)) {
|
2018-01-15 14:05:12 +01:00
|
|
|
|
$r = [];
|
2016-09-25 18:50:08 +02:00
|
|
|
|
|
2017-11-10 06:00:50 +01:00
|
|
|
|
if ($url != "") {
|
|
|
|
|
$r = q("SELECT * FROM `contact` WHERE `uid` = 0 AND `nurl` = '%s' LIMIT 1", dbesc(normalise_link($url)));
|
2016-09-25 18:50:08 +02:00
|
|
|
|
}
|
|
|
|
|
|
|