blob: e1ac58e6e2ab04ed76c991b74cf4b278ef237bae [file] [log] [blame]
Andrey Andreevf9938a22012-01-07 22:10:47 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreevf9938a22012-01-07 22:10:47 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreevf9938a22012-01-07 22:10:47 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * CodeIgniter Hooks Class
32 *
Derek Jones6f4d17f2010-03-02 13:34:23 -060033 * Provides a mechanism to extend the base system without hacking.
Derek Allard2067d1a2008-11-13 22:59:24 +000034 *
35 * @package CodeIgniter
36 * @subpackage Libraries
37 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/libraries/encryption.html
40 */
41class CI_Hooks {
42
David Behler9b5df592011-08-14 21:04:17 +020043 /**
44 * Determines wether hooks are enabled
45 *
46 * @var bool
47 */
Andrey Andreevf9938a22012-01-07 22:10:47 +020048 public $enabled = FALSE;
David Behler9b5df592011-08-14 21:04:17 +020049 /**
50 * List of all hooks set in config/hooks.php
51 *
52 * @var array
53 */
Andrey Andreevf9938a22012-01-07 22:10:47 +020054 public $hooks = array();
David Behler9b5df592011-08-14 21:04:17 +020055 /**
56 * Determines wether hook is in progress, used to prevent infinte loops
57 *
58 * @var bool
59 */
Andrey Andreevf9938a22012-01-07 22:10:47 +020060 public $in_progress = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000061
Andrey Andreevf9938a22012-01-07 22:10:47 +020062 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000063 {
64 $this->_initialize();
Andrey Andreevf9938a22012-01-07 22:10:47 +020065 log_message('debug', 'Hooks Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000066 }
Barry Mienydd671972010-10-04 16:33:58 +020067
Derek Allard2067d1a2008-11-13 22:59:24 +000068 // --------------------------------------------------------------------
69
70 /**
71 * Initialize the Hooks Preferences
72 *
Derek Allard2067d1a2008-11-13 22:59:24 +000073 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020074 */
Andrey Andreevf9938a22012-01-07 22:10:47 +020075 private function _initialize()
Barry Mienydd671972010-10-04 16:33:58 +020076 {
Derek Jonesc64ca012010-03-07 07:55:56 -060077 $CFG =& load_class('Config', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +000078
79 // If hooks are not enabled in the config file
80 // there is nothing else to do
Derek Allard2067d1a2008-11-13 22:59:24 +000081 if ($CFG->item('enable_hooks') == FALSE)
82 {
83 return;
84 }
85
86 // Grab the "hooks" definition file.
Greg Akerd96f8822011-12-27 16:23:47 -060087 if (defined('ENVIRONMENT') AND is_file(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'))
88 {
89 include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php');
90 }
91 elseif (is_file(APPPATH.'config/hooks.php'))
92 {
93 include(APPPATH.'config/hooks.php');
94 }
95
Andrey Andreevf9938a22012-01-07 22:10:47 +020096 // If there are no hooks, we're done.
Derek Allard2067d1a2008-11-13 22:59:24 +000097 if ( ! isset($hook) OR ! is_array($hook))
98 {
99 return;
100 }
101
102 $this->hooks =& $hook;
103 $this->enabled = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200104 }
105
Derek Allard2067d1a2008-11-13 22:59:24 +0000106 // --------------------------------------------------------------------
107
108 /**
109 * Call Hook
110 *
Andrey Andreevf9938a22012-01-07 22:10:47 +0200111 * Calls a particular hook. Called by CodeIgniter.php.
Derek Allard2067d1a2008-11-13 22:59:24 +0000112 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000113 * @param string the hook name
114 * @return mixed
115 */
Andrey Andreevf9938a22012-01-07 22:10:47 +0200116 public function _call_hook($which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 {
118 if ( ! $this->enabled OR ! isset($this->hooks[$which]))
119 {
120 return FALSE;
121 }
122
123 if (isset($this->hooks[$which][0]) AND is_array($this->hooks[$which][0]))
124 {
125 foreach ($this->hooks[$which] as $val)
126 {
127 $this->_run_hook($val);
128 }
129 }
130 else
131 {
132 $this->_run_hook($this->hooks[$which]);
133 }
134
135 return TRUE;
136 }
137
138 // --------------------------------------------------------------------
139
140 /**
141 * Run Hook
142 *
143 * Runs a particular hook
144 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 * @param array the hook details
146 * @return bool
147 */
Andrey Andreevf9938a22012-01-07 22:10:47 +0200148 protected function _run_hook($data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
150 if ( ! is_array($data))
151 {
152 return FALSE;
153 }
154
155 // -----------------------------------
156 // Safety - Prevents run-away loops
157 // -----------------------------------
158
159 // If the script being called happens to have the same
160 // hook call within it a loop can happen
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 if ($this->in_progress == TRUE)
162 {
163 return;
164 }
165
166 // -----------------------------------
167 // Set file path
168 // -----------------------------------
169
170 if ( ! isset($data['filepath']) OR ! isset($data['filename']))
171 {
172 return FALSE;
173 }
174
175 $filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
176
177 if ( ! file_exists($filepath))
178 {
179 return FALSE;
180 }
181
182 // -----------------------------------
183 // Set class/function name
184 // -----------------------------------
185
186 $class = FALSE;
187 $function = FALSE;
188 $params = '';
189
190 if (isset($data['class']) AND $data['class'] != '')
191 {
192 $class = $data['class'];
193 }
194
195 if (isset($data['function']))
196 {
197 $function = $data['function'];
198 }
199
200 if (isset($data['params']))
201 {
202 $params = $data['params'];
203 }
204
205 if ($class === FALSE AND $function === FALSE)
206 {
207 return FALSE;
208 }
209
210 // -----------------------------------
211 // Set the in_progress flag
212 // -----------------------------------
213
214 $this->in_progress = TRUE;
215
216 // -----------------------------------
217 // Call the requested class and/or function
218 // -----------------------------------
219
220 if ($class !== FALSE)
221 {
222 if ( ! class_exists($class))
223 {
224 require($filepath);
225 }
226
227 $HOOK = new $class;
228 $HOOK->$function($params);
229 }
230 else
231 {
232 if ( ! function_exists($function))
233 {
234 require($filepath);
235 }
236
237 $function($params);
238 }
239
240 $this->in_progress = FALSE;
241 return TRUE;
242 }
243
244}
245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246/* End of file Hooks.php */
Andrey Andreevf9938a22012-01-07 22:10:47 +0200247/* Location: ./system/core/Hooks.php */