blob: 389e7ac149a1636afb1f0dcc2ddfe7f5f9da005e [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
10 * @license http://www.codeignitor.com/user_guide/license.html
11 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Code Igniter Hooks Class
20 *
21 * Provides a mechanism to extend the base system without hacking. Most of
22 * this class is borrowed from Paul's Extension class in ExpressionEngine.
23 *
24 * @package CodeIgniter
25 * @subpackage Libraries
26 * @category Libraries
27 * @author Rick Ellis
28 * @link http://www.codeigniter.com/user_guide/libraries/encryption.html
29 */
30class CI_Hooks {
31
32 var $enabled = FALSE;
33 var $hooks = array();
34 var $in_progress = FALSE;
35
36 /**
37 * Constructor
38 *
39 */
40 function CI_Hooks()
41 {
42 log_message('debug', "Hooks Class Initialized");
43
44 $CFG =& _load_class('CI_Config');
45
46 // If hooks are not enabled in the config file
47 // there is nothing else to do
48
49 if ($CFG->item('enable_hooks') == FALSE)
50 {
51 return;
52 }
53
54 // Grab the "hooks" definition file.
55 // If there are no hooks, we're done.
56
57 @include(APPPATH.'config/hooks'.EXT);
58
59 if ( ! isset($hook) OR ! is_array($hook))
60 {
61 return;
62 }
63
64 $this->hooks =& $hook;
65 $this->enabled = TRUE;
66 }
67 // END CI_Hooks()
68
69 // --------------------------------------------------------------------
70
71 /**
72 * Does a given hook exist?
73 *
74 * Returns TRUE/FALSE based on whether a given hook exists
75 *
76 * @access private
77 * @param string
78 * @return bool
79 */
80 function _hook_exists($which = '')
81 {
82 if ( ! $this->enabled)
83 {
84 return FALSE;
85 }
86
87 if ( ! isset($this->hooks[$which]))
88 {
89 return FALSE;
90 }
91
92 return TRUE;
93 }
94 // END hook_exists()
95
96
97 // --------------------------------------------------------------------
98
99 /**
100 * Call Hook
101 *
102 * Calls a particular hook
103 *
104 * @access private
105 * @param string the hook name
106 * @return mixed
107 */
108 function _call_hook($which = '')
109 {
110 if (isset($this->hooks[$which][0]) AND is_array($this->hooks[$which][0]))
111 {
112 foreach ($this->hooks[$which] as $val)
113 {
114 $this->_run_hook($val);
115 }
116 }
117 else
118 {
119 $this->_run_hook($this->hooks[$which]);
120 }
121 }
122 // END hook_exists()
123
124 // --------------------------------------------------------------------
125
126 /**
127 * Run Hook
128 *
129 * Runs a particular hook
130 *
131 * @access private
132 * @param array the hook details
133 * @return bool
134 */
135 function _run_hook($data)
136 {
137 if ( ! is_array($data))
138 {
139 return FALSE;
140 }
141
142 // -----------------------------------
143 // Safety - Prevents run-away loops
144 // -----------------------------------
145
146 // If the script being called happens to have the same
147 // extension call within it a loop can happen
148
149 if ($this->in_progress == TRUE)
150 {
151 return;
152 }
153
154 // -----------------------------------
155 // Set file path
156 // -----------------------------------
157
158 if ( ! isset($data['filepath']) OR ! isset($data['filename']))
159 {
160 return FALSE;
161 }
162
163 $filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
164
165 if ( ! file_exists($filepath))
166 {
167 return FALSE;
168 }
169
170 // -----------------------------------
171 // Set class/function name
172 // -----------------------------------
173
174 $class = FALSE;
175 $function = FALSE;
176 $params = '';
177
178 if (isset($data['class']) AND $data['class'] != '')
179 {
180 $class = $data['class'];
181 }
182
183 if (isset($data['function']))
184 {
185 $function = $data['function'];
186 }
187
188 if (isset($data['params']))
189 {
190 $params = $data['params'];
191 }
192
193 if ($class === FALSE AND $function === FALSE)
194 {
195 return FALSE;
196 }
197
198 // -----------------------------------
199 // Set the in_progress flag
200 // -----------------------------------
201
202 $this->in_progress = TRUE;
203
204 // -----------------------------------
205 // Call the requested class and/or function
206 // -----------------------------------
207
208 if ($class !== FALSE)
209 {
210 if ( ! class_exists($class))
211 {
212 require($filepath);
213 }
214
215 $HOOK = new $class;
216 $HOOK->$function($params);
217 }
218 else
219 {
220 if ( ! function_exists($function))
221 {
222 require($filepath);
223 }
224
225 $function($params);
226 }
227
228 $this->in_progress = FALSE;
229 return TRUE;
230 }
231 // END _run_hook()
232
233
234}
235
236// END CI_Hooks class
237?>