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