<?
echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
/**
* Display da página de resultado da busca ou disponibiliza a quantidade de resultado
*/
        function search() {
                $url = $_REQUEST['url'];
                $lang = $_REQUEST['lang'];
                $query = preg_replace(array('/ AND /', '/ OR /', '/ and /'), ' ', $_REQUEST['searchExp']);
                $newurl = 'http://'.$url.'/metaiah/searchAjax.php?expression='.$query.'&lang='.$lang;
                if (fopen($newurl, "r")) {
                        $version = 4;
                        if ($_REQUEST["view"] == "htm")
                                $newurl = 'http://'.$url.'/metaiah/search.php?expression='.$query.'&lang='.$lang;
                } else {
                        $version = 3;
                        $query = trataSearchExp();
                        $newurl = "http://".$url."/metaiah/metaiah.php?connector=".$query[1]."&expression=".$query[0].'&lang='.$lang;
                }
                if ($_REQUEST["view"] == "htm"){
                        print "<script>window.document.location.href = \"$newurl\";</script>";
                        exit;
                }
                
                $content = "";
                $handle = fopen($newurl, "r");
                while (!feof($handle))
                        $content .= fread($handle, 100);
                fclose($handle);
                return array($content, $version);
        }
/**
* Acerto do termo de pesquisa, troca operadores booleanos por %20
*/
        function trataSearchExp(){
                $query = $_REQUEST['searchExp'];
                $operador = "OR";
                if (preg_match("/ and /i", $query))
                        $operador = "AND";
                $query = preg_replace(array('/ AND /i', '/ OR /i'),'%20',$query);
                return array($query, $operador);
        }
        function getResultados($htm, $qtd, $parametro, $type){
                $inipos = strpos($htm, $parametro);
/*                $type = 1;
                switch ($type){
                        case 1:*/
                                while (($htm[$inipos - 1] != '(' and ord($htm[$inipos-1]) >= 48 and ord($htm[$inipos-1]) <= 57) or (ord($htm[$inipos - 1]) == 160))
                                        $inipos -= 1;
/*                                break;
                        case 2:
                                while ($htm[$inipos - 1] != '(' and ord($htm[$inipos-1]) >= 48 and ord($htm[$inipos-1]) <= 57)
                                        $inipos -= 1;
                                break;
                }*/
                /*while (($htm[$inipos - 1] != '(' and ord($htm[$inipos-1]) >= 48 and ord($htm[$inipos-1]) <= 57) or (ord($htm[$inipos - 1]) == 160)){
                        $inipos -= 1;
                }*/
                $aux = "";
                while (ord($htm[$inipos]) >= 48 and ord($htm[$inipos]) <= 57){
                        $aux .= $htm[$inipos];
                        $inipos += 1;
                }
                $tam = strlen($parametro);
                $htm = substr($htm,($inipos + $tam));
                $qtd = $aux + $qtd;
                return array($qtd, $htm);
}
        function trataUL($htm, $parametro){
                $resultados = strpos($htm, $parametro);
                $ul = strpos($htm, '<ul>');
                $fimul = strpos($htm, '</ul>');
                if ($resultados > $ul){
                        if (($resultados > $ul) and ($resultados < $fimul)){
                                $tam = strlen($parametro);
                                $htm = substr($htm,($resultados + $tam));
                                $resultados = strpos($htm, $parametro);
                                $fimul = strpos($htm, '</ul>');
                                while ($resultados < $fimul){
                                        $htm = substr($htm,($resultados + $tam));
                                        $resultados = strpos($htm, $parametro);
                                        $fimul = strpos($htm, '</ul>');
                                }
                                $tam = strlen('</ul>');
                                $htm = substr($htm,($fimul + $tam));
                        } else {
                                if ($resultados > $fimul) {
                                        $tam = strlen('</ul>');
                                        $htm = substr($htm,($fimul + $tam));
                                }
                        }
                }
                return $htm;
}
/**
*Retirando o termo numérico da página de resultado para BVS v3.
*/
        function setTotalSearch($texto){
                $htm = $texto;
                $qtd = 0;
                $proc = strpos($htm, 'Procurando');
                if ($proc > 0){
                        $tam = strlen('Procurando');
                        $htm = substr($htm,($proc + $tam));
                        $parametro = ')';
                        $type = 1;
                } else {
                        $parametro = 'Resultados)';
                        $type = 2;
                }
                while (strpos($htm, $parametro) > 0){
                        $aux = getResultados($htm, $qtd, $parametro, $type);
                        $qtd = $aux[0];
                        $htm = $aux[1];
                        if (strpos($htm, '<ul>') > 0)
                                $htm = trataUL($htm, $parametro);
                }
                return $qtd;
}
/**
*Retirando o termo numérico da página de resultado para BVS v4.
*/
        function setTotalSearchBvs4($texto){
                $total = 0;
                $resultpos = strpos($texto, "<result-group");
                while($resultpos){
                        $texto = substr($texto, $resultpos);
                        $totalpos = strpos($texto, 'total="');
                        $tam = strlen('total="');
                        $texto = substr($texto,($totalpos + $tam));
                        $auxtotal = '';
                        for ($i = 0; $texto[$i] != '"'; $i++)
                                $auxtotal .= $texto[$i];
                        $auxtotal = sprintf("%d", $auxtotal);
                        $total += $auxtotal;
                        $resultpos = strpos($texto, "<result-group");
                }
                return $total;
}
/**
*Cria XML com resultado da busca e chama todas as funções
*/
        function Main(){
                if (!(isset($_REQUEST['searchExp'])) or $_REQUEST['searchExp'] == null){
                        $totalHits = 0;
                } else {
                        /*$url = $_REQUEST['url'];
                        $handle = fopen('http://'.$url, "r");
                        $content = fread($handle, 100);
                        fclose($handle);
                        if (strpos($content, 'DOCTYPE'))
                                $version = 4;
                        else
                                $version = 3;*/
                        $aux = search();
                        $texto = $aux[0];
                        $version = $aux[1];
                        if ($version == 3){
                                $totalHits = setTotalSearch($texto);
                        } else if ($version == 4) {
                                $totalHits = setTotalSearchBvs4($texto);
                        }
                        $totalHits = ereg_replace("[^[:digit:]]","",$totalHits);
                }
                //if ($totalHits == "" or !(isset($totalHits)))
                //        $totalHits = 0;
                 print "
<iah>
<prolog>
<total-hits>$totalHits</total-hits>
</prolog>
</iah>";
                //Header("Content-type: text/xml; charset=iso-8859-1");
        }
Main();
?>