<?php
ini_set('default_socket_timeout', 600);

class WsControllerCore extends InternoController
{
	
	
  public function indexAction()
    {   
    	$this->sinRender(); 
     //   $this->jsonRPC();
     //   die();
        if(isset($_GET['wsdl'])) {
            //return the WSDL
            $this->hadleWSDL();
        } else {
            //handle SOAP request
            $this->handleSOAP();
        }
    }

    private function hadleWSDL() {
        $autodiscover = new Zend_Soap_AutoDiscover();
        $ws = new Ws();
        $autodiscover->setClass('Ws');
        $autodiscover->handle();
        die();
    }
    
    private function jsonRPC(){
    	
    	$this->sinRender();
    	ob_clean();
    	// disable layouts and renderers
    	$this->getHelper('viewRenderer')->setNoRender(true);
    	
    	$server = new Zend_Json_Server();
    	$ws = new Ws();
    	$server->setClass('Ws');
    	ob_clean();
    	$server->handle();
    }
    
    private function handleSOAP() {
        $soap = new Zend_Soap_Server(null, array('uri'=>$this->_WSDL_URI)); 
        $ws = new Ws();
        $soap->setClass('Ws');
        $soap->handle();
    }
    
    public function clientAction() {
    	
    	/*
    	$ws = new Ws();
    	
    	echo $ws->login('21657026L', '3882');
    	
    	die();
    	*/
    	$options = array(
    			'location' => URL_SOAP,
    			'uri'      => URL_SOAP
    	);
    	$client = new Zend_Soap_Client(null, $options);
    	
        $client = new Ws();
			
    	
    	print_r($client->login('lcc', '220997')); 
       // $this->view->add_result = $client->math_add(11, 55);
        //$this->view->not_result = $client->logical_not(true);
        //$this->view->sort_result = $client->simple_sort(
       //array("d" => "lemon", "a" => "orange",
       //      "b" => "banana", "c" => "apple"));
        die();
    }
    
    
    
    public function soapAction()
    {$this->sinRender(); 
    ob_clean();
    	// disable layouts and renderers
    	$this->getHelper('viewRenderer')->setNoRender(true);
    
    	// initialize server and set URI
    	//$server = new Zend_Soap_Server(null, array('uri' => 'http://example.localhost/index/soap'));
    	$server = new Zend_Soap_Server( null , array('uri'=>URL_WSDL));
    	// set SOAP service class
    	$ws = new Ws();
    	$server->setClass('Ws');
    	ob_clean();
    	// register exceptions that generate SOAP faults
    	//$server->registerFaultException(array('Example_Exception'));
    
    	// handle request
    	$server->handle();
    	die();
    }
    
    public function wsdlAction(){
    
    $this->sinRender();
    ob_clean(); 
    
    	// disable layouts and renderers
    	$this->getHelper('viewRenderer')->setNoRender(true);
    
    	// set up WSDL auto-discovery
    	$wsdl = new Zend_Soap_AutoDiscover();
    	$ws = new Ws();
    	// attach SOAP service class
    	$wsdl->setClass('Ws');
    	ob_clean();
    	// set SOAP action URI
    	$wsdl->setUri(URL_SOAP);
    
    	// handle request
    	$wsdl->handle();
    	die();
    }

    public function jsonrpcAction(){
    	
    	set_time_limit(0);
    	print_r($_REQUEST);
		//die('aqui');
    	$this->sinRender();
    	//ob_clean();
    	// disable layouts and renderers
    	//$this->getHelper('viewRenderer')->setNoRender(true);
    	 
    	$server = new Zend_Json_Server();
    	$ws = new Ws();
    	$server->setClass('Ws');
    	ob_clean();
    	
    	if ('GET' == $_SERVER['REQUEST_METHOD']) {
    		// Indicate the URL endpoint, and the JSON-RPC version used:
    		
    		$server->setTarget(BASEURL.'/ws/jsonrpc')
    		->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
    	
    		// Grab the SMD
    		$smd = $server->getServiceMap();
    	
    		// Return the SMD to the client
    	//	header('Content-Type: application/json');
    		echo $smd;
    		die();
    	}
    	//
    	$server->handle();
    	
    	die();
    }
    
    
    public function verificacionAcl(){
    	
    }
    
}