removing rest routines from sysGeneric

This commit is contained in:
Erik Amaru Ortiz
2014-03-14 13:12:48 -04:00
parent 2809ab9f0e
commit 0eb31ffa27
2 changed files with 11 additions and 230 deletions

View File

@@ -568,7 +568,7 @@ class Bootstrap
* @param string $urlLink
* @return string
*/
static public function parseURI($uri, $isRestRequest = false)
static public function parseURI($uri)
{
// *** process the $_POST with magic_quotes enabled
// The magic_quotes_gpc feature has been DEPRECATED as of PHP 5.3.0.
@@ -577,11 +577,7 @@ class Bootstrap
}
$aRequestUri = explode('/', $uri);
if ($isRestRequest) {
$args = self::parseRestUri($aRequestUri);
} else {
$args = self::parseNormalUri($aRequestUri);
}
if (! empty($args)) {
define("SYS_LANG", $args ['SYS_LANG']);
@@ -949,147 +945,6 @@ class Bootstrap
return $res;
}
/**
* This method dispatch rest/api service
*
* @author Erik Amaru Ortiz <erik@colosa.com>
* @param $uri
* @param string $version
*/
public function dispatchApiService($uri, $version = '1.0')
{
// to handle a request with "OPTIONS" method
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS, HEADERS');
header('Access-Control-Allow-Headers: authorization, content-type');
header("Access-Control-Allow-Credentials", "false");
header('Access-Control-Max-Age: 60');
die();
}
/*
* Enable this header to allow "Cross Domain AJAX" requests;
* This works because processmaker is handling correctly requests with method 'OPTIONS'
* that automatically is sent by a client using XmlHttpRequest or similar.
*/
header('Access-Control-Allow-Origin: *');
/*
* $servicesDir contains directory where Services Classes are allocated
*/
$servicesDir = PATH_CORE . 'src' . PATH_SEP . 'Services' . PATH_SEP;
/*
* $apiDir - contains directory to scan classes and add them to Restler
*/
$apiDir = $servicesDir . 'Api' . PATH_SEP;
/*
* $apiIniFile - contains file name of api ini configuration
*/
$apiIniFile = $servicesDir . PATH_SEP . 'api.ini';
/*
* $authenticationClass - contains the class name that validate the authentication for Restler
*/
$authenticationClass = 'Services\\Api\\OAuth2\\Server';
/*
* $pmOauthClientId - contains PM Local OAuth Id (Web Designer)
*/
$pmOauthClientId = 'x-pm-local-client';
/*
* Load Api ini file for Rest Service
*/
$apiIniConf = array();
if (file_exists($apiIniFile)) {
$apiIniConf = self::parseIniFile($apiIniFile);
}
// Setting current workspace to Api class
\ProcessMaker\Services\Api::setWorkspace(SYS_SYS);
// TODO remove this setting on the future, it is not needed, but if it is not present is throwing a warning
Luracast\Restler\Format\HtmlFormat::$viewPath = $servicesDir . 'oauth2/views';
// create a new Restler instance
$rest = new Luracast\Restler\Restler();
// setting api version to Restler
$rest->setAPIVersion($version);
// adding $authenticationClass to Restler
$rest->addAuthenticationClass($authenticationClass, '');
// Setting database connection source
list($host, $port) = strpos(DB_HOST, ':') !== false ? explode(':', DB_HOST) : array(DB_HOST, '');
$port = empty($port) ? '' : ";port=$port";
\Services\Api\OAuth2\Server::setDatabaseSource(DB_USER, DB_PASS, DB_ADAPTER.":host=$host;dbname=".DB_NAME.$port);
// Setting default OAuth Client id, for local PM Web Designer
\Services\Api\OAuth2\Server::setPmClientId($pmOauthClientId);
require_once PATH_CORE . "src/Extension/Restler/UploadFormat.php";
//require_once PATH_CORE
//$rest->setSupportedFormats('JsonFormat', 'XmlFormat', 'UploadFormat');
//$rest->setOverridingFormats('UploadFormat', 'JsonFormat', 'XmlFormat', 'HtmlFormat');
$rest->setOverridingFormats('JsonFormat', 'UploadFormat');
// Override $_SERVER['REQUEST_URI'] to Restler handles the current url correctly
$isPluginRequest = strpos($uri, '/plugin-') !== false ? true : false;
if ($isPluginRequest) {
$tmp = explode('/', $uri);
array_shift($tmp);
$tmp = array_shift($tmp);
$tmp = explode('-', $tmp);
$pluginName = $tmp[1];
$uri = str_replace('/plugin-'.$pluginName, '', $uri);
}
$_SERVER['REQUEST_URI'] = $uri;
if (! $isPluginRequest) { // if it is not a request for a plugin endpoint
// scan all api directory to find api classes
$classesList = Bootstrap::rglob('*', 0, $apiDir);
foreach ($classesList as $classFile) {
if (pathinfo($classFile, PATHINFO_EXTENSION) === 'php') {
$namespace = '\\Services\\' . str_replace(
DIRECTORY_SEPARATOR,
'\\',
str_replace('.php', '', str_replace($servicesDir, '', $classFile))
);
//var_dump($namespace);
$rest->addAPIClass($namespace);
}
}
// adding aliases for Restler
if (array_key_exists('alias', $apiIniConf)) {
foreach ($apiIniConf['alias'] as $alias => $aliasData) {
if (is_array($aliasData)) {
foreach ($aliasData as $label => $namespace) {
$namespace = '\\' . ltrim($namespace, '\\');
$rest->addAPIClass($namespace, $alias);
}
}
}
}
} else {
// hook to get rest api classes from plugins
if (class_exists('PMPluginRegistry')) {
$pluginRegistry = & PMPluginRegistry::getSingleton();
$plugins = $pluginRegistry->getRegisteredRestServices();
if (is_array($plugins) && array_key_exists($pluginName, $plugins)) {
foreach ($plugins[$pluginName] as $class) {
$rest->addAPIClass($class['namespace']);
}
}
}
}
$rest->handle();
}
/**
* function to calculate the time used to render a page
*/
@@ -2199,50 +2054,6 @@ class Bootstrap
return $a_mobile_data;
}
/**
* This function parse a particular api/rest request
*
* Example url:
* GET /api/workflowdemo/user/rol?id=0000000000000000001
* POST /api/workflowdemo/cases
*
* - These urls are equivalents
* GET /api/<workspaceName>/cases/start
* GET /<workspaceName>-api/cases/start
*
* @param $url
* @return array return url parsed data
*/
public function parseRestUri($url)
{
array_shift($url);
$sysTemp = array_shift($url);
if ($sysTemp != 'api') {
return array();
}
$apiVersion = array_shift($url);
$sysTemp = array_shift($url);
define('SYS_TEMP', $sysTemp);
$restUri = '';
foreach ($url as $urlPart) {
$restUri .= '/' . $urlPart;
}
$env = self::getSystemConfiguration();
$args['SYS_LANG'] = 'en'; // TODO, this can be set from http header
$args['SYS_SKIN'] = $env['default_skin'];
$args['SYS_COLLECTION'] = '';
$args['SYS_TARGET'] = $restUri;
$args['API_VERSION'] = $apiVersion;
return $args;
}
/**
*
* @param unknown_type $aRequestUri

View File

@@ -208,7 +208,6 @@ define( 'FILE_PATHS_INSTALLED', PATH_CORE . 'config' . PATH_SEP . 'paths_install
define( 'PATH_WORKFLOW_MSSQL_DATA', PATH_CORE . 'data' . PATH_SEP . 'mssql' . PATH_SEP );
define( 'PATH_RBAC_MSSQL_DATA', PATH_RBAC_CORE . 'data' . PATH_SEP . 'mssql' . PATH_SEP );
define( 'PATH_CONTROLLERS', PATH_CORE . 'controllers' . PATH_SEP );
define( 'PATH_SERVICES_REST', PATH_CORE . 'services' . PATH_SEP . 'rest' . PATH_SEP );
// include Gulliver Class
require_once (PATH_GULLIVER . "class.bootstrap.php");
@@ -356,7 +355,6 @@ $virtualURITable['/html2ps_pdf/(*)'] = PATH_THIRDPARTY . 'html2ps_pdf/';
//$virtualURITable['/images/'] = 'errorFile';
//$virtualURITable['/skins/'] = 'errorFile';
//$virtualURITable['/files/'] = 'errorFile';
$virtualURITable['/(*)api/(*)'] = 'api-service';
$virtualURITable["/update/(*)"] = ($skinPathUpdate != "")? $skinPathUpdate : PATH_GULLIVER_HOME . "methods" . PATH_SEP . "update" . PATH_SEP;
//$virtualURITable['/(*)'] = PATH_HTML;
$virtualURITable['/css/(*)'] = PATH_HTML . 'css/'; //ugly
@@ -365,7 +363,6 @@ $virtualURITable['/skins/(*)'] = PATH_HTML . 'skins/'; //ugly
$virtualURITable['/images/(*)'] = PATH_HTML . 'images/'; //ugly
$virtualURITable['/[a-zA-Z][a-zA-Z0-9]{0,}/'] = 'errorFile';
$isRestRequest = false;
// Verify if we need to redirect or stream the file, if G:VirtualURI returns true means we are going to redirect the page
if (Bootstrap::virtualURI( $_SERVER['REQUEST_URI'], $virtualURITable, $realPath )) {
// review if the file requested belongs to public_html plugin
@@ -443,9 +440,6 @@ if (Bootstrap::virtualURI( $_SERVER['REQUEST_URI'], $virtualURITable, $realPath
break;
default:
//Process files loaded with tag head in HTML
if (substr( $realPath, 0, 11 ) == 'api-service') {
$isRestRequest = true;
} else {
$realPath = explode( '?', $realPath );
$realPath[0] .= strpos( basename( $realPath[0] ), '.' ) === false ? '.php' : '';
//NewRelic Snippet - By JHL
@@ -454,11 +448,10 @@ if (Bootstrap::virtualURI( $_SERVER['REQUEST_URI'], $virtualURITable, $realPath
Bootstrap::streamFile( $realPath[0] );
die();
}
}
} //virtual URI parser
// the request correspond to valid php page, now parse the URI
Bootstrap::parseURI( getenv( "REQUEST_URI" ), $isRestRequest );
Bootstrap::parseURI( getenv( "REQUEST_URI" ) );
// Bootstrap::mylog("sys_temp: ".SYS_TEMP);
if (Bootstrap::isPMUnderUpdating()) {
@@ -610,29 +603,6 @@ define( 'SERVER_PORT', $_SERVER['SERVER_PORT'] );
Bootstrap::LoadClass( 'memcached' );
$memcache = & PMmemcached::getSingleton( SYS_SYS );
// verify configuration for rest service
/*if ($isRestRequest) {
// disable until confirm that rest is enabled & configured on rest-config.ini file
$isRestRequest = false;
$confFile = '';
$restApiClassPath = '';
// try load and getting rest configuration
if (file_exists( PATH_DATA_SITE . 'rest-config.ini' )) {
$confFile = PATH_DATA_SITE . 'rest-config.ini';
$restApiClassPath = PATH_DATA_SITE;
} elseif (file_exists( PATH_CONFIG . 'rest-config.ini' )) {
$confFile = PATH_CONFIG . 'rest-config.ini';
}
if (! empty( $confFile ) && $restConfig = @parse_ini_file( $confFile, true )) {
if (array_key_exists( 'enable_service', $restConfig )) {
if ($restConfig['enable_service'] == 'true' || $restConfig['enable_service'] == '1') {
$isRestRequest = true; // rest service enabled
}
}
}
}*/
// load Plugins base class
Bootstrap::LoadClass( 'plugin' );
@@ -828,7 +798,7 @@ if (substr( SYS_COLLECTION, 0, 8 ) === 'gulliver') {
}
}
if (! $isControllerCall && ! file_exists( $phpFile ) && ! $isRestRequest) {
if (! $isControllerCall && ! file_exists( $phpFile )) {
$_SESSION['phpFileNotFound'] = $_SERVER['REQUEST_URI'];
header( "location: /errors/error404.php?url=" . urlencode( $_SERVER['REQUEST_URI'] ) );
die();
@@ -920,7 +890,7 @@ if (! defined( 'EXECUTE_BY_CRON' )) {
$noLoginFolders[] = 'installer';
// This sentence is used when you lost the Session
if (! in_array( SYS_TARGET, $noLoginFiles ) && ! in_array( SYS_COLLECTION, $noLoginFolders ) && $bWE != true && $collectionPlugin != 'services' && ! $isRestRequest) {
if (! in_array( SYS_TARGET, $noLoginFiles ) && ! in_array( SYS_COLLECTION, $noLoginFolders ) && $bWE != true && $collectionPlugin != 'services') {
$bRedirect = true;
if (isset( $_GET['sid'] )) {
Bootstrap::LoadClass( 'sessions' );