<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 15-5-5
 * Time: 下午2:41
 * To change this template use File | Settings | File Templates.
 */
class Default_DistribuctionController 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/distribuction.wsdl");
            $server->setClass('Api_ServiceForDistribuction');
            $server->handle();
        }else{
            echo 'Invalid SOAP request';
        }
        exit;
    }
    public function wsdlAction () {
        $host = $this->_request->getHttpHost();
        //echo $host;exit;
        header("Content-type: text/xml; Charset=utf-8");
        $content = file_get_contents(APPLICATION_PATH . "/../data/wsdl/distribuction.wsdl");
        $content = preg_replace('/exoms\.globex\.cn/', $host, $content);
        echo $content;
        exit();
    }
    public function wsdlFileAction () {
        $host = $this->_request->getHttpHost();
        $content =  file_get_contents(APPLICATION_PATH . "/../data/wsdl/distribuction.wsdl");
        $content = preg_replace('/exoms\.globex\.cn/',$host,$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.'-distribuction.wsdl';
        if(!file_exists($fileName)){
            file_put_contents($fileName, $content);
        }
        Common_Common::downloadFile($fileName);
        exit;
    }

}
