blob: 2cb416c0cdddc35fe775b880e41300d14cfd0b65 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevf9938a22012-01-07 22:10:47 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevf9938a22012-01-07 22:10:47 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Andrey Andreev80500af2013-01-01 08:16:53 +020021 * @copyright Copyright (c) 2008 - 2013, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030030 * Hooks Class
Derek Allard2067d1a2008-11-13 22:59:24 +000031 *
Derek Jones6f4d17f2010-03-02 13:34:23 -060032 * Provides a mechanism to extend the base system without hacking.
Derek Allard2067d1a2008-11-13 22:59:24 +000033 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/encryption.html
39 */
40class CI_Hooks {
41
David Behler9b5df592011-08-14 21:04:17 +020042 /**
Alex Bilbief512b732012-06-16 11:15:19 +010043 * Determines whether hooks are enabled
David Behler9b5df592011-08-14 21:04:17 +020044 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030045 * @var bool
David Behler9b5df592011-08-14 21:04:17 +020046 */
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030047 public $enabled = FALSE;
Andrey Andreev92ebfb62012-05-17 12:49:24 +030048
David Behler9b5df592011-08-14 21:04:17 +020049 /**
50 * List of all hooks set in config/hooks.php
51 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030052 * @var array
David Behler9b5df592011-08-14 21:04:17 +020053 */
Timothy Warren48a7fbb2012-04-23 11:58:16 -040054 public $hooks = array();
Andrey Andreev92ebfb62012-05-17 12:49:24 +030055
David Behler9b5df592011-08-14 21:04:17 +020056 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030057 * In progress flag
58 *
Alex Bilbief512b732012-06-16 11:15:19 +010059 * Determines whether hook is in progress, used to prevent infinte loops
David Behler9b5df592011-08-14 21:04:17 +020060 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030061 * @var bool
David Behler9b5df592011-08-14 21:04:17 +020062 */
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030063 protected $_in_progress = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000064
Derek Allard2067d1a2008-11-13 22:59:24 +000065 /**
Andrey Andreev3e9d2b82012-10-27 14:28:51 +030066 * Class constructor
Derek Allard2067d1a2008-11-13 22:59:24 +000067 *
Derek Allard2067d1a2008-11-13 22:59:24 +000068 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020069 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +030070 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +020071 {
Derek Jonesc64ca012010-03-07 07:55:56 -060072 $CFG =& load_class('Config', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +000073
Andrey Andreeva5dd2972012-03-26 14:43:01 +030074 log_message('debug', 'Hooks Class Initialized');
75
Derek Allard2067d1a2008-11-13 22:59:24 +000076 // If hooks are not enabled in the config file
77 // there is nothing else to do
Alex Bilbieed944a32012-06-02 11:07:47 +010078 if ($CFG->item('enable_hooks') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +000079 {
80 return;
81 }
82
83 // Grab the "hooks" definition file.
Andrey Andreeva5dd2972012-03-26 14:43:01 +030084 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'))
Greg Akerd96f8822011-12-27 16:23:47 -060085 {
86 include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php');
87 }
88 elseif (is_file(APPPATH.'config/hooks.php'))
89 {
90 include(APPPATH.'config/hooks.php');
91 }
92
Andrey Andreevf9938a22012-01-07 22:10:47 +020093 // If there are no hooks, we're done.
Derek Allard2067d1a2008-11-13 22:59:24 +000094 if ( ! isset($hook) OR ! is_array($hook))
95 {
96 return;
97 }
98
99 $this->hooks =& $hook;
100 $this->enabled = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200101 }
102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 // --------------------------------------------------------------------
104
105 /**
106 * Call Hook
107 *
Andrey Andreevf9938a22012-01-07 22:10:47 +0200108 * Calls a particular hook. Called by CodeIgniter.php.
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300110 * @uses CI_Hooks::_run_hook()
111 *
112 * @param string $which Hook name
113 * @return bool TRUE on success or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300115 public function call_hook($which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
117 if ( ! $this->enabled OR ! isset($this->hooks[$which]))
118 {
119 return FALSE;
120 }
121
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300122 if (isset($this->hooks[$which][0]) && is_array($this->hooks[$which][0]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 {
124 foreach ($this->hooks[$which] as $val)
125 {
126 $this->_run_hook($val);
127 }
128 }
129 else
130 {
131 $this->_run_hook($this->hooks[$which]);
132 }
133
134 return TRUE;
135 }
136
137 // --------------------------------------------------------------------
138
139 /**
140 * Run Hook
141 *
142 * Runs a particular hook
143 *
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300144 * @param array $data Hook details
145 * @return bool TRUE on success or FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000146 */
Andrey Andreevf9938a22012-01-07 22:10:47 +0200147 protected function _run_hook($data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 {
149 if ( ! is_array($data))
150 {
151 return FALSE;
152 }
153
154 // -----------------------------------
155 // Safety - Prevents run-away loops
156 // -----------------------------------
157
158 // If the script being called happens to have the same
159 // hook call within it a loop can happen
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300160 if ($this->_in_progress === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 {
162 return;
163 }
164
165 // -----------------------------------
166 // Set file path
167 // -----------------------------------
168
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300169 if ( ! isset($data['filepath'], $data['filename']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 return FALSE;
172 }
173
174 $filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
175
176 if ( ! file_exists($filepath))
177 {
178 return FALSE;
179 }
180
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300181 // Determine and class and/or function names
182 $class = empty($data['class']) ? FALSE : $data['class'];
183 $function = empty($data['function']) ? FALSE : $data['function'];
184 $params = isset($data['params']) ? $data['params'] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000185
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300186 if ($class === FALSE && $function === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 {
188 return FALSE;
189 }
190
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300191 // Set the _in_progress flag
192 $this->_in_progress = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 // Call the requested class and/or function
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 if ($class !== FALSE)
196 {
197 if ( ! class_exists($class))
198 {
199 require($filepath);
200 }
201
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300202 $HOOK = new $class();
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 $HOOK->$function($params);
204 }
205 else
206 {
207 if ( ! function_exists($function))
208 {
209 require($filepath);
210 }
211
212 $function($params);
213 }
214
Andrey Andreev3e9d2b82012-10-27 14:28:51 +0300215 $this->_in_progress = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 return TRUE;
217 }
218
219}
220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221/* End of file Hooks.php */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300222/* Location: ./system/core/Hooks.php */