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