PHP Classes

File: class_globallogger.php

Recommend this page to a friend!
  Classes of Matthew Daniel   PHP Register Globals Logger   class_globallogger.php   Download  
File: class_globallogger.php
Role: Class source
Content type: text/plain
Description: register and log registered_globals usage
Class: PHP Register Globals Logger
Log the usage of registered global variables
Author: By
Last change: php complains if you use things directly as non strings. It will still work fine but this change will prevent your error logs from filling up
Date: 10 years ago
Size: 3,409 bytes
 

Contents

Class file image Download
<?php
// The MIT License (MIT)
//
// Copyright (c) 2013 Matt Meisberger

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

class globalLogger
{
    protected
$value = null;
    protected
$varname = null;
    protected static
$logger = null;
    protected static
$initialized = false;
    protected static
$globalMap = array(
       
'e' => '_ENV',
       
'g' => '_GET',
       
'p' => '_POST',
       
'c' => '_COOKIE',
       
'r' => '_REQUEST',
       
's' => '_SERVER',
       
'f' => '_FILES',
    );

    protected function
__construct($value, $varname) {
       
$this->value = $value;
       
$this->varname = $varname;
    }

    public function
__toString() {
       
call_user_func_array(static::$logger, array($this->varname, $this->value, __FILE__));

        return
$this->value;
    }

   
/**
     * Initialize all registered globals;
     *
     * @param type $order
     * @return type
     */
   
public static function initialize(closure $logger, $order = 'egpcs')
    {
        if (static::
$initialized)
        {
            return;
        }

        static::
$initialized = true;

        static::
$logger = $logger;

       
// handle auto_globals_jit setting
       
$_SERVER && $_ENV && $_REQUEST;

       
$order = str_split(strtolower($order));

       
// order and filter the globals
       
$globals = array_merge(array_flip($order), static::$globalMap);
       
$globals = array_intersect_key($globals, array_flip($order));

       
array_map('static::registerGlobal',$globals);

       
set_error_handler(function ($errno, $errstr) {
           
// php can handle using class directly as an int but throws a notice
            // this will prevent that from being reported
           
if ($errno == 8 && stripos($errstr, 'Object of class globalLogger could not be converted') !== false) {
                return
true;
            }

            return
false;
        },
E_NOTICE);
    }
   
   
/**
     * Register a global variable like $_GET or $_POST
     *
     * @global array $globalName
     * @param string $globalName
     */
   
protected static function registerGlobal($globalName)
    {
        global $
$globalName;

        foreach( $
$globalName as $key => $val ) {
           
$GLOBALS[$key] = new self($val, $key);
        }
    }
}