Eli

To content | To menu | To search

PHP - PHP - Eli - page 3 - Elijaa

PHP - PHP - Eli - page 3 - Elijaa

PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML.

Wednesday, July 28 2010

PHP 5.2.14 released

This release focuses on improving the stability of the PHP 5.2.x branch with over 60 bug fixes, some of which are security related.

This release marks the end of the active support for PHP 5.2.
Following this release the PHP 5.2 series will receive no further active bug maintenance.
Security fixes for PHP 5.2 might be published on a case by cases basis.
All users of PHP 5.2 are encouraged to upgrade to PHP 5.3.

Release notes

Security Enhancements and Fixes in PHP 5.2.14 :

  • Rewrote var_export() to use smart_str rather than output buffering, prevents data disclosure if a fatal error occurs.
  • Fixed a possible interruption array leak in strrchr().(CVE-2010-2484)
  • Fixed a possible interruption array leak in strchr(), strstr(), substr(), chunk_split(), strtok(), addcslashes(), str_repeat(), trim().
  • Fixed a possible memory corruption in substr_replace().
  • Fixed SplObjectStorage unserialization problems (CVE-2010-2225).
  • Fixed a possible stack exaustion inside fnmatch().
  • Fixed a NULL pointer dereference when processing invalid XML-RPC requests (Fixes CVE-2010-0397, bug #51288).
  • Fixed handling of session variable serialization on certain prefix characters.
  • Fixed a possible arbitrary memory access inside sqlite extension. Reported by Mateusz Kocielski.d.

Key enhancements in PHP 5.2.14 include:

  • Upgraded bundled PCRE to version 8.02.
  • Updated timezone database to version 2010.5.
  • Fixed bug #52238 (Crash when an Exception occured in iterator_to_array).
  • Fixed bug #52237 (Crash when passing the reference of the property of a non-object).
  • Fixed bug #52041 (Memory leak when writing on uninitialized variable returned from function).
  • Fixed bug #51822 (Segfault with strange __destruct() for static class variables).
  • Fixed bug #51552 (debug_backtrace() causes segmentation fault and/or memory issues).
  • Fixed bug #49267 (Linking fails for iconv on MacOS: "Undefined symbols: _libiconv").

For a full list of changes in PHP 5.2.14 see the see the ChangeLog.




Monday, June 28 2010

Handle configuration in PHP with var_export()

Handling configuration file with PHP

During development i searched an easy way to handle configuration files, either via manual editing or via interface.
At first i was using parse_ini_file() but it becomes quickly clear that i was not made to handle complex data structure.

After some readings i finally end up using var_export() to handle my configuration files.
Note that var_export() also work with objects, but you need first to implement _set_state() which can be quickly painful, especially if your object link to other objects as everyone must have a _set_state() method, so in this example i will stay only with array.

The function var_export()

The goal of var_export() is to return a parsable string representation of a variable, in clear, it return valid PHP code.

mixed var_export  (  mixed $expression  [,  bool $return = false  ] )

It output immediately $expression in PHP code if $return is set to false, or return it as a string if $return is set to true.

What we want is to write our configuration array into a PHP file, so we will use it with $return at true, otherwise you can use ob_start() and ob_get_contents().

Here our configuration file example :

<?php
return array (
  'connection_timeout' => '3',
  'storage' => 'SQLite',
  'clusters' => 
  array (
    'production_cluster' => 
    array (
      0 => '127.0.1.1:11211',
      1 => '127.0.1.2:11211',
      2 => '127.0.1.3:11211',
    ),
    'integration_cluster' => 
    array (
      0 => '128.0.1.1:11211',
      1 => '128.0.1.2:11211',
      2 => '128.0.1.2:11211',
    ),
  ),
);

To include it and use it, we only have to require it, it will use PHP include_path to do so.

/**
 * Load and return configuration file of path $path
 *
 * @param string $path Configuration file path
 *
 * @return array
 */
function load($path) 
{
    return require $path;
}
 
# Now we can use our configuration as an array ...
$myConf = load('Config/file.php');

We can go even further by adding an environment variable (dev, prod, ...) to it to handle and load the right configuration for the right place.

After load, we can implement all things that are needed to use and edit this configuration.
To save it as a PHP file, we must add a php open brace, the return instruction and a ; after the array for this instruction.
All the PHP code returned will be well formatted by var_export() to keep reading easy.

/**
 * Save configuration array $array to path $path
 * Return true if successful, false otherwise
 *
 * @param string $path Configuration file path
 * @param string $array Configuration array
 *
 * @return boolean
 */
function save($path, $array) 
{
    $content = '<?php' . PHP_EOL . 'return ' . var_export($array, true) . ';';
    return is_numeric(file_put_contents($path, $content));
}
 
# Saving configuration
save('Config/file.php', $myModifiedConf);

Now we can load and save one configuration file, how about doing some array_merge() to load base configuration file, and an user redefined configuration file.

Why not using serialize() or json_encode() ?

Both serialize() and unserialize are faster than json_encode() / json_decode() and json functions are not made for this.
Even if it could be a good option, serialized content look likes a big line with a lot of number to most developer and manual editing is a bit tricky, since you must count length of each items, and modify the s:, a: in consequence.

a:3:{s:18:"connection_timeout";s:1:"3";s:7:"storage";s:6:"SQLite";s:8:"clusters";a:2:{s:18:"production_cluster";a:3:{i:0;s:15:"127.0.1.1:11211";i:1;s:15:"127.0.1.2:11211";i:2;s:15:"127.0.1.3:11211";}s:19:"integration_cluster";a:3:{i:0;s:15:"128.0.1.1:11211";i:1;s:15:"128.0.1.2:11211";i:2;s:15:"128.0.1.2:11211";}}}

So I will stick with var_export() for the moment as it keep the configuration code clean and easy editable.




Tuesday, April 20 2010

Handling fatal error in PHP with register_shutdown_function

PHP 5.2.0 bring us a new function : error_get_last()

It return an associative array describing the last error with keys "type", "message", "file" and "line" about the last error that occurred.

We can now use it with register_shutdown_function to handle fatal error, log, redirect, html page, instead of the infamous white page.

/**
 * Handling fatal error
 *
 * @return void
 */
function fatalErrorHandler()
{
    # Getting last error
    $error = error_get_last();
 
    # Checking if last error is a fatal error 
    if(($error['type'] === E_ERROR) || ($error['type'] === E_USER_ERROR))
    {
         # Here we handle the error, displaying HTML, logging, ...
         echo 'Sorry, a serious error has occured in ' . $error['file'];
    }
}
 
# Registering shutdown function
register_shutdown_function('fatalErrorHandler');

Resources :

  • http://www.php.net/manual/en/function.error-get-last.php
  • http://php.net/manual/en/errorfunc.constants.php



page 3 of 3 -