blob: 68e30ef0f0614d313897000b61911c6d8afb04c9 [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 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 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
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * CodeIgniter Hooks Class
30 *
Derek Jones6f4d17f2010-03-02 13:34:23 -060031 * Provides a mechanism to extend the base system without hacking.
Derek Allard2067d1a2008-11-13 22:59:24 +000032 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Libraries
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/encryption.html
38 */
39class CI_Hooks {
40
David Behler9b5df592011-08-14 21:04:17 +020041 /**
42 * Determines wether hooks are enabled
43 *
44 * @var bool
45 */
Andrey Andreevf9938a22012-01-07 22:10:47 +020046 public $enabled = FALSE;
David Behler9b5df592011-08-14 21:04:17 +020047 /**
48 * List of all hooks set in config/hooks.php
49 *
50 * @var array
51 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +030052 public $hooks = array();
David Behler9b5df592011-08-14 21:04:17 +020053 /**
54 * Determines wether hook is in progress, used to prevent infinte loops
55 *
56 * @var bool
57 */
Andrey Andreevf9938a22012-01-07 22:10:47 +020058 public $in_progress = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +000059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 /**
61 * Initialize the Hooks Preferences
62 *
Derek Allard2067d1a2008-11-13 22:59:24 +000063 * @return void
Barry Mienydd671972010-10-04 16:33:58 +020064 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +030065 public function __construct()
Barry Mienydd671972010-10-04 16:33:58 +020066 {
Derek Jonesc64ca012010-03-07 07:55:56 -060067 $CFG =& load_class('Config', 'core');
Derek Allard2067d1a2008-11-13 22:59:24 +000068
Andrey Andreeva5dd2972012-03-26 14:43:01 +030069 log_message('debug', 'Hooks Class Initialized');
70
Derek Allard2067d1a2008-11-13 22:59:24 +000071 // If hooks are not enabled in the config file
72 // there is nothing else to do
Derek Allard2067d1a2008-11-13 22:59:24 +000073 if ($CFG->item('enable_hooks') == FALSE)
74 {
75 return;
76 }
77
78 // Grab the "hooks" definition file.
Andrey Andreeva5dd2972012-03-26 14:43:01 +030079 if (defined('ENVIRONMENT') && is_file(APPPATH.'config/'.ENVIRONMENT.'/hooks.php'))
Greg Akerd96f8822011-12-27 16:23:47 -060080 {
81 include(APPPATH.'config/'.ENVIRONMENT.'/hooks.php');
82 }
83 elseif (is_file(APPPATH.'config/hooks.php'))
84 {
85 include(APPPATH.'config/hooks.php');
86 }
87
Andrey Andreevf9938a22012-01-07 22:10:47 +020088 // If there are no hooks, we're done.
Derek Allard2067d1a2008-11-13 22:59:24 +000089 if ( ! isset($hook) OR ! is_array($hook))
90 {
91 return;
92 }
93
94 $this->hooks =& $hook;
95 $this->enabled = TRUE;
Barry Mienydd671972010-10-04 16:33:58 +020096 }
97
Derek Allard2067d1a2008-11-13 22:59:24 +000098 // --------------------------------------------------------------------
99
100 /**
101 * Call Hook
102 *
Andrey Andreevf9938a22012-01-07 22:10:47 +0200103 * Calls a particular hook. Called by CodeIgniter.php.
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 * @param string the hook name
106 * @return mixed
107 */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300108 public function call_hook($which = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 {
110 if ( ! $this->enabled OR ! isset($this->hooks[$which]))
111 {
112 return FALSE;
113 }
114
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300115 if (isset($this->hooks[$which][0]) && is_array($this->hooks[$which][0]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
117 foreach ($this->hooks[$which] as $val)
118 {
119 $this->_run_hook($val);
120 }
121 }
122 else
123 {
124 $this->_run_hook($this->hooks[$which]);
125 }
126
127 return TRUE;
128 }
129
130 // --------------------------------------------------------------------
131
132 /**
133 * Run Hook
134 *
135 * Runs a particular hook
136 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 * @param array the hook details
138 * @return bool
139 */
Andrey Andreevf9938a22012-01-07 22:10:47 +0200140 protected function _run_hook($data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
142 if ( ! is_array($data))
143 {
144 return FALSE;
145 }
146
147 // -----------------------------------
148 // Safety - Prevents run-away loops
149 // -----------------------------------
150
151 // If the script being called happens to have the same
152 // hook call within it a loop can happen
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 if ($this->in_progress == TRUE)
154 {
155 return;
156 }
157
158 // -----------------------------------
159 // Set file path
160 // -----------------------------------
161
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300162 if ( ! isset($data['filepath'], $data['filename']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 {
164 return FALSE;
165 }
166
167 $filepath = APPPATH.$data['filepath'].'/'.$data['filename'];
168
169 if ( ! file_exists($filepath))
170 {
171 return FALSE;
172 }
173
174 // -----------------------------------
175 // Set class/function name
176 // -----------------------------------
177
178 $class = FALSE;
179 $function = FALSE;
180 $params = '';
181
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300182 if ( ! empty($data['class']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
184 $class = $data['class'];
185 }
186
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300187 if ( ! empty($data['function']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 {
189 $function = $data['function'];
190 }
191
192 if (isset($data['params']))
193 {
194 $params = $data['params'];
195 }
196
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300197 if ($class === FALSE && $function === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 {
199 return FALSE;
200 }
201
202 // -----------------------------------
203 // Set the in_progress flag
204 // -----------------------------------
205
206 $this->in_progress = TRUE;
207
208 // -----------------------------------
209 // Call the requested class and/or function
210 // -----------------------------------
211
212 if ($class !== FALSE)
213 {
214 if ( ! class_exists($class))
215 {
216 require($filepath);
217 }
218
219 $HOOK = new $class;
220 $HOOK->$function($params);
221 }
222 else
223 {
224 if ( ! function_exists($function))
225 {
226 require($filepath);
227 }
228
229 $function($params);
230 }
231
232 $this->in_progress = FALSE;
233 return TRUE;
234 }
235
236}
237
Derek Allard2067d1a2008-11-13 22:59:24 +0000238/* End of file Hooks.php */
Andrey Andreeva5dd2972012-03-26 14:43:01 +0300239/* Location: ./system/core/Hooks.php */