Updating API Rest Dispatching, adding alias support from a conf api.ini file

This commit is contained in:
Erik Amaru Ortiz
2013-11-26 17:17:36 -04:00
parent fca8338b24
commit 1faafa8ee4
103 changed files with 218 additions and 1160 deletions

View File

@@ -1,26 +0,0 @@
<?php
/**
* Created by JetBrains PhpStorm.
* User: erik
* Date: 8/21/13
* Time: 4:55 PM
* To change this template use File | Settings | File Templates.
*/
class Services_Api_ProcessMaker_Trigger
{
public function hello($to='world')
{
return array('success'=>true, "message"=>"Hello $to!");
}
public function hi($name)
{
return "Hi $name";
}
protected function secret()
{
return 'S E C R E T';
}
}

View File

@@ -1,32 +0,0 @@
<?php
// determine where the sqlite DB will go
$dbfile = __DIR__.'/oauth.sqlite';
// remove sqlite file if it exists
if (file_exists($dbfile)) {
unlink($dbfile);
}
if (!is_writable(__DIR__)) {
// try to set permissions.
if (!@chmod(__DIR__, 0777)) {
throw new Exception("Unable to write to $dbfile");
}
}
// rebuild the DB
$db = new PDO(sprintf('sqlite://%s', $dbfile));
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->exec('CREATE TABLE oauth_clients (client_id TEXT, client_secret TEXT, redirect_uri TEXT)');
$db->exec('CREATE TABLE oauth_access_tokens (access_token TEXT, client_id TEXT, user_id TEXT, expires TIMESTAMP, scope TEXT)');
$db->exec('CREATE TABLE oauth_authorization_codes (authorization_code TEXT, client_id TEXT, user_id TEXT, redirect_uri TEXT, expires TIMESTAMP, scope TEXT)');
$db->exec('CREATE TABLE oauth_refresh_tokens (refresh_token TEXT, client_id TEXT, user_id TEXT, expires TIMESTAMP, scope TEXT)');
// add test data
$db->exec('INSERT INTO oauth_clients (client_id, client_secret) VALUES ("demoapp", "demopass")');
chmod($dbfile, 0777);
// $db->exec('INSERT INTO oauth_access_tokens (access_token, client_id) VALUES ("testtoken", "Some Client")');
// $db->exec('INSERT INTO oauth_authorization_codes (authorization_code, client_id) VALUES ("testcode", "Some Client")');

View File

@@ -1,4 +0,0 @@
# views
### home for the restler view files
When HtmlFormat is used, it looks into this folder for view (template) files

File diff suppressed because one or more lines are too long

View File

@@ -1,158 +0,0 @@
<?php
use Luracast\Restler\Restler;
use Luracast\Restler\Util;
$call_trace = '';
function exceptions()
{
global $call_trace;
$r = Util::$restler;
$source = $r->_exceptions;
if (count($source)) {
$source = end($source);
$traces = array();
do {
$traces += $source->getTrace();
} while ($source = $source->getPrevious());
$traces += debug_backtrace();
$call_trace
= parse_backtrace($traces, 0);
} else {
$call_trace
= parse_backtrace(debug_backtrace());
}
}
exceptions();
function parse_backtrace($raw, $skip = 1)
{
$output = "";
foreach ($raw as $entry) {
if ($skip-- > 0) {
continue;
}
//$output .= print_r($entry, true) . "\n";
$output .= "\nFile: " . $entry['file'] . " (Line: " . $entry['line'] . ")\n";
if (isset($entry['class']))
$output .= $entry['class'] . "::";
$output .= $entry['function']
. "( " . json_encode($entry['args']) . " )\n";
}
return $output;
}
//print_r(get_defined_vars());
//print_r($response);
$icon;
if ($success && isset($api)) {
$arguments = implode(', ', $api->parameters);
$icon = "<icon class=\"success\"></icon>";
$title = "{$api->className}::"
. "{$api->methodName}({$arguments})";
} else {
if (isset($response['error']['message'])) {
$icon = '<icon class="denied"></icon>';
$title = end(explode(':',$response['error']['message']));
} else {
$icon = '<icon class="warning"></icon>';
$title = 'No Matching Resource';
}
}
function render($data, $shadow=true)
{
$r = '';
if (empty($data))
return $r;
$r .= $shadow ? "<ul class=\"shadow\">\n": "<ul>\n";
if (is_array($data)) {
// field name
foreach ($data as $key => $value) {
$r .= '<li>';
$r .= is_numeric($key)
? "[<strong>$key</strong>] "
: "<strong>$key: </strong>";
$r .= '<span>';
if (is_array($value)) {
// recursive
$r .= render($value,false);
} else {
// value, with hyperlinked hyperlinks
if (is_bool($value)) {
$value = $value ? 'true' : 'false';
}
$value = htmlentities($value, ENT_COMPAT, 'UTF-8');
if (strpos($value, 'http://') === 0) {
$r .= '<a href="' . $value . '">' . $value . '</a>';
} else {
$r .= $value;
}
}
$r .= "</span></li>\n";
}
} elseif (is_bool($data)) {
$r .= '<li>' . ($data ? 'true' : 'false') . '</li>';
} else {
$r .= "<li><strong>$data</strong></li>";
}
$r .= "</ul>\n";
return $r;
}
$reqHeadersArr = array();
$requestHeaders = $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . ' ' . $_SERVER['SERVER_PROTOCOL'] . PHP_EOL;
foreach ($reqHeadersArr as $key => $value) {
if ($key == 'Host')
continue;
$requestHeaders .= "$key: $value" . PHP_EOL;
}
// $requestHeaders = $this->encode(apache_request_headers(), FALSE,
// FALSE);
$responseHeaders = implode(PHP_EOL, headers_list()).PHP_EOL.'Status: HTTP/1.1 ';
$responseHeaders .= Util::$restler->responseCode.' '.\Luracast\Restler\RestException::$codes[Util::$restler->responseCode];
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title?></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style>
<?php include __DIR__.'/debug.css'; ?>
</style>
</head>
<body>
<div id="breadcrumbs-one">
<?php
if(Util::$restler->exception){
$stages = Util::$restler->exception->getStages();
$curStage = Util::$restler->exception->getStage();
foreach($stages['success'] as $stage){
echo "<a href=\"#\">$stage</a>";
}
foreach($stages['failure'] as $stage){
echo '<a href="#" class="failure">'
. $stage
. ($stage==$curStage ? ' <span class="state"/> ' : '')
. '</a>';
}
} else {
foreach(Util::$restler->_events as $stage){
echo "<a href=\"#\">$stage</a>";
}
}
?>
</div>
<header>
<h1><?php echo $title ?></h1>
</header>
<article>
<h2>Response:<right><?php echo $icon;?></right></h2>
<pre class="header"><?php echo $responseHeaders ?></pre>
<?php echo render($response); ?>
<p>Restler v<?php echo Restler::VERSION?></p>
</article>
</body>
</html>

View File

@@ -1,15 +0,0 @@
{% if googleAnalyticsCode is defined %}
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', '{{ googleAnalyticsCode }}']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
{% endif %}

View File

@@ -1,61 +0,0 @@
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="{{ basePath }}/css/demo.css">
<link rel="stylesheet" href="{{ basePath }}/css/shared.css">
</head>
<body>
<!--[if lt IE 7]>
<p class="chromeframe">You are using an outdated browser. <a href="http://browsehappy.com/">Upgrade your browser today</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to better experience this site.</p>
<![endif]-->
{% include 'oauth2/analytics.twig' %}
{% include 'oauth2/github.twig' %}
<div id="container">
<header role="banner">
{% block header %}
<hgroup>
<h1>Restler 3</h1>
<h2>OAuth 2 Demo App</h2>
</hgroup>
<nav class="primary">
<ul class="menu">
<li class="current">
<a href="http://luracast.com">Home</a>
</li>
<li>
<a href="../">More</a>
</li>
<li>
<a href="http://github.com/Luracast">Source</a>
</li>
<li>
<a href="http://twitter.com/Luracast">Twitter</a>
</li>
<li>
<a href="http://github.com/bshaffer">OAuth2</a>
</li>
</ul>
</nav>
{% endblock %}
</header>
<article class="home" role="main">
<div role="main">
{% block content %}
{% endblock %}
</div>
</article>
</div>
</body>
</html>

View File

@@ -1,21 +0,0 @@
{% extends "oauth2/client/base.twig" %}
{% block content %}
<h3>Authorization Failed!</h3>
<p>
It seems authorization has been denied for the following reasons:
</p>
<ul>
<li>
{% if response.error.error_description %}
{{ response.error.error_description }}
{# optional "error_uri" param #}
{% if response.error_uri %}
(<a href="{{ response.error.error_uri }}">more information</a>)
{% endif %}
{% else %}
<em>authorization server did not supply an error message</em>
{% endif %}
</li>
</ul>
{% endblock %}

View File

@@ -1,12 +0,0 @@
{% extends "oauth2/client/base.twig" %}
{% block content %}
<h3>Authorization Error</h3>
<p>
{{ response.error.error_description }}
{% if response.error.error_uri is defined %}
(<a href="{{response.error.error_uri}}">more information</a>)
{% endif %}
</p>
<a href="{{ basePath }}">back</a>
{% endblock %}

View File

@@ -1,15 +0,0 @@
{% extends "oauth2/client/base.twig" %}
{% block content %}
<h3>Authorization Granted!</h3>
<pre><code> Access Token: {{ response.token }} </code></pre>
<p>
Here are your friends:
</p>
<ul>
{% for friend in response.friends %}
<li>{{ friend }}</li>
{% endfor %}
</ul>
<div class="help"><em>The API call can be seen at <a href="{{ response.endpoint }}" target="_blank">{{ response.endpoint }}</a></em></div>
{% endblock %}

View File

@@ -1,13 +0,0 @@
{% extends "oauth2/client/base.twig" %}
{% block content %}
<h3>Demo App</h3>
<p>
We would like to use your information in order to integrate with your friends,
use your personal information for nefarious purposes, and to make your life better somehow.
</p>
<p>
Click below to integrate with that service you belong to:
</p>
<a class="button" href="{{ response.authorize_url }}?response_type=code&client_id=demoapp&redirect_uri={{ response.authorize_redirect_url|url_encode() }}">Authorize</a>
{% endblock %}

View File

@@ -1 +0,0 @@
<a href="https://github.com/Luracast/Restler"><img style="position: absolute; top: 0; right: 0; border: 0;z-index:100000" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>

View File

@@ -1,29 +0,0 @@
<p>
<strong><?php echo $response['client_details']['CLIENT_NAME']?></strong> would like to access the following data:
</p>
<ul>
<?php foreach($response['requestedScope'] as $scope) {?>
<li><?php echo $response['supportedScope'][$scope] ?></li>
<?php } ?>
</ul>
<p>It will use this data to:</p>
<ul>
<li>integrate with ProcessMaker</li>
<li>make your life better</li>
<li>miscellaneous nefarious purposes</li>
</ul>
<ul class="authorize_options">
<li>
<form action="/api/1.0/workflow/authorize?<?php echo $response['query_string']?>" method="post">
<input type="submit" class="button authorize" value="Yes, I Authorize This Request"/>
<input type="hidden" name="authorize" value="1"/>
</form>
</li>
<li class="cancel">
<form id="cancel" action="/api/1.0/workflow/authorize?<?php echo $response['query_string']?>" method="post">
<a href="#" onclick="document.getElementById('cancel').submit()">cancel</a>
<input type="hidden" name="authorize" value="0"/>
</form>
</li>
</ul>

View File

@@ -1,36 +0,0 @@
{% extends "oauth2/server/base.twig" %}
{% block content %}
<p>
<strong>Demo App</strong> would like to access the following data:
</p>
<ul>
<li>friends</li>
<li>memories</li>
<li>hopes, dreams, passions, etc.</li>
<li>sock drawer</li>
</ul>
<p>It will use this data to:</p>
<ul>
<li>integrate with friends</li>
<li>make your life better</li>
<li>miscellaneous nefarious purposes</li>
</ul>
<ul class="authorize_options">
<li>
<form action="{{ 'authorize' ~ '?' ~ response.queryString }}" method="post">
<input type="submit" class="button authorize"
value="Yes, I Authorize This Request"/>
<input type="hidden" name="authorize" value="1"/>
</form>
</li>
<li class="cancel">
<form id="cancel" action="{{ 'authorize' ~ '?' ~ response.queryString }}"
method="post">
<a href="#"
onclick="document.getElementById('cancel').submit()">cancel</a>
<input type="hidden" name="authorize" value="0"/>
</form>
</li>
</ul>
{% endblock %}

View File

@@ -1,34 +0,0 @@
<!DOCTYPE html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="{{ basePath }}/css/lockdin.css">
<link rel="stylesheet" href="{{ basePath }}/css/shared.css">
</head>
<body>
<!--[if lt IE 7]>
<p class="chromeframe">You are using an outdated browser. <a href="http://browsehappy.com/">Upgrade your browser today</a> or <a href="http://www.google.com/chromeframe/?redirect=true">install Google Chrome Frame</a> to better experience this site.</p>
<![endif]-->
{% include 'oauth2/analytics.twig' %}
{% include 'oauth2/github.twig' %}
{% include 'oauth2/server/header.html' %}
<div id="container">
<article class="home" role="main">
<div id="content" role="main">
{% block content %}
{% endblock %}
</div>
</article>
</div>
</body>
</html>

View File

@@ -1,52 +0,0 @@
<div id="header" class="global-nav member">
<div class="top-nav">
<div class="wrapper">
<ul class="util" role="navigation">
<li class="tab username-cont">
<a href="#" class="tab-name username">You <span class="menu-indicator"></span></a>
</li>
</ul>
<h2 class="logo"><a href="#" title="Home">Lock'd <span>in</span></a></h2>
<div class="account">
<span>You'll never get your data back</span>
</div>
</div>
</div>
<div class="bottom-nav">
<div class="wrapper">
</ul>
</li>
<ul class="nav" role="navigation">
<li id="nav-primary-home" class="tab selected"><a href="#" class="tab-name"><span>Home</span></a></li>
<li id="nav-primary-profile" class="tab"><a href="#" class="tab-name"><span>Profile</span></a></li>
<li id="nav-primary-contacts" class="tab "><a href="/#" class="tab-name"><span>Contacts</span></a></li>
<li id="nav-primary-groups" class="tab "><a href="#" class="tab-name"><span>Groups</span></a></li>
<li id="nav-primary-jobs" class="tab "><a href="#" class="tab-name"><span>Jobs</span></a></li>
<li id="nav-primary-inbox-links" class="tab "><a href="#" class="tab-name"><span>Inbox</span></a></li>
<li id="nav-primary-company" class="tab"><a href="#" class="tab-name"><span>Companies</span></a></li>
<li id="nav-primary-news" class="tab"><a href="#" class="tab-name"><span>News</span></a></li>
<li id="nav-primary-more" class="tab "><a href="#" class="tab-name"><span>More</span></a></li>
</ul>
<form id="global-search" role="search" action="/search/fpsearch" method="get" accept-charset="UTF-8" name="commonSearch" class="global-search ">
<fieldset>
<div class="search-scope styled-dropdown" id="yui-gen1"><span class="label"> <span> People </span> </span></div>
<span class="search-box-container">
<span id="search-autocomplete-container" title="Tip: You can also search by keyword, company, school..." class="/typeahead">
<label for="main-search-box" class="ghost" id="yui-gen2" style="display: none; ">Search...</label>
<input name="keywords" id="main-search-box" class="search-term yui-ac-input" type="text" value="" autocomplete="off" placeholder="Search...">
<span id="search-typeahead-container"></span>
</span>
<input name="search" value="Search" class="search-go" type="submit">
</span>
</fieldset>
<a id="search-link" class="search-link" href="/search?trk=advsrch">Advanced</a>
</form>
</div>
</div>
</div>

View File

@@ -1,7 +0,0 @@
<?php
echo $_SESSION['ERIK'];

View File

@@ -1,2 +0,0 @@
<?php
echo $basePath;

View File

@@ -1 +0,0 @@
Showing {{basePath}}

View File

@@ -1 +0,0 @@
Showing "{{ basePath }}"

View File

@@ -1,56 +0,0 @@
<?php return <<<TEMPLATE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<base href="{$basePath}/"/>
<title>{$title}</title>
<!-- Including the jQuery UI Human Theme -->
<link rel="stylesheet"
href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/themes/humanity/jquery-ui.css"
type="text/css" media="all"/>
<!-- Our own stylesheet -->
<link rel="stylesheet" type="text/css" href="styles.css"/>
</head>
<body>
<h1>{$title}</h1>
<h2>
<a href="../">Go
Back to the Examples &raquo;</a></h2>
<div id="main">
<ul class="todoList">
{$_('include','todo/list.php','response')}
</ul>
<a id="addButton" class="green-button" href="#">Add a Task</a>
</div>
<!-- This div is used as the base for the confirmation jQuery UI POPUP. Hidden by CSS. -->
<div id="dialog-confirm" title="Delete TODO Item?">Are you sure you want to
delete this task?
</div>
<p class="note">{$description}</p>
<!-- Including our scripts -->
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript"
src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.0/jquery-ui.min.js"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
TEMPLATE;

View File

@@ -1,12 +0,0 @@
<?php return <<<TEMPLATE
<li id="todo-{$id}" class="todo">
<div class="text">{$text}</div>
<div class="actions">
<a href="#" class="edit">Edit</a>
<a href="#" class="delete">Delete</a>
</div>
</li>
TEMPLATE;

View File

@@ -1,6 +1,6 @@
<?php
namespace Api\OAuth2;
namespace Services\Api\OAuth2;
/**
* Simple PmPDO storage for all storage types

View File

@@ -1,5 +1,5 @@
<?php
namespace Api\OAuth2;
namespace Services\Api\OAuth2;
use Luracast\Restler\iAuthenticate;

View File

@@ -0,0 +1,116 @@
<?php
namespace Services\Api\ProcessMaker;
use \ProcessMaker\Api;
use \Luracast\Restler\RestException;
class Test extends Api
{
protected $data = array();
function __construct()
{
if (! isset($_SESSION['__rest_tmp__'])) {
$this->data[1] = array(
'id' => '1',
'name' => 'John',
'lastname' => 'Doe',
'age' => '27'
);
$this->saveData();
} else {
$this->loadData();
}
}
function index()
{
return array_values($this->data);
}
function get($id)
{
if (array_key_exists($id, $this->data)) {
return $this->data[$id];
}
throw new RestException(400, "Record not found. Record with id: $id does not exist!");
}
function post($request_data = NULL)
{
$id = count($this->data) + 1;
$this->data[$id] = array(
'id' => $id,
'name' => '',
'lastname' => '',
'age' => ''
);
if (array_key_exists('name', $request_data)) {
$this->data[$id]['name'] = $request_data['name'];
}
if (array_key_exists('lastname', $request_data)) {
$this->data[$id]['lastname'] = $request_data['lastname'];
}
if (array_key_exists('age', $request_data)) {
$this->data[$id]['age'] = $request_data['age'];
}
$this->saveData();
return $this->data[$id];
}
function put($id, $request_data = NULL)
{
if (array_key_exists($id, $this->data)) {
if (array_key_exists('name', $request_data)) {
$this->data[$id] = $request_data['name'];
}
if (array_key_exists('lastname', $request_data)) {
$this->data[$id] = $request_data['lastname'];
}
if (array_key_exists('age', $request_data)) {
$this->data[$id] = $request_data['age'];
}
$this->saveData();
return $this->data[$id];
} else {
throw new RestException(400, "Record not found. Record with id: $id does not exist!");
}
}
function delete($id)
{
if (array_key_exists($id, $this->data)) {
$row = $this->data[$id];
unset($this->data[$id]);
$this->saveData();
return $row;
} else {
throw new RestException(400, "Record not found. Record with id: $id does not exist!");
}
}
/**
* @url GET /testing/sample/:param
*/
function doOverride($param)
{
return $param;
}
/* Private methods */
private function loadData()
{
$this->data = $_SESSION['__rest_tmp__'];
}
private function saveData()
{
$_SESSION['__rest_tmp__'] = $this->data;
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace Services\Api\ProcessMaker;
use \ProcessMaker\Api;
use \Luracast\Restler\RestException;
class Test2 extends Api
{
function hello()
{
return 'GEEET ALL';
}
/**
* @url GET /getHello
*/
function helloworld($param = '')
{
return 'Greetings, from a overridden url ' . $param;
}
/**
* @url GET /sample/other/large/:name
*/
function sampleOther($name)
{
return 'Name: ' . $name;
}
}

View File

@@ -0,0 +1,6 @@
;
; API Rest Configuration File
;
[alias]
test = "Services\Api\ProcessMaker\Test2"

Some files were not shown because too many files have changed in this diff Show More