PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 7ms RepoModel.GetById 33ms app.codeStats 0ms

/tags/rel-1_5_1/squirrelmail/functions/plugin.php

#
PHP | 238 lines | 118 code | 27 blank | 93 comment | 26 complexity | 48601db33e940776b396432a02ade962 MD5 | raw file
  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 10594 2006-01-28 19:12:19Z kink $
 13 * @package squirrelmail
 14 */
 15
 16/** Everything needs global.. */
 17require_once(SM_PATH . 'functions/global.php');
 18require_once(SM_PATH . 'config/config.php');
 19require_once(SM_PATH . 'functions/prefs.php');
 20
 21global $squirrelmail_plugin_hooks;
 22$squirrelmail_plugin_hooks = array();
 23
 24/**
 25 * This function adds a plugin.
 26 * @param string $name Internal plugin name (ie. delete_move_next)
 27 * @return void
 28 */
 29function use_plugin ($name) {
 30    if (file_exists(SM_PATH . "plugins/$name/setup.php")) {
 31        include_once(SM_PATH . "plugins/$name/setup.php");
 32        $function = "squirrelmail_plugin_init_$name";
 33        if (function_exists($function)) {
 34            $function();
 35        }
 36    }
 37}
 38
 39/**
 40 * This function executes a hook.
 41 * @param string $name Name of hook to fire
 42 * @return mixed $data
 43 */
 44function do_hook ($name) {
 45    global $squirrelmail_plugin_hooks, $currentHookName;
 46    $data = func_get_args();
 47    $currentHookName = $name;
 48
 49    if (isset($squirrelmail_plugin_hooks[$name])
 50          && is_array($squirrelmail_plugin_hooks[$name])) {
 51        foreach ($squirrelmail_plugin_hooks[$name] as $function) {
 52            /* Add something to set correct gettext domain for plugin. */
 53            if (function_exists($function)) {
 54                $function($data);
 55            }
 56        }
 57    }
 58
 59    $currentHookName = '';
 60
 61    /* Variable-length argument lists have a slight problem when */
 62    /* passing values by reference. Pity. This is a workaround.  */
 63    return $data;
 64}
 65
 66/**
 67 * This function executes a hook and allows for parameters to be passed.
 68 *
 69 * @param string name the name of the hook
 70 * @param mixed param the parameters to pass to the hook function
 71 * @return mixed the return value of the hook function
 72 */
 73function do_hook_function($name,$parm=NULL) {
 74    global $squirrelmail_plugin_hooks, $currentHookName;
 75    $ret = '';
 76    $currentHookName = $name;
 77
 78    if (isset($squirrelmail_plugin_hooks[$name])
 79          && is_array($squirrelmail_plugin_hooks[$name])) {
 80        foreach ($squirrelmail_plugin_hooks[$name] as $function) {
 81            /* Add something to set correct gettext domain for plugin. */
 82            if (function_exists($function)) {
 83                $ret = $function($parm);
 84            }
 85        }
 86    }
 87
 88    $currentHookName = '';
 89
 90    /* Variable-length argument lists have a slight problem when */
 91    /* passing values by reference. Pity. This is a workaround.  */
 92    return $ret;
 93}
 94
 95/**
 96 * This function executes a hook, concatenating the results of each
 97 * plugin that has the hook defined.
 98 *
 99 * @param string name the name of the hook
100 * @param mixed parm optional hook function parameters
101 * @return string a concatenation of the results of each plugin function
102 */
103function concat_hook_function($name,$parm=NULL) {
104    global $squirrelmail_plugin_hooks, $currentHookName;
105    $ret = '';
106    $currentHookName = $name;
107
108    if (isset($squirrelmail_plugin_hooks[$name])
109          && is_array($squirrelmail_plugin_hooks[$name])) {
110        foreach ($squirrelmail_plugin_hooks[$name] as $function) {
111            /* Concatenate results from hook. */
112            if (function_exists($function)) {
113                $ret .= $function($parm);
114            }
115        }
116    }
117
118    $currentHookName = '';
119
120    /* Variable-length argument lists have a slight problem when */
121    /* passing values by reference. Pity. This is a workaround.  */
122    return $ret;
123}
124
125/**
126 * This function is used for hooks which are to return true or
127 * false. If $priority is > 0, any one or more trues will override
128 * any falses. If $priority < 0, then one or more falses will
129 * override any trues.
130 * Priority 0 means majority rules.  Ties will be broken with $tie
131 *
132 * @param string name the hook name
133 * @param mixed parm the parameters for the hook function
134 * @param int priority
135 * @param bool tie
136 * @return bool the result of the function
137 */
138function boolean_hook_function($name,$parm=NULL,$priority=0,$tie=false) {
139    global $squirrelmail_plugin_hooks, $currentHookName;
140    $yea = 0;
141    $nay = 0;
142    $ret = $tie;
143
144    if (isset($squirrelmail_plugin_hooks[$name]) &&
145        is_array($squirrelmail_plugin_hooks[$name])) {
146
147        /* Loop over the plugins that registered the hook */
148        $currentHookName = $name;
149        foreach ($squirrelmail_plugin_hooks[$name] as $function) {
150            if (function_exists($function)) {
151                $ret = $function($parm);
152                if ($ret) {
153                    $yea++;
154                } else {
155                    $nay++;
156                }
157            }
158        }
159        $currentHookName = '';
160
161        /* Examine the aftermath and assign the return value appropriately */
162        if (($priority > 0) && ($yea)) {
163            $ret = true;
164        } elseif (($priority < 0) && ($nay)) {
165            $ret = false;
166        } elseif ($yea > $nay) {
167            $ret = true;
168        } elseif ($nay > $yea) {
169            $ret = false;
170        } else {
171            // There's a tie, no action needed.
172        }
173        return $ret;
174    }
175    // If the code gets here, there was a problem - no hooks, etc.
176    return NULL;
177}
178
179/**
180 * This function checks whether the user's USER_AGENT is known to
181 * be broken. If so, returns true and the plugin is invisible to the
182 * offending browser.
183 * *** THIS IS A TEST FOR JAVASCRIPT SUPPORT ***
184 * FIXME: This function needs to have its name changed!
185 *
186 * @return bool whether this browser properly supports JavaScript
187 * @deprecated use checkForJavascript() since 1.5.1
188 */
189function soupNazi(){
190    return !checkForJavascript();
191}
192
193/**
194 * Check if plugin is enabled
195 * @param string $plugin_name plugin name
196 * @since 1.5.1
197 * @return boolean
198 */
199function is_plugin_enabled($plugin_name) {
200  global $plugins;
201
202  /**
203   * check if variable is empty. if var is not set, php empty 
204   * returns true without error notice.
205   *
206   * then check if it is an array
207   */
208  if (empty($plugins) || ! is_array($plugins))
209    return false;
210
211  if ( in_array($plugin_name,$plugins) ) {
212    return true;
213  } else {
214    return false;
215  }
216}
217
218/*************************************/
219/*** MAIN PLUGIN LOADING CODE HERE ***/
220/*************************************/
221
222/* On startup, register all plugins configured for use. */
223if (isset($plugins) && is_array($plugins)) {
224    // turn on output buffering in order to prevent output of new lines
225    ob_start();
226    foreach ($plugins as $name) {
227        use_plugin($name);
228    }
229    // get output and remove whitespace
230    $output = trim(ob_get_contents());
231    ob_end_clean();
232    // if plugins output more than newlines and spacing, stop script execution.
233    if (!empty($output)) {
234        die($output);
235    }
236}
237
238?>