Zend Framework

E_WARNING is thrown (and no exception) when parse_ini_file fails in Zend_Config_Ini

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.5.1
  • Fix Version/s: 1.6.0
  • Component/s: Zend_Config
  • Labels:
    None

Description

When there is a problem parsing the ini file passed to Zend_Config_Ini constructor, the PHP warning message is not caught and no exception is thrown. This is problematic because the developer has no way of knowing that a problem has happened - plus the E_WARNING message should not be displayed.

Use the following ini file to reproduce:

[default]
foo = ("bar")

Then, try to load this file with Zend_Config_Ini

Expected result: An exception thrown

Activity

Hide
julien PAULI added a comment -

I cannot reproduce your use-cased bug with PHP 5.2.5 and ZF at r9372

However, there is a PHP warning if the file loaded cannot be found or is not readable
You can find more info in #ZF-2207

Show
julien PAULI added a comment - I cannot reproduce your use-cased bug with PHP 5.2.5 and ZF at r9372 However, there is a PHP warning if the file loaded cannot be found or is not readable You can find more info in #ZF-2207
Hide
Rob Allen added a comment -

For PHP 5.2.0 or higher we can use error_get_last() to find the warning. e.g:

$iniArray = @parse_ini_file($filename, true); // convert any warnings into exceptions
        $lastError = error_get_last();
        if($lastError && $lastError['line'] = (__LINE__-2) && strstr($lastError['file'], 'Ini.php')) {
            /**
             * @see Zend_Config_Exception
             */
            require_once 'Zend/Config/Exception.php';
            throw new Zend_Config_Exception($lastError['message']);
        }

As we need to support PHP 5.1.4, we have to use a custom error handler:

$old_error_handler = set_error_handler(array('Zend_Config_Ini', 'parseIniFileErrorHandler'));
        $iniArray = parse_ini_file($filename, true); // convert any warnings into exceptions
        restore_error_handler();
Show
Rob Allen added a comment - For PHP 5.2.0 or higher we can use error_get_last() to find the warning. e.g:
$iniArray = @parse_ini_file($filename, true); // convert any warnings into exceptions
        $lastError = error_get_last();
        if($lastError && $lastError['line'] = (__LINE__-2) && strstr($lastError['file'], 'Ini.php')) {
            /**
             * @see Zend_Config_Exception
             */
            require_once 'Zend/Config/Exception.php';
            throw new Zend_Config_Exception($lastError['message']);
        }
As we need to support PHP 5.1.4, we have to use a custom error handler:
$old_error_handler = set_error_handler(array('Zend_Config_Ini', 'parseIniFileErrorHandler'));
        $iniArray = parse_ini_file($filename, true); // convert any warnings into exceptions
        restore_error_handler();
Hide
Rob Allen added a comment -

Fixed on trunk in svn r9636.

Note that this introduces a small BC break if anyone was using the code with display_errors off, as now an exception will happen if this error occurs. Fixed only on trunk as a result.

Show
Rob Allen added a comment - Fixed on trunk in svn r9636. Note that this introduces a small BC break if anyone was using the code with display_errors off, as now an exception will happen if this error occurs. Fixed only on trunk as a result.
Hide
julien PAULI added a comment -

You could use the track_errors cheat as well :

<?php
unset($php_errormsg);
if (ini_get('track_errors') == 0) {
    ini_set('track_errors',1);
}
@my_php_function();
ini_restore('track_errors');
if(isset($php_errormsg)) {
    throw new exception($php_errormsg);
}
Show
julien PAULI added a comment - You could use the track_errors cheat as well :
<?php
unset($php_errormsg);
if (ini_get('track_errors') == 0) {
    ini_set('track_errors',1);
}
@my_php_function();
ini_restore('track_errors');
if(isset($php_errormsg)) {
    throw new exception($php_errormsg);
}
Hide
Wil Sinclair added a comment -

Updating for the 1.6.0 release.

Show
Wil Sinclair added a comment - Updating for the 1.6.0 release.

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: