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