Welcome Guest, Not a member yet? Register   Sign In
load_class function in common doesnt perform basic checks
#1

[eluser]Eddie Monge[/eluser]
File /system/codeigniter/Common.php
Lines 88-150
function &load;_class


This function does basic checks to see if the file exists in the application folder and just assumes the core files exist. This is not good as they may not always exist or something weird may be going on. The function should also test to see if the base files also exist since it is requiring them.

Original Code:

Code:
/**
* Class registry
*
* This function acts as a singleton.  If the requested class does not
* exist it is instantiated and set to a static variable.  If it has
* previously been instantiated the variable is returned.
*
* @access    public
* @param    string    the class name being requested
* @param    bool    optional flag that lets classes get loaded but not instantiated
* @return    object
*/
function &load;_class($class, $instantiate = TRUE)
{
    static $objects = array();

    // Does the class exist?  If so, we're done...
    if (isset($objects[$class]))
    {
        return $objects[$class];
    }

    // If the requested class does not exist in the application/libraries
    // folder we'll load the native class from the system/libraries folder.    
    if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT))
    {
        require(BASEPATH.'libraries/'.$class.EXT);
        require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
        $is_subclass = TRUE;
    }
    else
    {
        if (file_exists(APPPATH.'libraries/'.$class.EXT))
        {
            require(APPPATH.'libraries/'.$class.EXT);
            $is_subclass = FALSE;
        }
        else
        {
            require(BASEPATH.'libraries/'.$class.EXT);
            $is_subclass = FALSE;
        }
    }

    if ($instantiate == FALSE)
    {
        $objects[$class] = TRUE;
        return $objects[$class];
    }

    if ($is_subclass == TRUE)
    {
        $name = config_item('subclass_prefix').$class;

        $objects[$class] =& instantiate_class(new $name());
        return $objects[$class];
    }

    $name = ($class != 'Controller') ? 'CI_'.$class : $class;

    $objects[$class] =& instantiate_class(new $name());
    return $objects[$class];
}


Example of new code. Not sure what should happen in the bolded else statement
Quote:/**
* Class registry
*
* This function acts as a singleton. If the requested class does not
* exist it is instantiated and set to a static variable. If it has
* previously been instantiated the variable is returned.
*
* @access public
* @param string the class name being requested
* @param bool optional flag that lets classes get loaded but not instantiated
* @return object
*/
function &load;_class($class, $instantiate = TRUE)
{
static $objects = array();

// Does the class exist? If so, we're done...
if (isset($objects[$class]))
{
return $objects[$class];
}

// If the requested class does not exist in the application/libraries
// folder we'll load the native class from the system/libraries folder.
if (file_exists(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT) && file_exists(BASEPATH.'libraries/'.$class.EXT))
{
require(BASEPATH.'libraries/'.$class.EXT);
require(APPPATH.'libraries/'.config_item('subclass_prefix').$class.EXT);
$is_subclass = TRUE;
}
else
{
if (file_exists(APPPATH.'libraries/'.$class.EXT))
{
require(APPPATH.'libraries/'.$class.EXT);
$is_subclass = FALSE;
}
elseif (file_exists(BASEPATH.'libraries/'.$class.EXT))
{
require(BASEPATH.'libraries/'.$class.EXT);
$is_subclass = FALSE;
}
else
{
show_error($class . ' could not be found.', 404);
}

}

if ($instantiate == FALSE)
{
$objects[$class] = TRUE;
return $objects[$class];
}

if ($is_subclass == TRUE)
{
$name = config_item('subclass_prefix').$class;

$objects[$class] =& instantiate_class(new $name());
return $objects[$class];
}

$name = ($class != 'Controller') ? 'CI_'.$class : $class;

$objects[$class] =& instantiate_class(new $name());
return $objects[$class];
}
#2

[eluser]n0xie[/eluser]
Could you describe a situation where you would use the CI Framework without the Core files?




Theme © iAndrew 2016 - Forum software by © MyBB