blob: fd6380f0a3550f4ddc52a483d4bf6b42c8fcd4ad [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Greg Aker0711dc82011-01-05 10:49:40 -06009 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * CodeIgniter Hooks Class
20 *
Derek Jones6f4d17f2010-03-02 13:34:23 -060021 * Provides a mechanism to extend the base system without hacking.
Derek Allard2067d1a2008-11-13 22:59:24 +000022 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category Libraries
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/encryption.html
28 */
29class CI_Hooks {
30
Barry Mienydd671972010-10-04 16:33:58 +020031 var $enabled = FALSE;
32 var $hooks = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000033 var $in_progress = FALSE;
34
35 /**
36 * Constructor
37 *
38 */
Greg Akera9263282010-11-10 15:26:43 -060039 function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000040 {
41 $this->_initialize();
42 log_message('debug', "Hooks Class Initialized");
43 }
Barry Mienydd671972010-10-04 16:33:58 +020044
Derek Allard2067d1a2008-11-13 22:59:24 +000045 // --------------------------------------------------------------------
46
47 /**
48 * Initialize the Hooks Preferences
49 *
50 * @access private
51 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020052 */
53 function _initialize()
54 {
Derek Jonesc64ca012010-03-07 07:55:56 -060055 $CFG =& load_class('Config', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +000056
57 // If hooks are not enabled in the config file
58 // there is nothing else to do
59
60 if ($CFG->item('enable_hooks') == FALSE)
61 {
62 return;
63 }
64
65 // Grab the "hooks" definition file.
66 // If there are no hooks, we're done.
67
Greg Aker3a746652011-04-19 10:59:47 -050068 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'))
bubbafoley0ea04142011-03-17 14:55:41 -050069 {
Greg Aker3a746652011-04-19 10:59:47 -050070 include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php');
bubbafoley0ea04142011-03-17 14:55:41 -050071 }
Greg Aker3a746652011-04-19 10:59:47 -050072 elseif (is_file(APPPATH.'config/hooks.php'))
bubbafoley0ea04142011-03-17 14:55:41 -050073 {
Greg Aker3a746652011-04-19 10:59:47 -050074 include(APPPATH.'config/hooks.php');
bubbafoley0ea04142011-03-17 14:55:41 -050075 }
Eric Barnes92808342011-03-18 09:02:37 -040076
Derek Allard2067d1a2008-11-13 22:59:24 +000077
78 if ( ! isset($hook) OR ! is_array($hook))
79 {
80 return;
81 }
82
83 $this->hooks =& $hook;
84 $this->enabled = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +020085 }
86
Derek Allard2067d1a2008-11-13 22:59:24 +000087 // --------------------------------------------------------------------
88
89 /**
90 * Call Hook
91 *
92 * Calls a particular hook
93 *
94 * @access private
95 * @param string the hook name
96 * @return mixed
97 */
98 function _call_hook($which = '')
99 {
100 if ( ! $this->enabled OR ! isset($this->hooks[$which]))
101 {
102 return FALSE;
103 }
104
105 if (isset($this->hooks[$which][0]) AND is_array($this->hooks[$which][0]))
106 {
107 foreach ($this->hooks[$which] as $val)
108 {
109 $this->_run_hook($val);
110 }
111 }
112 else
113 {
114 $this->_run_hook($this->hooks[$which]);
115 }
116
117 return TRUE;
118 }
119
120 // --------------------------------------------------------------------
121
122 /**
123 * Run Hook
124 *
125 * Runs a particular hook
126 *
127 * @access private
128 * @param array the hook details
129 * @return bool
130 */
131 function _run_hook($data)
132 {
133 if ( ! is_array($data))
134 {
135 return FALSE;
136 }
137
138 // -----------------------------------
139 // Safety - Prevents run-away loops
140 // -----------------------------------
141
142 // If the script being called happens to have the same
143 // hook call within it a loop can happen
144
145 if ($this->in_progress == TRUE)
146 {
147 return;
148 }
149
150 // -----------------------------------
151 // Set file path
152 // -----------------------------------
153
154 if ( ! isset($data['filepath']) OR ! isset($data['filename']))
155 {
156 return FALSE;
157 }
158
159 $filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
160
161 if ( ! file_exists($filepath))
162 {
163 return FALSE;
164 }
165
166 // -----------------------------------
167 // Set class/function name
168 // -----------------------------------
169
170 $class = FALSE;
171 $function = FALSE;
172 $params = '';
173
174 if (isset($data['class']) AND $data['class'] != '')
175 {
176 $class = $data['class'];
177 }
178
179 if (isset($data['function']))
180 {
181 $function = $data['function'];
182 }
183
184 if (isset($data['params']))
185 {
186 $params = $data['params'];
187 }
188
189 if ($class === FALSE AND $function === FALSE)
190 {
191 return FALSE;
192 }
193
194 // -----------------------------------
195 // Set the in_progress flag
196 // -----------------------------------
197
198 $this->in_progress = TRUE;
199
200 // -----------------------------------
201 // Call the requested class and/or function
202 // -----------------------------------
203
204 if ($class !== FALSE)
205 {
206 if ( ! class_exists($class))
207 {
208 require($filepath);
209 }
210
211 $HOOK = new $class;
212 $HOOK->$function($params);
213 }
214 else
215 {
216 if ( ! function_exists($function))
217 {
218 require($filepath);
219 }
220
221 $function($params);
222 }
223
224 $this->in_progress = FALSE;
225 return TRUE;
226 }
227
228}
229
230// END CI_Hooks class
231
232/* End of file Hooks.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600233/* Location: ./system/core/Hooks.php */