Location: PHPKode > projects > FiForms Framework > FiForms/FiForms-includes/FiForms_QueryParser.inc.php
<?php
/* ?><code><?php */

class QueryParser
{
    var $words;
    var $query;
    var $positions;
    
    function QueryParser($query = "")
    {
        $this->words = array(1 => 'SELECT',2 => 'FROM',3 => 'WHERE',4 => 'GROUP BY',5 =>'HAVING',6 => 'ORDER BY',7 => 'LIMIT');
        $this->positions = array(1 => false,2 => false,3 => false,4 => false,5 => false,6 => false,7 => false);
        $this->query = $query;
    }
    
    function checkTicks($str,$startpos)
    {
        $count = 0;
        $pos = strpos($str,"`");
        
        while($pos < strlen($str) && $pos !== FALSE)
        {
        if($this->checkDoubleQuotes($str,$startpos,$pos))
            $count ++;
        $pos = strpos($str,"`",$pos+1);
        }
        return !($count % 2);
    }
    
    function checkQuotes($str,$startpos,$endpos)
    {
        $count = 0;
        $pos = strpos($str,"'",$endpos);
        
        while($pos < $endpos && $pos !== FALSE)
        {
        if($this->checkDoubleQuotes($str,$startpos,$pos))
            $count ++;
        $pos = strpos($str,"'",$pos+1);
        }
        return !($count % 2);
    }
    
    function checkDoubleQuotes($str,$startpos,$endpos)
    {
        $count = 0;
        $pos = strpos($str,'"',$startpos);
        
        while($pos < $endpos && $pos !== FALSE)
        {
            $count ++;
        $pos = strpos($str,'"',$pos+1);
        }
        return !($count % 2);
    }
    
    function checkParenthesis($str,$startpos)
    {
        $openCount = 0;
        $closeCount = 0;
        $pos = strpos($str,"(");
        while($pos < strlen($str) && $pos !== FALSE)
        {
        if($this->checkDoubleQuotes($str,$startpos,$pos) && $this->checkQuotes($str,$startpos,$pos))
            $openCount ++;
        $pos = strpos($str,"(",$pos+1);
        }
        $pos = strpos($str,")");
        while($pos < strlen($str) && $pos !== FALSE)
        {
        if($this->checkDoubleQuotes($str,$startpos,$pos) && $this->checkQuotes($str,$startpos,$pos))
            $closeCount ++;
        $pos = strpos($str,")",$pos+1);
        }
        return $openCount == $closeCount;
    }
    
    function isfullword($word,$query,$pos)
    {
        $whitespace = array(" ","\n","\r","\t");
    
        if($pos > 0)
            $prev = substr($query,$pos-1,1);
        else
        $prev = " ";
    
        if($pos+strlen($word) < strlen($query))
            $post = substr($query,$pos + strlen($word),1);
        else
        $post = " ";
    
        return in_array($prev,$whitespace) && in_array($post,$whitespace);
    }
    
    function findWord($word)
    {
        global $words;
        $startpos = 0;
        if(!in_array($word,$this->words))
        {
            return FALSE;
        }
    
        // search for a starting point in the positions array (for optimising)
        $thiskey = array_search($word,$this->words);
        $lastkey = $thiskey;
        if($this->positions[$lastkey])
        {
            return $this->positions[$lastkey];
        }
        do
        {
            $lastkey -= 1;
            if($this->positions[$lastkey])
            {
            $startpos = $this->positions;
            break;
            }
        } while($lastkey > 1);
        $str = strtoupper($this->query);
        $pos = strpos($str,$word,$startpos);
        if($pos === FALSE)
        {
            return FALSE;
        }
        $firstHalf = substr($str,$startpos,$pos);
        $count = 0;
        while(!$this->checkParenthesis($firstHalf,$startpos) || 
            !$this->checkDoubleQuotes($firstHalf,$startpos,strlen($firstHalf)) || 
            !$this->checkQuotes($firstHalf,$startpos,strlen($firstHelf)) || 
            !$this->checkTicks($firstHalf,$startpos) || 
            !$this->isfullword($word,$str,$pos))
        {
            $pos = strpos($str,$word,$pos+1);
            if($pos === FALSE)
            {
                return FALSE;
            }
            $firstHalf = substr($str,$startpos,$pos);
        } // while inside a subquery
        $this->positions[$thiskey] = $pos;
        return $pos;
    }  // function findWord
    
    function getClause($word)
    {
        //echo " <br /> <b>$word</b>";
        $start = $this->findWord($word);
        if($start === false)
            return false;
        $id = array_search($word,$this->words);
        //echo $this->words[$id]." ";
        do
        {
            $id++;
            $end = $this->findWord($this->words[$id]);
            //echo $this->words[$id]." ";
        } while($end === FALSE && $id < count($this->words));
        //print_r($this->positions);die();
        if($end !== false)
            return substr($this->query,$start,$end-$start);
        else
            return substr($this->query,$start);
    }

} // class QueryParser

/* ?></code><?php */

?>
Return current item: FiForms Framework