<?
function ORACLE_IMPORT(){if (isset($GLOBALS['ORACLE_IMPORT'])) return($GLOBALS['ORACLE_IMPORT']); else return($GLOBALS['ORACLE_IMPORT']=new i_oracle_import);}

class i_oracle_import {

    var $conn ;
    var $oracle_login ;
    var $oracle_pass ;

    function __construct() {
        $this->oracle_login=$GLOBALS['LS_esmo_gateway_ORACLE_login'];
        $this->oracle_pass=$GLOBALS['LS_esmo_gateway_ORACLE_password'];
        $this->connent_to_oracle() ;
    }

    function connent_to_oracle() {

        $connection_string=$this->get_connection_string();

        if ($GLOBALS['LS_esmo_gateway_ORACLE_connection_string']) $connection_string=$GLOBALS['LS_esmo_gateway_ORACLE_connection_string'];

        $this->conn = oci_connect($this->oracle_login, $this->oracle_pass, $connection_string,'AL32UTF8');
        if (!$this->conn) {
            $e = oci_error();
            trigger_error(htmlentities($e['message'], ENT_QUOTES), E_USER_ERROR);
            die() ;
        }
    }

    function get_connection_string() {
        $connection_array=[]; $result='';

        if ($GLOBALS['LS_esmo_gateway_ORACLE_ADDRESS_LIST_ADDRESS_PROTOCOL']) $connection_array['ADDRESS_LIST']['ADDRESS']['PROTOCOL']='(PROTOCOL = '.$GLOBALS['LS_esmo_gateway_ORACLE_ADDRESS_LIST_ADDRESS_PROTOCOL'].')';
        if ($GLOBALS['LS_esmo_gateway_ORACLE_ADDRESS_LIST_ADDRESS_HOST']) $connection_array['ADDRESS_LIST']['ADDRESS']['HOST']='(HOST = '.$GLOBALS['LS_esmo_gateway_ORACLE_ADDRESS_LIST_ADDRESS_HOST'].')';
        if ($GLOBALS['LS_esmo_gateway_ORACLE_ADDRESS_LIST_ADDRESS_PORT']) $connection_array['ADDRESS_LIST']['ADDRESS']['PORT']='(PORT = '.$GLOBALS['LS_esmo_gateway_ORACLE_ADDRESS_LIST_ADDRESS_PORT'].')';

        if (_sizeof($connection_array['ADDRESS_LIST']['ADDRESS'])) {
            $connection_array['ADDRESS_LIST'] = '(ADDRESS = '.implode('',$connection_array['ADDRESS_LIST']['ADDRESS']).')';
        }

        if ($GLOBALS['LS_esmo_gateway_ORACLE_CONNECT_DATA_SID']) $connection_array['CONNECT_DATA']['SID']='(SID = '.$GLOBALS['LS_esmo_gateway_ORACLE_CONNECT_DATA_SID'].')';
        if ($GLOBALS['LS_esmo_gateway_ORACLE_CONNECT_DATA_SERVER']) $connection_array['CONNECT_DATA']['SERVER']='(SERVER = '.$GLOBALS['LS_esmo_gateway_ORACLE_CONNECT_DATA_SERVER'].')';
        if ($GLOBALS['LS_esmo_gateway_ORACLE_CONNECT_DATA_SERVICE_NAME']) $connection_array['CONNECT_DATA']['SERVICE_NAME']='(SERVICE_NAME = '.$GLOBALS['LS_esmo_gateway_ORACLE_CONNECT_DATA_SERVICE_NAME'].')';

        if (_sizeof($connection_array['CONNECT_DATA'])) {
            $connection_array['CONNECT_DATA'] = '(CONNECT_DATA = '.implode('',$connection_array['CONNECT_DATA']).')';
        }

        if (_sizeof($connection_array)) {
            $result = '(DESCRIPTION = '.implode('',$connection_array).')';
        }

        return $result;
    }

    function execSQL_oracle($sql) {
        $recs=array() ;
        $stid = oci_parse($this->conn, $sql);
        oci_execute($stid);
        while ($row = oci_fetch_array($stid, OCI_ASSOC+OCI_RETURN_NULLS)) $recs[]=$row ;
        return($recs) ;
    }

}

?>
