<?php
/**
* Servicio Wweb
*
*/
//include_once(dirname(__FILE__).'/../../lib/Serializer.class.php');
require_once('/websites/indatcom/panel/lib/Serializer.class.php');
class Core_WebService
{
/**
* Constante para representar JSON
* @access private
* @var int
*/
const JSON = 0;
/**
* Constante para representar el formato de URL
* @access private
* @var int
*/
const URL = 1;
/**
* Constante para representar el formato de XML
* @access private
* @var int
*/
const XML = 2;
/**
* Constante para representar el formato de PHP
* @access private
* @var int
*/
const PHP = 3;
/**
* Aplicacion en ejecucion
* @access private
* @var string
*/
private $_app = '';
/**
* Module al que pertenece el servicio
* @access private
* @var string
*/
private $_module = '';
/**
* nombre del servicio representado
* @access private
* @var string
*/
private $_service = '';
/**
* Parametros que resive el servicio
* @access private
* @var array
*/
private $_params = array();
/**
* Accion a ser ejecutado por el cliente
* @access private
* @var array
*/
private $_action = array();
/**
* Debug a ser mostrado por el usuario
* @access private
* @var array
*/
private $_log = array();
/**
* Formato en que se devuelve el resultado del servicio
* @access private
* @var int
*/
private $_format = 0;
/**
* Valor devuelto despues de ser eecutado el servidor
* @access private
* @var string
*/
private $_result = '';
private $_executed = false;
/**
* Constructor
*
* Resibe como argumento el modulo, servicio, el formato de respuesta y los parametros
* de inicialización del sevicio. En el caso del formato de respuesta se verifica
* que sea uno de los definidos en la clase, por defecto de usa JSON. El formato de los
* parametros debe ser:
*
* $_param['var1'] = valor1;
* $_param['var2'] = valor2;
* ...
*
* Este formato debe estandar para mandar llamar un servicio.
*
* @access public
*
* @param $__module string Modulo al que pertenece el servicio.
* @param $__service string Nombre del servicio.
* @param $__params array Lista de argumentos.
*/
public function Core_WebService(&$app)
{
$this->_app = &$app;
}
/**
* Inicializa el servicio
*
*/
public function init()
{
$this->_db = $this->_app->manager_db->getConnection();
$module = new Core_VarWeb("_module",array('GET','POST'));
$service = new Core_VarWeb("_service",array('GET','POST'));
$format = new Core_VarWeb("_format",array('GET','POST'));
$params = new Core_VarWeb("_params",array('GET','POST'));
$this->setModule($module->getValue());
$this->setService($service->getValue());
$this->setFormat($format->getValue());
$this->setParams($params->getValue());
}
/**
* Asigna el module
*
* Toma com argumento un nombre de modulo y lo asigna
* a la propiedad privada
*
* @param $__module string Modulo al que pertenece el servicio.
*/
public function setModule($__module)
{
$this->_module = $__module;
}
/**
* Asigna el nombre de servicio
*
* Toma com argumento un nombre del servicio y lo asigna a la
* propiedad privada.
*
* @access public
* @param $__service string Nombre del servicio
*/
public function setService($__service)
{
$this->_service = $__service;
}
/**
* Asigna los parametros a pasar al servicio
*
* Toma como argumento una lista de los argumentos.
* Cada uno de los argumentos pueden ser de tipo simple
* como string, int, float o pueden ser variables mas complejas
* como Object, array, etc. y asi sucesivamente.
* Es importante si se utiliza json o XML inicializar que tipo
* de contenedor se utilizara.
*
* @access public
* @param $__service string Nombre del servicio
*/
public function setParams($__params)
{
$this->_params = $__params;
}
/**
* Define el formato de respuesta del servicio
*
* El formato de los
* parametros debe ser:
*
* $_param['var1'] = valor1;
* $_param['var2'] = valor2;
* ...
*
* Este formato debe estandar para mandar llamar un servicio.
*
* @access public
* @param $__service string Nombre del servicio
*/
public function setFormat($__format)
{
switch($__format)
{
case self::JSON:
$this->_format = self::JSON;
break;
case self::URL:
$this->_format = self::URL;
break;
case self::XML:
$this->_format = self::XML;
break;
case self::PHP:
$this->_format = self::PHP;
break;
default:
$this->_format = self::JSON;
break;
}
}
/**
* Asigna el codigo a ser ejecutado por el cliente
*
* Asigna el codigo normalmente javascript que debe
* ser ejecutado por el cliente.
*
* @param string $action
* @retun void
*/
public function setAction($action)
{
$this->_action = $action;
}
/**
* Obtiene la accion a ser ejecutado por el cliente
*
* @return string
*/
public function getAction()
{
return $this->_action;
}
/* Asigna el codigo a ser mostrado como log en el cliente
*
*
* @param string $debug
* @retun void
*/
public function setLog($log)
{
$this->_log = $log;
}
/* Agrega un log a ser enciado al cliente
*
*
* @param string $type
* @param string $msg
* @retun void
*/
public function addLog($type,$msg)
{
$this->_log[] = array($type, $msg);
}
/* Agrega un log del tipo Notice
*
*
* @param string $msg
* @retun void
*/
public function addNotice($msg)
{
$this->_log[] = array('Notice', $msg);
}
/* Agrega un log del tipo Warning
*
*
* @param string $msg
* @retun void
*/
public function addWarn($msg)
{
$this->_log[] = array('Warning', $msg);
}
/* Agrega un log del tipo Error
*
*
* @param string $msg
* @retun void
*/
public function addError($msg)
{
$this->_log[] = array('Error', $msg);
}
/**
* Obtiene el log a ser mostrado por el cliente
*
* @return string
*/
public function getLog()
{
return $this->_log;
}
/**
* No permite obtener el valo de constantes desde una instancia
*
* Recibe como argumento el nombre de la constante.
*
* @access public
* @var string Nombre de la constante.
*
*/
public static function __get($name)
{
if(defined("self::$name"))
{
return constant("self::$name");
}
trigger_error ("$name no esta definido");
}
public function getModule()
{
return $this->_module;
}
public function getName()
{
return $this->_service;
}
public function getParams()
{
return $this->_params;
}
public function getResult()
{
$result = null;
$result->code = $this->getCode();
$result->comment = $this->getComment();
if($this->_format == self::URL)
$result->value = addcslashes($this->processURLEncoding($this->_result),'=,&');
else
$result->value = $this->_result;
$result->action = $this->getAction();
$result->log = $this->getLog();
return $this->processFormat( $result );
}
public function execute($_value)
{
$this->_result = $_value ;
$this->_executed = true;
}
private function getCode()
{
if(!$this->_executed)
return 401;
if($this->_result == false)
return 400;
if($this->_result != false AND $this->_result != '' AND $this->_result != null)
return 200;
}
private function getComment()
{
switch($this->getCode())
{
case 200:
return 'ok';
break;
case 400:
return 'error';
break;
case 401:
return 'service no found';
break;
}
}
private function processFormat( $object )
{
switch($this->_format)
{
case self::JSON:
return Serializer::serialize($object,'js');
case self::URL:
return $this->ProcessURLEncoding($object);
case self::XML:
return Serializer::serialize($object,'xml');
case self::PHP:
return Serializer::serialize($object,'php');
}
}
/*
private function ProcessFormat( $object )
{
switch($this->_format)
{
case self::_JSON:
return json_encode($object);
case self::_URL:
return $this->ProcessURLEncoding($object);
case self::_XMLRPC:
return xmlrpc_encode($object);
}
}
*/
private function processURLEncoding($a)
{
$s = '';
foreach($a as $key => $value )
{
if($s != '') $s .= '&';
$s .= $key.'='.$value;
}
return $s;
}
}
?>