A bit of api cleanup. Don't get excited. This is like a 0.005% cleanup but you have to start somewhere.
This commit is contained in:
parent
e985436b3b
commit
3ba4b2c1c1
24
Zotlabs/Lib/Api_router.php
Normal file
24
Zotlabs/Lib/Api_router.php
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Zotlabs\Lib;
|
||||||
|
|
||||||
|
|
||||||
|
class Api_router {
|
||||||
|
|
||||||
|
static private $routes = array();
|
||||||
|
|
||||||
|
static function register($path,$fn,$auth_required) {
|
||||||
|
self::$routes[$path] = [ 'func' => $fn, 'auth' => $auth_required ];
|
||||||
|
}
|
||||||
|
|
||||||
|
static function find($path) {
|
||||||
|
if(array_key_exists($path,self::$routes))
|
||||||
|
return self::$routes[$path];
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
static function dbg() {
|
||||||
|
return self::$routes;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -8,20 +8,15 @@ require_once('include/api.php');
|
|||||||
class Api extends \Zotlabs\Web\Controller {
|
class Api extends \Zotlabs\Web\Controller {
|
||||||
|
|
||||||
function post() {
|
function post() {
|
||||||
|
|
||||||
if(! local_channel()) {
|
if(! local_channel()) {
|
||||||
notice( t('Permission denied.') . EOL);
|
notice( t('Permission denied.') . EOL);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(count(\App::$user) && x(\App::$user,'uid') && \App::$user['uid'] != local_channel()) {
|
|
||||||
notice( t('Permission denied.') . EOL);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function get() {
|
function get() {
|
||||||
|
|
||||||
if(\App::$cmd=='api/oauth/authorize'){
|
if(\App::$cmd=='api/oauth/authorize'){
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -33,7 +28,8 @@ class Api extends \Zotlabs\Web\Controller {
|
|||||||
// get consumer/client from request token
|
// get consumer/client from request token
|
||||||
try {
|
try {
|
||||||
$request = OAuth1Request::from_request();
|
$request = OAuth1Request::from_request();
|
||||||
} catch(Exception $e) {
|
}
|
||||||
|
catch(Exception $e) {
|
||||||
echo "<pre>"; var_dump($e); killme();
|
echo "<pre>"; var_dump($e); killme();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -41,17 +37,20 @@ class Api extends \Zotlabs\Web\Controller {
|
|||||||
if(x($_POST,'oauth_yes')){
|
if(x($_POST,'oauth_yes')){
|
||||||
|
|
||||||
$app = $this->oauth_get_client($request);
|
$app = $this->oauth_get_client($request);
|
||||||
if (is_null($app)) return "Invalid request. Unknown token.";
|
if (is_null($app))
|
||||||
|
return "Invalid request. Unknown token.";
|
||||||
|
|
||||||
$consumer = new OAuth1Consumer($app['client_id'], $app['pw'], $app['redirect_uri']);
|
$consumer = new OAuth1Consumer($app['client_id'], $app['pw'], $app['redirect_uri']);
|
||||||
|
|
||||||
$verifier = md5($app['secret'].local_channel());
|
$verifier = md5($app['secret'].local_channel());
|
||||||
set_config("oauth", $verifier, local_channel());
|
set_config("oauth", $verifier, local_channel());
|
||||||
|
|
||||||
|
|
||||||
if($consumer->callback_url!=null) {
|
if($consumer->callback_url != null) {
|
||||||
$params = $request->get_parameters();
|
$params = $request->get_parameters();
|
||||||
$glue="?";
|
$glue = '?';
|
||||||
if (strstr($consumer->callback_url,$glue)) $glue="?";
|
if(strstr($consumer->callback_url,$glue))
|
||||||
|
$glue = '?';
|
||||||
goaway($consumer->callback_url . $glue . "oauth_token=" . OAuth1Util::urlencode_rfc3986($params['oauth_token']) . "&oauth_verifier=" . OAuth1Util::urlencode_rfc3986($verifier));
|
goaway($consumer->callback_url . $glue . "oauth_token=" . OAuth1Util::urlencode_rfc3986($params['oauth_token']) . "&oauth_verifier=" . OAuth1Util::urlencode_rfc3986($verifier));
|
||||||
killme();
|
killme();
|
||||||
}
|
}
|
||||||
@ -59,7 +58,7 @@ class Api extends \Zotlabs\Web\Controller {
|
|||||||
$tpl = get_markup_template("oauth_authorize_done.tpl");
|
$tpl = get_markup_template("oauth_authorize_done.tpl");
|
||||||
$o = replace_macros($tpl, array(
|
$o = replace_macros($tpl, array(
|
||||||
'$title' => t('Authorize application connection'),
|
'$title' => t('Authorize application connection'),
|
||||||
'$info' => t('Return to your app and insert this Securty Code:'),
|
'$info' => t('Return to your app and insert this Security Code:'),
|
||||||
'$code' => $verifier,
|
'$code' => $verifier,
|
||||||
));
|
));
|
||||||
|
|
||||||
@ -72,13 +71,10 @@ class Api extends \Zotlabs\Web\Controller {
|
|||||||
notice( t('Please login to continue.') . EOL );
|
notice( t('Please login to continue.') . EOL );
|
||||||
return login(false,'api-login',$request->get_parameters());
|
return login(false,'api-login',$request->get_parameters());
|
||||||
}
|
}
|
||||||
//FKOAuth1::loginUser(4);
|
|
||||||
|
|
||||||
$app = $this->oauth_get_client($request);
|
$app = $this->oauth_get_client($request);
|
||||||
if (is_null($app)) return "Invalid request. Unknown token.";
|
if (is_null($app))
|
||||||
|
return "Invalid request. Unknown token.";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$tpl = get_markup_template('oauth_authorize.tpl');
|
$tpl = get_markup_template('oauth_authorize.tpl');
|
||||||
$o = replace_macros($tpl, array(
|
$o = replace_macros($tpl, array(
|
||||||
@ -100,23 +96,18 @@ class Api extends \Zotlabs\Web\Controller {
|
|||||||
|
|
||||||
function oauth_get_client($request){
|
function oauth_get_client($request){
|
||||||
|
|
||||||
|
|
||||||
$params = $request->get_parameters();
|
$params = $request->get_parameters();
|
||||||
$token = $params['oauth_token'];
|
$token = $params['oauth_token'];
|
||||||
|
|
||||||
$r = q("SELECT `clients`.*
|
$r = q("SELECT clients.* FROM clients, tokens WHERE clients.client_id = tokens.client_id
|
||||||
FROM `clients`, `tokens`
|
AND tokens.id = '%s' AND tokens.auth_scope = 'request' ",
|
||||||
WHERE `clients`.`client_id`=`tokens`.`client_id`
|
dbesc($token)
|
||||||
AND `tokens`.`id`='%s' AND `tokens`.`auth_scope`='request'",
|
);
|
||||||
dbesc($token));
|
if($r)
|
||||||
|
return $r[0];
|
||||||
|
|
||||||
if (!count($r))
|
return null;
|
||||||
return null;
|
|
||||||
|
|
||||||
return $r[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
178
include/api.php
178
include/api.php
@ -62,9 +62,10 @@ require_once('include/api_auth.php');
|
|||||||
|
|
||||||
|
|
||||||
function api_register_func($path, $func, $auth=false){
|
function api_register_func($path, $func, $auth=false){
|
||||||
global $API;
|
\Zotlabs\Lib\Api_router::register($path,$func,$auth);
|
||||||
$API[$path] = array('func'=>$func,
|
// global $API;
|
||||||
'auth'=>$auth);
|
// $API[$path] = array('func'=>$func,
|
||||||
|
// 'auth'=>$auth);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -73,99 +74,104 @@ require_once('include/api_auth.php');
|
|||||||
**************************/
|
**************************/
|
||||||
|
|
||||||
function api_call($a){
|
function api_call($a){
|
||||||
GLOBAL $API, $called_api;
|
GLOBAL $called_api;
|
||||||
|
|
||||||
// preset
|
|
||||||
$type="json";
|
|
||||||
|
|
||||||
foreach ($API as $p=>$info){
|
$type = 'json';
|
||||||
if (strpos(App::$query_string, $p)===0){
|
$p = App::$cmd;
|
||||||
$called_api= explode("/",$p);
|
|
||||||
//unset($_SERVER['PHP_AUTH_USER']);
|
|
||||||
if ($info['auth'] === true && api_user() === false) {
|
|
||||||
api_login($a);
|
|
||||||
}
|
|
||||||
|
|
||||||
load_contact_links(api_user());
|
if(strrpos($p,'.')) {
|
||||||
|
$type = substr($p,strrpos($p,'.')+1);
|
||||||
$channel = App::get_channel();
|
$p = substr($p,0,strrpos($p,'.'));
|
||||||
|
|
||||||
logger('API call for ' . $channel['channel_name'] . ': ' . App::$query_string);
|
|
||||||
logger('API parameters: ' . print_r($_REQUEST,true));
|
|
||||||
|
|
||||||
$type="json";
|
|
||||||
|
|
||||||
if (strpos(App::$query_string, ".xml")>0) $type="xml";
|
|
||||||
if (strpos(App::$query_string, ".json")>0) $type="json";
|
|
||||||
if (strpos(App::$query_string, ".rss")>0) $type="rss";
|
|
||||||
if (strpos(App::$query_string, ".atom")>0) $type="atom";
|
|
||||||
if (strpos(App::$query_string, ".as")>0) $type="as";
|
|
||||||
|
|
||||||
$r = call_user_func($info['func'], $a, $type);
|
|
||||||
if ($r===false) return;
|
|
||||||
|
|
||||||
switch($type){
|
|
||||||
case "xml":
|
|
||||||
$r = mb_convert_encoding($r, "UTF-8",mb_detect_encoding($r));
|
|
||||||
header ("Content-Type: text/xml");
|
|
||||||
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
|
|
||||||
break;
|
|
||||||
case "json":
|
|
||||||
header ("Content-Type: application/json");
|
|
||||||
foreach($r as $rr) {
|
|
||||||
if(! $rr)
|
|
||||||
$rr = array();
|
|
||||||
$json = json_encode($rr);
|
|
||||||
}
|
|
||||||
if ($_GET['callback'])
|
|
||||||
$json = $_GET['callback']."(".$json.")";
|
|
||||||
return $json;
|
|
||||||
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;
|
|
||||||
case "as":
|
|
||||||
//header ("Content-Type: application/json");
|
|
||||||
//foreach($r as $rr)
|
|
||||||
// return json_encode($rr);
|
|
||||||
return json_encode($r);
|
|
||||||
break;
|
|
||||||
|
|
||||||
}
|
|
||||||
//echo "<pre>"; var_dump($r); die();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
header("HTTP/1.1 404 Not Found");
|
|
||||||
logger('API call not implemented: '.App::$query_string." - ".print_r($_REQUEST,true));
|
$info = \Zotlabs\Lib\Api_router::find($p);
|
||||||
$r = '<status><error>not implemented</error></status>';
|
|
||||||
switch($type){
|
logger('info: ' . $p . ' type: ' . $type . ' ' . print_r($info,true));
|
||||||
case "xml":
|
|
||||||
header ("Content-Type: text/xml");
|
if($info) {
|
||||||
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
|
$called_api= explode("/",$p);
|
||||||
break;
|
|
||||||
case "json":
|
if ($info['auth'] === true && api_user() === false) {
|
||||||
header ("Content-Type: application/json");
|
api_login($a);
|
||||||
return json_encode(array('error' => 'not implemented'));
|
}
|
||||||
break;
|
|
||||||
case "rss":
|
load_contact_links(api_user());
|
||||||
header ("Content-Type: application/rss+xml");
|
|
||||||
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
|
$channel = App::get_channel();
|
||||||
break;
|
|
||||||
case "atom":
|
logger('API call for ' . $channel['channel_name'] . ': ' . App::$query_string);
|
||||||
header ("Content-Type: application/atom+xml");
|
logger('API parameters: ' . print_r($_REQUEST,true));
|
||||||
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
|
|
||||||
break;
|
$r = call_user_func($info['func'], $a, $type);
|
||||||
|
|
||||||
|
if($r === false)
|
||||||
|
return;
|
||||||
|
|
||||||
|
switch($type){
|
||||||
|
case "xml":
|
||||||
|
$r = mb_convert_encoding($r, "UTF-8",mb_detect_encoding($r));
|
||||||
|
header ("Content-Type: text/xml");
|
||||||
|
return '<?xml version="1.0" encoding="UTF-8"?>'."\n".$r;
|
||||||
|
break;
|
||||||
|
case "json":
|
||||||
|
header ("Content-Type: application/json");
|
||||||
|
foreach($r as $rr) {
|
||||||
|
if(! $rr)
|
||||||
|
$rr = array();
|
||||||
|
$json = json_encode($rr);
|
||||||
|
}
|
||||||
|
if ($_GET['callback'])
|
||||||
|
$json = $_GET['callback']."(".$json.")";
|
||||||
|
return $json;
|
||||||
|
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;
|
||||||
|
case "as":
|
||||||
|
//header ("Content-Type: application/json");
|
||||||
|
//foreach($r as $rr)
|
||||||
|
// return json_encode($rr);
|
||||||
|
return json_encode($r);
|
||||||
|
break;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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>';
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RSS extra info
|
* RSS extra info
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function api_rss_extra($a, $arr, $user_info){
|
function api_rss_extra($a, $arr, $user_info){
|
||||||
if (is_null($user_info)) $user_info = api_get_user($a);
|
if (is_null($user_info)) $user_info = api_get_user($a);
|
||||||
$arr['$user'] = $user_info;
|
$arr['$user'] = $user_info;
|
||||||
|
@ -2975,3 +2975,38 @@ function text_highlight($s,$lang) {
|
|||||||
return('<code>' . $o . '</code>');
|
return('<code>' . $o . '</code>');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// function to convert multi-dimensional array to xml
|
||||||
|
// create new instance of simplexml
|
||||||
|
|
||||||
|
// $xml = new SimpleXMLElement('<root/>');
|
||||||
|
|
||||||
|
// function callback
|
||||||
|
// array2XML($xml, $my_array);
|
||||||
|
|
||||||
|
// save as xml file
|
||||||
|
// echo (($xml->asXML('data.xml')) ? 'Your XML file has been generated successfully!' : 'Error generating XML file!');
|
||||||
|
|
||||||
|
function arrtoxml($root_elem,$arr) {
|
||||||
|
$xml = new SimpleXMLElement('<' . $root_elem . '/>');
|
||||||
|
array2XML($xml,$arr);
|
||||||
|
return $xml->asXML();
|
||||||
|
}
|
||||||
|
|
||||||
|
function array2XML($obj, $array)
|
||||||
|
{
|
||||||
|
foreach ($array as $key => $value)
|
||||||
|
{
|
||||||
|
if(is_numeric($key))
|
||||||
|
$key = 'item' . $key;
|
||||||
|
|
||||||
|
if (is_array($value))
|
||||||
|
{
|
||||||
|
$node = $obj->addChild($key);
|
||||||
|
array2XML($node, $value);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$obj->addChild($key, htmlspecialchars($value));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user