streams/include/api.php

253 lines
6.1 KiB
PHP
Raw Normal View History

2019-06-13 03:57:28 +00:00
<?php
use Zotlabs\Lib\Api_router;
2013-01-19 14:08:13 +00:00
2015-12-09 22:56:08 +00:00
require_once("include/conversation.php");
require_once("include/oauth.php");
require_once("include/html2plain.php");
2013-01-19 14:08:13 +00:00
require_once('include/security.php');
require_once('include/photos.php');
require_once('include/attach.php');
require_once('include/api_auth.php');
2016-10-11 23:13:52 +00:00
require_once('include/api_zot.php');
2013-01-16 23:51:21 +00:00
/*
*
2019-06-13 03:57:28 +00:00
* Zot API.
*
2011-02-15 11:24:21 +00:00
*/
2013-01-19 14:08:13 +00:00
$API = array();
2019-06-13 03:57:28 +00:00
$called_api = null;
2011-04-21 15:03:31 +00:00
// All commands which require authentication accept a "channel" parameter
// which is the left hand side of the channel address/nickname.
2016-11-09 01:47:56 +00:00
// If provided, the desired channel is selected before carrying out the command.
// If not provided, the default channel associated with the account is used.
// If channel selection fails, the API command requiring login will fail.
2013-01-16 23:51:21 +00:00
function api_user() {
$aid = get_account_id();
2016-03-31 23:06:03 +00:00
$channel = App::get_channel();
2019-06-13 03:57:28 +00:00
if (($aid) && (x($_REQUEST,'channel'))) {
// Only change channel if it is different than the current channel
2019-06-13 03:57:28 +00:00
if ($channel && x($channel,'channel_address') && $channel['channel_address'] != $_REQUEST['channel']) {
$c = q("select channel_id from channel where channel_address = '%s' and channel_account_id = %d limit 1",
dbesc($_REQUEST['channel']),
intval($aid)
);
2019-06-13 03:57:28 +00:00
if ((! $c) || (! change_channel($c[0]['channel_id'])))
return false;
}
}
2019-06-13 03:57:28 +00:00
if ($_SESSION['allow_api']) {
2015-01-29 04:56:04 +00:00
return local_channel();
2019-06-13 03:57:28 +00:00
}
2013-01-16 23:51:21 +00:00
return false;
}
2019-06-13 03:57:28 +00:00
function api_date($str) {
// Wed May 23 06:01:13 +0000 2007
return datetime_convert('UTC', 'UTC', $str, 'D M d H:i:s +0000 Y' );
2011-02-15 11:24:21 +00:00
}
2016-10-12 01:37:47 +00:00
function api_register_func($path, $func, $auth = false) {
2019-06-13 03:57:28 +00:00
Api_router::register($path,$func,$auth);
2011-02-15 11:24:21 +00:00
}
2011-02-15 11:24:21 +00:00
2011-04-21 15:03:31 +00:00
/**************************
* MAIN API ENTRY POINT *
**************************/
2013-01-19 14:08:13 +00:00
2019-06-13 03:57:28 +00:00
function api_call() {
$p = App::$cmd;
$type = null;
2019-06-13 03:57:28 +00:00
if (strrpos($p,'.')) {
$type = substr($p,strrpos($p,'.')+1);
2019-06-13 03:57:28 +00:00
if (strpos($type,'/') === false) {
$p = substr($p,0,strrpos($p,'.'));
// recalculate App argc,argv since we just extracted the type from it
App::$argv = explode('/',$p);
App::$argc = count(App::$argv);
}
2011-02-15 11:24:21 +00:00
}
2019-06-13 03:57:28 +00:00
if ((! $type) || (! in_array($type, [ 'json', 'xml', 'rss', 'as', 'atom' ]))) {
$type = 'json';
2019-06-13 03:57:28 +00:00
}
2019-06-13 03:57:28 +00:00
$info = Api_router::find($p);
2019-06-13 03:57:28 +00:00
if (in_array($type, [ 'rss', 'atom', 'as' ])) {
2016-10-11 23:13:52 +00:00
// These types no longer supported.
$info = false;
}
logger('API info: ' . $p . ' type: ' . $type . ' ' . print_r($info,true), LOGGER_DEBUG,LOG_INFO);
2019-06-13 03:57:28 +00:00
if ($info) {
if ($info['auth'] === true && api_user() === false) {
2019-05-15 05:16:49 +00:00
api_login();
}
load_contact_links(api_user());
$channel = App::get_channel();
logger('API call for ' . $channel['channel_name'] . ': ' . App::$query_string);
logger('API parameters: ' . print_r($_REQUEST,true));
2016-08-12 02:42:44 +00:00
$r = call_user_func($info['func'],$type);
2019-06-13 03:57:28 +00:00
if ($r === false) {
return;
2019-06-13 03:57:28 +00:00
}
2019-06-13 03:57:28 +00:00
switch ($type) {
case 'xml':
header ('Content-Type: text/xml');
2016-10-11 23:13:52 +00:00
return $r;
break;
case 'json':
header ('Content-Type: application/json');
// Lookup JSONP to understand these lines. They provide cross-domain AJAX ability.
2019-06-13 03:57:28 +00:00
if ($_GET['callback']) {
2016-10-11 23:13:52 +00:00
$r = $_GET['callback'] . '(' . $r . ')' ;
2019-06-13 03:57:28 +00:00
}
2016-10-11 23:13:52 +00:00
return $r;
break;
}
2011-08-01 03:01:00 +00:00
}
2018-08-22 06:13:54 +00:00
$x = [ 'path' => App::$query_string ];
call_hooks('api_not_found',$x);
2011-04-21 15:03:31 +00:00
header('HTTP/1.1 404 Not Found');
logger('API call not implemented: ' . App::$query_string . ' - ' . print_r($_REQUEST,true));
$r = '<status><error>not implemented</error></status>';
2019-06-13 03:57:28 +00:00
switch ($type){
case 'xml':
header ('Content-Type: text/xml');
return '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $r;
break;
case "json":
header ('Content-Type: application/json');
return json_encode(array('error' => 'not implemented'));
break;
case "rss":
header ('Content-Type: application/rss+xml');
return '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $r;
break;
case "atom":
header ('Content-Type: application/atom+xml');
return '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . $r;
break;
}
}
/**
* load api $templatename for $type and replace $data array
*/
function api_apply_template($templatename, $type, $data){
2019-06-13 03:57:28 +00:00
switch ($type){
case 'xml':
2019-06-13 03:57:28 +00:00
if ($data) {
foreach ($data as $k => $v)
$ret = arrtoxml(str_replace('$','',$k),$v);
}
break;
case 'json':
default:
2019-06-13 03:57:28 +00:00
if ($data) {
foreach ($data as $rv) {
$ret = json_encode($rv);
}
}
break;
}
return $ret;
}
2016-08-12 02:42:44 +00:00
function api_client_register($type) {
2019-06-13 03:57:28 +00:00
$ret = [];
$key = random_string(16);
$secret = random_string(16);
$name = trim(escape_tags($_REQUEST['application_name']));
2019-06-13 03:57:28 +00:00
if (! $name) {
json_return_and_die($ret);
2019-06-13 03:57:28 +00:00
}
if (is_array($_REQUEST['redirect_uris'])) {
$redirect = trim($_REQUEST['redirect_uris'][0]);
2019-06-13 03:57:28 +00:00
}
else {
$redirect = trim($_REQUEST['redirect_uris']);
2019-06-13 03:57:28 +00:00
}
$grant_types = trim($_REQUEST['grant_types']);
$scope = trim($_REQUEST['scope']);
$icon = trim($_REQUEST['logo_uri']);
2019-08-05 00:30:07 +00:00
$r = q("INSERT INTO oauth_clients (client_id, client_secret, redirect_uri, grant_types, scope, user_id, client_name)
VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) ",
dbesc($key),
dbesc($secret),
dbesc($redirect),
dbesc($grant_types),
dbesc($scope),
dbesc((string) api_user())
);
$ret['client_id'] = $key;
$ret['client_secret'] = $secret;
$ret['expires_at'] = 0;
json_return_and_die($ret);
}
2019-06-13 03:57:28 +00:00
function api_oauth_request_token($type) {
try {
$oauth = new ZotOAuth1();
$req = OAuth1Request::from_request();
logger('Req: ' . var_export($req,true),LOGGER_DATA);
$r = $oauth->fetch_request_token($req);
2019-06-13 03:57:28 +00:00
}
catch(Exception $e) {
logger('oauth_exception: ' . print_r($e->getMessage(),true));
echo 'error=' . OAuth1Util::urlencode_rfc3986($e->getMessage());
killme();
}
echo $r;
killme();
}
2019-06-13 03:57:28 +00:00
function api_oauth_access_token($type) {
try {
$oauth = new ZotOAuth1();
$req = OAuth1Request::from_request();
$r = $oauth->fetch_access_token($req);
}
catch(Exception $e) {
echo 'error=' . OAuth1Util::urlencode_rfc3986($e->getMessage());
killme();
2011-04-21 15:03:31 +00:00
}
echo $r;
killme();
2011-02-15 11:24:21 +00:00
}
2016-10-13 00:00:50 +00:00