blob: 5e017ccb70799eba7406aa6c1fba299a9ca7ac6c [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 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, 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
31 var $enabled = FALSE;
32 var $hooks = array();
33 var $in_progress = FALSE;
34
35 /**
36 * Constructor
37 *
38 */
39 function CI_Hooks()
40 {
41 $this->_initialize();
42 log_message('debug', "Hooks Class Initialized");
43 }
44
45 // --------------------------------------------------------------------
46
47 /**
48 * Initialize the Hooks Preferences
49 *
50 * @access private
51 * @return void
52 */
53 function _initialize()
54 {
55 $CFG =& load_class('Config');
56
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
68 @include(APPPATH.'config/hooks'.EXT);
69
70 if ( ! isset($hook) OR ! is_array($hook))
71 {
72 return;
73 }
74
75 $this->hooks =& $hook;
76 $this->enabled = TRUE;
77 }
78
79 // --------------------------------------------------------------------
80
81 /**
82 * Call Hook
83 *
84 * Calls a particular hook
85 *
86 * @access private
87 * @param string the hook name
88 * @return mixed
89 */
90 function _call_hook($which = '')
91 {
92 if ( ! $this->enabled OR ! isset($this->hooks[$which]))
93 {
94 return FALSE;
95 }
96
97 if (isset($this->hooks[$which][0]) AND is_array($this->hooks[$which][0]))
98 {
99 foreach ($this->hooks[$which] as $val)
100 {
101 $this->_run_hook($val);
102 }
103 }
104 else
105 {
106 $this->_run_hook($this->hooks[$which]);
107 }
108
109 return TRUE;
110 }
111
112 // --------------------------------------------------------------------
113
114 /**
115 * Run Hook
116 *
117 * Runs a particular hook
118 *
119 * @access private
120 * @param array the hook details
121 * @return bool
122 */
123 function _run_hook($data)
124 {
125 if ( ! is_array($data))
126 {
127 return FALSE;
128 }
129
130 // -----------------------------------
131 // Safety - Prevents run-away loops
132 // -----------------------------------
133
134 // If the script being called happens to have the same
135 // hook call within it a loop can happen
136
137 if ($this->in_progress == TRUE)
138 {
139 return;
140 }
141
142 // -----------------------------------
143 // Set file path
144 // -----------------------------------
145
146 if ( ! isset($data['filepath']) OR ! isset($data['filename']))
147 {
148 return FALSE;
149 }
150
151 $filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
152
153 if ( ! file_exists($filepath))
154 {
155 return FALSE;
156 }
157
158 // -----------------------------------
159 // Set class/function name
160 // -----------------------------------
161
162 $class = FALSE;
163 $function = FALSE;
164 $params = '';
165
166 if (isset($data['class']) AND $data['class'] != '')
167 {
168 $class = $data['class'];
169 }
170
171 if (isset($data['function']))
172 {
173 $function = $data['function'];
174 }
175
176 if (isset($data['params']))
177 {
178 $params = $data['params'];
179 }
180
181 if ($class === FALSE AND $function === FALSE)
182 {
183 return FALSE;
184 }
185
186 // -----------------------------------
187 // Set the in_progress flag
188 // -----------------------------------
189
190 $this->in_progress = TRUE;
191
192 // -----------------------------------
193 // Call the requested class and/or function
194 // -----------------------------------
195
196 if ($class !== FALSE)
197 {
198 if ( ! class_exists($class))
199 {
200 require($filepath);
201 }
202
203 $HOOK = new $class;
204 $HOOK->$function($params);
205 }
206 else
207 {
208 if ( ! function_exists($function))
209 {
210 require($filepath);
211 }
212
213 $function($params);
214 }
215
216 $this->in_progress = FALSE;
217 return TRUE;
218 }
219
220}
221
222// END CI_Hooks class
223
224/* End of file Hooks.php */
Derek Jonesc68dfbf2010-03-02 12:59:23 -0600225/* Location: ./system/core/Hooks.php */