1: <?php
2: /**
3: * This file contains the CEC hook class.
4: *
5: * @package Core
6: * @subpackage CEC
7: * @version SVN Revision $Rev:$
8: *
9: * @author Timo A. Hummel
10: * @author Murat Purc <murat@purc.de>
11: * @copyright four for business AG <www.4fb.de>
12: * @license http://www.contenido.org/license/LIZENZ.txt
13: * @link http://www.4fb.de
14: * @link http://www.contenido.org
15: */
16:
17: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
18:
19: /**
20: * Static CEC Hook class, provides some public methods to process registered
21: * chains
22: * at CEC (CONTENIDO Extension Chainer).
23: *
24: * Usage:
25: * <code>
26: * // example of executing a cec without a parameter and return value
27: * cApiCecHook::execute('Contenido.Content.Somewhere');
28: *
29: * // example of executing a cec with a parameter but without a return value
30: * $param = 'some value';
31: * cApiCecHook::execute('Contenido.Content.Somewhere', $param);
32: *
33: * // example of executing a cec with multiple parameter but without a return
34: * value
35: * $param = array('foo' => $bar, 'foo2' => $bar2);
36: * $param = cApiCecHook::execute('Contenido.Content.Somewhere', $param);
37: *
38: *
39: * // example of executing a cec without a parameter but a return value (with
40: * predefined
41: * // default return value)
42: * cApiCecHook::setDefaultReturnValue('this is the default title');
43: * $title = cApiCecHook::executeAndReturn('Contenido.Content.CreateTitletag');
44: *
45: * // example of executing a cec with a parameter and a return value
46: * // (usually the modified version of passed parameter)
47: * $baseHref = cRegistry::getFrontendUrl();
48: * $newBaseHref =
49: * cApiCecHook::executeAndReturn('Contenido.Frontend.BaseHrefGeneration',
50: * $baseHref);
51: *
52: *
53: * // example of executing a cec with a break condition and default return value
54: * cApiCecHook::setBreakCondition(false, true); // break condition = "false",
55: * default return value = "true"
56: * $allow = cApiCecHook::executeWhileBreakCondition('Contenido.Frontend.AllowEdit',
57: * $lang, $idcat, $idart, $auth->auth['uid']);
58: * if ($allow == false) {
59: * die('You're not coming in!');
60: * }
61: *
62: * // another example of executing a cec with a break condition and default
63: * return value
64: * cApiCecHook::setBreakCondition(true, false); // break condition = "true",
65: * default return value = "false"
66: * $allow =
67: * cApiCecHook::executeWhileBreakCondition('Contenido.Frontend.CategoryAccess',
68: * $lang, $idcat, $auth->auth['uid']);
69: * if ($allow == false) {
70: * die('I said, you're not coming in!');
71: * }
72: * </code>
73: *
74: * @package Core
75: * @subpackage CEC
76: */
77: class cApiCecHook {
78:
79: /**
80: * Temporaly stored break condition.
81: *
82: * @var int
83: */
84: private static $_breakCondition = NULL;
85:
86: /**
87: * Temporaly stored default return value of CEC functions
88: *
89: * @var mixed
90: */
91: private static $_defaultReturnValue = NULL;
92:
93: /**
94: * Temporaly stored position of argument to return.
95: * It's used by cApiCecHook::executeAndReturn()
96: * to store/extract the return value into/from arguments list.
97: *
98: * @var int
99: */
100: private static $_returnArgumentPos = 1;
101:
102: /**
103: * Temporaly setting of break condition and optional the default return
104: * value.
105: *
106: * @param mixed $condition
107: * @param mixed $defaultReturnValue
108: */
109: public static function setBreakCondition($condition, $defaultReturnValue = NULL) {
110: self::$_breakCondition = $condition;
111: self::setDefaultReturnValue($defaultReturnValue);
112: }
113:
114: /**
115: * Temporaly setting of default return value.
116: *
117: * @param mixed $defaultReturnValue
118: */
119: public static function setDefaultReturnValue($defaultReturnValue) {
120: self::$_defaultReturnValue = $defaultReturnValue;
121: }
122:
123: /**
124: * Temporaly setting of position in argument to return.
125: *
126: * @param int $pos Position, feasible value greater 0
127: *
128: * @throws cInvalidArgumentException if the given position is less than 1
129: */
130: public static function setReturnArgumentPos($pos) {
131: if ((int)$pos < 1) {
132: throw new cInvalidArgumentException('Return position has to be greater or equal than 1.');
133: }
134: self::$_returnArgumentPos = (int)$pos;
135: }
136:
137: /**
138: * Method to execute registered functions for CONTENIDO Extension Chainer
139: * (CEC).
140: *
141: * Gets the desired CEC iterator and executes each registered chain function
142: * by passing the achieved
143: * arguments to it. There is
144: *
145: * @param string $chainName The chain name to process
146: * @param mixed $param1 First parameter which will be forwarded to
147: * registered chain functions
148: * @param mixed $param2 Second parameter which will be forwarded to
149: * registered chain functions
150: * @param mixed $param3 Third parameter which will be forwarded to
151: * registered chain functions
152: * NOTE: There is no restriction for number of passed parameter.
153: */
154: public static function execute() {
155: // get arguments
156: $args = func_get_args();
157:
158: // get chainname
159: $chainName = array_shift($args);
160:
161: // process CEC
162: $cecIterator = cApiCecRegistry::getInstance()->getIterator($chainName);
163: if ($cecIterator->count() > 0) {
164: $cecIterator->reset();
165:
166: while (($chainEntry = $cecIterator->next()) !== false) {
167: // invoke CEC function
168: $chainEntry->setTemporaryArguments($args);
169: $chainEntry->execute();
170: }
171: }
172:
173: // reset properties to defaults
174: self::_reset();
175: }
176:
177: /**
178: * Method to execute registered functions for CONTENIDO Extension Chainer
179: * (CEC).
180: *
181: * Gets the desired CEC iterator and executes each registered chain
182: * function. You can pass as much
183: * parameter as you want.
184: *
185: * @param string $chainName The chain name to process
186: * @param mixed $param1 First parameter which will be forwarded to
187: * registered chain functions
188: * @param mixed $param2 Second parameter which will be forwarded to
189: * registered chain functions
190: * @param mixed $param3 Third parameter which will be forwarded to
191: * registered chain functions
192: * NOTE: There is no restriction for number of passed parameter.
193: *
194: * @return mixed Parameter changed/processed by chain functions.
195: * Note: If no chain function is registered, the first parameter
196: * $param after
197: * $chainName will be returned
198: */
199: public static function executeAndReturn() {
200: // get arguments
201: $args = func_get_args();
202:
203: // get chainname
204: $chainName = array_shift($args);
205:
206: // position of return value in arguments list
207: $pos = self::$_returnArgumentPos - 1;
208:
209: // default return value
210: $return = self::$_defaultReturnValue;
211:
212: // process CEC
213: $cecIterator = cApiCecRegistry::getInstance()->getIterator($chainName);
214: if ($cecIterator->count() > 0) {
215: $cecIterator->reset();
216:
217: while (($chainEntry = $cecIterator->next()) !== false) {
218: // invoke CEC function
219: $chainEntry->setTemporaryArguments($args);
220: $return = $chainEntry->execute();
221: if (isset($args[$pos])) {
222: $args[$pos] = $return;
223: }
224: }
225: }
226:
227: if (isset($args[$pos])) {
228: $return = $args[$pos];
229: }
230:
231: // reset properties to defaults
232: self::_reset();
233:
234: return $return;
235: }
236:
237: /**
238: * CEC function to process chains untill a break condition occurs.
239: *
240: * Gets the desired CEC iterator and executes each registered chain function
241: * as long as defined break
242: * condition doesn't occur.
243: *
244: * @param string $chainName The chain name to process
245: * @param mixed $param1 First parameter which will be forwarded to
246: * registered chain functions
247: * @param mixed $param2 Second parameter which will be forwarded to
248: * registered chain functions
249: * @param mixed $param3 Third parameter which will be forwarded to
250: * registered chain functions
251: * NOTE: There is no restriction for number of passed parameter.
252: *
253: * @return mixed The break condition or it's default value
254: */
255: public static function executeWhileBreakCondition() {
256: // get arguments
257: $args = func_get_args();
258:
259: // get chainname
260: $chainName = array_shift($args);
261:
262: // break condition and default return value
263: $breakCondition = self::$_breakCondition;
264: $return = self::$_defaultReturnValue;
265:
266: // process CEC
267: $cecIterator = cApiCecRegistry::getInstance()->getIterator($chainName);
268: if ($cecIterator->count() > 0) {
269: $cecIterator->reset();
270:
271: while (($chainEntry = $cecIterator->next()) !== false) {
272: // invoke CEC function
273: $chainEntry->setTemporaryArguments($args);
274: $return = $chainEntry->execute();
275: // process return value
276: if (isset($return) && $return === $breakCondition) {
277: self::_reset();
278:
279: return $return;
280: break;
281: }
282: }
283: }
284:
285: // reset properties to defaults
286: self::_reset();
287:
288: return $return;
289: }
290:
291: /**
292: * Resets some properties to defaults
293: */
294: private static function _reset() {
295: self::$_breakCondition = NULL;
296: self::$_defaultReturnValue = NULL;
297: self::$_returnArgumentPos = 1;
298: }
299:
300: /**
301: * Used to debug some status informations.
302: *
303: * @todo Implement cec_hook debug mode for automatic logging when activated. Writes the debug value into a logfile (see contenido/data/log/cec_hook_debug.log).
304: *
305: * @param mixed $var The variable to dump
306: * @param string $msg Additional message
307: */
308: private static function _debug($var, $msg = '') {
309: global $cfg;
310:
311: $content = ($msg !== '') ? $msg . ': ' : '';
312: if (is_object($var) || is_array($var)) {
313: $content .= print_r($var, true);
314: } else {
315: $content .= $var . "\n";
316: }
317:
318: $sLogPathName = $cfg['path']['contenido_logs'] . 'cec_hook_debug.log';
319: cFileHandler::write($sLogPathName, $content . "\n", true);
320:
321: cDebug::out($content);
322: }
323:
324: }
325:
326: