PHP Classes
elePHPant
Icontem

File: phpizer.proc.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Asher Wolfstein  >  phpizer  >  phpizer.proc.php  >  Download  
File: phpizer.proc.php
Role: Auxiliary script
Content type: text/plain
Description: Equiv. Procedures for PHP3
Class: phpizer
Load and adapt class to current PHP version (3.x+)
Author: By
Last change: updated script to match others
Date: 8 years ago
Size: 9,365 bytes
 

Contents

Class file image Download
<?php

/* Advanced Versioning Procedures ( FOR PHP 3 )
 * 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
 *
 * These procedures are 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. Due to
 * the scope of eval inside of a method/function global variables must be
 * assigned using either the keyword global (not recommended) or through
 * $GLOBALS (recommended) within the filtered file.
 *
 * PLEASE NOTE!!! THESE PROCEDURES ASSUME YOU ARE RUNNING PHP >= 3.0.9. FOR PHP 4+
 * PLEASE INCLUDE/REQUIRE THE .class.php FILE INSTEAD!
 *
 * 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' );

function include_file( $filepath, $options = 0 ) {
    if (
$options & V_FILEBASE ) {
       
$filepath = Filebase::inc_path() . DIR_SEP . $filepath;
    }
   
    if (
$options & V_INC_PATH ) {
       
$code = file( $filepath, 1 );
    } else {
       
$code = file( $filepath );
    }
       
    if ( !(
$options & V_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 (
check_version( $v1, true ) ) {
                       
$chg_set = true;
                    }
                } elseif ( isset(
$v2 ) ) {
                    if (
check_version( $v1 ) &&
                            (
check_version( $v2, true ) ||
                                !
check_version( $v2 ) ) ) {
                       
$chg_set = true;
                    }
                } else {
                    if (
check_version( $v1 ) ) {
                       
$chg_set = true;
                    }
                }
            } else {
                    if ( !
$r ) {
                        if (
check_version( $v1 . '.0' ) &&
                                !
check_version(
                                   
implode( '.', array( $v1_1, $v1_2 + 1, '0' ) ) ) ) {
                           
$chg_set = true;
                        }
                    } elseif ( isset(
$v2 ) ) {
                        if (
check_version( $v1 ) &&
                                (
check_version( $v2, true ) ||
                                    !
check_version( $v2 ) ) ) {
                           
$chg_set = true;
                        }
                    } else {
                        if (
check_version( $v1 . '.0' ) ) {
                           
$chg_set = true;
                        }
                    }
                }
        } else {
            if ( !
$r ) {
                if (
check_version( $v1 . '.0.0' ) &&
                        !
check_version( $v1 + 1 . '.0.0' ) ) {
                   
$chg_set = true;
                }
            } elseif ( isset(
$v2 ) ) {
                    if (
check_version( $v1 ) &&
                            (
check_version( $v2, true ) ||
                                !
check_version( $v2 ) ) ) {
                       
$chg_set = true;
                    }
            } else {
                if (
check_version( $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, 'cmp_rpl' );
    foreach (
$chg as $change ) {
       
$l = strlen( $code );
       
$code = substr( $code, 0, $change['loc'] - $adjust ) .
               
$change['aft'] .
               
substr( $code, $change['loc'] - $adjust + $change['len'] );
       
$adjust += $l - strlen( $code );
    }
   
   
$code = preg_replace( array( VB_COMMENT, VL_COMMENT ), '', $code );
   
$offlimits = 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( '/(?<!\\\\)([\'"]).*(?<!\\\\)\\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] );
    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;
}
   
// Sorting for replacements
function cmp_rpl( $a, $b ) {
    if (
$a['loc'] == $b['loc'] ) {
        return
0;
    }
   
    return (
$a['loc'] < $b['loc'] ) ? -1 : 1;
}

   
/* from php.net anonymous
 * modified by Asher Holley
 * if $req is '>=' PHP_VERSION returns true else return false */
function check_version( $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;
}

?>