<?php
class Default_CostSoapController extends Zend_Controller_Action{
	public function preDispatch(){
		$this->tplDirectory	= "default/views/default/";
        ini_set("soap.wsdl_cache_enabled", "0");
	}
	public function indexAction () {
		$this->_forward('wsdl');
	}
	
	public function webServiceAction () {
		$input = file_get_contents('php://input');
		if(!empty($input)){
			$server = new SoapServer(APPLICATION_PATH . "/../data/wsdl/ServiceForCost.wsdl");
			$server->setClass('Api_ServiceForCost');
			$server->handle();
		}else{
			echo 'Invalid SOAP request';
		}
		exit;
	}
	public function wsdlAction () {
        $host = $this->_request->getHttpHost();
        $scheme = $this->_request->getScheme();
        $server = 'soap:address location="'.$scheme.'://'.$host;

        header("Content-type: text/xml; Charset=utf-8");
        $content = file_get_contents(APPLICATION_PATH . "/../data/wsdl/ServiceForCost.wsdl");
        $content = preg_replace('/soap:address location=\"(http\:\/\/|https\:\/\/)?\S+(\.com|\.cn)/', $server, $content);

        echo $content;
        exit();
	}
	public function wsdlFileAction () {

        $host = $this->_request->getHttpHost();
        $scheme = $this->_request->getScheme();
        $server = 'soap:address location="'.$scheme.'://'.$host;

		$content =  file_get_contents(APPLICATION_PATH . "/../data/wsdl/ServiceForCost.wsdl");
        $content = preg_replace('/soap:address location=\"(http\:\/\/|https\:\/\/)?\S+(\.com|\.cn)/', $server, $content);

		$fileName = preg_replace('/([a-zA-Z_0-9]+)\.([a-zA-Z_0-9]+)\.([a-zA-Z_0-9]+)/e', 'strtolower(\\1)', $host);
		$fileName = APPLICATION_PATH.'/../data/cache/'.$fileName.'-ServiceForOrder.wsdl';
		if(!file_exists($fileName)){
			file_put_contents($fileName, $content);
		}
		Common_Common::downloadFile($fileName);
		exit;
	}
}