<?
define('MAX_SEGMENT_SIZE',65535);
function IB(){ if (!is_object($GLOBALS['IB'])) $GLOBALS['IB']=new i_IB();return($GLOBALS['IB']);}
function IB_WORK(){ if (!is_object($GLOBALS['IB_WORK'])) $GLOBALS['IB_WORK']=new i_IB_work();return($GLOBALS['IB_WORK']);}

class i_IB_work extends i_IB
{
 function __construct()
 {  parent::__construct();
	//$this->db_name='localhost:/var/www/IB/medinterfacebace.fdb' ; $this->login='SYSDBA' ; $this->pass='123456' ;// БУФЕРНАЯ БАЗА тестовая
    //$this->db_name='172.20.5.253:E:\ACS\Base\medinterface.fdb' ;  $this->login='SYSDBA' ; $this->pass='masterkey' ;// БУФЕРНАЯ БАЗА РАБОЧАЯ
    $this->db_name='172.20.5.253:E:\ACS\Base\ACS.FDB' ; $this->login='SYSDBA' ; $this->pass='masterkey' ;// БОЕВАЯ БАЗА СКД
 }

}

class i_IB
{ var $db_name ;
  var $login ;
  var $pass ;

  function __construct()
  {  //$this->db_name='localhost:/var/www/IB/medinterfacebace.fdb' ; $this->login='SYSDBA' ; $this->pass='123456' ;// БУФЕРНАЯ БАЗА тестовая
     $this->db_name='172.20.5.253:E:\ACS\Base\medinterface.fdb' ;  $this->login='SYSDBA' ; $this->pass='masterkey' ;// БУФЕРНАЯ БАЗА РАБОЧАЯ
     //$this->db_name='172.20.5.253:E:\ACS\Base\ACS.FDB' ; $this->login='SYSDBA' ; $this->pass='masterkey' ;// БОЕВАЯ БАЗА СКД
  }

  function execSQL($sql,$options=array())
  {   $db_name=($options['db_name'])? $options['db_name']:$this->db_name ;
      $dbh = ibase_connect($db_name,$this->login,$this->pass);
      $sth = ibase_query($dbh, $sql);
      //print_r($dbh) ; print_r($sth) ;
      $i=0 ; $recs=array() ;
      while ($row = ibase_fetch_assoc($sth))
        {  //echo $i.'<br>' ;
            foreach($row as $id=>$value) 
            {   if (isset($options['BLOB'][$id]))
                {   //$desc_file=$options['BLOB'][$id] ;
                    $desc_file=_DIR_TO_ROOT.'/temp/blob_'.$i.'.jpg' ;
                    file_put_contents($desc_file,$this->blob_extract($dbh,$value)) ;
                    $recs[$i][$id]=$desc_file ;
                }
                else $recs[$i][$id]=iconv('windows-1251','utf-8',$value) ;
            }    
            $i++ ;
        }
      ibase_free_result($sth);
      ibase_close($dbh);
      return($recs) ;

  }
  function execSQL_BLOB($sql,$options=array())
  {   $db_name=($options['db_name'])? $options['db_name']:$this->db_name ;
      $dbh = ibase_connect($db_name,$this->login,$this->pass);
      $sth = ibase_query($dbh, $sql);
      //print_r($dbh) ; print_r($sth) ;
      $i=0 ; $recs=array() ;
      while ($row = ibase_fetch_assoc($sth))
        {   foreach($row as $id=>$value)
            {   $desc_file=($options['file_name'])? $options['file_name']:_TEMP_DIR.'blob_'.$id.'_'.$i.'.jpg' ;
                file_put_contents($desc_file,$this->blob_extract($dbh,$value)) ;
                $recs[$i][$id]=$desc_file ;
            }
            $i++ ;
        }
      ibase_free_result($sth);
      ibase_close($dbh);
      return($recs) ;

  }

  function execSQL_update($sql)
  {   $dbh = ibase_connect($this->db_name,$this->login,$this->pass);
      ibase_query($dbh, $sql);
      ibase_close($dbh);
  }

  function blob_create($data) 
  {	if(strlen($data) == 0) return false;
    $dbh = ibase_connect($this->db_name,$this->login,$this->pass);
	$handle = ibase_blob_create($dbh);
	$len = strlen($data);
	for ($pos = 0; $pos < $len; $pos += MAX_SEGMENT_SIZE) 
    {
		$buflen = ($pos + MAX_SEGMENT_SIZE > $len) ? ($len - $pos) : MAX_SEGMENT_SIZE;
		$buf = substr($data,$pos,$buflen);
		ibase_blob_add($handle,$buf);
	}
    $res=ibase_blob_close($handle);
    ibase_close($dbh);
	return($res) ;
  }    

  function blob_extract($sth,$blob_id) 
  {  	if (!$blob_id)  return "";
   		list ($length,$numseg,$maxseg,$stream,$isnull) = ibase_blob_info($sth,$blob_id);
   		$handle = ibase_blob_open($sth,$blob_id);
   		$x = "dummy";
   		$data = "";
   		while($x != "") 
        {
   			$x = ibase_blob_get($handle,1024);
   			$data .= $x;
   		}
   		ibase_blob_close($handle);
   		return $data;
   	}

   function add_MO_result($pers_id,$dopusk,$mo_id,$mo_time,$srok_time)
   {
       //IB()->execSQL_update("insert into EVENTS_IN3 (PERS_ID,RESULT,ID,DATE_OSMOTRA,DATE_DEISTVIJA) values (100046,'T',3,'1999-06-25 20:45:45','1999-06-25 20:45:45');") ;
       $result=($dopusk==1)? 'T':'F' ;
       if (!$mo_time) $mo_time=time() ;
       if (!$srok_time) $srok_time=time()+60*60*2 ;

       $date_from=date('d-M-Y H:i:s',$mo_time) ;
       $date_to=date('d-M-Y H:i:s',$srok_time) ;

       $sql="insert into EVENTS_IN (PERS_ID,RESULT,ID,DATE_OSMOTRA,DATE_DEISTVIJA) values ($pers_id,'$result',$mo_id,'$date_from','$date_to');" ;
       echo $sql.'<br>' ;
       IB()->execSQL_update($sql) ;
       //IB()->execSQL_update("insert into EVENTS_IN (PERS_ID,RESULT,ID,DATE_OSMOTRA,DATE_DEISTVIJA) values (100029,'T',4,'".date('d-M-Y H:i:s')."','".date('d-M-Y')." 21:00:00');") ;
       //echo date('d-M-Y H:i:s') ;
       //print_r($dbh) ;

   }

}
?>