PHP Classes
elePHPant
Icontem

File: Versioning/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  >  Platform versioning  >  Versioning/proc.php  >  Download  
File: Versioning/proc.php
Role: Auxiliary script
Content type: text/plain
Description: Procedural PHP File
Class: Platform versioning
Check and compare the current PHP version
Author: By
Last change:
Date: 7 years ago
Size: 6,495 bytes
 

Contents

Class file image Download
<?php
/*------------------------------------------------------------------------------
// File: [Platform/Versioning/]proc.php
// Description: PHP 3, 4, 5 versioning utility file
// Purpose: This utility file determines the running PHP version and makes
// comparisons
// Version: 1.0
// Application: PHCL-WTK
// Author: Asher Kadar Wolfstein
// Email: asher@asherwolf.com
// Homepage: http://www.asherwolf.com/projects/php/platform/versioning/index.html
//------------------------------------------------------------------------------
//
// ALL CODE AND (GENERATED) DOCUMENTATION
// IS COPYRIGHT 2007 WOLFSTEIN CONSULTING (COLORADO)
//
// LICENSE INFORMATION
//
// http://www.asherwolf.com/code_license_1.php?p=Platform_versioning&l=lgpl&l2=c30
//
// THIS PROGRAM IS DISTRIBUTED WITHOUT ANY WARRANTY INCLUDING THE IMPLIED
// WARRANTY OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE.
//
// THIS FILE IS PART OF 'Platform_versioning'.
//
// 'Platform_versioning' IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR
// MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE AS
// PUBLISHED BY THE FREE SOFTWARE FOUNDATION;
//
// 'Platform_versioning' IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
// BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
// MERCHANTIBILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE
// GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
//
// YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE
// ALONG WITH 'Platform_versioning'; IF NOT, WRITE TO THE FREE SOFTWARE
// FOUNDATION, INC., 51 FRANKLIN ST, FIFTH FLOOR, BOSTON, MA 02110-1301 USA
//
// http://www.opensource.org/licenses/lgpl-license.php
//
// FURTHER LICENSED UNDER THE CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
// http://creativecommons.org/licenses/by/3.0/
//
//------------------------------------------------------------------------------
// Revision History
//------------------------------------------------------------------------------
// Format: strftime - %D
// 07/01/2007 Created - Asher Wolfstein
//
//----------------------------------------------------------------------------*/

/**
 * PHP 3, 4, 5 versioning utility file
 *
 * This utility file determines the running PHP version and makes comparisons
 *
 * @author Asher Wolfstein <asher@asherwolf.com>
 * @copyright Copyright &copy; 2007, Wolfstein Consulting (Colorado).
 * @license http://www.asherwolf.com/code_license_1.php?p=Platform_versioning&l=lgpl&l2=c30 License Information
 * @package platform
 * @version 1.0
 * @subpackage versioning
 * @category compatibility
 */

/**
 * Normalize a comparison operator string representation
 *
 * @see platform_versioning_compare_val()
 * @param string $c Operator
 * @return string Normalized operator
 */
function platform_versioning_normalize_comparison( $c ) {
   
// http://www.php.net/manual/en/function.version-compare.php#43710
   
if ( preg_match( '/^[lg][te]|=|eq|ne|<>$/i', $c ) ) {
        switch(
$c ) {
            case
'lt':
                return
'<';
                break;
           
            case
'gt':
                return
'>';
                break;
           
            case
'ge':
                return
'>=';
                break;
           
            case
'le':
                return
'<=';
                break;
           
            case
'=':
            case
'eq':
                return
'==';
                break;
           
            case
'ne':
            case
'<>':
                return
'!=';
        }
    }
   
    return
$c;
}

/**
 * This function enables variable operator value comparison
 *
 * @param mixed $v1 Left comparison operand
 * @param mixed $v2 Right comparison operand
 * @param $string $c Comparison operator
 * @return boolean Result of comparison
 */
function platform_versioning_compare_val( $v1, $v2, $c ) {
   
$gt = $lt = $eq = $ne = false;
   
// http://www.php.net/manual/en/function.version-compare.php#43710
   
switch( platform_versioning_normalize_comparison( $c ) ) {
        case
'>':
           
$gt = true;
            break;
       
        case
'<':
           
$lt = true;
            break;
       
        case
'>=':
           
$gt = $eq = true;
            break;
       
        case
'<=':
           
$lt = $eq = true;
            break;
       
        case
'==':
           
$eq = true;
            break;
       
        case
'!=':
           
$ne = true;
    }
   
    if ( (
$ne && ( $v1 != $v2 ) ) ||
            (
$eq && ( $v1 == $v2 ) ) ||
            (
$gt && ( $v1 > $v2 ) ) ||
            (
$lt && ( $v1 < $v2 ) ) ) {
        return
true;
    }
   
    return
false;
}

/**
 * Compares given version with running version the proper logic
 *
 * Uses version compare if available, if not applies the multi-tiered logic
 * required for comparing versions.
 *
 * @param string $version Version to compare - phpversion() output perhaps
 * @param string $comp Comparison operator
 * @param string $sep String seperator
 * @return boolean Whether the comparison is true or false
 */
function platform_compare_versions( $version, $comp, $sep = '.' ) {
   
$comp = platform_versioning_normalize_comparison( $comp );
    list(
$v1, $v2, $v3 ) = explode( '.', phpversion() );
   
// not the best
   
$v3_s = strlen( $v3 ) > 1 ? substr( $v3, 0, 1 ) : $v3;
    if (
$v1 >= 4 ||
            (
$v1 == 4 && $v3_s >= 7 ) ) {
        return
version_compare( "$v1.$v2.$v3", $version, $comp );
    } else {
       
// doesn't work with alphas or betas etc.
       
$v = explode( $sep, $version );
        if ( isset(
$v[2] ) ) {
           
$v[2] = strlen( $v[2] ) > 1 ? substr( $v[2], 0, 1 ) : $v[2];
        }
       
       
$vc = count( $v );
       
$neq = strpos( $comp, '!' ) ? true : false;
        if ( (
$vc == 1 && platform_versioning_compare_val( $v1, $v[0], $comp ) ) ||
                (
$vc == 2 && ( !$neq && ( (
                       
platform_versioning_compare_val( $v1, $v[0], '==' ) &&
                           
platform_versioning_compare_val( $v2, $v[1], $comp ) ) ||
                       
platform_versioning_compare_val( $v1, $v[0], substr( $comp, 0, 1 ), 'x' ) ) ) ||
                    (
$neq && (
                       
platform_versioning_compare_val( $v1, $v[0], $comp ) ||
                       
platform_versioning_compare_val( $v2, $v[1], $comp ) ) ) ) ||
                (
$vc == 3 && ( !$neq && ( (
                       
platform_versioning_compare_val( $v1, $v[0], '==' ) &&
                           
platform_versioning_compare_val( $v2, $v[1], '==' ) &&
                           
platform_versioning_compare_val( $v3, $v[2], $comp ) ) ||
                        (
platform_versioning_compare_val( $v1, $v[0], '==' ) &&
                           
platform_versioning_compare_val( $v2, $v[1], $comp ) ) ||
                       
platform_versioning_compare_val( $v1, $v[0], substr( $comp, 0, 1 ) ) ) ) ||
                    (
$neq &&
                        (
platform_versioning_compare_val( $v1, $v[0], $comp ) ||
                       
platform_versioning_compare_val( $v1, $v[1], $comp ) ||
                       
platform_versioning_compare_val( $v1, $v[2], $comp ) ) ) ) ) {
            return
true;
        }
       
        return
false;
    }
}

/**
 * True if this file has been included
 *
 * @name PLATFORM_VERSIONING_CONF
 */
define( 'PLATFORM_VERSIONING_PROC_CONF', true );

?>