PHP extension/module check with extension info
By: Daniel

Have you ever needed to check to see if curl, ldap, or mysql was loaded in php for your script to work? With the code below, you can now check much more than that. With the heavily commented code below, you will be able to list all installed php extensions, check if an extension is loaded and view the settings of that extension.

*Please note that this is a PHP5 script

Save the code below as class.phpextensions.php:

<?php
class moduleCheck {

  public $Modules;
  
  //function parseModules() {
  function __construct() {
   ob_start(); // Stop output of the code and hold in buffer
   phpinfo(INFO_MODULES); // get loaded modules and their respective settings.
   $data = ob_get_contents(); // Get the buffer contents and store in $data variable
   ob_end_clean(); // Clear buffer
  
   $data = strip_tags($data,'<h2><th><td>'); // Keep only the items in the <h2>,<th> and <td> tags
   
   // Use regular expressions to filter out needed data
   // Replace everything in the <th> tags and put in <info> tags
   $data = preg_replace('/<th[^>]*>([^<]+)<\/th>/',"<info>\\1</info>",$data);
   
   // Replace everything in <td> tags and put in <info> tags
   $data = preg_replace('/<td[^>]*>([^<]+)<\/td>/',"<info>\\1</info>",$data);
   
   // Split the data into an array
   $vTmp = preg_split('/(<h2>[^<]+<\/h2>)/',$data,-1,PREG_SPLIT_DELIM_CAPTURE);
   $vModules = array();
   $count = count($vTmp);
   for ($i=1;$i<$count; $i+=2) { // Loop through array and add 2 instead of 1
   
    if (preg_match('/<h2>([^<]+)<\/h2>/',$vTmp[$i],$vMat)) { // Check to make sure value is a module
    
     $moduleName = trim($vMat[1]); // Get the module name 
     $vTmp2 = explode("\n",$vTmp[$i+1]);
     foreach ($vTmp2 AS $vOne) {
       $vPat = '<info>([^<]+)<\/info>'; // Specify the pattern we created above
       $vPat3 = "/$vPat\s*$vPat\s*$vPat/"; // Pattern for 2 settings (Local and Master values)
       $vPat2 = "/$vPat\s*$vPat/"; // Pattern for 1 settings
       if (preg_match($vPat3,$vOne,$vMat)) { // This setting has a Local and Master value
         $vModules[$moduleName][trim($vMat[1])] = array(trim($vMat[2]),trim($vMat[3]));
       } elseif (preg_match($vPat2,$vOne,$vMat)) { // This setting only has a value
         $vModules[$moduleName][trim($vMat[1])] = trim($vMat[2]);
       }
     }
     
    }
   }
   $this->Modules = $vModules; // Store modules in Modules variable
  }
  
  // Quick check if module is loaded
  // Returns true if loaded, false if not
  public function isLoaded($moduleName) {
    if($this->Modules[$moduleName]) { 
      return true;
    }
    return false;
  } // End function isLoaded
  
  // Get a module setting
  // Can be a single setting by specifying $setting value or all settings by not specifying $setting value
  public function getModuleSetting($moduleName, $setting = '') {
    // check if module is loaded before continuing
    if($this->isLoaded($moduleName)==false) {
      return 'Module not loaded'; // Module not loaded so return error
    }
    
    if($this->Modules[$moduleName][$setting]) { // You requested an individual setting
      return $this->Modules[$moduleName][$setting];
    } elseif(empty($setting)) { // List all settings
      return $this->Modules[$moduleName];
    }
    // If setting specified and no value found return error
    return 'Setting not found';
  } // End function getModuleSetting
  
  // List all php modules installed with no settings
  public function listModules() {
    foreach($this->Modules as $moduleName=>$values) { // Loop through modules
      // $moduleName is the key of $this->Modules, which is also module name
      $onlyModules[] = $moduleName;
    }
    return $onlyModules; // Return array of all module names
  } // End function listModules();
}
?>

Now to list all of the installed extensions:

<?php
require('class.phpextensions.php');
$modules = new moduleCheck(); // Start the moduleCheck class
echo '<pre>';
print_r($modules->listModules()); // List all installed modules
echo '</pre>';
?>

This would produce something like:

Array
(
    [0] => bcmath
    [1] => calendar
    [2] => com_dotnet
    [3] => ctype
    [4] => curl
    [5] => date
    [6] => dom
    [7] => filter
    [8] => ftp
    [9] => hash
    [10] => iconv
    [11] => ISAPI
    [12] => json
    [13] => ldap
    [14] => libxml
    [15] => mssql
    [16] => mysql
    [17] => odbc
    [18] => pcre
    [19] => Reflection
    [20] => session
    [21] => SimpleXML
    [22] => SPL
    [23] => standard
    [24] => tokenizer
    [25] => wddx
    [26] => xml
    [27] => xmlreader
    [28] => xmlwriter
    [29] => zlib
)

Now check if a single module (curl) is loaded, then get a setting:

<?php
require('class.phpextensions.php');
$modules = new moduleCheck();
if($modules->isLoaded('curl')) { // Test if curl is loaded
  echo 'Curl Loaded<br />';
  echo $modules->getModuleSetting('curl', 'cURL Information'); // Get specific information about a setting in curl
} else {
  echo 'Curl not loaded';
}
?>

This would produce something like:

Curl Loaded
libcurl/7.14.0 OpenSSL/0.9.8d zlib/1.2.3

To list all settings for the curl extension:

<?php
require('class.phpextensions.php');
$modules = new moduleCheck();
echo '<pre>';
print_r($modules->getModuleSetting('curl'));
echo '</pre>';
?>

This would look like:

Array
(
    [cURL support] => enabled
    [cURL Information] => libcurl/7.14.0 OpenSSL/0.9.8d zlib/1.2.3
)

You will use these settings as the second argument for getModuleSetting(‘extension’, ’setting’)

Now when you create an installer application for a script that you have just written, you can create a checklist showing what extensions are loaded and which still need to be loaded in order to continue.

Hope you find this useful.

1 Star2 Stars3 Stars4 Stars5 Stars (15 votes, average: 4.13 out of 5)
Loading ... Loading ...

2 Responses to “PHP extension/module check with extension info”

  1. marcellus Says:

    check the native functions get_loaded_extensions and extension_loaded ffs, saves you all the bs preg matching on the phpinfo() output

  2. Joel Says:

    Maybe you should check the get_loaded_extensions() function in php.
    Goodl Luck

Leave a Reply

geovisitors