Changeset 47

Show
Ignore:
Timestamp:
05/15/2007 04:00:09 PM (19 months ago)
Author:
Shadowhand
Message:

Fixed bug #1: enable GET; created a common _get_global function for retrieving GET, POST, COOKIE, and SERVER variables; enabled autload in Loader

Location:
trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/application/config/config.php

    r17 r47  
    115115*/ 
    116116$config['permitted_uri_chars'] = 'a-z 0-9~%.:_-'; 
     117 
     118 
     119/* 
     120|-------------------------------------------------------------------------- 
     121| ENABLE GET 
     122|-------------------------------------------------------------------------- 
     123| 
     124| This option will enable $this->input->get('key') and $_GET['key'], but 
     125| will allow you to continue using segment based URLs, unlike the 
     126| 'enable_query_strings' option, which will disable segment based URLs. 
     127| 
     128| Options are: TRUE or FALSE (default) 
     129| 
     130| NOTE: You do not have to change the permitted_uri_chars option, as GET 
     131| requests are not parsed as part of the URI. 
     132| 
     133*/ 
     134$config['enable_get_requests'] = FALSE; 
    117135 
    118136 
  • trunk/system/libraries/Input.php

    r24 r47  
    4747                $CFG =& load_class('Config'); 
    4848                $this->use_xss_clean    = ($CFG->item('global_xss_filtering') === TRUE) ? TRUE : FALSE; 
    49                 $this->allow_get_array  = ($CFG->item('enable_query_strings') === TRUE) ? TRUE : FALSE;          
     49                $this->allow_get_array  = ($CFG->item('enable_query_strings') === TRUE 
     50                                                                OR $CFG->item('enable_get_requests')  === TRUE) ? TRUE : FALSE; 
    5051                $this->_sanitize_globals(); 
    5152        } 
     
    107108                { 
    108109                        foreach($_POST as $key => $val) 
    109                         {                                
     110                        { 
    110111                                $_POST[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); 
    111                         }                        
     112                        } 
    112113                } 
    113114         
     
    116117                { 
    117118                        foreach($_COOKIE as $key => $val) 
    118                         {                        
     119                        { 
    119120                                $_COOKIE[$this->_clean_input_keys($key)] = $this->_clean_input_data($val); 
    120                         }        
     121                        } 
    121122                } 
    122123                 
     
    188189         
    189190        /** 
     191         * Fetch an item from a global array 
     192         * 
     193         * @access      private 
     194         * @param       string 
     195         * @param       string 
     196         * @param       bool 
     197         * @return      string 
     198         */ 
     199        function _get_global($global, $index = FALSE, $xss_clean = FALSE) 
     200        { 
     201                $global = '_'.strtoupper($global); 
     202                if ( ! isset($$global)) 
     203                        return FALSE; 
     204                 
     205                if ($index === FALSE) 
     206                { 
     207                        return $$global; 
     208                } 
     209                 
     210                if ( ! isset($$global[$index])) 
     211                { 
     212                        return FALSE; 
     213                } 
     214 
     215                if ($xss_clean === TRUE) 
     216                { 
     217                        if (is_array($$global[$index])) 
     218                        { 
     219                                foreach($$global[$index] as $key => $val) 
     220                                { 
     221                                        $$global[$index][$key] = $this->xss_clean($val); 
     222                                } 
     223                        } 
     224                        else 
     225                        { 
     226                                return $this->xss_clean($$global[$index]); 
     227                        } 
     228                } 
     229 
     230                return $$global[$index]; 
     231        } 
     232         
     233        // -------------------------------------------------------------------- 
     234         
     235        /** 
    190236         * Fetch an item from the GET array 
    191237         * 
     
    195241         * @return      string 
    196242         */ 
    197         function get($index = '', $xss_clean = FALSE) 
    198         {                
    199                 if ( ! isset($_GET[$index])) 
    200                 { 
    201                         return FALSE; 
    202                 } 
    203  
    204                 if ($xss_clean === TRUE) 
    205                 { 
    206                         if (is_array($_GET[$index])) 
    207                         { 
    208                                 foreach($_GET[$index] as $key => $val) 
    209                                 {                                        
    210                                         $_GET[$index][$key] = $this->xss_clean($val); 
    211                                 } 
    212                         } 
    213                         else 
    214                         { 
    215                                 return $this->xss_clean($_GET[$index]); 
    216                         } 
    217                 } 
    218  
    219                 return $_GET[$index]; 
     243        function get($index = FALSE, $xss_clean = FALSE) 
     244        { 
     245                return $this->_get_global('get', $index, $xss_clean); 
    220246        } 
    221247         
     
    230256         * @return      string 
    231257         */ 
    232         function post($index = '', $xss_clean = FALSE) 
    233         {                
    234                 if ( ! isset($_POST[$index])) 
    235                 { 
    236                         return FALSE; 
    237                 } 
    238  
    239                 if ($xss_clean === TRUE) 
    240                 { 
    241                         if (is_array($_POST[$index])) 
    242                         { 
    243                                 foreach($_POST[$index] as $key => $val) 
    244                                 {                                        
    245                                         $_POST[$index][$key] = $this->xss_clean($val); 
    246                                 } 
    247                         } 
    248                         else 
    249                         { 
    250                                 return $this->xss_clean($_POST[$index]); 
    251                         } 
    252                 } 
    253  
    254                 return $_POST[$index]; 
     258        function post($index = FALSE, $xss_clean = FALSE) 
     259        { 
     260                return $this->_get_global('post', $index, $xss_clean); 
    255261        } 
    256262         
     
    265271         * @return      string 
    266272         */ 
    267         function cookie($index = '', $xss_clean = FALSE) 
    268         { 
    269                 if ( ! isset($_COOKIE[$index])) 
    270                 { 
    271                         return FALSE; 
    272                 } 
    273  
    274                 if ($xss_clean === TRUE) 
    275                 { 
    276                         if (is_array($_COOKIE[$index])) 
    277                         { 
    278                                 $cookie = array(); 
    279                                 foreach($_COOKIE[$index] as $key => $val) 
    280                                 { 
    281                                         $cookie[$key] = $this->xss_clean($val); 
    282                                 } 
    283                  
    284                                 return $cookie; 
    285                         } 
    286                         else 
    287                         { 
    288                                 return $this->xss_clean($_COOKIE[$index]); 
    289                         } 
    290                 } 
    291                 else 
    292                 { 
    293                         return $_COOKIE[$index]; 
    294                 } 
     273        function cookie($index = FALSE, $xss_clean = FALSE) 
     274        { 
     275                return $this->_get_global('cookie', $index, $xss_clean); 
    295276        } 
    296277 
     
    305286         * @return      string 
    306287         */ 
    307         function server($index = '', $xss_clean = FALSE) 
    308         {                
    309                 if ( ! isset($_SERVER[$index])) 
    310                 { 
    311                         return FALSE; 
    312                 } 
    313  
    314                 if ($xss_clean === TRUE) 
    315                 { 
    316                         return $this->xss_clean($_SERVER[$index]); 
    317                 } 
    318                  
    319                 return $_SERVER[$index]; 
     288        function server($index = FALSE, $xss_clean = FALSE) 
     289        { 
     290                return $this->_get_global('server', $index, $xss_clean); 
    320291        } 
    321292         
  • trunk/system/libraries/Loader.php

    r24 r47  
    2727 * @link                http://blueflame.ciforge.com/user_guide/libraries/loader.html 
    2828 */ 
     29 
     30if (floor(phpversion()) >= 5) 
     31{ 
     32        /** 
     33         * Autoloader 
     34         *  
     35         * Implements auto-loading of libraries in PHP >= 5, using CI_Loader 
     36         *  
     37         * @access      public 
     38         * @param       string  class name 
     39         */ 
     40        function __autoload($class) 
     41        { 
     42                if (! function_exists('get_instance')) 
     43                        return; 
     44 
     45                $CI =& get_instance(); 
     46                $fp = $CI->load->_ci_find_class($class)); 
     47                 
     48                if ($fp !== FALSE AND $fp !== TRUE) 
     49                { 
     50                        require_once($fp); 
     51                } 
     52        } 
     53} 
     54 
    2955class CI_Loader { 
    3056 
     
    7399         */      
    74100        function library($library = '', $params = NULL) 
    75         {                
     101        { 
    76102                if ($library == '') 
    77103                { 
     
    119145                { 
    120146                        $x = explode('/', $model); 
    121                         $model = end($x);                        
     147                        $model = end($x); 
    122148                        unset($x[count($x)-1]); 
    123149                        $path = implode('/', $x).'/'; 
     
    146172                        show_error('Unable to locate the model you have specified: '.$model); 
    147173                } 
    148                                  
     174                 
    149175                if ($db_conn !== FALSE AND ! class_exists('CI_DB')) 
    150176                { 
     
    163189 
    164190                $model = ucfirst($model); 
    165                                  
     191                 
    166192                $CI->$name = new $model(); 
    167193                $CI->$name->_assign_libraries(); 
    168194                 
    169                 $this->_ci_models[] = $name;     
     195                $this->_ci_models[] = $name; 
    170196        } 
    171197                 
     
    663689 
    664690        // -------------------------------------------------------------------- 
    665  
     691         
    666692        /** 
    667693         * Load class 
     
    677703        {        
    678704                // Get the class name 
    679                 $class = str_replace(EXT, '', $class); 
    680  
     705                $class = str_replace(EXT, '', strtolower($class)); 
     706                 
    681707                // We'll test for both lowercase and capitalized versions of the file name 
    682                 foreach (array(ucfirst($class), strtolower($class)) as $class) 
    683                 { 
    684                         // Is this a class extension request? 
    685                         if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT)) 
    686                         { 
    687                                 if ( ! file_exists(BASEPATH.'libraries/'.ucfirst($class).EXT)) 
    688                                 { 
    689                                         log_message('error', "Unable to load the requested class: ".$class); 
    690                                         show_error("Unable to load the requested class: ".$class); 
    691                                 } 
    692          
    693                                 include(BASEPATH.'libraries/'.ucfirst($class).EXT); 
    694                                 include(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT); 
    695          
    696                                 return $this->_ci_init_class($class, config_item('subclass_prefix'), $params);                   
    697                         } 
    698                  
    699                         // Lets search for the requested library file and load it. 
    700                         $is_duplicate = FALSE;           
    701                         for ($i = 1; $i < 3; $i++) 
    702                         { 
    703                                 $path = ($i % 2) ? APPPATH : BASEPATH;   
    704                                 $fp = $path.'libraries/'.$class.EXT; 
    705                                  
    706                                 // Does the file exist?  No?  Bummer... 
    707                                 if ( ! file_exists($fp)) 
    708                                 { 
    709                                         continue; 
    710                                 } 
    711                                  
    712                                 // Safety:  Was the class already loaded by a previous call? 
    713                                 if (in_array($fp, $this->_ci_classes)) 
    714                                 { 
    715                                         $is_duplicate = TRUE; 
    716                                         log_message('debug', $class." class already loaded. Second attempt ignored."); 
    717                                         return; 
    718                                 } 
    719                                  
     708                foreach (array($class, ucfirst($class)) as $class) 
     709                { 
     710                        $fp = $this->_ci_find_class($class); 
     711                        $ex = $this->_ci_find_class(config_item('subclass_prefix').$class); 
     712                         
     713                        // Extension found, but no class found 
     714                        if ($ex == TRUE AND $fp == FALSE) 
     715                        { 
     716                                log_message('error', "Unable to load the requested class: ".$class); 
     717                                show_error("Unable to load the requested class: ".$class); 
     718                        } 
     719                         
     720                        // Class is already loaded, log a message and stop 
     721                        if ($fp === TRUE) 
     722                        { 
     723                                log_message('debug', $class." class already loaded. Second attempt ignored."); 
     724                                return; 
     725                        } 
     726                         
     727                        // No class found 
     728                        if ($fp == FALSE) 
     729                        { 
     730                                continue; 
     731                        } 
     732                        else 
     733                        { 
    720734                                include($fp); 
    721                                 $this->_ci_classes[] = $fp; 
     735                        } 
     736                         
     737                        // For safety checks 
     738                        $this->_ci_classes[] = $fp; 
     739                         
     740                        // Include extension, if one was found 
     741                        if ($ex == TRUE) 
     742                        { 
     743                                include($ex); 
     744                                return $this->_ci_init_class($class, config_item('subclass_prefix'), $params); 
     745                        } 
     746                        else 
     747                        { 
    722748                                return $this->_ci_init_class($class, '', $params); 
    723749                        } 
    724                 } // END FOREACH 
    725                  
    726                 // If we got this far we were unable to find the requested class. 
    727                 // We do not issue errors if the load call failed due to a duplicate request 
    728                 if ($is_duplicate == FALSE) 
    729                 { 
    730                         log_message('error', "Unable to load the requested class: ".$class); 
    731                         show_error("Unable to load the requested class: ".$class); 
    732                 } 
     750                }// END FOREACH 
     751                 
     752                // If we got this far we were unable to find the requested class 
     753                log_message('error', "Unable to load the requested class: ".$class); 
     754                show_error("Unable to load the requested class: ".$class); 
     755        } 
     756         
     757        // -------------------------------------------------------------------- 
     758         
     759        /** 
     760         * Find class 
     761         * 
     762         * This function finds the requested class. 
     763         * 
     764         * @access      private 
     765         * @param       string  the item that is being loaded 
     766         * @param       array   paths to search in 
     767         * @return      void 
     768         */ 
     769        function _ci_find_class($class, $paths = false)  
     770        { 
     771                // Default to using the standard paths 
     772                if ( ! is_array($paths)) 
     773                { 
     774                        $paths = array(APPPATH, BASEPATH); 
     775                } 
     776                 
     777                foreach ($paths as $path) 
     778                { 
     779                        $fp = $path.'libraries/'.$class.EXT; 
     780                        // Safety:  Was the class already loaded by a previous call? 
     781                        if (in_array($fp, $this->_ci_classes)) 
     782                        { 
     783                                return TRUE; 
     784                        } 
     785                        // Does the file exist? 
     786                        if (file_exists($fp)) 
     787                        { 
     788                                return $fp; 
     789                        } 
     790                } 
     791                 
     792                // No class was found 
     793                return FALSE; 
    733794        } 
    734795         
  • trunk/system/libraries/Router.php

    r24 r47  
    404404        function _filter_uri($str) 
    405405        { 
     406                $str = rawurldecode($str); 
     407                 
     408                if ($this->config->item('enable_get_requests'))  
     409                { 
     410                        $end = strpos($str, '?'); 
     411                        $str = ($end !== FALSE) ? substr($str, 0, $end) : $str; 
     412                } 
     413                 
    406414                if ($this->config->item('permitted_uri_chars') != '') 
    407415                { 
     
    410418                                exit('The URI you submitted has disallowed characters.'); 
    411419                        } 
    412                 }        
    413                         return $str; 
     420                } 
     421                 
     422                return $str; 
    414423        } 
    415424         
     
    443452                if (isset($this->routes[$uri])) 
    444453                { 
    445                         $this->_compile_segments(explode('/', $this->routes[$uri]));             
     454                        $this->_compile_segments(explode('/', $this->routes[$uri])); 
    446455                        return; 
    447456                } 
     
    449458                // Loop through the route array looking for wild-cards 
    450459                foreach (array_slice($this->routes, 1) as $key => $val) 
    451                 {                                                
     460                { 
    452461                        // Convert wild-cards to RegEx 
    453462                        $key = str_replace(':any', '.+', str_replace(':num', '[0-9]+', $key)); 
     
    455464                        // Does the RegEx match? 
    456465                        if (preg_match('#^'.$key.'$#', $uri)) 
    457                         {                        
     466                        { 
    458467                                // Do we have a back-reference? 
    459468                                if (strpos($val, '$') !== FALSE AND strpos($key, '(') !== FALSE) 
     
    462471                                } 
    463472                         
    464                                 $this->_compile_segments(explode('/', $val));            
     473                                $this->_compile_segments(explode('/', $val)); 
    465474                                return; 
    466475                        }