<?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();
			$aryExport = array('Username' => $aryArgs[0], 'Password' => $aryArgs[1], 'RequestID' => $aryArgs[2],'UserID' => $aryArgs[3],'ReceiverID' => $aryArgs[4],'ServiceID' => $aryArgs[5],'CommandCode' => $aryArgs[6], 'Info' => $aryArgs[7], 'ReceiveTime' => $aryArgs[8]);

			error_log(date('Y-m-d H:i:s').' -- VARIABLES: '. var_export($aryExport, TRUE). "\n", 3, '/srv/www/sbg/log/sms.log');
			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, $info);
			error_log(date('Y-m-d H:i:s').' -- INFO VAR: '. var_export($info, TRUE). "\n\n", 3, '/srv/www/sbg/log/sms.log');
			$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);
				$aryData = array();
				$aryData['message'] = $aryOutput[1];
				$xml = $this->load->view('mo', $aryData, TRUE);
				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 */ 