PHP:safe_dl — Safely Load a PHP extension at runtime

<?php
$extension = “php_domxml”;
var_dump(safe_dl($extension));
/**
* Loads extension safely
*
* Checks whether the extension is already loaded, if so does not try to reload it.
* Depending ot current OS it will try to load “.dll” or “.so” file.
* Some checks are made for GD library.
*
* @param    string    extension the extension that will be loaded
* @author RAJI <raji.peaceful@gmail.com>
* @author RAJI
*/
function safe_dl($extension)
{
$res = 0;
$extension = strtolower(trim($extension));
// if these is an extension supplied remove it
if (($posit = strpos($extension, “.”)) !== false)
$extension = substr($extension, 0, $posit);
// special check for gd1 & gd2
if ($extension == ‘gd’ || $extension == ‘gd2′ )
{
$ar = @gd_info();
if (is_array($ar)
&& !empty($ar["GD Version"])
&& (
// gd1
( ($extension == ‘gd’ && strpos($ar["GD Version"], “1.”) !== false)
||
// gd2
$extension == ‘gd2′&& strpos($ar["GD Version"], “2.”) !== false)
)
)
return true;
else
return false;
}
// if already loading return true
if (!empty($extension)
&& extension_loaded($extension))
return true;
// if not extension not loaded try to load it
if ( !empty($extension) &&
!extension_loaded($extension) &&
( @ini_get(”enable_dl”) == 1 ||
strtolower(@ini_get(”enable_dl”) ) == “on”)
)
{
// depending on OS load appopriate extension
$res = @dl($extension . “.”
. (strpos(strtolower(PHP_OS), “win”) !== false
? ‘dll’
: ’so’));
}
return $res
? true
: false;
}
?>

Tags: , , , , , , ,

Leave a Reply