Happy New Year
[squirrelmail.git] / functions / plugin.php
index dfaefd72901c5e11b81cd678ac50bdb206423750..b53a4a16b65643911591bfaf58ac99e15bfcd049 100644 (file)
@@ -7,7 +7,7 @@
  *
  * Documentation on how to write plugins might show up some time.
  *
- * @copyright © 1999-2007 The SquirrelMail Project Team
+ * @copyright 1999-2018 The SquirrelMail Project Team
  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  * @version $Id$
  * @package squirrelmail
@@ -58,7 +58,7 @@ function use_plugin ($name) {
  * independent of) the return value for this hook.
  *
  * @param string $name Name of hook being executed
- * @param mixed  $args A single value or an array of arguments 
+ * @param mixed &$args A single value or an array of arguments 
  *                     that are to be passed to all plugins 
  *                     operating off the hook being called.  
  *                     Note that this argument is passed by 
@@ -120,7 +120,7 @@ function do_hook($name, &$args) {
  * independent of) the return value for this hook.
  *
  * @param string  $name Name of hook being executed
- * @param mixed   $args A single value or an array of arguments 
+ * @param mixed &$args A single value or an array of arguments 
  *                      that are to be passed to all plugins 
  *                      operating off the hook being called.  
  *                      Note that this argument is passed by 
@@ -187,7 +187,7 @@ function concat_hook_function($name, &$args, $force_array=FALSE) {
  * independent of) the return value for this hook.
  *
  * @param string  $name     The hook name
- * @param mixed   $args     A single value or an array of arguments 
+ * @param mixed   &$args    A single value or an array of arguments 
  *                          that are to be passed to all plugins 
  *                          operating off the hook being called.  
  *                          Note that this argument is passed by 
@@ -551,25 +551,31 @@ function check_plugin_version($plugin_name,
   * 
   * @since 1.5.2
   *
-  * @param string plugin_name   Name of the plugin to
-  *                             check; must precisely
-  *                             match the plugin
-  *                             directory name
-  * @param string requirement   The desired requirement name
-  * @param bool force_inclusion Try to get requirement info
-  *                             for plugins not activated?
-  *                             (default FALSE)
+  * @param string  $plugin_name         Name of the plugin to
+  *                                     check; must precisely
+  *                                     match the plugin
+  *                                     directory name
+  * @param string  $requirement         The desired requirement name
+  * @param boolean $ignore_incompatible When TRUE, version incompatibility
+  *                                     information will NOT be returned
+  *                                     if found; when FALSE, it will be
+  *                                     (OPTIONAL; default TRUE)
+  * @param boolean $force_inclusion     Try to get requirement info
+  *                                     for plugins not activated?
+  *                                     (OPTIONAL; default FALSE)
   *
   * @return mixed NULL is returned if the plugin could not be 
   *               found or does not include the given requirement,
-  *               the string "INCOMPATIBLE" is returned if the
+  *               the constant SQ_INCOMPATIBLE is returned if the
   *               given plugin is entirely incompatible with the
-  *               current SquirrelMail version, otherwise the 
+  *               current SquirrelMail version (unless
+  *               $ignore_incompatible is TRUE), otherwise the 
   *               value of the requirement is returned, whatever 
   *               that may be (varies per requirement type).
   *
   */
 function get_plugin_requirement($plugin_name, $requirement, 
+                                $ignore_incompatible = TRUE,
                                 $force_inclusion = FALSE)
 {
 
@@ -667,10 +673,19 @@ function get_plugin_requirement($plugin_name, $requirement,
          $b = $version_array[1];
          $c = $version_array[2];
 
+         // complicated way to say we are interested in these overrides
+         // if the version is applicable to us and if the overrides include
+         // the requirement we are looking for, or if the plugin is not
+         // compatible with this version of SquirrelMail (unless we are
+         // told to ignore such)
+         // 
          if (check_sm_version($a, $b, $c) 
-          && ( !empty($requirement_overrides['INCOMPATIBLE']) 
-          || (isset($requirement_overrides[$requirement])
-          && !is_null($requirement_overrides[$requirement]))))
+          && ((!$ignore_incompatible
+            && (!empty($requirement_overrides[SQ_INCOMPATIBLE]) 
+             || $requirement_overrides === SQ_INCOMPATIBLE))
+           || (is_array($requirement_overrides)
+            && isset($requirement_overrides[$requirement])
+            && !is_null($requirement_overrides[$requirement]))))
          {
 
             if (empty($highest_version_array)
@@ -682,8 +697,9 @@ function get_plugin_requirement($plugin_name, $requirement,
              && $highest_version_array[2] < $c))
             {
                $highest_version_array = $version_array;
-               if (!empty($requirement_overrides['INCOMPATIBLE']))
-                  $requirement_value_override = 'INCOMPATIBLE';
+               if (!empty($requirement_overrides[SQ_INCOMPATIBLE])
+                || $requirement_overrides === SQ_INCOMPATIBLE)
+                  $requirement_value_override = SQ_INCOMPATIBLE;
                else
                   $requirement_value_override = $requirement_overrides[$requirement];
             }
@@ -746,7 +762,7 @@ function get_plugin_requirement($plugin_name, $requirement,
   * @return mixed Boolean FALSE is returned if the plugin
   *               could not be found or does not indicate
   *               whether it has other plugin dependencies, 
-  *               the string "INCOMPATIBLE" is returned if 
+  *               the constant SQ_INCOMPATIBLE is returned if 
   *               the given plugin is entirely incompatible 
   *               with the current SquirrelMail version, 
   *               otherwise an array is returned where keys 
@@ -756,12 +772,17 @@ function get_plugin_requirement($plugin_name, $requirement,
   *               corresponding values) will be available: 
   *               'version' - value is the minimum version 
   *               required for that plugin (the format of 
-  *               which might vary per the value of $do_parse), 
-  *               'activate' - value is boolean: TRUE indicates 
-  *               that the plugin must also be activated, FALSE 
-  *               means that it only needs to be present, but 
-  *               does not need to be activated.  Note that 
-  *               the return value might be an empty array, 
+  *               which might vary per the value of $do_parse
+  *               as well as if the plugin requires a SquirrelMail
+  *               core plugin, in which case it is "CORE" or
+  *               "CORE:1.5.2" or similar, or, if the plugin is
+  *               actually incompatible (not required) with this
+  *               one, the constant SQ_INCOMPATIBLE will be found
+  *               here), 'activate' - value is boolean: TRUE
+  *               indicates that the plugin must also be activated,
+  *               FALSE means that it only needs to be present,
+  *               but does not need to be activated.  Note that
+  *               the return value might be an empty array,
   *               indicating that the plugin has no dependencies.
   *
   */
@@ -771,11 +792,12 @@ function get_plugin_dependencies($plugin_name, $force_inclusion = FALSE,
 
    $plugin_dependencies = get_plugin_requirement($plugin_name, 
                                                  'required_plugins', 
+                                                 FALSE,
                                                  $force_inclusion);
 
    // the plugin is simply incompatible, no need to continue here
    //
-   if ($plugin_dependencies === 'INCOMPATIBLE')
+   if ($plugin_dependencies === SQ_INCOMPATIBLE)
       return $plugin_dependencies;
 
 
@@ -814,7 +836,7 @@ function get_plugin_dependencies($plugin_name, $force_inclusion = FALSE,
 
          // parse version into something we understand?
          //
-         if ($do_parse)
+         if ($do_parse && $plugin_requirements['version'] != SQ_INCOMPATIBLE)
          {
 
             // massage version number into something we understand
@@ -832,10 +854,21 @@ function get_plugin_dependencies($plugin_name, $force_inclusion = FALSE,
             // the regexps are wrapped in a trim that makes sure the version
             // does not start or end with a decimal point
             //
-            $plugin_requirements['version']
-               = trim(preg_replace(array('/[^0-9.]+.*$/', '/[^0-9.]/'), 
-                                   '', $plugin_requirements['version']), 
-                                   '.');
+            if (strpos(strtoupper($plugin_requirements['version']), 'CORE') === 0)
+            {
+               if (strpos($plugin_requirements['version'], ':') === FALSE)
+                  $plugin_requirements['version'] = 'CORE';
+               else
+                  $plugin_requirements['version']
+                     = 'CORE:' . trim(preg_replace(array('/[^0-9.]+.*$/', '/[^0-9.]/'), 
+                                         '', substr($plugin_requirements['version'], strpos($plugin_requirements['version'], ':') + 1)), 
+                                         '.');
+            }
+            else
+               $plugin_requirements['version']
+                  = trim(preg_replace(array('/[^0-9.]+.*$/', '/[^0-9.]/'), 
+                                      '', $plugin_requirements['version']), 
+                                      '.');
 
          }
 
@@ -886,7 +919,7 @@ function get_plugin_dependencies($plugin_name, $force_inclusion = FALSE,
   *
   * @return mixed Boolean TRUE if all of the plugin's 
   *               required plugins are correctly installed,
-  *               the string "INCOMPATIBLE" is returned if 
+  *               the constant SQ_INCOMPATIBLE is returned if 
   *               the given plugin is entirely incompatible 
   *               with the current SquirrelMail version, 
   *               otherwise an array of the required plugins
@@ -897,10 +930,12 @@ function get_plugin_dependencies($plugin_name, $force_inclusion = FALSE,
   *               corresponding values) will be available: 
   *               'version' - value is the minimum version 
   *               required for that plugin (in printable, non-
-  *               parsed format), 'activate' - value is boolean: 
-  *               TRUE indicates that the plugin must also be 
-  *               activated, FALSE means that it only needs to 
-  *               be present, but does not need to be activated.  
+  *               parsed format) or the constant SQ_INCOMPATIBLE,
+  *               which indicates that the plugin is actually
+  *               incompatible (not required), 'activate' - value
+  *               is boolean: TRUE indicates that the plugin must
+  *               also be activated, FALSE means that it only needs
+  *               to be present, but does not need to be activated.  
   *
   */
 function check_plugin_dependencies($plugin_name, $force_inclusion = FALSE)
@@ -908,15 +943,72 @@ function check_plugin_dependencies($plugin_name, $force_inclusion = FALSE)
 
    $dependencies = get_plugin_dependencies($plugin_name, $force_inclusion);
    if (!$dependencies) return TRUE;
-   if ($dependencies === 'INCOMPATIBLE') return $dependencies;
+   if ($dependencies === SQ_INCOMPATIBLE) return $dependencies;
    $missing_or_bad = array();
 
    foreach ($dependencies as $depend_name => $depend_requirements)
    {
-      $version = preg_split('/\./', $depend_requirements['version'], 3);
+
+      // check for core plugins first
+      //
+      if (strpos(strtoupper($depend_requirements['version']), 'CORE') === 0)
+      {
+
+         // see if the plugin is in the core (just check if the directory exists)
+         //
+         if (!file_exists(SM_PATH . 'plugins/' . $depend_name))
+            $missing_or_bad[$depend_name] = $depend_requirements;
+
+
+         // check if it is activated if need be
+         //
+         else if ($depend_requirements['activate'] && !is_plugin_enabled($depend_name))
+            $missing_or_bad[$depend_name] = $depend_requirements;
+
+
+         // check if this is the right core version if one is given
+         // (note this is pretty useless - a plugin should specify
+         // whether or not it itself is compatible with this version
+         // of SM in the first place)
+         //
+         else if (strpos($depend_requirements['version'], ':') !== FALSE)
+         {
+            $version = explode('.', substr($depend_requirements['version'], strpos($depend_requirements['version'], ':') + 1), 3);
+            $version[0] = intval($version[0]);
+            if (isset($version[1])) $version[1] = intval($version[1]);
+            else $version[1] = 0;
+            if (isset($version[2])) $version[2] = intval($version[2]);
+            else $version[2] = 0;
+
+            if (!check_sm_version($version[0], $version[1], $version[2]))
+               $missing_or_bad[$depend_name] = $depend_requirements;
+         }
+
+         continue;
+
+      }
+
+      // if the plugin is actually incompatible; check that it
+      // is not activated
+      //
+      if ($depend_requirements['version'] == SQ_INCOMPATIBLE)
+      {
+
+         if (is_plugin_enabled($depend_name))
+            $missing_or_bad[$depend_name] = $depend_requirements;
+
+         continue;
+
+      }
+
+      // check for normal plugins
+      //
+      $version = explode('.', $depend_requirements['version'], 3);
       $version[0] = intval($version[0]);
-      $version[1] = intval($version[1]);
-      $version[2] = intval($version[2]);
+      if (isset($version[1])) $version[1] = intval($version[1]);
+      else $version[1] = 0;
+      if (isset($version[2])) $version[2] = intval($version[2]);
+      else $version[2] = 0;
 
       $force_dependency_inclusion = !$depend_requirements['activate'];