blob: 6a36ce9637eda4f8ef0ead872b5461ba6994108e [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.
Greg Aker5c1aa632011-12-25 01:24:29 -060094 load_environ_config('hooks');
Derek Allard2067d1a2008-11-13 22:59:24 +000095
96 if ( ! isset($hook) OR ! is_array($hook))
97 {
98 return;
99 }
100
101 $this->hooks =& $hook;
102 $this->enabled = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200103 }
104
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 // --------------------------------------------------------------------
106
107 /**
108 * Call Hook
109 *
110 * Calls a particular hook
111 *
112 * @access private
113 * @param string the hook name
114 * @return mixed
115 */
116 function _call_hook($which = '')
117 {
118 if ( ! $this->enabled OR ! isset($this->hooks[$which]))
119 {
120 return FALSE;
121 }
122
123 if (isset($this->hooks[$which][0]) AND is_array($this->hooks[$which][0]))
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 *
145 * @access private
146 * @param array the hook details
147 * @return bool
148 */
149 function _run_hook($data)
150 {
151 if ( ! is_array($data))
152 {
153 return FALSE;
154 }
155
156 // -----------------------------------
157 // Safety - Prevents run-away loops
158 // -----------------------------------
159
160 // If the script being called happens to have the same
161 // hook call within it a loop can happen
162
163 if ($this->in_progress == TRUE)
164 {
165 return;
166 }
167
168 // -----------------------------------
169 // Set file path
170 // -----------------------------------
171
172 if ( ! isset($data['filepath']) OR ! isset($data['filename']))
173 {
174 return FALSE;
175 }
176
177 $filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
178
179 if ( ! file_exists($filepath))
180 {
181 return FALSE;
182 }
183
184 // -----------------------------------
185 // Set class/function name
186 // -----------------------------------
187
188 $class = FALSE;
189 $function = FALSE;
190 $params = '';
191
192 if (isset($data['class']) AND $data['class'] != '')
193 {
194 $class = $data['class'];
195 }
196
197 if (isset($data['function']))
198 {
199 $function = $data['function'];
200 }
201
202 if (isset($data['params']))
203 {
204 $params = $data['params'];
205 }
206
207 if ($class === FALSE AND $function === FALSE)
208 {
209 return FALSE;
210 }
211
212 // -----------------------------------
213 // Set the in_progress flag
214 // -----------------------------------
215
216 $this->in_progress = TRUE;
217
218 // -----------------------------------
219 // Call the requested class and/or function
220 // -----------------------------------
221
222 if ($class !== FALSE)
223 {
224 if ( ! class_exists($class))
225 {
226 require($filepath);
227 }
228
229 $HOOK = new $class;
230 $HOOK->$function($params);
231 }
232 else
233 {
234 if ( ! function_exists($function))
235 {
236 require($filepath);
237 }
238
239 $function($params);
240 }
241
242 $this->in_progress = FALSE;
243 return TRUE;
244 }
245
246}
247
248// END CI_Hooks class
249
250/* End of file Hooks.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600251/* Location: ./system/core/Hooks.php */