Location: PHPKode > projects > RIPS > functions/tokens.php
<?php
/** 

RIPS - A static source code analyser for vulnerabilities in PHP scripts 
	by Johannes Dahse (hide@address.com)
			
			
Open source under the BSD License.

Copyright (c) 2010, Johannes Dahse
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
- Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.		

**/
	
	// delete all tokens to ignore while scanning, mostly whitespaces	
	function prepare_tokens($tokens, $T_IGNORE)
	{	
		// delete whitespaces and other unimportant tokens
		for($i=0, $c=count($tokens); $i<$c; $i++)
		{
			if( is_array($tokens[$i]) ) 
			{
				if( in_array($tokens[$i][0], $T_IGNORE) )
					unset($tokens[$i]);
				else if( $tokens[$i][0] === T_CLOSE_TAG )
					$tokens[$i] = ';';	
			}
			// ternary operator gives headaches, only partly supported
			else if($tokens[$i] === '?')
			{
				$d=1;
				// find last token before '?'
				while( !isset($tokens[$i-$d]) )
				{
					if($d>10)break;
					$d++;
				}
				// if condition in paranthesis ( )
				if($tokens[$i-$d] === ')')
				{
					$d=1;
					while( isset($tokens[$i-$d]) && $tokens[$i-$d] !== '(' )
					{
						// delete condition, because vars should not be traced
						unset($tokens[$i-$d]);
						if($d>20)break;
						$d++;
					}
				}
			}
		}
		
		// return tokens with rearranged key index
		return array_values($tokens);
	}	
	
	// adds braces around offsets
	function wrapbraces($tokens, $start, $between, $end)
	{
		$tokens = array_merge(
			array_slice($tokens, 0, $start), array('{'), 
			array_slice($tokens, $start, $between), array('}'),
			array_slice($tokens, $end)
		);	
		return $tokens;
	}
		
	// some tokenchains need to be fixed to scan correctly later	
	function fix_tokens($tokens)
	{	
		for($i=0; $i<count($tokens); $i++)
		{
		// convert `backticks` to backticks()
			if( $tokens[$i] === '`' )
			{		
				$f=1;
				while( $tokens[$i+$f] !== '`' && $tokens[$i+$f] !== ';' )
				{		
					// get line_nr of any near token
					if( is_array($tokens[$i+$f]) )
						$line_nr = $tokens[$i+$f][2];
						
					if($f>50)break;
					
					$f++;
				}
				if(!empty($line_nr))
				{ 
					$tokens[$i+$f] = ')';
					$tokens[$i] = array(T_STRING, 'backticks', $line_nr);
				
					// add element backticks() to array 			
					$tokens = array_merge(
						array_slice($tokens, 0, $i+1), array('('), 
						array_slice($tokens, $i+1)
					);	
				}

			}
		// rewrite $array{index} to $array[index]
			else if( $tokens[$i] === '{'
			&& ((is_array($tokens[$i-1]) && $tokens[$i-1][0] === T_VARIABLE)
			|| $tokens[$i-1] === ']') )
			{
				$tokens[$i] = '[';
				$f=1;
				while($tokens[$i+$f] !== '}')
				{
					$f++;
				}
				$tokens[$i+$f] = ']';
			}
		// rebuild if-clauses without { }
			else if( is_array($tokens[$i]) 
			&& ($tokens[$i][0] === T_IF
			|| $tokens[$i][0] === T_ELSEIF) )
			{				
				$f=4; $start=$end=0;
				while( $tokens[$i+$f] !== '{' )
				{		
					// idea: if there is a var or functioncall with a ')' infront 
					// it must be a if() without { }
					if( is_array($tokens[$i+$f])
					&& $tokens[$i+$f-1] === ')' 
					&& ($tokens[$i+$f][0] === T_VARIABLE
					|| in_array($tokens[$i+$f][0], $GLOBALS['T_FUNCTIONS']) ) )
						$start = $i+$f;
						
					if ( $tokens[$i+$f] === ';' )
					{
						$end = $i+$f; break;
					}
					
					if($f>50)break;
						
					$f++;
				}
				
				if($start && $end)
				{ 
					$tokens = wrapbraces($tokens, $start, $end-$start+1, $end+1);
					$i = $start;
				}		
			} 
		// rebuild else without { }	
			else if( is_array($tokens[$i]) 
			&& $tokens[$i][0] === T_ELSE 
			&& $tokens[$i+1][0] !== T_IF
			&& $tokens[$i+1] !== '{')
			{	
				$f=2;
				while( $tokens[$i+$f] !== ';' )
				{		
					if($f>50)break;
					$f++;
				}
				$tokens = wrapbraces($tokens, $i+1, $f, $i+$f+1);
			}
		// rebuild switch case: without { }	
			else if( is_array($tokens[$i]) 
			&& $tokens[$i][0] === T_CASE
			&& $tokens[$i+2] === ':'
			&& $tokens[$i+3] !== '{' )
			{
				$f=3;
				while( isset($tokens[$i+$f]) 
				&& !(is_array($tokens[$i+$f]) && $tokens[$i+$f][0] === T_BREAK ) )
				{		
					if($f>250)break;
					$f++;
				}
				$tokens = wrapbraces($tokens, $i+3, $f-1, $i+$f+2);
				$i++;
			}
		// rebuild switch default: without { }	
			else if( is_array($tokens[$i]) 
			&& $tokens[$i][0] === T_DEFAULT
			&& $tokens[$i+2] !== '{' )
			{
				$f=2;
				while( $tokens[$i+$f] != ';' )
				{		
					if($f>250)break;
					$f++;
				}
				$tokens = wrapbraces($tokens, $i+2, $f-1, $i+$f+1);
			}
		}
		// return tokens with rearranged key index
		return array_values($tokens);
	}

?>	
Return current item: RIPS