<?php
class cX2
{
   public $db;
   
   function __construct()
   {
      include('taxis_db.php');
      
      $this->db = ADONewConnection($coo->type);
      if ($coo->type==='oci8') {
         $this->db->charSet = 'utf8';
      }
      
      if (!$this->db->PConnect($coo->host, $coo->user, $coo->password, $coo->database))
         die( "Database connection failed! ".$this->db->ErrorMsg() );
         
      $GLOBALS['ADODB_FETCH_MODE'] = ADODB_FETCH_BOTH; // ADODB_FETCH_ASSOC, ADODB_FETCH_NUM, ADODB_FETCH_BOTH
      
      if ($this->db->databaseType==='mysqlt') {
         $query="SET NAMES 'utf8' COLLATE 'utf8_general_ci' ";
         if (!$this->db->Execute($query)) {
            $msg=$this->db->ErrorMsg();
            $this->db->Close();
            $this->db=false;
            die($msg);
         } 
      }    
      else if ($this->db->databaseType==='oci8') {
         //
         // Both next 2 commands are required to sort and compare case-insensitive
         //      
         $query="ALTER SESSION SET NLS_SORT=BINARY_CI"; // BINARY or any valid linguistic sort name
         if (!$this->db->Execute($query)) {
            echo thesi(__FILE__, __LINE__).$this->db->ErrorMsg();
         }
      
         $query="ALTER SESSION SET NLS_COMP=LINGUISTIC"; // BINARY , LINGUISTIC, or ANSI
         if (!$this->db->Execute($query)) {
            echo thesi(__FILE__, __LINE__).$this->db->ErrorMsg();
         }
      }
   }
   
   function __destruct()
   {
      if ($this->db!==false)
         $this->db->Close();
   }
}
//------------------------------------------------------------------------------
function getSignFromArray($esign)
{
   $bytes=array();
      
   foreach($esign as $hex) {
      $l=strlen($hex);
      // take care for padding chars. if on last block after 'AES decrypt'
      if ($l>40 && ord($hex{40})<=16) {
         $l=40;
      }
      
      for ($i=0;$i<$l;$i+=2) {
         $s1=substr($hex,$i,2);
         sscanf($s1, "%X", $x);
         $bytes[]= $x;
      }
   }
      
   $iv = call_user_func_array("pack", array_merge(array("C*"), $bytes));
      
   return strtoupper(sha1($iv)); 
}
//--------------------------------------
function HexToBin($src)
{
   $bytes=array();
   $l=strlen($src);           
   for ($i=0;$i<$l;$i+=2) {
      $s1=substr($src,$i,2);
      sscanf($s1, "%X", $x);
      $bytes[]= $x;
   }
   $bin = call_user_func_array("pack", array_merge(array("C*"), $bytes));
   return $bin;
}
//------------------------------------------------------------------------------
?>
