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 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 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 |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 |
Andrey Andreev | 80500af | 2013-01-01 08:16:53 +0200 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 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 | */ |
Andrey Andreev | c5536aa | 2012-11-01 17:33:58 +0200 | [diff] [blame] | 27 | defined('BASEPATH') OR exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 28 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 29 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 30 | * Hooks Class |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 31 | * |
Derek Jones | 6f4d17f | 2010-03-02 13:34:23 -0600 | [diff] [blame] | 32 | * Provides a mechanism to extend the base system without hacking. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 33 | * |
| 34 | * @package CodeIgniter |
| 35 | * @subpackage Libraries |
| 36 | * @category Libraries |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 37 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 38 | * @link http://codeigniter.com/user_guide/libraries/encryption.html |
| 39 | */ |
| 40 | class CI_Hooks { |
| 41 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 42 | /** |
Alex Bilbie | f512b73 | 2012-06-16 11:15:19 +0100 | [diff] [blame] | 43 | * Determines whether hooks are enabled |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 44 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 45 | * @var bool |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 46 | */ |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 47 | public $enabled = FALSE; |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 48 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 49 | /** |
| 50 | * List of all hooks set in config/hooks.php |
| 51 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 52 | * @var array |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 53 | */ |
Timothy Warren | 48a7fbb | 2012-04-23 11:58:16 -0400 | [diff] [blame] | 54 | public $hooks = array(); |
Andrey Andreev | 92ebfb6 | 2012-05-17 12:49:24 +0300 | [diff] [blame] | 55 | |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 56 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 57 | * In progress flag |
| 58 | * |
Alex Bilbie | f512b73 | 2012-06-16 11:15:19 +0100 | [diff] [blame] | 59 | * Determines whether hook is in progress, used to prevent infinte loops |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 60 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 61 | * @var bool |
David Behler | 9b5df59 | 2011-08-14 21:04:17 +0200 | [diff] [blame] | 62 | */ |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 63 | protected $_in_progress = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 64 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | /** |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 66 | * Class constructor |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 67 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 68 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 69 | */ |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 70 | public function __construct() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 71 | { |
Derek Jones | c64ca01 | 2010-03-07 07:55:56 -0600 | [diff] [blame] | 72 | $CFG =& load_class('Config', 'core'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 73 | |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 74 | log_message('debug', 'Hooks Class Initialized'); |
| 75 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 76 | // If hooks are not enabled in the config file |
| 77 | // there is nothing else to do |
Alex Bilbie | ed944a3 | 2012-06-02 11:07:47 +0100 | [diff] [blame] | 78 | if ($CFG->item('enable_hooks') === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 79 | { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // Grab the "hooks" definition file. |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 84 | if (file_exists(APPPATH.'config/'.ENVIRONMENT.'/hooks.php')) |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 85 | { |
| 86 | include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'); |
| 87 | } |
Andrey Andreev | 0687911 | 2013-01-29 15:05:02 +0200 | [diff] [blame] | 88 | |
| 89 | if (file_exists(APPPATH.'config/hooks.php')) |
Greg Aker | d96f882 | 2011-12-27 16:23:47 -0600 | [diff] [blame] | 90 | { |
| 91 | include(APPPATH.'config/hooks.php'); |
| 92 | } |
| 93 | |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 94 | // If there are no hooks, we're done. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 95 | if ( ! isset($hook) OR ! is_array($hook)) |
| 96 | { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | $this->hooks =& $hook; |
| 101 | $this->enabled = TRUE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 102 | } |
| 103 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | // -------------------------------------------------------------------- |
| 105 | |
| 106 | /** |
| 107 | * Call Hook |
| 108 | * |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 109 | * Calls a particular hook. Called by CodeIgniter.php. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 110 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 111 | * @uses CI_Hooks::_run_hook() |
| 112 | * |
| 113 | * @param string $which Hook name |
| 114 | * @return bool TRUE on success or FALSE on failure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | */ |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 116 | public function call_hook($which = '') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 117 | { |
| 118 | if ( ! $this->enabled OR ! isset($this->hooks[$which])) |
| 119 | { |
| 120 | return FALSE; |
| 121 | } |
| 122 | |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 123 | if (isset($this->hooks[$which][0]) && is_array($this->hooks[$which][0])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | { |
| 125 | foreach ($this->hooks[$which] as $val) |
| 126 | { |
| 127 | $this->_run_hook($val); |
| 128 | } |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | $this->_run_hook($this->hooks[$which]); |
| 133 | } |
| 134 | |
| 135 | return TRUE; |
| 136 | } |
| 137 | |
| 138 | // -------------------------------------------------------------------- |
| 139 | |
| 140 | /** |
| 141 | * Run Hook |
| 142 | * |
| 143 | * Runs a particular hook |
| 144 | * |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 145 | * @param array $data Hook details |
| 146 | * @return bool TRUE on success or FALSE on failure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 147 | */ |
Andrey Andreev | f9938a2 | 2012-01-07 22:10:47 +0200 | [diff] [blame] | 148 | protected function _run_hook($data) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | { |
| 150 | if ( ! is_array($data)) |
| 151 | { |
| 152 | return FALSE; |
| 153 | } |
| 154 | |
| 155 | // ----------------------------------- |
| 156 | // Safety - Prevents run-away loops |
| 157 | // ----------------------------------- |
| 158 | |
| 159 | // If the script being called happens to have the same |
| 160 | // hook call within it a loop can happen |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 161 | if ($this->_in_progress === TRUE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | // ----------------------------------- |
| 167 | // Set file path |
| 168 | // ----------------------------------- |
| 169 | |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 170 | if ( ! isset($data['filepath'], $data['filename'])) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | { |
| 172 | return FALSE; |
| 173 | } |
| 174 | |
| 175 | $filepath = APPPATH.$data['filepath'].'/'.$data['filename']; |
| 176 | |
| 177 | if ( ! file_exists($filepath)) |
| 178 | { |
| 179 | return FALSE; |
| 180 | } |
| 181 | |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 182 | // Determine and class and/or function names |
| 183 | $class = empty($data['class']) ? FALSE : $data['class']; |
| 184 | $function = empty($data['function']) ? FALSE : $data['function']; |
| 185 | $params = isset($data['params']) ? $data['params'] : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 186 | |
Andrey Andreev | da8c7a5 | 2014-01-07 18:08:26 +0200 | [diff] [blame^] | 187 | if (empty($function)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | { |
| 189 | return FALSE; |
| 190 | } |
| 191 | |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 192 | // Set the _in_progress flag |
| 193 | $this->_in_progress = TRUE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 194 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 195 | // Call the requested class and/or function |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | if ($class !== FALSE) |
| 197 | { |
Andrey Andreev | da8c7a5 | 2014-01-07 18:08:26 +0200 | [diff] [blame^] | 198 | class_exists($class, FALSE) OR require_once($filepath); |
| 199 | |
| 200 | if ( ! class_exists($class, FALSE) OR ! method_exists($class, $function)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | { |
Andrey Andreev | da8c7a5 | 2014-01-07 18:08:26 +0200 | [diff] [blame^] | 202 | return $this->_in_progress = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 205 | $HOOK = new $class(); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 206 | $HOOK->$function($params); |
| 207 | } |
| 208 | else |
| 209 | { |
Andrey Andreev | da8c7a5 | 2014-01-07 18:08:26 +0200 | [diff] [blame^] | 210 | function_exists($function) OR require_once($filepath); |
| 211 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 212 | if ( ! function_exists($function)) |
| 213 | { |
Andrey Andreev | da8c7a5 | 2014-01-07 18:08:26 +0200 | [diff] [blame^] | 214 | return $this->_in_progress = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | $function($params); |
| 218 | } |
| 219 | |
Andrey Andreev | 3e9d2b8 | 2012-10-27 14:28:51 +0300 | [diff] [blame] | 220 | $this->_in_progress = FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 221 | return TRUE; |
| 222 | } |
| 223 | |
| 224 | } |
| 225 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 226 | /* End of file Hooks.php */ |
Andrey Andreev | a5dd297 | 2012-03-26 14:43:01 +0300 | [diff] [blame] | 227 | /* Location: ./system/core/Hooks.php */ |