Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
|
| 2 | /**
|
| 3 | * CodeIgniter
|
| 4 | *
|
| 5 | * An open source application development framework for PHP 4.3.2 or newer
|
| 6 | *
|
| 7 | * @package CodeIgniter
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 8 | * @author ExpressionEngine Dev Team
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 9 | * @copyright Copyright (c) 2006, EllisLab, Inc.
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 10 | * @license http://codeigniter.com/user_guide/license.html
|
| 11 | * @link http://codeigniter.com
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 12 | * @since Version 1.0
|
| 13 | * @filesource
|
| 14 | */
|
| 15 |
|
| 16 | // ------------------------------------------------------------------------
|
| 17 |
|
| 18 | /**
|
| 19 | * CodeIgniter Hooks Class
|
| 20 | *
|
| 21 | * Provides a mechanism to extend the base system without hacking. Most of
|
| 22 | * this class is borrowed from Paul's Extension class in ExpressionEngine.
|
| 23 | *
|
| 24 | * @package CodeIgniter
|
| 25 | * @subpackage Libraries
|
| 26 | * @category Libraries
|
Derek Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame] | 27 | * @author ExpressionEngine Dev Team
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 28 | * @link http://codeigniter.com/user_guide/libraries/encryption.html
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 29 | */
|
| 30 | class CI_Hooks {
|
| 31 |
|
| 32 | var $enabled = FALSE;
|
| 33 | var $hooks = array();
|
| 34 | var $in_progress = FALSE;
|
| 35 |
|
| 36 | /**
|
| 37 | * Constructor
|
| 38 | *
|
| 39 | */
|
| 40 | function CI_Hooks()
|
| 41 | {
|
| 42 | $this->_initialize();
|
| 43 | log_message('debug', "Hooks Class Initialized");
|
| 44 | }
|
| 45 |
|
| 46 | // --------------------------------------------------------------------
|
| 47 |
|
| 48 | /**
|
| 49 | * Initialize the Hooks Preferences
|
| 50 | *
|
| 51 | * @access private
|
| 52 | * @return void
|
| 53 | */
|
| 54 | function _initialize()
|
| 55 | {
|
| 56 | $CFG =& load_class('Config');
|
| 57 |
|
| 58 | // If hooks are not enabled in the config file
|
| 59 | // there is nothing else to do
|
| 60 |
|
| 61 | if ($CFG->item('enable_hooks') == FALSE)
|
| 62 | {
|
| 63 | return;
|
| 64 | }
|
| 65 |
|
| 66 | // Grab the "hooks" definition file.
|
| 67 | // If there are no hooks, we're done.
|
| 68 |
|
| 69 | @include(APPPATH.'config/hooks'.EXT);
|
| 70 |
|
Derek Allard | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 71 | if (! isset($hook) OR ! is_array($hook))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 72 | {
|
| 73 | return;
|
| 74 | }
|
| 75 |
|
| 76 | $this->hooks =& $hook;
|
| 77 | $this->enabled = TRUE;
|
| 78 | }
|
| 79 |
|
| 80 | // --------------------------------------------------------------------
|
| 81 |
|
| 82 | /**
|
| 83 | * Call Hook
|
| 84 | *
|
| 85 | * Calls a particular hook
|
| 86 | *
|
| 87 | * @access private
|
| 88 | * @param string the hook name
|
| 89 | * @return mixed
|
| 90 | */
|
| 91 | function _call_hook($which = '')
|
| 92 | {
|
Derek Allard | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 93 | if (! $this->enabled OR ! isset($this->hooks[$which]))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 94 | {
|
| 95 | return FALSE;
|
| 96 | }
|
| 97 |
|
| 98 | if (isset($this->hooks[$which][0]) AND is_array($this->hooks[$which][0]))
|
| 99 | {
|
| 100 | foreach ($this->hooks[$which] as $val)
|
| 101 | {
|
| 102 | $this->_run_hook($val);
|
| 103 | }
|
| 104 | }
|
| 105 | else
|
| 106 | {
|
| 107 | $this->_run_hook($this->hooks[$which]);
|
| 108 | }
|
| 109 |
|
| 110 | return TRUE;
|
| 111 | }
|
| 112 |
|
| 113 | // --------------------------------------------------------------------
|
| 114 |
|
| 115 | /**
|
| 116 | * Run Hook
|
| 117 | *
|
| 118 | * Runs a particular hook
|
| 119 | *
|
| 120 | * @access private
|
| 121 | * @param array the hook details
|
| 122 | * @return bool
|
| 123 | */
|
| 124 | function _run_hook($data)
|
| 125 | {
|
Derek Allard | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 126 | if (! is_array($data))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 127 | {
|
| 128 | return FALSE;
|
| 129 | }
|
| 130 |
|
| 131 | // -----------------------------------
|
| 132 | // Safety - Prevents run-away loops
|
| 133 | // -----------------------------------
|
| 134 |
|
| 135 | // If the script being called happens to have the same
|
| 136 | // hook call within it a loop can happen
|
| 137 |
|
| 138 | if ($this->in_progress == TRUE)
|
| 139 | {
|
| 140 | return;
|
| 141 | }
|
| 142 |
|
| 143 | // -----------------------------------
|
| 144 | // Set file path
|
| 145 | // -----------------------------------
|
| 146 |
|
Derek Allard | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 147 | if (! isset($data['filepath']) OR ! isset($data['filename']))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 148 | {
|
| 149 | return FALSE;
|
| 150 | }
|
| 151 |
|
| 152 | $filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
|
| 153 |
|
Derek Allard | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 154 | if (! file_exists($filepath))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 155 | {
|
| 156 | return FALSE;
|
| 157 | }
|
| 158 |
|
| 159 | // -----------------------------------
|
| 160 | // Set class/function name
|
| 161 | // -----------------------------------
|
| 162 |
|
| 163 | $class = FALSE;
|
| 164 | $function = FALSE;
|
| 165 | $params = '';
|
| 166 |
|
| 167 | if (isset($data['class']) AND $data['class'] != '')
|
| 168 | {
|
| 169 | $class = $data['class'];
|
| 170 | }
|
| 171 |
|
| 172 | if (isset($data['function']))
|
| 173 | {
|
| 174 | $function = $data['function'];
|
| 175 | }
|
| 176 |
|
| 177 | if (isset($data['params']))
|
| 178 | {
|
| 179 | $params = $data['params'];
|
| 180 | }
|
| 181 |
|
| 182 | if ($class === FALSE AND $function === FALSE)
|
| 183 | {
|
| 184 | return FALSE;
|
| 185 | }
|
| 186 |
|
| 187 | // -----------------------------------
|
| 188 | // Set the in_progress flag
|
| 189 | // -----------------------------------
|
| 190 |
|
| 191 | $this->in_progress = TRUE;
|
| 192 |
|
| 193 | // -----------------------------------
|
| 194 | // Call the requested class and/or function
|
| 195 | // -----------------------------------
|
| 196 |
|
| 197 | if ($class !== FALSE)
|
| 198 | {
|
Derek Allard | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 199 | if (! class_exists($class))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 200 | {
|
| 201 | require($filepath);
|
| 202 | }
|
| 203 |
|
| 204 | $HOOK = new $class;
|
| 205 | $HOOK->$function($params);
|
| 206 | }
|
| 207 | else
|
| 208 | {
|
Derek Allard | 7327499 | 2008-05-05 16:39:18 +0000 | [diff] [blame] | 209 | if (! function_exists($function))
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 210 | {
|
| 211 | require($filepath);
|
| 212 | }
|
| 213 |
|
| 214 | $function($params);
|
| 215 | }
|
| 216 |
|
| 217 | $this->in_progress = FALSE;
|
| 218 | return TRUE;
|
| 219 | }
|
| 220 |
|
| 221 | }
|
| 222 |
|
| 223 | // END CI_Hooks class
|
Derek Jones | c7deac9 | 2008-05-11 16:27:41 +0000 | [diff] [blame^] | 224 | ?> |