blob: 8767e10d09826ed78466d8e9b8e4aef60bf92314 [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");
admine95cd262006-10-02 23:17:20 +000043 $this->_initialize();
44 }
45
46 // --------------------------------------------------------------------
47
48 /**
49 * Initialize the Hooks Preferences
50 *
51 * @access private
52 * @return void
53 */
54 function _initialize()
55 {
admin33de9a12006-09-28 06:50:16 +000056 $CFG =& _load_class('Config');
adminb0dd10f2006-08-25 17:25:49 +000057
58 // If hooks are not enabled in the config file
59 // there is nothing else to do
60
61 if ($CFG->item('enable_hooks') == FALSE)
62 {
63 return;
64 }
65
66 // Grab the "hooks" definition file.
67 // If there are no hooks, we're done.
68
69 @include(APPPATH.'config/hooks'.EXT);
70
71 if ( ! isset($hook) OR ! is_array($hook))
72 {
73 return;
74 }
75
76 $this->hooks =& $hook;
77 $this->enabled = TRUE;
admine95cd262006-10-02 23:17:20 +000078 }
adminb0dd10f2006-08-25 17:25:49 +000079
80 // --------------------------------------------------------------------
81
82 /**
adminb0dd10f2006-08-25 17:25:49 +000083 * Call Hook
84 *
85 * Calls a particular hook
86 *
87 * @access private
88 * @param string the hook name
89 * @return mixed
90 */
91 function _call_hook($which = '')
92 {
admin764672b2006-09-15 20:02:05 +000093 if ( ! $this->enabled OR ! isset($this->hooks[$which]))
94 {
95 return FALSE;
96 }
97
adminb0dd10f2006-08-25 17:25:49 +000098 if (isset($this->hooks[$which][0]) AND is_array($this->hooks[$which][0]))
99 {
100 foreach ($this->hooks[$which] as $val)
101 {
102 $this->_run_hook($val);
103 }
104 }
105 else
106 {
107 $this->_run_hook($this->hooks[$which]);
108 }
admin764672b2006-09-15 20:02:05 +0000109
110 return TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000111 }
112 // END hook_exists()
113
114 // --------------------------------------------------------------------
115
116 /**
117 * Run Hook
118 *
119 * Runs a particular hook
120 *
121 * @access private
122 * @param array the hook details
123 * @return bool
124 */
125 function _run_hook($data)
126 {
127 if ( ! is_array($data))
128 {
129 return FALSE;
130 }
131
132 // -----------------------------------
133 // Safety - Prevents run-away loops
134 // -----------------------------------
135
136 // If the script being called happens to have the same
admin764672b2006-09-15 20:02:05 +0000137 // hook call within it a loop can happen
adminb0dd10f2006-08-25 17:25:49 +0000138
139 if ($this->in_progress == TRUE)
140 {
141 return;
142 }
143
144 // -----------------------------------
145 // Set file path
146 // -----------------------------------
147
148 if ( ! isset($data['filepath']) OR ! isset($data['filename']))
149 {
150 return FALSE;
151 }
152
153 $filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
154
155 if ( ! file_exists($filepath))
156 {
157 return FALSE;
158 }
159
160 // -----------------------------------
161 // Set class/function name
162 // -----------------------------------
163
164 $class = FALSE;
165 $function = FALSE;
166 $params = '';
167
168 if (isset($data['class']) AND $data['class'] != '')
169 {
170 $class = $data['class'];
171 }
172
173 if (isset($data['function']))
174 {
175 $function = $data['function'];
176 }
177
178 if (isset($data['params']))
179 {
180 $params = $data['params'];
181 }
182
183 if ($class === FALSE AND $function === FALSE)
184 {
185 return FALSE;
186 }
187
188 // -----------------------------------
189 // Set the in_progress flag
190 // -----------------------------------
191
192 $this->in_progress = TRUE;
193
194 // -----------------------------------
195 // Call the requested class and/or function
196 // -----------------------------------
197
198 if ($class !== FALSE)
199 {
200 if ( ! class_exists($class))
201 {
202 require($filepath);
203 }
204
205 $HOOK = new $class;
206 $HOOK->$function($params);
207 }
208 else
209 {
210 if ( ! function_exists($function))
211 {
212 require($filepath);
213 }
214
215 $function($params);
216 }
217
218 $this->in_progress = FALSE;
219 return TRUE;
220 }
221 // END _run_hook()
222
223
224}
225
226// END CI_Hooks class
227?>