New helper function for checking plugin version numbers check_plugin_version()
[squirrelmail.git] / functions / plugin.php
index eaf90da685564a0f38c60f97b95f8eaeea97bb6d..844f9007c4089d0d3c4fb8bec6650a657d4bde46 100644 (file)
@@ -206,3 +206,100 @@ function is_plugin_enabled($plugin_name) {
     return false;
   }
 }
+
+/**
+  * Check a plugin's version.
+  *
+  * Returns TRUE if the given plugin is installed, 
+  * activated and is at minimum version $a.$b.$c.
+  * If any one of those conditions fails, FALSE
+  * will be returned (careful of plugins that are
+  * sufficiently versioned but are not activated).
+  *
+  * By overriding the default value of $force_inclusion,
+  * this function will attempt to grab versioning
+  * information from the given plugin even if it
+  * is not activated (plugin still has to be 
+  * unpackaged and set in place in the plugins 
+  * directory).  Use with care - some plugins
+  * might break SquirrelMail when this is used.
+  *
+  * Note that this function assumes plugin 
+  * versioning is consistently applied in the same 
+  * fashion that SquirrelMail versions are, with the 
+  * exception that an applicable SquirrelMail 
+  * version may be appended to the version number 
+  * (which will be ignored herein).  That is, plugin 
+  * version number schemes are expected in the following
+  * format:  1.2.3, or 1.2.3-1.4.0.  
+  *
+  * Any characters after the third number are discarded, 
+  * so formats such as the following will also work, 
+  * although extra information about beta versions can 
+  * possibly confuse the desired results of the version 
+  * check:  1.2.3-beta4, 1.2.3.RC2, and so forth.
+  * 
+  * @since 1.5.1
+  *
+  * @param string plugin_name   name of the plugin to
+  *                             check; must precisely
+  *                             match the plugin
+  *                             directory name
+  * @param int  a               major version number
+  * @param int  b               minor version number
+  * @param int  c               release number
+  * @param bool force_inclusion try to get version info
+  *                             for plugins not activated?
+  *                             (default FALSE)
+  *
+  * @return bool
+  *
+  */
+function check_plugin_version($plugin_name, 
+                              $a = 0, $b = 0, $c = 0, 
+                              $force_inclusion = FALSE)
+{
+
+   $version_function = $plugin_name . '_version';
+   $plugin_version = FALSE;
+
+
+   // attempt to find version function and get version from plugin
+   //
+   if (function_exists($version_function))
+      $plugin_version = $version_function();
+   else if ($force_inclusion 
+    && file_exists(SM_PATH . 'plugins/' . $plugin_name . '/setup.php'))
+   {
+      include_once(SM_PATH . 'plugins/' . $plugin_name . '/setup.php');
+      if (function_exists($version_function))
+         $plugin_version = $version_function();
+   }
+
+   if (!$plugin_version) return FALSE;
+
+
+   // now massage version number into something we understand
+   //
+   $plugin_version = trim(preg_replace(array('/[^0-9.]+.*$/', '/[^0-9.]/'), 
+                                       '', $plugin_version), 
+                          '.');
+   $plugin_version = explode('.', $plugin_version);
+   if (!isset($plugin_version[0])) $plugin_version[0] = 0;
+   if (!isset($plugin_version[1])) $plugin_version[1] = 0;
+   if (!isset($plugin_version[2])) $plugin_version[2] = 0;
+//   sm_print_r($plugin_version);
+
+
+   // now test the version number
+   //
+   if ($plugin_version[0] < $a ||
+      ($plugin_version[0] == $a && $plugin_version[1] < $b) ||
+      ($plugin_version[0] == $a && $plugin_version[1] == $b && $plugin_version[2] < $c))
+         return FALSE;
+
+
+   return TRUE;
+
+}
+