<?php
define('idDiGIR_searchResponse','$Id: DiGIR_searchResponse.php,v 1.2 2003/05/06 20:33:23 vieglais Exp $');
if (defined('idDIGIR_VERSION_REQUEST'))
    return;

require_once('sock_http.php');

/**
* This class reads a DiGIR search response document and extracts the records,
* keeping them in an internal array.  It is meant to be used with the
* multiTarget class to perform multiple simultaneous data retrieval threads,
* but can also be used independently by calling the Load() method (implemented
* in xmlTarget class).
*/
class DiGIRSearchResponse extends xmlTarget
{
    //array or arrays - each sub array is a record indexed by column numbers
    var $records;
    
    //map of column names to column numbers for the records.
    var $columns;

    //maximum number of records to retrieve
    var $maxrecs;
    
    //internal stuff
    var $_inRecords;
    var $_rowBuf;
    var $_cIndex;
    var $_recordCount;

    //function DiGIRSearchResponse($id,$host,$port,$path,$data)
    function DiGIRSearchResponse($id,$URL,$maxRecs=100)
    {
        $res = parse_url($URL);
        $host = $res['host'];
        $port = 80;
        if (isset($res['port']))
            $port = $res['port'];
        $path = $res['path'];
        $data = $res['query'];
        xmlTarget::xmlTarget($id,$host,$port,$path,$data);
        $this->records = array();
        $this->columns = array();
        $this->maxrecs = $maxRecs;

        $this->_recordCount = 0;
        $this->_inRecord = FALSE;
        $this->_rowBuf = array();
        $this->_cIndex = null;
        $this->_inElement = FALSE;
    }

    function _startelement($parser, $name, $attrs)
    {
        if ($this->_inRecord)
        {
            $this->_inElement = TRUE;
            $res = array_search($name,$this->columns);
            if ($res === FALSE)
            {
                $this->_cIndex = array_push($this->columns,$name)-1;
            }
            else
                $this->_cIndex = $res;
        }
        if (strcasecmp($name,"record") == 0)
        {
            if ($this->_recordCount < $this->maxrecs)
                $this->_inRecord = TRUE;
        }
    }

    function _endelement($parser, $name)
    {
        $this->_inElement = FALSE;
        if (!$this->_inRecord)
            return;
        if (strcasecmp($name,"record") == 0)
        {
            $this->_inRecord = FALSE;
            array_push($this->records,$this->_rowBuf);
            $this->_rowBuf = array();
            $this->_recordCount ++;
        }
    }

    function _cdata($parser,$data)
    {
        if ($this->_inElement)
        {
            $tmp = "";
            if (isset($this->_rowBuf[$this->_cIndex]))
                $tmp = $this->_rowBuf[$this->_cIndex];
            $tmp .= $data;
            $this->_rowBuf[$this->_cIndex]= $tmp;
        }
    }

    function _pidata($parser,$target,$pidata)
    {
    }

    function _defaulthandler($parser,$data)
    {
    }

    function _updecl($parser,
                     $entity_name,
                     $base,
                     $system_id,
                     $public_id,
                     $notation_name)
    {
    }

    function _notdecl($parser, $notation_name, $base, $system_id, $public_id)
    {
    }

    function _extref($parser, $open_entity_names, $base, $system_id, $public_id)
    {
        return 1;
    }

    function _namespace($parser,$name,$space)
    {
    }

}

//////////////////////////////////////////////////////////////////

/**
* This class reads a DiGIR search response document and extracts the records,
* caching them to disk files.  It is meant to be used with the
* multiTarget class to perform multiple simultaneous data retrieval threads,
* but can also be used independently by calling the Load() method (implemented
* in xmlTarget class).
*/
class DiGIRSearchFiler extends xmlTarget
{
    var $fHDest;
    var $fRDest;
    var $fDDest;

    var $section;
    var $cbuffer;

    function DiGIRSearchFiler($id,$URL,&$fHeaderDest,&$fRecordsDest,&$fDiagsDest)
    {
        $res = parse_url($URL);
        $host = $res['host'];
        $port = 80;
        if (isset($res['port']))
            $port = $res['port'];
        $path = $res['path'];
        $data = $res['query'];
        
        xmlTarget::xmlTarget($id,$host,$port,$path,$data);
        $this->fHDest = $fHeaderDest;
        $this->fRDest = $fRecordsDest;
        $this->fDDest = $fDiagsDest;
        $this->section = "";
        $this->cbuffer = "";
    }

    function _startelement($parser, $name, $attrs)
    {
        //look for namespaces to add
        foreach ($attrs as $k=>$v)
        {
            if (strpos($k,"xmlns") === 0)
            {
                $this->addNameSpace($k,$v);
            }
        }
        if (($this->section == "") &&
            ((strcasecmp($name,"header") == 0) ||
             (strcasecmp($name,"record") == 0) ||
             (strcasecmp($name,"diagnostic") == 0)))
        {
            $this->section = $name;
            $this->cbuffer = "<".$name;
            foreach ($attrs as $n=>$v)
            {
                $this->cbuffer .= " ".$n."='".$v."'";
            }
            $this->cbuffer .= ">";
            return;
        }
        if ($this->section != "")
        {
            $this->cbuffer .= "<".$name;
            foreach ($attrs as $n=>$v)
            {
                $this->cbuffer .= " ".$n."='".$v."'";
            }
            $this->cbuffer .= ">";
        }
    }

    function _endelement($parser, $name)
    {
        if (($this->section == "header") &&
            (strcasecmp($name,"header") == 0))
        {
            $this->section = "";
            $this->cbuffer .= "</".$name.">";
            fputs($this->fHDest,$this->cbuffer);
            $this->cbuffer = "";
            return;
        }
        if (($this->section == "record") &&
            (strcasecmp($name,"record") == 0))
        {
            $this->section = "";
            $this->cbuffer .= "</".$name.">";
            fputs($this->fRDest,$this->cbuffer);
            $this->cbuffer = "";
            return;
        }
        if (($this->section == "diagnostic") &&
            (strcasecmp($name,"diagnostic") == 0))
        {
            $this->section = "";
            $this->cbuffer .= "</".$name.">";
            fputs($this->fDDest,$this->cbuffer);
            $this->cbuffer = "";
            return;
        }
        if ($this->section != "")
        {
            $this->cbuffer .= "</".$name.">";
            return;
        }
    }

    function _cdata($parser,$data)
    {
        if ($this->section != "")
        {
            $this->cbuffer .= $data;
        }
    }

}

/**
* This class reads a DiGIR search response document and extracts the records,
* caching them to disk files.  It is meant to be used with the
* multiTarget class to perform multiple simultaneous data retrieval threads,
* but can also be used independently by calling the Load() method (implemented
* in xmlTarget class).
*/
class DiGIRSearchFiler2 extends xmlTarget
{
    var $fHDest;
    var $fRDest;
    var $fDDest;
    var $recCount;

    var $section;
    var $cbuffer;

    function DiGIRSearchFiler2($id,$URL,$payload,&$fHeaderDest,&$fRecordsDest,&$fDiagsDest,$method='GET')
    {
        $res = parse_url($URL);
        $host = $res['host'];
        $port = 80;
        if (isset($res['port']))
            $port = $res['port'];
        $path = $res['path'];
        $data = '';
        if (isset($res['query']))
            $data = $res['query'];
        if (is_null($payload))
            $payload = $data;

        xmlTarget::xmlTarget($id,$host,$port,$path,$payload);
        $this->fHDest = $fHeaderDest;
        $this->fRDest = $fRecordsDest;
        $this->fDDest = $fDiagsDest;
        $this->section = "";
        $this->cbuffer = "";
        $this->recCount = 0;
    }

    function _startelement($parser, $name, $attrs)
    {
        //look for namespaces to add
        foreach ($attrs as $k=>$v)
        {
            if (strpos($k,"xmlns") === 0)
            {
                $this->addNameSpace($k,$v);
            }
        }
        if (($this->section == "") &&
            ((strcasecmp($name,"header") == 0) ||
             (strcasecmp($name,"record") == 0) ||
             (strcasecmp($name,"diagnostic") == 0) ||
             (strcasecmp($name,'metadata') == 0)))
        {
            $this->section = $name;
            $this->cbuffer = "<".$name;
            foreach ($attrs as $n=>$v)
            {
                $this->cbuffer .= " ".$n."='".$v."'";
            }
            $this->cbuffer .= ">";
            return;
        }
        if ($this->section != "")
        {
            $this->cbuffer .= "<".$name;
            foreach ($attrs as $n=>$v)
            {
                $this->cbuffer .= " ".$n."='".$v."'";
            }
            $this->cbuffer .= ">";
        }
    }

    function _endelement($parser, $name)
    {
        if (($this->section == "header") &&
            (strcasecmp($name,"header") == 0))
        {
            $this->section = "";
            $this->cbuffer .= "</".$name.">";
            fputs($this->fHDest,$this->cbuffer);
            $this->cbuffer = "";
            return;
        }
        if (($this->section == "record") &&
            (strcasecmp($name,"record") == 0))
        {
            $this->recCount ++;
            $this->section = "";
            $this->cbuffer .= "</".$name.">";
            fputs($this->fRDest,$this->cbuffer);
            $this->cbuffer = "";
            return;
        }
        if (($this->section == "metadata") &&
            (strcasecmp($name,"metadata") == 0))
        {
            $this->recCount ++;
            $this->section = "";
            $this->cbuffer .= "</".$name.">";
            fputs($this->fRDest,$this->cbuffer);
            $this->cbuffer = "";
            return;
        }
        if (($this->section == "diagnostic") &&
            (strcasecmp($name,"diagnostic") == 0))
        {
            $this->section = "";
            $this->cbuffer .= "</".$name.">";
            fputs($this->fDDest,$this->cbuffer);
            $this->cbuffer = "";
            return;
        }
        if ($this->section != "")
        {
            $this->cbuffer .= "</".$name.">";
            return;
        }
    }

    function _cdata($parser,$data)
    {
        if ($this->section != "")
        {
            $this->cbuffer .= $data;
        }
    }

}



//////////////////////
/*
echo "<pre>";

set_magic_quotes_runtime(0);
dl('php_sockets.dll');

$URL = "http://velo.dyndns.info/phpdev/digir/digir.php?";
$URL .= "request=http://velo.dyndns.info/phpdev/digir/req/requestExample.xml";


$targets = new multiTarget();
$target = new DiGIRSearchResponse('1',$URL,1);
$idx = $targets->addTarget($target);
$targets->doJob();

$t = &$targets->getTarget($idx);
print_r($t->columns);
print_r($t->records);

//now load seperately
$target->Load();
print_r($target->columns);
print_r($target->records);

echo "</pre>";
*/
/*
echo "<pre>";
set_magic_quotes_runtime(0);
dl('php_sockets.dll');

$URL = "http://velo.dyndns.info/phpdev/digir/digir.php?";
$URL .= "request=http://velo.dyndns.info/phpdev/digir/req/requestExample.xml";
$cached = "c:\\temp\\digir\\mail";
$nfh = tempnam($cached,"head");
$fh = fopen($nfh,"wb");
$nfr = tempnam($cached,"rec");
$fr = fopen($nfr,"wb");
$nfd = tempnam($cached,"diag");
$fd = fopen($nfd,"wb");

$target = new DiGIRSearchFiler('1',$URL,$fh,$fr,$fd);
$target->Load();

fclose($fh);
fclose($fr);
fclose($fd);
echo "</pre>";
*/
?>
