Location: PHPKode > scripts > phpizer > phpizer/phpizer.class.php
<?php

/* Advanced Versioning Class
 * Written by Asher Holley ( http://www.wolfoxinc.com/opensource/ )
 * Released under the GNU General Public License ( http://www.opensource.org/licenses/gpl-license.html )
 * Copyright © 2006 Asher Holley
 *
 * This class is useful for determining what version of PHP you are running,
 *   as well as filtering included classes and function libraries so that the
 *   appropriate lines are executed for the appropriate PHP version.
 *
 * In order to make this class 'compatible' or in the least parseable without
 *   errors, 'vars' were avoided along with any constructor.  You can use
 *   this class without making an instance of it.	*/

require_once( 'phpizer.define.php' );
// require_once( 'filebase.class5.php' );

class Phpizer {

	function include_file( $filepath, $options = 0 ) {
		$chg = $b_matches = $l_matches = $matches = array();
		$above_1 = Phpizer::check_ver_1( '4.1.0' );	// efficiency
		
		if ( $options & V_FILEBASE ) {
			$filepath = Filebase::inc_path() . DIR_SEP . $filepath;
		}
		
		if ( $above_1 ) {
			if ( $options & V_INC_PATH ) {
				$code = file_get_contents( $filepath, 1 );
			} else {
				$code = file_get_contents( $filepath );
			}
			
			if ( !( $options & V_TRUNC_PHP ) ) {
				$code = substr( $code, 5, -2 );
			}
		} else {
			if ( $options & V_INC_PATH ) {
				$code = file( $filepath, 1 );
			} else {
				$code = file( $filepath );
			}
			
			if ( !( $options & V_NOT_TRUNC_PHP ) ) {
				$code[0] = substr( $code[0], 5 );
				$code[ count( $code ) - 1 ] =
					substr( $code[ count( $code ) - 1 ], 0, -2 );
				$code = join( '', $code );
			}
		}

		preg_match_all( VL_FIND_PHP, $code, $l_matches, PREG_SET_ORDER + PREG_OFFSET_CAPTURE );
		preg_match_all( VB_FIND_PHP, $code, $b_matches, PREG_SET_ORDER + PREG_OFFSET_CAPTURE );
		
		$matches = array_merge( $l_matches, $b_matches );
		foreach ( $matches as $match ) {
			for ( $a = 2; $a < 9; $a++ ) {
				if ( !isset( $match[$a] ) ) {
					$match[$a] = null;
				}
			}
			
			unset( $v1, $v2, $v1_1, $v1_2, $v1_3, $v2_1, $v2_2, $v2_3 );
			if ( $v1_1 = $match[2][0] ) {
				$v1 = $v1_1;
				if ( $v1_2 = $match[3][0] ) {
					$v1 .= ".$v1_2";
					if ( $v1_3 = $match[4][0] ) {
						$v1 = ".$v1_3";
					}
				}
			} else {
				trigger_error( 'Phpizer::include_file range without start',
							E_USER_ERROR );
			}
			
			if ( $r = $match[5][0] ) {
				if ( $match[6][0] ) {
					$v2 = $v2_1 = $match[6][0];
					if ( $v2_2 = $match[7][0] ? $match[7][0] : 0 ) {
						$v2 = ".$v2_2";
					}
					
					if ( $v2_3 = $match[8][0] ? $match[8][0] : 0 ) {
						$v2 = ".$v2_3";
					}
				}
			}
			
			$chg_set = false;
			if ( isset( $v1_2 ) ) {
				if ( isset( $v1_3 ) ) {
					if ( !$r ) {
						if ( $above_1 ) {
							if ( Phpizer::check_ver_2( 'eq', $v1 ) ) {
								$chg_set = true;
							}
						} else {
							if ( Phpizer::check_ver_1( $v1, true ) ) {
								$chg_set = true;
							}
						}
					} elseif ( isset( $v2 ) ) {
						if ( $above_1 ) {
							if ( Phpizer::check_ver_2( '>=', $v1 ) &&
									Phpizer::check_ver_2( '<', $v2 ) ) {
								$chg_set = true;
							}
						} else {
							if ( Phpizer::check_ver_1( $v1 ) &&
									( Phpizer::check_ver_1( $v2, true ) ||
										!Phpizer::check_ver_1( $v2 ) ) ) {
								$chg_set = true;
							}
						}
					} else {
						if ( $above_1 ) {
							if ( Phpizer::check_ver_2( '>=', $v1 ) ) {
								$chg_set = true;
							}
						} else {
							if ( Phpizer::check_ver_1( $v1 ) ) {
								$chg_set = true;
							}
						}
					}
				} else {
					if ( !$r ) {
						if ( $above_1 ) {
							if ( Phpizer::check_ver_2( '>=', $v1 . '.0' ) &&
									Phpizer::check_ver_2( '<',
										implode( '.', array( $v1_1, $v1_2 + 1, '0' ) ) ) ) {
								$chg_set = true;
							}
						} else {
							if ( Phpizer::check_ver_1( $v1 . '.0' ) &&
									!Phpizer::check_ver_1(
										implode( '.', array( $v1_1, $v1_2 + 1, '0' ) ) ) ) {
								$chg_set = true;
							}
						}
					} elseif ( isset( $v2 ) ) {
						if ( $above_1 ) {
							if ( Phpizer::check_ver_2( '>=', $v1 ) &&
									Phpizer::check_ver_2( '<', $v2 ) ) {
								$chg_set = true;
							}
						} else {
							if ( Phpizer::check_ver_1( $v1 ) &&
									( Phpizer::check_ver_1( $v2, true ) ||
										!Phpizer::check_ver_1( $v2 ) ) ) {
								$chg_set = true;
							}
						}
					} else {
						if ( $above_1 ) {
							if ( Phpizer::check_ver_2( '>=', $v1 . '.0' ) ) {
								$chg_set = true;
							}
						} else {
							if ( Phpizer::check_ver_1( $v1 . '.0' ) ) {
								$chg_set = true;
							}
						}
					}
				}
			} else {
				if ( !$r ) {
					if ( $above_1 ) {
						if ( Phpizer::check_ver_2( '>=', $v1 . '.0.0' ) &&
								Phpizer::check_ver_2( '<', $v1 + 1 . '.0.0' ) ) {
							$chg_set = true;
						}
					} else {
						if ( Phpizer::check_ver_1( $v1 . '.0.0' ) &&
								!Phpizer::check_ver_1( $v1 + 1 . '.0.0' ) ) {
							$chg_set = true;
						}
					}
				} elseif ( isset( $v2 ) ) {
					if ( $above_1 ) {
						if ( Phpizer::check_ver_2( '>=', $v1 ) &&
								Phpizer::check_ver_2( '<', $v2 ) ) {
							$chg_set = true;
						}
					} else {
						if ( Phpizer::check_ver_1( $v1 ) &&
								( Phpizer::check_ver_1( $v2, true ) ||
									!Phpizer::check_ver_1( $v2 ) ) ) {
							$chg_set = true;
						}
					}
				} else {
					if ( $above_1 ) {
						if ( Phpizer::check_ver_2( '>=', $v1 . '.0.0' ) ) {
							$chg_set = true;
						}
					} else {
						if ( Phpizer::check_ver_1( $v1 . '.0.0' ) ) {
							$chg_set = true;
						}
					}
				}
			}
			
			if ( $chg_set ) {
				$chg[] = array( 'loc' => $match[0][1],
								'len' => strlen( $match[0][0] ),
								'aft' => $match[1][0] );
			}
		}
		
		$adjust = 0;
		foreach ( $chg as $change ) {
			$l = strlen( $code );
			$code = substr_replace( $code, $change['aft'] . "\n",
									$change['loc'] - $adjust, $change['len'] );
			$adjust += $l - strlen( $code );
		}
		
		$code = preg_replace( array( VB_COMMENT, VL_COMMENT ), '', $code );
		$offlimits = Phpizer::get_outer_sections( $code );
		$v_matches = array();
		preg_match_all( '/\$[a-zA-Z_][a-zA-Z0-9_]*/', $code, $v_matches, PREG_OFFSET_CAPTURE );
		$adjust = 0;
		foreach( $v_matches[0] as $match ) {
			if ( $match[0] == '$GLOBALS' ) {
				break;
			}
			
			$outer = true;
			foreach ( $offlimits as $limit ) {
				if ( $match[1] >= $limit['s'] && $match[1] <= $limit['e'] ) {
					$outer = false;
					break;
				}
			}
			
			if ( $outer ) {
				$code = substr_replace( $code, '$GLOBALS[\'' . $match[0] . '\']',
							$match[1] + $adjust * 12, strlen( $match[0] ) );
				$adjust++;
			}
		}
		
		if ( !( $options & V_NOT_TRUNC_PHP ) ) {
			$ret = eval( $code );
			return array( $ret, $code );
		}
		
		return $code;
	}
	
	function get_outer_sections( $code ) {
		$l_matches = $r_matches = $c_matches = $f_matches = array();
		preg_match_all( '/class\s*\w+\s*(extends.*)?\s*\{/Uis',
					$code, $c_matches, PREG_OFFSET_CAPTURE );
		preg_match_all( '/function\s*\w+\s*\(.*\)\s*\{/Uis',
					$code, $f_matches, PREG_OFFSET_CAPTURE );
		preg_match_all( '/\{/', $code, $l_matches, PREG_OFFSET_CAPTURE );
		preg_match_all( '/\}/', $code, $r_matches, PREG_OFFSET_CAPTURE );
		preg_match_all( '/(?<!\\\\)([\'"]).*(?<!\\\\)\\1/Uis',
			$code, $q_matches, PREG_OFFSET_CAPTURE );
		for ( $a = 0; $a < count( $q_matches[0] ); $a++ ) {
			if ( strlen( $q_matches[0][$a][0] ) == 2 ) {
				unset( $q_matches[0][$a] );
			}
		}
		
		preg_match_all( '/\{/', $code, $l_matches, PREG_OFFSET_CAPTURE );
		preg_match_all( '/\}/', $code, $r_matches, PREG_OFFSET_CAPTURE );
		$mast = array_merge( $l_matches[0], $r_matches[0] );
		usort( $mast, array( 'Phpizer', 'cmp_tok' ) );
		foreach ( $q_matches[0] as $loc ) {
			for ( $a = 0; $a < count( $mast ); $a++ ) {
				if ( ( $end_char = $loc[1] + strlen( $loc[0] ) ) < $mast[$a][1] ) {
					break;
				}
				
				if ( !in_array( $a, $g ) ) {
					if ( $mast[$a][1] >= $loc[1] &&
							$mast[$a][1] <= $end_char ) {
						unset( $mast[$a] );
						$g[] = $a;
					}
				}
			}
			
			$c = array();
			if ( preg_match_all( '/\\\\\\$[a-zA-Z_][a-zA-Z0-9_]*/',
					$loc[0], $c, PREG_OFFSET_CAPTURE ) ) {
				foreach ( $c[0] as $m ) {
					$ret[] = array( 's' => $m[1] + $loc[1],
									'e' => ( $m[1] + $loc[1] ) + strlen( $m[0] ) );
				}
			}
		}
		
		$mast = array_merge( $c_matches[0], $f_matches[0], $mast );
		usort( $mast, array( 'Phpizer', 'cmp_tok' ) );
		reset( $mast );
		while ( ( $e_tok = each( $mast ) ) !== false ) {
			$tok = $e_tok[1];
			$s = array( 's' => 0, 'e' => 0 );
			$bcount = 0;
			if ( substr( $tok[0], 0, 5 ) == 'class' ) {
				$s['s'] = $tok[1];
				$e_tok = each( $mast );
				if ( $e_tok[1][0] != '{' ) {
					trigger_error( 'Phpizer::get_outer_sections class token not ' .
								'followed by bracket', E_USER_ERROR );
				} else {
					$bcount++;
				}
				
				while ( ( $e_tok = each( $mast ) ) !== false ) {
					$tok = $e_tok[1];
					if ( $tok[0] == '{' ) {
						$bcount++;
					} elseif ( $tok[0] == '}' ) {
						$bcount--;
					}
					
					if ( $bcount == 0 ) {
						$s['e'] = $tok[1];
						break;
					}
				}
			} elseif ( substr( $tok[0], 0, 8 ) == 'function' ) {
				$s['s'] = $tok[1];
				$e_tok = each( $mast );
				if ( $e_tok[1][0] != '{' ) {
					trigger_error( 'Phpizer::get_outer_sections class token not ' .
								'followed by bracket', E_USER_ERROR );
				} else {
					$bcount++;
				}
				
				while ( ( $e_tok = each( $mast ) ) !== false ) {
					$tok = $e_tok[1];
					if ( $tok[0] == '{' ) {
						$bcount++;
					} elseif ( $tok[0] == '}' ) {
						$bcount--;
					}
					
					if ( $bcount == 0 ) {
						$s['e'] = $tok[1];
						break;
					}
				}
			}
			
			$ret[] = $s;
		}
		
		return $ret;
	}
	
	// Sorting algorithm for outer sections code
	function cmp_tok( $a, $b ) {
		if ( $a[1] == $b[1] ) {
			return 0;
		}
		
		return ( $a[1] < $b[1] ) ? -1 : 1;
	}
	
	/* from php.net anonymous
	 * modified by Asher Holley
	 * if $req is '>=' PHP_VERSION returns true else return false
	 * if $opt is set returns true if $req == PHP_VERSION else false */
	function check_ver_1( $req, $opt = false ) {
		list( $c_major, $c_minor, $c_edit ) = split( '[/.-]', PHP_VERSION );
		list( $r_major, $r_minor, $r_edit ) = split( '[/.-]', $req );
	
		// Asher Holley modification
		if ( $opt ) {
			if ( $c_major == $r_major ) {
				if ( $c_minor == $r_minor ) {
					if ( $c_edit == $r_edit ) {
						return true;
					} else {
						return false;
					}
				} else {
					return false;
				}
			} else {
				return false;
			}
		}
		
		if ( $c_major > $r_major ) {
			return true;
		} else if ( $c_major < $r_major ) {
			return false;
		}
	
		// same major - check minor
		if ( $c_minor > $r_minor ) {
			return true;
		} else if ( $c_minor < $r_major ) {
			return false;
		}
	
		// same minor - check edit
		if ( $c_edit > $r_edit ) {
			return true;
		} else if ( $c_edit < $r_edit ) {
			return false;
		}
		
		// equal versions
		return true;
	}
	
	/* from mina86 at tlen dot pl
	 *   http://us3.php.net/manual/en/function.version-compare.php#43710	*/
	function check_ver_2( $arg1, $arg2 = null, $arg3 = null ) {
		static $phpversion = null;
		if ( $phpversion === null ) {
			$phpversion = phpversion();
		}
	
		switch ( func_num_args() ) {
			case 1:
				return version_compare( $phpversion, $arg1 );
				break;

			case 2:
				if ( preg_match('/^[lg][te]|[<>]=?|[!=]?=|eq|ne|<>$/i', $arg1 ) ) {
					return version_compare( $phpversion, $arg2, $arg1 );
				} elseif ( preg_match('/^[lg][te]|[<>]=?|[!=]?=|eq|ne|<>$/i', $arg2 ) ) {
					return version_compare( $phpversion, $arg1, $arg2 );
				}
			
				return version_compare( $arg1, $arg2 );
				break;

			default:
				$ver1 = $arg1;
				if ( preg_match('/^[lg][te]|[<>]=?|[!=]?=|eq|ne|<>$/i', $arg2 ) ) {
					return version_compare( $arg1, $arg3, $arg2 );
				} else {
					return version_compare( $arg1, $arg2, $arg3 );
				}
				
				break;
		}
	}
	
}

?>
Return current item: phpizer