blob: 7ff0592ff9e7d31542e5095155fc78cf5e5b3dcf [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 /**
adminb0dd10f2006-08-25 17:25:49 +000072 * Call Hook
73 *
74 * Calls a particular hook
75 *
76 * @access private
77 * @param string the hook name
78 * @return mixed
79 */
80 function _call_hook($which = '')
81 {
admin764672b2006-09-15 20:02:05 +000082 if ( ! $this->enabled OR ! isset($this->hooks[$which]))
83 {
84 return FALSE;
85 }
86
adminb0dd10f2006-08-25 17:25:49 +000087 if (isset($this->hooks[$which][0]) AND is_array($this->hooks[$which][0]))
88 {
89 foreach ($this->hooks[$which] as $val)
90 {
91 $this->_run_hook($val);
92 }
93 }
94 else
95 {
96 $this->_run_hook($this->hooks[$which]);
97 }
admin764672b2006-09-15 20:02:05 +000098
99 return TRUE;
adminb0dd10f2006-08-25 17:25:49 +0000100 }
101 // END hook_exists()
102
103 // --------------------------------------------------------------------
104
105 /**
106 * Run Hook
107 *
108 * Runs a particular hook
109 *
110 * @access private
111 * @param array the hook details
112 * @return bool
113 */
114 function _run_hook($data)
115 {
116 if ( ! is_array($data))
117 {
118 return FALSE;
119 }
120
121 // -----------------------------------
122 // Safety - Prevents run-away loops
123 // -----------------------------------
124
125 // If the script being called happens to have the same
admin764672b2006-09-15 20:02:05 +0000126 // hook call within it a loop can happen
adminb0dd10f2006-08-25 17:25:49 +0000127
128 if ($this->in_progress == TRUE)
129 {
130 return;
131 }
132
133 // -----------------------------------
134 // Set file path
135 // -----------------------------------
136
137 if ( ! isset($data['filepath']) OR ! isset($data['filename']))
138 {
139 return FALSE;
140 }
141
142 $filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
143
144 if ( ! file_exists($filepath))
145 {
146 return FALSE;
147 }
148
149 // -----------------------------------
150 // Set class/function name
151 // -----------------------------------
152
153 $class = FALSE;
154 $function = FALSE;
155 $params = '';
156
157 if (isset($data['class']) AND $data['class'] != '')
158 {
159 $class = $data['class'];
160 }
161
162 if (isset($data['function']))
163 {
164 $function = $data['function'];
165 }
166
167 if (isset($data['params']))
168 {
169 $params = $data['params'];
170 }
171
172 if ($class === FALSE AND $function === FALSE)
173 {
174 return FALSE;
175 }
176
177 // -----------------------------------
178 // Set the in_progress flag
179 // -----------------------------------
180
181 $this->in_progress = TRUE;
182
183 // -----------------------------------
184 // Call the requested class and/or function
185 // -----------------------------------
186
187 if ($class !== FALSE)
188 {
189 if ( ! class_exists($class))
190 {
191 require($filepath);
192 }
193
194 $HOOK = new $class;
195 $HOOK->$function($params);
196 }
197 else
198 {
199 if ( ! function_exists($function))
200 {
201 require($filepath);
202 }
203
204 $function($params);
205 }
206
207 $this->in_progress = FALSE;
208 return TRUE;
209 }
210 // END _run_hook()
211
212
213}
214
215// END CI_Hooks class
216?>