<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * Moservice Class
 * 
 * @author dzungnv02
 *
 */

class Moservice extends MX_Controller 
{
	private $_wsdl = 'http://210.211.97.53/mo.wsdl';
	private $_server = null;
		
	public function __construct() 
	{
		parent::__construct();
		ini_set("soap.wsdl_cache_enabled","1");		
		$this->_server = new SoapServer(NULL,
					array('soap_version' => SOAP_1_2, 'encoding' => 'UTF-8', 'uri' => 'http://tempuri.org/')
				);
		
	}
	
	public function index () 
	{
		function messageReceiver()
		{
			$aryArgs = func_get_args();
			list($username, $password, $requestID, $userID, $receiverID, $serviceID, $commandCode, $contentType ,$info, $receiveTime) = $aryArgs;
			return '1|0|'.$commandCode.$serviceID;
		}
		
		$this->_server->addFunction('messageReceiver');		

		try {
			ob_start();
			$soapOutput = '';
			$this->_server->handle();
			
			if (ob_get_length() > 0) {
				$soapOutput = ob_get_clean();
			}			
			
			if ($soapOutput != '') { 
				$pattern = '/<[^>]*[^\/]>/i';
				$aryOutput = preg_split ($pattern, $soapOutput, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
				
				$xml = '<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
						   <S:Body>
						      <messageReceiverResponse xmlns="http://tempuri.org/">
						         <return>'.$aryOutput[1].'</return>
						      </messageReceiverResponse>
						   </S:Body>
						</S:Envelope>';
				
				header('Content-Type: text/xml; charset: utf-8');
				echo $xml;
			}
			else {
				header('Content-Type: text/html; charset: utf-8');
				echo 'See service <a href="'.base_url().'services/moservice/wsdl">WSDL page</a>';
			}
		}
		catch (Exception $e) {
		    $this->_server->fault('Sender', $e->getMessage());
		}		
	}
	
	public function wsdl ()
	{
		header('Content-Type: text/xml; charset: utf-8');
		echo file_get_contents($this->_wsdl);
	}
}
