<?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.
*
* NOTE: THIS VERSION OF THE CLASS ASSUMES YOU ARE RUNNING PHP >= 5.0.0
*
* 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 {
public static function include_file( $filepath, $options = 0 ) {
$chg = $b_matches = $l_matches = $matches = array();
if ( $options & V_FILEBASE ) {
$filepath = Filebase::inc_path() . DIR_SEP . $filepath;
}
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 );
}
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 ( Phpizer::check_ver( 'eq', $v1 ) ) {
$chg_set = true;
}
} elseif ( isset( $v2 ) ) {
if ( Phpizer::check_ver( '>=', $v1 ) &&
Phpizer::check_ver( '<', $v2 ) ) {
$chg_set = true;
}
} else {
if ( Phpizer::check_ver( '>=', $v1 ) ) {
$chg_set = true;
}
}
} else {
if ( !$r ) {
if ( Phpizer::check_ver( '>=', $v1 . '.0' ) &&
Phpizer::check_ver( '<',
implode( '.', array( $v1_1, $v1_2 + 1, '0' ) ) ) ) {
$chg_set = true;
}
} elseif ( isset( $v2 ) ) {
if ( Phpizer::check_ver( '>=', $v1 ) &&
Phpizer::check_ver( '<', $v2 ) ) {
$chg_set = true;
}
} else {
if ( Phpizer::check_ver( '>=', $v1 . '.0' ) ) {
$chg_set = true;
}
}
}
} else {
if ( !$r ) {
if ( Phpizer::check_ver( '>=', $v1 . '.0.0' ) &&
Phpizer::check_ver( '<', $v1 + 1 . '.0.0' ) ) {
$chg_set = true;
}
} elseif ( isset( $v2 ) ) {
if ( Phpizer::check_ver( '>=', $v1 ) &&
Phpizer::check_ver( '<', $v2 ) ) {
$chg_set = true;
}
} else {
if ( Phpizer::check_ver( '>=', $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;
usort( $chg, array( 'Phpizer', 'cmp_rpl' ) );
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;
}
private static 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( '/(?<!\\\\)([\'"]).*(?<!\\\\)\\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
private static function cmp_tok( $a, $b ) {
if ( $a[1] == $b[1] ) {
return 0;
}
return ( $a[1] < $b[1] ) ? -1 : 1;
}
// Sorting for replacements
private static function cmp_rpl( $a, $b ) {
if ( $a['loc'] == $b['loc'] ) {
return 0;
}
return ( $a['loc'] < $b['loc'] ) ? -1 : 1;
}
/* from mina86 at tlen dot pl
* http://us3.php.net/manual/en/function.version-compare.php#43710 */
function check_ver( $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;
}
}
}
?>