2011-02-26 17:15:50 +00:00
|
|
|
<?php
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2011-02-26 17:15:50 +00:00
|
|
|
/**
|
|
|
|
|
* HttpProxyController
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2011-02-26 17:15:50 +00:00
|
|
|
* @author Erik Amaru Ortiz <erik@colosa.com, aortiz.erik@gmail.com>
|
|
|
|
|
* @package gulliver.system
|
|
|
|
|
* @access private
|
|
|
|
|
*/
|
2012-10-18 10:54:46 -04:00
|
|
|
class HttpProxyController
|
|
|
|
|
{
|
|
|
|
|
|
2011-02-26 17:15:50 +00:00
|
|
|
/**
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2011-02-26 17:15:50 +00:00
|
|
|
* @var array - private array to store proxy data
|
|
|
|
|
*/
|
2012-10-18 10:54:46 -04:00
|
|
|
private $__data__ = array ();
|
|
|
|
|
|
2011-02-26 17:15:50 +00:00
|
|
|
/**
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
|
|
|
|
* @var object - private object to store the http request data
|
2011-02-26 17:15:50 +00:00
|
|
|
*/
|
|
|
|
|
private $__request__;
|
2011-10-28 13:02:22 -04:00
|
|
|
|
|
|
|
|
public $jsonResponse = true;
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2011-08-19 16:47:44 -04:00
|
|
|
private $sendResponse = true;
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2013-01-31 11:44:52 -04:00
|
|
|
public function __construct() {
|
|
|
|
|
$this->__request__ = new stdclass();
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-26 17:15:50 +00:00
|
|
|
/**
|
|
|
|
|
* Magic setter method
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2011-02-26 17:15:50 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @param string $value
|
|
|
|
|
*/
|
2012-10-18 10:54:46 -04:00
|
|
|
public function __set ($name, $value)
|
|
|
|
|
{
|
2011-02-26 17:15:50 +00:00
|
|
|
//echo "Setting '$name' to '$value'\n";
|
|
|
|
|
$this->__data__[$name] = $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Magic getter method
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2011-02-26 17:15:50 +00:00
|
|
|
* @param string $name
|
|
|
|
|
* @return string or NULL if the internal var doesn't exist
|
|
|
|
|
*/
|
2012-10-18 10:54:46 -04:00
|
|
|
public function __get ($name)
|
|
|
|
|
{
|
2011-02-26 17:15:50 +00:00
|
|
|
//echo "Getting '$name'\n";
|
2012-10-18 10:54:46 -04:00
|
|
|
if (array_key_exists( $name, $this->__data__ )) {
|
2011-02-26 17:15:50 +00:00
|
|
|
return $this->__data__[$name];
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-26 22:57:37 +00:00
|
|
|
/*$trace = debug_backtrace();
|
2011-02-26 17:15:50 +00:00
|
|
|
trigger_error(
|
|
|
|
|
'Undefined property via __get(): ' . $name .
|
|
|
|
|
' in ' . $trace[0]['file'] .
|
|
|
|
|
' on line ' . $trace[0]['line'],
|
|
|
|
|
E_USER_NOTICE);
|
2011-02-26 22:57:37 +00:00
|
|
|
return null;*/
|
2011-02-26 17:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Magic isset method
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2011-02-26 17:15:50 +00:00
|
|
|
* @param string $name
|
|
|
|
|
*/
|
2012-10-18 10:54:46 -04:00
|
|
|
public function __isset ($name)
|
|
|
|
|
{
|
2011-02-26 17:15:50 +00:00
|
|
|
//echo "Is '$name' set?\n";
|
2012-10-18 10:54:46 -04:00
|
|
|
return isset( $this->__data__[$name] );
|
2011-02-26 17:15:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Magic unset method
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2011-02-26 17:15:50 +00:00
|
|
|
* @param string $name
|
|
|
|
|
*/
|
2012-10-18 10:54:46 -04:00
|
|
|
public function __unset ($name)
|
|
|
|
|
{
|
2011-02-26 17:15:50 +00:00
|
|
|
//echo "Unsetting '$name'\n";
|
2012-10-18 10:54:46 -04:00
|
|
|
unset( $this->__data__[$name] );
|
2011-02-26 17:15:50 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2011-02-26 17:15:50 +00:00
|
|
|
/**
|
|
|
|
|
* call to execute a internal proxy method and handle its exceptions
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2011-02-26 17:15:50 +00:00
|
|
|
* @param string $name
|
|
|
|
|
*/
|
2012-10-18 10:54:46 -04:00
|
|
|
public function call ($name)
|
2011-02-26 17:15:50 +00:00
|
|
|
{
|
|
|
|
|
try {
|
2012-10-18 10:54:46 -04:00
|
|
|
$result = $this->$name( $this->__request__ );
|
2011-10-28 13:02:22 -04:00
|
|
|
|
2012-10-18 10:54:46 -04:00
|
|
|
if (! $this->jsonResponse) {
|
2011-10-28 13:02:22 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
if (! $result) {
|
|
|
|
|
$result = $this->__data__;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-26 17:15:50 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
|
$result->success = false;
|
2011-08-30 09:35:32 -04:00
|
|
|
$result->message = $result->msg = $e->getMessage();
|
2012-10-18 10:54:46 -04:00
|
|
|
switch (get_class( $e )) {
|
|
|
|
|
case 'Exception':
|
|
|
|
|
$error = "SYSTEM ERROR";
|
|
|
|
|
break;
|
|
|
|
|
case 'PMException':
|
|
|
|
|
$error = "PROCESSMAKER ERROR";
|
|
|
|
|
break;
|
|
|
|
|
case 'PropelException':
|
|
|
|
|
$error = "DATABASE ERROR";
|
|
|
|
|
break;
|
|
|
|
|
case 'UserException':
|
|
|
|
|
$error = "USER ERROR";
|
|
|
|
|
break;
|
2011-02-26 17:15:50 +00:00
|
|
|
}
|
2011-11-17 12:42:13 -04:00
|
|
|
$result->error = $e->getMessage();
|
2012-10-18 10:54:46 -04:00
|
|
|
|
|
|
|
|
$result->exception->class = get_class( $e );
|
2011-02-26 17:15:50 +00:00
|
|
|
$result->exception->code = $e->getCode();
|
2011-07-08 19:06:32 -04:00
|
|
|
$result->exception->trace = $e->getTraceAsString();
|
2011-02-26 17:15:50 +00:00
|
|
|
}
|
2011-08-19 16:47:44 -04:00
|
|
|
|
|
|
|
|
if ($this->sendResponse) {
|
2012-10-18 10:54:46 -04:00
|
|
|
print G::json_encode( $result );
|
2011-08-19 16:47:44 -04:00
|
|
|
}
|
2011-02-26 17:15:50 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|
2011-02-26 17:15:50 +00:00
|
|
|
/**
|
|
|
|
|
* Set the http request data
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2011-02-26 17:15:50 +00:00
|
|
|
* @param array $data
|
|
|
|
|
*/
|
2012-10-18 10:54:46 -04:00
|
|
|
public function setHttpRequestData ($data)
|
2011-02-26 17:15:50 +00:00
|
|
|
{
|
2012-10-18 10:54:46 -04:00
|
|
|
if (is_array( $data )) {
|
|
|
|
|
while ($var = each( $data )) {
|
|
|
|
|
$this->__request__->$var['key'] = $var['value'];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2011-02-26 17:15:50 +00:00
|
|
|
$this->__request__ = $data;
|
2012-10-18 10:54:46 -04:00
|
|
|
}
|
2011-02-26 17:15:50 +00:00
|
|
|
}
|
2011-08-19 16:47:44 -04:00
|
|
|
|
2012-10-18 10:54:46 -04:00
|
|
|
public function setJsonResponse ($bool)
|
2011-10-28 13:02:22 -04:00
|
|
|
{
|
|
|
|
|
$this->jsonResponse = $bool;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-19 16:47:44 -04:00
|
|
|
/**
|
|
|
|
|
* Send response to client
|
2012-10-18 10:54:46 -04:00
|
|
|
*
|
2011-08-19 16:47:44 -04:00
|
|
|
* @param boolean $val
|
|
|
|
|
*/
|
2012-10-18 10:54:46 -04:00
|
|
|
public function setSendResponse ($val)
|
2011-08-19 16:47:44 -04:00
|
|
|
{
|
|
|
|
|
$this->sendResponse = $val;
|
|
|
|
|
}
|
2011-02-26 17:15:50 +00:00
|
|
|
}
|
2012-10-18 10:54:46 -04:00
|
|
|
|