24fa1055b88250914776e0764486e681d2aadf32
[living-lab-site.git] / system / core / Hooks.php
1 <?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2 /**
3  * CodeIgniter
4  *
5  * An open source application development framework for PHP 5.1.6 or newer
6  *
7  * @package             CodeIgniter
8  * @author              ExpressionEngine Dev Team
9  * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
10  * @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  *
21  * Provides a mechanism to extend the base system without hacking.
22  *
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  */
29 class CI_Hooks {
30
31         var $enabled            = FALSE;
32         var $hooks                      = array();
33         var $in_progress        = FALSE;
34
35         /**
36          * Constructor
37          *
38          */
39         function __construct()
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', 'core');
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                 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/hooks'.EXT))
69                 {
70                     include(APPPATH.'config/'.ENVIRONMENT.'/hooks'.EXT);
71                 }
72                 elseif (is_file(APPPATH.'config/hooks'.EXT))
73                 {
74                         include(APPPATH.'config/hooks'.EXT);
75                 }
76
77
78                 if ( ! isset($hook) OR ! is_array($hook))
79                 {
80                         return;
81                 }
82
83                 $this->hooks =& $hook;
84                 $this->enabled = TRUE;
85         }
86
87         // --------------------------------------------------------------------
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 */
233 /* Location: ./system/core/Hooks.php */