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

class Viettel extends MX_Controller 
{
	
	private $_server = null;
	
	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 () 
	{
		if($this->uri->segment(4) == "wsdl") {
			header('Content-Type: text/xml; charset: utf-8');
			$this->load->view('mowsdl');
		} else {
			header('Content-Type: text/html; charset: utf-8');
			echo 'See service <a href="'.base_url().'violetservice/soanbai/mo/wsdl">WSDL page</a>';
		}			
	}
	
	public function mo() 
	{	
		function messageReceiver(){
			$aryArgs = func_get_args();
			list($username, $password, $requestID, $userID, $receiverID, $serviceID, $commandCode, $info, $receiveTime) = $aryArgs;
			
			$msgType = '0'; //text message
			$errCode = '1'; //#1:error; 1:Success;
			
			$CI =& get_instance();			
			$mo_username = $CI->config->item('mo_username');
			$mo_password = $CI->config->item('mo_password');

			if (!$username) return '0|0|Username is required!';
			if (!$password) return '0|0|Password is required!';
			if ($username != $mo_username) return '0|0|Username is not corrected!';
			if ($password != $mo_password) return '0|0|Password is not corrected!';
			if (!$requestID) return '0|0|requestID is required!';			
			if (!$userID) return '0|0|UserID is required!';
			if (!$serviceID) return '0|0|serviceID is required!';
			if ($serviceID != '8062') return '0|0|serviceID is not corrected!';

			$CI->load->model('Services_model');
			$CI->load->model('frontend/User_model', 'objUserModel');
			$result = $CI->Services_model->processMORequest($userID, strtoupper($info));
			$CI->objUserModel->insertSmslog($receiverID, $serviceID, $commandCode, $info, $receiveTime);
					
			$aryResult = explode ('|', $result);
			return $aryResult[0].'|'.$msgType.'|'.$aryResult[1];
		}
		
		$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().'violetservice/soanbai/mo/wsdl">WSDL page</a>';
			}
		}
		catch (Exception $e) {
			$this->_server->fault('Sender', $e->getMessage());
		}
	}
}

/* End of file viettel.php */
/* Location: ./application/modules/service/controllers/viettel.php */ 