<?php

abstract class WebServiceOperator
{
    
    protected $xml_IN = null;
    
    protected $xml_OUT = null;
    
    protected $xml_FAULT = null;
    
    protected $hFault = false;
    
    protected $xmlStruct = null;
    
    protected $connecteur = null;
    
    protected $name = null;
    
    private $log;
    
    private $logsql = '';
    
    function __construct($className = null)
    {
        $this->name = $className;
        
        $this->xmlStruct = new XMLStruct();
        $this->xmlStruct->addNamespace('SOAP-ENV', 'http://schemas.xmlsoap.org/soap/envelope/');
        $this->xmlStruct->addNamespace('SOAP-ENC', 'http://schemas.xmlsoap.org/soap/encoding/');
        $this->xmlStruct->addNamespace('soapenv', 'http://schemas.xmlsoap.org/soap/envelope/');
        $this->xmlStruct->addNamespace('xsd', 'http://www.w3.org/2001/XMLSchema');
        $this->xmlStruct->addNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
        $this->xmlStruct->addNamespace('hel', 'http://helloworldwebservice.lisi.ensma.fr/');
        $this->xmlStruct->addNamespace('not', "http://notebookwebservice.lisi.ensma.fr/");
    }
    
    abstract protected function works();
    
    public function init()
    {
        global $HTTP_RAW_POST_DATA;
        $HTTP_RAW_POST_DATA = file_get_contents("php://input");

        $this->xmlStruct->input($HTTP_RAW_POST_DATA);
        $this->xmlStruct->cd('//SOAP-ENV:Envelope/SOAP-ENV:Body'); // UNIPAAS
    }
    
    public function load(& $xml)
    {
        $this->xmlStruct->input($xml);
    }
    
    public function setXMLIn(& $xml)
    {
        $this->xml_IN = $xml;
    }
    
    public function &getXMLOut()
    {
        if (empty($this->xml_FAULT))
            return $this->xml_OUT;
            else
                return $this->xml_FAULT;
    }
    
    public function addToXMLOut($xml)
    {
        $this->xml_OUT .= $xml;
    }
    
    public function addLogSQL($sql)
    {
        if (! empty($this->logsql))
            file_put_contents($this->logsql, date('[d/m/Y H:i:s] | ') . $this->name . ' | ' . $sql . "\r\n", FILE_APPEND);
    }
    
    public function setPathToLogSQL($path)
    {
        $this->logsql = $path;
    }
    
    public function &getPathLogSQL()
    {
        return $this->logsql;
    }
    
    public function hasFault()
    {
        return $this->hFault;
    }
    
    public function setConnecteur($co)
    {
        $this->connecteur = $co;
    }
    
    public function &getConnecteur()
    {
        return $this->connecteur;
    }
    
    public function &getName()
    {
        return $this->name;
    }
    
    public function &sendResponse()
    {
        // Version UNIPAAS
        if ($_SESSION['web_test']??'')
            echo '<?xml-stylesheet href="php/dev/main.xsl" type="text/xsl" ?>';
            
            $this->log = '<SOAP-ENV:Envelope xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Body><global>' . "\r\n";
            
            $this->log .= $this->xml_OUT;
            
            /*
             * if (!empty($this->xml_FAULT))
             * {
             * $this->log .= '<SOAP-ENV:Fault>';
             * $this->log .= $this->xml_FAULT;
             * $this->log .= '</SOAP-ENV:Fault>';
             * }
             */
             
             $this->log .= '</global></SOAP-ENV:Body>' . "\r\n";
             $this->log .= '</SOAP-ENV:Envelope>' . "\r";
             // FIN VERSION UNIPAAS
             
             header("Content-length: " . strlen($this->log));
             print $this->log;
             
             file_put_contents($_SESSION['dir_tmp'] . 'OXALINK_WSDL_FAULT.txt', $this->xml_FAULT);
             
             return $this->log;
    }
    
    public function fault($error)
    {
        $this->xml_FAULT .= '<soap:faultstring xsi:type="xsd:string">' . htmlspecialchars(utf8_encode($error)) . '</soap:faultstring>';
        $this->hFault = true;
    }
    
    public function XMLFault($fxml)
    {
        $this->xml_FAULT .= $fxml;
        $this->hFault = true;
    }
}