blob: 46bfec02a974938b170a3551ff6fa1df06049799 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * 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 Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @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 Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Hooks Class
32 *
Derek Jones6f4d17f2010-03-02 13:34:23 -060033 * Provides a mechanism to extend the base system without hacking.
Derek Allard2067d1a2008-11-13 22:59:24 +000034 *
35 * @package CodeIgniter
36 * @subpackage Libraries
37 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/libraries/encryption.html
40 */
41class CI_Hooks {
42
David Behler9b5df592011-08-14 21:04:17 +020043 /**
44 * Determines wether hooks are enabled
45 *
46 * @var bool
47 */
Barry Mienydd671972010-10-04 16:33:58 +020048 var $enabled = FALSE;
David Behler9b5df592011-08-14 21:04:17 +020049 /**
50 * List of all hooks set in config/hooks.php
51 *
52 * @var array
53 */
Barry Mienydd671972010-10-04 16:33:58 +020054 var $hooks = array();
David Behler9b5df592011-08-14 21:04:17 +020055 /**
56 * Determines wether hook is in progress, used to prevent infinte loops
57 *
58 * @var bool
59 */
Derek Allard2067d1a2008-11-13 22:59:24 +000060 var $in_progress = FALSE;
61
62 /**
63 * Constructor
64 *
65 */
Greg Akera9263282010-11-10 15:26:43 -060066 function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000067 {
68 $this->_initialize();
69 log_message('debug', "Hooks Class Initialized");
70 }
Barry Mienydd671972010-10-04 16:33:58 +020071
Derek Allard2067d1a2008-11-13 22:59:24 +000072 // --------------------------------------------------------------------
73
74 /**
75 * Initialize the Hooks Preferences
76 *
77 * @access private
78 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020079 */
80 function _initialize()
81 {
Derek Jonesc64ca012010-03-07 07:55:56 -060082 $CFG =& load_class('Config', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +000083
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 Aker3a746652011-04-19 10:59:47 -050095 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'))
bubbafoley0ea04142011-03-17 14:55:41 -050096 {
Derek Jones37f4b9c2011-07-01 17:56:50 -050097 include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php');
bubbafoley0ea04142011-03-17 14:55:41 -050098 }
Greg Aker3a746652011-04-19 10:59:47 -050099 elseif (is_file(APPPATH.'config/hooks.php'))
bubbafoley0ea04142011-03-17 14:55:41 -0500100 {
Greg Aker3a746652011-04-19 10:59:47 -0500101 include(APPPATH.'config/hooks.php');
bubbafoley0ea04142011-03-17 14:55:41 -0500102 }
Eric Barnes92808342011-03-18 09:02:37 -0400103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104
105 if ( ! isset($hook) OR ! is_array($hook))
106 {
107 return;
108 }
109
110 $this->hooks =& $hook;
111 $this->enabled = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200112 }
113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 // --------------------------------------------------------------------
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 Jonesc68dfbf2010-03-02 12:59:23 -0600260/* Location: ./system/core/Hooks.php */