Arreglo de conflictos

This commit is contained in:
Brayan Osmar Pereyra Suxo
2014-04-03 12:20:34 -04:00
16 changed files with 484 additions and 206 deletions

View File

@@ -0,0 +1,155 @@
<?php
namespace Luracast\Restler\Format;
use Luracast\Restler\RestException;
/**
* Extending UploadFormat Support for Multi Part Form Data and File Uploads
*
* @category Framework
* @author Erik Amaru Ortiz <erik@colosa.com>
*/
class UploadFormat extends Format
{
const MIME = 'multipart/form-data';
const EXTENSION = 'post';
/**
* use it if you need to restrict uploads based on file type
* setting it as an empty array allows all file types
* default is to allow only png and jpeg images
*
* @var array
*/
public static $allowedMimeTypes = array(
'image/jpeg',
'image/png',
'image/png',
'application/octet-stream',
'text/plain',
'text/xml',
'text/html',
'text/css'
);
/**
* use it to restrict uploads based on file size
* set it to 0 to allow all sizes
* please note that it upload restrictions in the server
* takes precedence so it has to be lower than or equal to that
* default value is 1MB (1024x1024)bytes
* usual value for the server is 8388608
*
* @var int
*/
public static $maximumFileSize = 1048576;
/**
* Your own validation function for validating each uploaded file
* it can return false or throw an exception for invalid file
* use anonymous function / closure in PHP 5.3 and above
* use function name in other cases
*
* @var Callable
*/
public static $customValidationFunction;
/**
* Since exceptions are triggered way before at the `get` stage
*
* @var bool
*/
public static $suppressExceptionsAsError = false;
protected static function checkFile(& $file, $doMimeCheck = false, $doSizeCheck = false)
{
try {
if ($file['error']) {
//server is throwing an error
//assume that the error is due to maximum size limit
throw new RestException(413, "Uploaded file ({$file['name']}) is too big.");
}
if ($doMimeCheck && !in_array($file['type'],
self::$allowedMimeTypes)
) {
throw new RestException(403, "File type ({$file['type']}) is not supported.");
}
if ($doSizeCheck && $file['size'] > self::$maximumFileSize) {
throw new RestException(413, "Uploaded file ({$file['name']}) is too big.");
}
if (self::$customValidationFunction) {
if (!call_user_func(self::$customValidationFunction, $file)) {
throw new RestException(403, "File ({$file['name']}) is not supported.");
}
}
} catch (RestException $e) {
if (static::$suppressExceptionsAsError) {
$file['error'] = true;
$file['exception'] = $e;
} else {
throw $e;
}
}
}
public function encode($data, $humanReadable = false)
{
throw new RestException(500, 'UploadFormat is read only');
}
public function decode($data)
{
$doMimeCheck = !empty(self::$allowedMimeTypes);
$doSizeCheck = self::$maximumFileSize ? TRUE : FALSE;
//validate
foreach ($_FILES as & $file) {
if (is_array($file['error'])) {
foreach ($file['error'] as $i => $error) {
$innerFile = array();
foreach ($file as $property => $value) {
$innerFile[$property] = $value[$i];
}
if ($innerFile['name'])
static::checkFile($innerFile, $doMimeCheck, $doSizeCheck);
if (isset($innerFile['exception'])) {
$file['error'] = true;
$file['exception'] = $innerFile['exception'];
break;
}
}
} else {
if ($file['name'])
static::checkFile($file, $doMimeCheck, $doSizeCheck);
if (isset($innerFile['exception'])) {
break;
}
}
}
//sort file order if needed;
return $_FILES + $_POST;
}
function isWritable()
{
return false;
}
}
///**
// * Extending UploadFormat Support for Multi Part Form Data and File Uploads
// *
// * @category Framework
// * @author Erik Amaru Ortiz <erik@colosa.com>
// */
//class UploadFormat extends \Luracast\Restler\Format\UploadFormat
//{
// protected static function checkFile(& $file, $doMimeCheck = false, $doSizeCheck = false)
// {
// self::$allowedMimeTypes = array(
// 'image/jpeg',
// 'image/png',
// 'application/octet-stream'
// );
//
// parent::checkFile($file, $doMimeCheck, $doSizeCheck);
// }
//}

View File

@@ -1,14 +1,15 @@
<?php
namespace Maveriks;
use ProcessMaker\Services\Api;
use Maveriks\Util;
use Luracast\Restler\RestException;
use ProcessMaker\Services;
class WebApplication
{
protected $rootDir = "";
protected $workflowDir = "";
protected $workspaceDir = "";
protected $workspaceCacheDir = "";
protected $requestUri = "";
protected $responseMultipart = array();
@@ -86,7 +87,7 @@ class WebApplication
} else {
$this->dispatchApiRequest($request["uri"], $request["version"]);
}
Util\Logger::log("API::End Dispatching ".$_SERVER["REQUEST_METHOD"]." ".$request["uri"]);
Util\Logger::log("API::End Dispatch");
break;
}
}
@@ -105,19 +106,10 @@ class WebApplication
public function multipart($uri, $version = "1.0")
{
$stringInput = file_get_contents('php://input');
if (empty($stringInput)) {
$rest = new \Maveriks\Extension\Restler();
$rest->setMessage(new RestException(Api::STAT_APP_EXCEPTION, "Invalid Request, multipart without body."));
exit();
} else {
$input = json_decode($stringInput);
if (empty($input->calls)) {
$rest = new \Maveriks\Extension\Restler();
$rest->setMessage(new RestException(Api::STAT_APP_EXCEPTION, "Invalid Request, multipart body without calls."));
exit();
}
if (is_null($stringInput)) {
return array(); //no body
}
$input = json_decode($stringInput);
$baseUrl = (empty($input->base_url)) ? $uri : $input->base_url;
foreach($input->calls as $value) {
@@ -152,6 +144,8 @@ class WebApplication
*/
header('Access-Control-Allow-Origin: *');
require_once $this->rootDir . "framework/src/Maveriks/Extension/Restler/UploadFormat.php";
// $servicesDir contains directory where Services Classes are allocated
$servicesDir = $this->workflowDir . 'engine' . DS . 'src' . DS . 'ProcessMaker' . DS . 'Services' . DS;
// $apiDir - contains directory to scan classes and add them to Restler
@@ -166,15 +160,27 @@ class WebApplication
/*
* Load Api ini file for Rest Service
*/
$apiIniConf = array();
$config = array();
if (file_exists($apiIniFile)) {
$apiIniConf = Util\Common::parseIniFile($apiIniFile);
$cachedConfig = $this->workspaceCacheDir . "api-config.php";
// verify if config cache file exists, is array and the last modification date is the same when cache was created.
if (! file_exists($cachedConfig) || ! is_array($config = include($cachedConfig)) || $config["_chk"] != filemtime($apiIniFile)) {
$config = Util\Common::parseIniFile($apiIniFile);
$config["_chk"] = filemtime($apiIniFile);
if (! is_dir(dirname($cachedConfig))) {
Util\Common::mk_dir(dirname($cachedConfig));
}
file_put_contents($cachedConfig, "<?php return " . var_export($config, true).";");
Util\Logger::log("Configuration cache was loaded and cached to: $cachedConfig");
} else {
Util\Logger::log("Loading Api Configuration from: $cachedConfig");
}
}
// 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';
Services\Api::setWorkspace(SYS_SYS);
// create a new Restler instance
//$rest = new \Luracast\Restler\Restler();
@@ -190,20 +196,13 @@ class WebApplication
// Setting database connection source
list($host, $port) = strpos(DB_HOST, ':') !== false ? explode(':', DB_HOST) : array(DB_HOST, '');
$port = empty($port) ? '' : ";port=$port";
\ProcessMaker\Services\OAuth2\Server::setDatabaseSource(DB_USER, DB_PASS, DB_ADAPTER.":host=$host;dbname=".DB_NAME.$port);
Services\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
\ProcessMaker\Services\OAuth2\Server::setPmClientId($pmOauthClientId);
Services\OAuth2\Server::setPmClientId($pmOauthClientId);
require_once $this->workflowDir . "engine/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) {
@@ -215,6 +214,7 @@ class WebApplication
$uri = str_replace('/plugin-'.$pluginName, '', $uri);
}
// Override $_SERVER['REQUEST_URI'] to Restler handles the modified url
$_SERVER['REQUEST_URI'] = $uri;
if (! $isPluginRequest) { // if it is not a request for a plugin endpoint
@@ -224,19 +224,17 @@ class WebApplication
foreach ($classesList as $classFile) {
if (pathinfo($classFile, PATHINFO_EXTENSION) === 'php') {
$namespace = '\\ProcessMaker\\Services\\' . str_replace(
DIRECTORY_SEPARATOR,
'\\',
str_replace('.php', '', str_replace($servicesDir, '', $classFile))
);
//var_dump($namespace); die;
DIRECTORY_SEPARATOR,
'\\',
str_replace('.php', '', str_replace($servicesDir, '', $classFile))
);
$rest->addAPIClass($namespace);
}
}
// adding aliases for Restler
//print_r($apiIniConf);
if (array_key_exists('alias', $apiIniConf)) {
foreach ($apiIniConf['alias'] as $alias => $aliasData) {
if (array_key_exists('alias', $config)) {
foreach ($config['alias'] as $alias => $aliasData) {
if (is_array($aliasData)) {
foreach ($aliasData as $label => $namespace) {
$namespace = '\\' . ltrim($namespace, '\\');
@@ -391,7 +389,10 @@ class WebApplication
require_once (PATH_DB . SYS_SYS . "/db.php");
// defining constant for workspace shared directory
define("PATH_WORKSPACE", PATH_DB . SYS_SYS . PATH_SEP);
$this->workspaceDir = PATH_DB . SYS_SYS . PATH_SEP;
$this->workspaceCacheDir = PATH_DB . SYS_SYS . PATH_SEP . "cache" . PATH_SEP;
define("PATH_WORKSPACE", $this->workspaceDir);
// including workspace shared classes -> particularlly for pmTables
set_include_path(get_include_path() . PATH_SEPARATOR . PATH_WORKSPACE);