Source for file plugin.php

Documentation is available at plugin.php

  1. <?php
  2.  
  3. /**
  4.  * plugin.php
  5.  *
  6.  * This file provides the framework for a plugin architecture.
  7.  *
  8.  * Documentation on how to write plugins might show up some time.
  9.  *
  10.  * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  11.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  12.  * @version $Id: plugin.php,v 1.30.2.7 2006/07/18 07:58:34 tokul Exp $
  13.  * @package squirrelmail
  14.  */
  15.  
  16. /** Everything needs global.. */
  17. require_once(SM_PATH 'functions/global.php');
  18.  
  19. global $squirrelmail_plugin_hooks;
  20. $squirrelmail_plugin_hooks array();
  21.  
  22. /**
  23.  * This function adds a plugin.
  24.  * @param string $name Internal plugin name (ie. delete_move_next)
  25.  * @return void 
  26.  */
  27. function use_plugin ($name{
  28.     if (file_exists(SM_PATH "plugins/$name/setup.php")) {
  29.         include_once(SM_PATH "plugins/$name/setup.php");
  30.         $function "squirrelmail_plugin_init_$name";
  31.         if (function_exists($function)) {
  32.             $function();
  33.         }
  34.     }
  35. }
  36.  
  37. /**
  38.  * This function executes a hook.
  39.  * @param string $name Name of hook to fire
  40.  * @return mixed $data
  41.  */
  42. function do_hook ($name{
  43.     global $squirrelmail_plugin_hooks;
  44.     $data func_get_args();
  45.     $ret '';
  46.  
  47.     if (isset($squirrelmail_plugin_hooks[$name])
  48.           && is_array($squirrelmail_plugin_hooks[$name])) {
  49.         foreach ($squirrelmail_plugin_hooks[$nameas $function{
  50.             /* Add something to set correct gettext domain for plugin. */
  51.             if (function_exists($function)) {
  52.                 $function($data);
  53.             }
  54.         }
  55.     }
  56.  
  57.     /* Variable-length argument lists have a slight problem when */
  58.     /* passing values by reference. Pity. This is a workaround.  */
  59.     return $data;
  60. }
  61.  
  62. /**
  63.  * This function executes a hook and allows for parameters to be passed.
  64.  *
  65.  * @param string name the name of the hook
  66.  * @param mixed param the parameters to pass to the hook function
  67.  * @return mixed the return value of the hook function
  68.  */
  69. function do_hook_function($name,$parm=NULL{
  70.     global $squirrelmail_plugin_hooks;
  71.     $ret '';
  72.  
  73.     if (isset($squirrelmail_plugin_hooks[$name])
  74.           && is_array($squirrelmail_plugin_hooks[$name])) {
  75.         foreach ($squirrelmail_plugin_hooks[$nameas $function{
  76.             /* Add something to set correct gettext domain for plugin. */
  77.             if (function_exists($function)) {
  78.                 $ret $function($parm);
  79.             }
  80.         }
  81.     }
  82.  
  83.     /* Variable-length argument lists have a slight problem when */
  84.     /* passing values by reference. Pity. This is a workaround.  */
  85.     return $ret;
  86. }
  87.  
  88. /**
  89.  * This function executes a hook, concatenating the results of each
  90.  * plugin that has the hook defined.
  91.  *
  92.  * @param string name the name of the hook
  93.  * @param mixed parm optional hook function parameters
  94.  * @return string a concatenation of the results of each plugin function
  95.  */
  96. function concat_hook_function($name,$parm=NULL{
  97.     global $squirrelmail_plugin_hooks;
  98.     $ret '';
  99.  
  100.     if (isset($squirrelmail_plugin_hooks[$name])
  101.           && is_array($squirrelmail_plugin_hooks[$name])) {
  102.         foreach ($squirrelmail_plugin_hooks[$nameas $function{
  103.             /* Concatenate results from hook. */
  104.             if (function_exists($function)) {
  105.                 $ret .= $function($parm);
  106.             }
  107.         }
  108.     }
  109.  
  110.     /* Variable-length argument lists have a slight problem when */
  111.     /* passing values by reference. Pity. This is a workaround.  */
  112.     return $ret;
  113. }
  114.  
  115. /**
  116.  * This function is used for hooks which are to return true or
  117.  * false. If $priority is > 0, any one or more trues will override
  118.  * any falses. If $priority < 0, then one or more falses will
  119.  * override any trues.
  120.  * Priority 0 means majority rules.  Ties will be broken with $tie
  121.  *
  122.  * @param string name the hook name
  123.  * @param mixed parm the parameters for the hook function
  124.  * @param int priority
  125.  * @param bool tie
  126.  * @return bool the result of the function
  127.  */
  128. function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false{
  129.     global $squirrelmail_plugin_hooks;
  130.     $yea 0;
  131.     $nay 0;
  132.     $ret $tie;
  133.  
  134.     if (isset($squirrelmail_plugin_hooks[$name]&&
  135.         is_array($squirrelmail_plugin_hooks[$name])) {
  136.  
  137.         /* Loop over the plugins that registered the hook */
  138.         foreach ($squirrelmail_plugin_hooks[$nameas $function{
  139.             if (function_exists($function)) {
  140.                 $ret $function($parm);
  141.                 if ($ret{
  142.                     $yea++;
  143.                 else {
  144.                     $nay++;
  145.                 }
  146.             }
  147.         }
  148.  
  149.         /* Examine the aftermath and assign the return value appropriately */
  150.         if (($priority 0&& ($yea)) {
  151.             $ret true;
  152.         elseif (($priority 0&& ($nay)) {
  153.             $ret false;
  154.         elseif ($yea $nay{
  155.             $ret true;
  156.         elseif ($nay $yea{
  157.             $ret false;
  158.         else {
  159.             // There's a tie, no action needed.
  160.         }
  161.         return $ret;
  162.     }
  163.     // If the code gets here, there was a problem - no hooks, etc.
  164.     return NULL;
  165. }
  166.  
  167. /**
  168.  * This function checks whether the user's USER_AGENT is known to
  169.  * be broken. If so, returns true and the plugin is invisible to the
  170.  * offending browser.
  171.  * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
  172.  * FIXME: This function needs to have its name changed!
  173.  *
  174.  * @return bool whether this browser properly supports JavaScript
  175.  */
  176. function soupNazi(){
  177.  
  178.     $soup_menu array('Mozilla/3','Mozilla/2','Mozilla/1''Opera 4',
  179.                        'Opera/4''OmniWeb''Lynx');
  180.     sqgetGlobalVar('HTTP_USER_AGENT'$user_agentSQ_SERVER);
  181.     foreach($soup_menu as $browser{
  182.         if(stristr($user_agent$browser)) {
  183.             return 1;
  184.         }
  185.     }
  186.     return 0;
  187. }
  188. /*************************************/
  189. /*** MAIN PLUGIN LOADING CODE HERE ***/
  190. /*************************************/
  191.  
  192. /* On startup, register all plugins configured for use. */
  193. if (isset($plugins&& is_array($plugins)) {
  194.     // turn on output buffering in order to prevent output of new lines
  195.         ob_start();
  196.     foreach ($plugins as $name{
  197.         use_plugin($name);
  198.     }
  199.     $output trim(ob_get_contents());
  200.     ob_end_clean();
  201.     // if plugins output more than newlines and spacing, stop script execution.
  202.         if (!empty($output)) {
  203.         die($output);
  204.     }
  205. }
  206.  
  207. ?>

Documentation generated on Sat, 07 Oct 2006 16:33:02 +0300 by phpDocumentor 1.3.0RC6