Duh
[squirrelmail.git] / functions / plugin.php
index 8cf261b73cb23d3d3d47c77f16b42d4d84ec42a2..5576ab4d875550272b0ef171fbe7e5a76b561900 100644 (file)
@@ -7,19 +7,12 @@
  *
  * Documentation on how to write plugins might show up some time.
  *
- * @copyright © 1999-2005 The SquirrelMail Project Team
+ * @copyright © 1999-2006 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package squirrelmail
  */
 
-/** Everything needs global.. */
-require_once(SM_PATH . 'functions/global.php');
-require_once(SM_PATH . 'functions/prefs.php');
-
-global $squirrelmail_plugin_hooks;
-$squirrelmail_plugin_hooks = array();
-
 /**
  * This function adds a plugin.
  * @param string $name Internal plugin name (ie. delete_move_next)
@@ -199,7 +192,7 @@ function is_plugin_enabled($plugin_name) {
   global $plugins;
 
   /**
-   * check if variable is empty. if var is not set, php empty 
+   * check if variable is empty. if var is not set, php empty
    * returns true without error notice.
    *
    * then check if it is an array
@@ -214,15 +207,99 @@ function is_plugin_enabled($plugin_name) {
   }
 }
 
-/*************************************/
-/*** MAIN PLUGIN LOADING CODE HERE ***/
-/*************************************/
+/**
+  * 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.2
+  *
+  * @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;
 
-/* On startup, register all plugins configured for use. */
-if (isset($plugins) && is_array($plugins)) {
-    foreach ($plugins as $name) {
-        use_plugin($name);
-    }
 }
 
-?>
\ No newline at end of file