Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 1 | <?php |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Andrey Andreev | fe9309d | 2015-01-09 17:48:58 +0200 | [diff] [blame] | 5 | * An open source application development framework for PHP |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 7 | * This content is released under the MIT License (MIT) |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 8 | * |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 9 | * Copyright (c) 2014 - 2018, British Columbia Institute of Technology |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 10 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 11 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 12 | * of this software and associated documentation files (the "Software"), to deal |
| 13 | * in the Software without restriction, including without limitation the rights |
| 14 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 15 | * copies of the Software, and to permit persons to whom the Software is |
| 16 | * furnished to do so, subject to the following conditions: |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 17 | * |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 18 | * The above copyright notice and this permission notice shall be included in |
| 19 | * all copies or substantial portions of the Software. |
| 20 | * |
| 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 22 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 23 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 24 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 25 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 26 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 27 | * THE SOFTWARE. |
| 28 | * |
| 29 | * @package CodeIgniter |
| 30 | * @author EllisLab Dev Team |
Andrey Andreev | 1924e87 | 2016-01-11 12:55:34 +0200 | [diff] [blame] | 31 | * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/) |
Andrey Andreev | cce6bd1 | 2018-01-09 11:32:02 +0200 | [diff] [blame^] | 32 | * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/) |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 33 | * @license http://opensource.org/licenses/MIT MIT License |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 34 | * @link https://codeigniter.com |
Andrey Andreev | bdb96ca | 2014-10-28 00:13:31 +0200 | [diff] [blame] | 35 | * @since Version 1.0.0 |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 36 | * @filesource |
| 37 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 38 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 40 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 41 | * Hooks Class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 42 | * |
Derek Jones | 6f4d17f | 2010-03-02 13:34:23 -0600 | [diff] [blame] | 43 | * Provides a mechanism to extend the base system without hacking. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 44 | * |
| 45 | * @package CodeIgniter |
| 46 | * @subpackage Libraries |
| 47 | * @category Libraries |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 48 | * @author EllisLab Dev Team |
Andrey Andreev | bd202c9 | 2016-01-11 12:50:18 +0200 | [diff] [blame] | 49 | * @link https://codeigniter.com/user_guide/general/hooks.html |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 50 | */ |
| 51 | class CI_Hooks { |
| 52 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 53 | /** |
Alex Bilbie | f512b73 | 2012-06-16 11:15:19 +0100 | [diff] [blame] | 54 | * Determines whether hooks are enabled |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 55 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 56 | * @var bool |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 57 | */ |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 58 | public $enabled = FALSE; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 59 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 60 | /** |
| 61 | * List of all hooks set in config/hooks.php |
| 62 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 63 | * @var array |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 64 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 65 | public $hooks = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 66 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 67 | /** |
Marcos SF Filho | 0f667c9 | 2014-01-07 15:01:56 -0200 | [diff] [blame] | 68 | * Array with class objects to use hooks methods |
| 69 | * |
| 70 | * @var array |
| 71 | */ |
Marcos SF Filho | bdfef07 | 2014-01-08 09:48:09 -0200 | [diff] [blame] | 72 | protected $_objects = array(); |
Marcos SF Filho | 0f667c9 | 2014-01-07 15:01:56 -0200 | [diff] [blame] | 73 | |
| 74 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 75 | * In progress flag |
| 76 | * |
Alex Bilbie | f512b73 | 2012-06-16 11:15:19 +0100 | [diff] [blame] | 77 | * Determines whether hook is in progress, used to prevent infinte loops |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 78 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 79 | * @var bool |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 80 | */ |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 81 | protected $_in_progress = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 82 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 83 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 84 | * Class constructor |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 85 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 86 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 87 | */ |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 88 | public function __construct() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 89 | { |
Derek Jones | c64ca01 | 2010-03-07 07:55:56 -0600 | [diff] [blame] | 90 | $CFG =& load_class('Config', 'core'); |
Andrey Andreev | 90726b8 | 2015-01-20 12:39:22 +0200 | [diff] [blame] | 91 | log_message('info', 'Hooks Class Initialized'); |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 92 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 93 | // If hooks are not enabled in the config file |
| 94 | // there is nothing else to do |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 95 | if ($CFG->item('enable_hooks') === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | // Grab the "hooks" definition file. |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 101 | if (file_exists(APPPATH.'config/hooks.php')) |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 102 | { |
| 103 | include(APPPATH.'config/hooks.php'); |
| 104 | } |
| 105 | |
vlakoff | 4be1604 | 2015-01-04 17:16:20 +0100 | [diff] [blame] | 106 | if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/hooks.php')) |
| 107 | { |
| 108 | include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'); |
| 109 | } |
| 110 | |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 111 | // If there are no hooks, we're done. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 112 | if ( ! isset($hook) OR ! is_array($hook)) |
| 113 | { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | $this->hooks =& $hook; |
| 118 | $this->enabled = TRUE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 121 | // -------------------------------------------------------------------- |
| 122 | |
| 123 | /** |
| 124 | * Call Hook |
| 125 | * |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 126 | * Calls a particular hook. Called by CodeIgniter.php. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 128 | * @uses CI_Hooks::_run_hook() |
| 129 | * |
| 130 | * @param string $which Hook name |
| 131 | * @return bool TRUE on success or FALSE on failure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 132 | */ |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 133 | public function call_hook($which = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | { |
| 135 | if ( ! $this->enabled OR ! isset($this->hooks[$which])) |
| 136 | { |
| 137 | return FALSE; |
| 138 | } |
| 139 | |
Andrey Andreev | 94293ad | 2014-06-12 11:33:43 +0300 | [diff] [blame] | 140 | if (is_array($this->hooks[$which]) && ! isset($this->hooks[$which]['function'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 141 | { |
| 142 | foreach ($this->hooks[$which] as $val) |
| 143 | { |
| 144 | $this->_run_hook($val); |
| 145 | } |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | $this->_run_hook($this->hooks[$which]); |
| 150 | } |
| 151 | |
| 152 | return TRUE; |
| 153 | } |
| 154 | |
| 155 | // -------------------------------------------------------------------- |
| 156 | |
| 157 | /** |
| 158 | * Run Hook |
| 159 | * |
| 160 | * Runs a particular hook |
| 161 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 162 | * @param array $data Hook details |
| 163 | * @return bool TRUE on success or FALSE on failure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | */ |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 165 | protected function _run_hook($data) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 166 | { |
Andrey Andreev | 8351404 | 2014-03-06 00:28:55 +0200 | [diff] [blame] | 167 | // Closures/lambda functions and array($object, 'method') callables |
| 168 | if (is_callable($data)) |
| 169 | { |
| 170 | is_array($data) |
| 171 | ? $data[0]->{$data[1]}() |
| 172 | : $data(); |
| 173 | |
| 174 | return TRUE; |
| 175 | } |
| 176 | elseif ( ! is_array($data)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 177 | { |
| 178 | return FALSE; |
| 179 | } |
| 180 | |
| 181 | // ----------------------------------- |
| 182 | // Safety - Prevents run-away loops |
| 183 | // ----------------------------------- |
| 184 | |
| 185 | // If the script being called happens to have the same |
| 186 | // hook call within it a loop can happen |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 187 | if ($this->_in_progress === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | { |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | // ----------------------------------- |
| 193 | // Set file path |
| 194 | // ----------------------------------- |
| 195 | |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 196 | if ( ! isset($data['filepath'], $data['filename'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 197 | { |
| 198 | return FALSE; |
| 199 | } |
| 200 | |
| 201 | $filepath = APPPATH.$data['filepath'].'/'.$data['filename']; |
| 202 | |
| 203 | if ( ! file_exists($filepath)) |
| 204 | { |
| 205 | return FALSE; |
| 206 | } |
| 207 | |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 208 | // Determine and class and/or function names |
| 209 | $class = empty($data['class']) ? FALSE : $data['class']; |
| 210 | $function = empty($data['function']) ? FALSE : $data['function']; |
| 211 | $params = isset($data['params']) ? $data['params'] : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | |
Andrey Andreev | da8c7a5 | 2014-01-07 18:08:26 +0200 | [diff] [blame] | 213 | if (empty($function)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 214 | { |
| 215 | return FALSE; |
| 216 | } |
| 217 | |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 218 | // Set the _in_progress flag |
| 219 | $this->_in_progress = TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 220 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | // Call the requested class and/or function |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | if ($class !== FALSE) |
| 223 | { |
Marcos SF Filho | 0f667c9 | 2014-01-07 15:01:56 -0200 | [diff] [blame] | 224 | // The object is stored? |
Marcos SF Filho | bdfef07 | 2014-01-08 09:48:09 -0200 | [diff] [blame] | 225 | if (isset($this->_objects[$class])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | { |
Marcos SF Filho | bdfef07 | 2014-01-08 09:48:09 -0200 | [diff] [blame] | 227 | if (method_exists($this->_objects[$class], $function)) |
| 228 | { |
| 229 | $this->_objects[$class]->$function($params); |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | return $this->_in_progress = FALSE; |
| 234 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | } |
Marcos SF Filho | 0f667c9 | 2014-01-07 15:01:56 -0200 | [diff] [blame] | 236 | else |
Marcos SF Filho | bdfef07 | 2014-01-08 09:48:09 -0200 | [diff] [blame] | 237 | { |
Marcos SF Filho | 0f667c9 | 2014-01-07 15:01:56 -0200 | [diff] [blame] | 238 | class_exists($class, FALSE) OR require_once($filepath); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 239 | |
Marcos SF Filho | 0f667c9 | 2014-01-07 15:01:56 -0200 | [diff] [blame] | 240 | if ( ! class_exists($class, FALSE) OR ! method_exists($class, $function)) |
| 241 | { |
| 242 | return $this->_in_progress = FALSE; |
| 243 | } |
Marcos SF Filho | bdfef07 | 2014-01-08 09:48:09 -0200 | [diff] [blame] | 244 | |
| 245 | // Store the object and execute the method |
| 246 | $this->_objects[$class] = new $class(); |
| 247 | $this->_objects[$class]->$function($params); |
Marcos SF Filho | 0f667c9 | 2014-01-07 15:01:56 -0200 | [diff] [blame] | 248 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 249 | } |
| 250 | else |
| 251 | { |
Andrey Andreev | da8c7a5 | 2014-01-07 18:08:26 +0200 | [diff] [blame] | 252 | function_exists($function) OR require_once($filepath); |
| 253 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 254 | if ( ! function_exists($function)) |
| 255 | { |
Andrey Andreev | da8c7a5 | 2014-01-07 18:08:26 +0200 | [diff] [blame] | 256 | return $this->_in_progress = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | $function($params); |
| 260 | } |
| 261 | |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 262 | $this->_in_progress = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 263 | return TRUE; |
| 264 | } |
| 265 | |
| 266 | } |