• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Examples
  • File List

system/classes/superglobal.php

00001 <?php
00007 namespace Habari;
00008 
00013 class SuperGlobal extends \ArrayIterator
00014 {
00015   protected $values = array();
00016   protected $raw_values = array();
00017 
00018   public function __construct( $array )
00019   {
00020     if ( !is_array( $array ) && !$array instanceof SuperGlobal ) {
00021       throw new \Exception( 'Parameter must be array or SuperGlobal' );
00022     }
00023     parent::__construct( $array );
00024   }
00025 
00031   public static function process_gps()
00032   {
00033     /* We should only revert the magic quotes once per page hit */
00034     static $revert = true;
00035 
00036     if ( !$revert ) {
00037       // our work has already been done
00038       return;
00039     }
00040 
00041     if ( get_magic_quotes_gpc() ) {
00042       $_GET = Utils::stripslashes( $_GET );
00043       $_POST = Utils::stripslashes( $_POST );
00044     }
00045 
00046     $_GET = new SuperGlobal( $_GET );
00047     $_POST = new SuperGlobal( $_POST );
00048     $_SERVER = new SuperGlobal( $_SERVER );
00049     unset( $_REQUEST );
00050 
00051     $revert = false;
00052   }
00053 
00058   public static function process_c()
00059   {
00060     /* We should only revert the magic quotes once per page hit */
00061     static $revert = true;
00062 
00063     if ( !$revert ) {
00064       // our work has already been done
00065       return;
00066     }
00067 
00068     if ( get_magic_quotes_gpc() ) {
00069       $_COOKIE = Utils::stripslashes( $_COOKIE );
00070     }
00071 
00072     $_COOKIE = new SuperGlobal( $_COOKIE );
00073 
00074     $revert = false;
00075   }
00076 
00083   public function raw( $index )
00084   {
00085     if ( isset( $this->raw_values[$index] ) ) {
00086       return $this->raw_values[$index];
00087     }
00088     $cp = $this->get_array_copy_raw();
00089     if ( isset( $cp[$index] ) ) {
00090       $this->raw_values[$index] = $cp[$index];
00091       return $this->raw_values[$index];
00092     }
00093   }
00094 
00103   public function escape( $index )
00104   {
00105     return htmlentities( $this->raw( $index ), ENT_QUOTES );
00106   }
00107   
00111   public function getArrayCopy()
00112   {
00113     return array_map( array( $this, 'base_filter' ), parent::getArrayCopy() );
00114   }
00115   
00119   public function get_array_copy_raw()
00120   {
00121     return parent::getArrayCopy();
00122   }
00123   
00127   public function current()
00128   {
00129     return $this->offsetGet( parent::key() );
00130   }
00131 
00138   public function offsetGet( $index )
00139   {
00140     if ( isset( $this->values[$index] ) ) {
00141       return $this->values[$index];
00142     }
00143     $cp = $this->get_array_copy_raw();
00144     if ( isset( $cp[$index] ) ) {
00145       $this->values[$index] = $this->base_filter( $cp[$index] );
00146       return $this->values[$index];
00147     }
00148   }
00149   
00156   public function offsetSet( $index, $value )
00157   {
00158     unset( $this->values[$index] );
00159     unset( $this->raw_values[$index] );
00160     parent::offsetSet( $index, $value );
00161   }
00162 
00169   protected function base_filter( $value )
00170   {
00171     if ( is_array( $value ) ) {
00172       return array_map( array( $this, 'base_filter' ), $value );
00173     }
00174     elseif ( is_string( $value ) ) {
00175       return InputFilter::filter( $value );
00176     }
00177     else {
00178       return $value;
00179     }
00180   }
00181 
00188   public function merge()
00189   {
00190     $args = func_get_args();
00191     $cp = $this->get_array_copy_raw();
00192     foreach ( $args as $ary ) {
00193       if ( is_array( $ary ) ) {
00194         foreach ( $ary as $key => $value ) {
00195           if ( is_numeric( $key ) ) {
00196             $cp[] = $value;
00197           }
00198           else {
00199             $cp[$key] = $value;
00200           }
00201         }
00202       }
00203       elseif ( $ary instanceof SuperGlobal ) {
00204         // loop to get raw data.
00205         while ( $ary->valid() ) {
00206           if ( is_numeric( $ary->key() ) ) {
00207             $cp[] = $ary->raw( $ary->key() );
00208           }
00209           else {
00210             $cp[$ary->key()] = $ary->raw( $ary->key() );
00211           }
00212           $ary->next();
00213         }
00214       }
00215       elseif ( $ary instanceof \ArrayObject || $ary instanceof \ArrayIterator ) {
00216         $arycp = $ary->getArrayCopy();  // Don't trigger offsetGet for ArrayObject
00217         foreach ( $arycp as $key => $value ) {
00218           if ( is_numeric( $key ) ) {
00219             $cp[] = $value;
00220           }
00221           else {
00222             $cp[$key] = $value;
00223           }
00224         }
00225       }
00226       else {
00227         $cp[] = $ary;
00228       }
00229     }
00230     return new SuperGlobal( $cp );
00231   }
00232 
00239   public function filter_keys()
00240   {
00241     $keys = array();
00242     $args = func_get_args();
00243     foreach ( $args as $ary ) {
00244       if ( !is_array( $ary ) ) {
00245         $ary = array( $ary );
00246       }
00247       $keys = array_merge( $keys, array_values( $ary ) );
00248     }
00249     $cp = $this->get_array_copy_raw();
00250     $cp = array_intersect_key( $cp, array_flip( $keys ) );
00251     return new SuperGlobal( $cp );
00252   }
00253   
00259   public function map( $fn )
00260   {
00261     return new SuperGlobal( array_map( $fn, $this->get_array_copy_raw() ) );
00262   }
00263   
00270   public function rekey( $replacement = '{\$$0}', $search_regex = '/^.*$/' )
00271   {
00272     $output = array();
00273     foreach ( $this->get_array_copy_raw() as $k => $v ) {
00274       $output[preg_replace( $search_regex, $replacement, $k )] = $v;
00275     }
00276     return new SuperGlobal( $output );
00277   }
00278 }
00279 
00280 ?>

Generated on Sun Aug 4 2013 12:51:43 for Habari by  doxygen 1.7.1