0012488.patch

Administrator Admin, 2009-11-06 10:49

Download (16 KB)

View differences:

tests/t3lib/t3lib_matchcondition_testcase.php (Arbeitskopie)
142 142
	 * @return	void
143 143
	 * @test
144 144
	 */
145
	public function browserInfoHookIsCalled() {
145
	public function deprecatedBrowserInfoHookIsCalled() {
146 146
		$classRef = uniqid('tx_browserInfoHook');
147 147
		$browserInfoHookMock = $this->getMock($classRef, array('browserInfo'));
148 148
		$browserInfoHookMock->expects($this->atLeastOnce())->method('browserInfo');
......
180 180
	 * @return	void
181 181
	 * @test
182 182
	 */
183
	public function whichDeviceHookIsCalled() {
183
	public function deprecatedWhichDeviceHookIsCalled() {
184 184
		$classRef = uniqid('tx_whichDeviceHook');
185 185
		$whichDeviceHookMock = $this->getMock($classRef, array('whichDevice'));
186 186
		$whichDeviceHookMock->expects($this->atLeastOnce())->method('whichDevice');
t3lib/class.t3lib_matchcondition.php (Arbeitskopie)
77 77
 * @subpackage t3lib
78 78
 * @see t3lib_TStemplate::matching(), t3lib_TStemplate::generateConfig()
79 79
 */
80
class t3lib_matchCondition {
80
class t3lib_matchCondition extends t3lib_matchCondition_frontend {
81 81
	var $matchAlternative=array();		// If this array has elements, the matching returns true if a whole "matchline" is found in the array!
82 82
	var $matchAll=0;					// If set all is matched!
83 83

  
......
88 88
	 * Constructor for this class
89 89
	 *
90 90
	 * @return	void
91
	 * @deprecated	since TYPO3 4.3, will be removed in TYPO3 4.5 - The functionality was moved to t3lib_matchCondition_frontend
91 92
	 */
92 93
	function __construct()	{
93
		global $TYPO3_CONF_VARS;
94
		t3lib_div::logDeprecatedFunction();
94 95

  
95
		// Usage (ext_localconf.php):
96
		// $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['matchConditionClass'][] =
97
		// 'EXT:my_ext/class.browserinfo.php:MyBrowserInfoClass';
98
		if (is_array($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['matchConditionClass'])) {
99
			foreach ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['matchConditionClass'] as $classRef) {
100
				$this->hookObjectsArr[] = t3lib_div::getUserObj($classRef, '');
101
			}
102
		}
96
		parent::__construct();
103 97
	}
104 98

  
105 99
	/**
106
	 * Constructor for this class
107
	 *
108
	 * @return	void
109
	 */
110
	function t3lib_matchCondition() {
111
		$this->__construct();
112
	}
113

  
114
	/**
115 100
	 * Matching TS condition
116 101
	 *
117 102
	 * @param	string		Line to match
......
119 104
	 */
120 105
	function match($condition_line) {
121 106
		if ($this->matchAll) {
122
			return true;
107
			parent::setSimulateMatchResult(true);
123 108
		}
124 109
		if (count($this->matchAlternative))	{
125
			return in_array($condition_line, $this->matchAlternative);
110
			parent::setSimulateMatchConditions($this->matchAlternative);
126 111
		}
127 112

  
128
			// Getting the value from inside of the wrapping square brackets of the condition line:
129
		$insideSqrBrackets = substr(trim($condition_line), 1, strlen($condition_line) - 2);
130
		$insideSqrBrackets = preg_replace('/\]\s*OR\s*\[/i', ']||[', $insideSqrBrackets);
131
		$insideSqrBrackets = preg_replace('/\]\s*AND\s*\[/i', ']&&[', $insideSqrBrackets);
132

  
133
			// The "weak" operator "||" (OR) takes precedence: backwards compatible, [XYZ][ZYX] does still work as OR
134
		$orParts = preg_split('/\]\s*(\|\|){0,1}\s*\[/',$insideSqrBrackets);
135

  
136
		foreach ($orParts as $partString)	{
137
			$matches = false;
138

  
139
				// Splits by the "&&" (AND) operator:
140
			$andParts = preg_split('/\]\s*&&\s*\[/',$partString);
141
			foreach ($andParts as $condStr)	{
142
				$matches = $this->evalConditionStr($condStr);
143
				if ($matches===false)	{
144
					break;		// only true AND true = true, so we have to break here
145
				}
146
			}
147

  
148
			if ($matches===true)	{
149
				break;		// true OR false = true, so we break if we have a positive result
150
			}
151
		}
152

  
153
		return $matches;
113
		return parent::match($condition_line);
154 114
	}
155 115

  
156 116

  
......
163 123
	 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=292&cHash=c6c7d43d2f
164 124
	 */
165 125
	function evalConditionStr($string)	{
166
		if (!is_array($this->altRootLine)) {
167
			$this->altRootLine = array();
168
		}
169
		list($key, $value) = explode('=', $string, 2);
170
		$key = trim($key);
171
		$value = trim($value);
172
		if (t3lib_div::inList('browser,version,system,useragent', strtolower($key))) {
173
			$browserInfo = $this->browserInfo(t3lib_div::getIndpEnv('HTTP_USER_AGENT'));
174
		}
175
		switch ($key) {
176
			case 'browser':
177
				$values = t3lib_div::trimExplode(',', $value, true);
178
				foreach ($values as $test) {
179
					if (strpos($browserInfo['browser'] . $browserInfo['version'], $test) !== false) {
180
						return true;
181
					}
182
				}
183
			break;
184
			case 'version':
185
				$values = t3lib_div::trimExplode(',', $value, true);
186
				foreach ($values as $test) {
187
					if (strcspn($test, '=<>') == 0) {
188
						switch (substr($test, 0, 1)) {
189
							case '=':
190
								if (doubleval(substr($test, 1)) == $browserInfo['version']) {
191
									return true;
192
								}
193
							break;
194
							case '<':
195
								if (doubleval(substr($test, 1)) > $browserInfo['version']) {
196
									return true;
197
								}
198
							break;
199
							case '>':
200
								if (doubleval(substr($test, 1)) < $browserInfo['version']) {
201
									return true;
202
								}
203
							break;
204
						}
205
					} else {
206
						if (strpos(' ' . $browserInfo['version'], $test) == 1) {
207
							return true;
208
						}
209
					}
210
				}
211
			break;
212
			case 'system':
213
				$values = t3lib_div::trimExplode(',', $value, true);
214
				foreach ($values as $test) {
215
					if (strpos(' ' . $browserInfo['system'], $test) == 1) {
216
						return true;
217
					}
218
				}
219
			break;
220
			case 'device':
221
				if (!isset($this->deviceInfo)) {
222
					$this->deviceInfo = $this->whichDevice(t3lib_div::getIndpEnv('HTTP_USER_AGENT'));
223
				}
224
				$values = t3lib_div::trimExplode(',', $value, true);
225
				foreach ($values as $test) {
226
					if ($this->deviceInfo == $test) {
227
						return true;
228
					}
229
				}
230
			break;
231
			case 'useragent':
232
				$test = trim($value);
233
				if (strlen($test)) {
234
					return $this->matchWild($browserInfo['useragent'], $test);
235
				}
236
			break;
237
			case 'language':
238
				$values = t3lib_div::trimExplode(',', $value, true);
239
				foreach ($values as $test) {
240
					if (preg_match('/^\*.+\*$/', $test)) {
241
						$allLanguages = preg_split('/[,;]/', t3lib_div::getIndpEnv('HTTP_ACCEPT_LANGUAGE'));
242
						if (in_array(substr($test, 1, -1), $allLanguages)) {
243
							return true;
244
						}
245
					} else if (t3lib_div::getIndpEnv('HTTP_ACCEPT_LANGUAGE') == $test) {
246
						return true;
247
					}
248
				}
249
			break;
250
			case 'IP':
251
				if (t3lib_div::cmpIP(t3lib_div::getIndpEnv('REMOTE_ADDR'), $value)) {
252
					return true;
253
				}
254
			break;
255
			case 'hostname':
256
				if (t3lib_div::cmpFQDN(t3lib_div::getIndpEnv('REMOTE_ADDR'), $value)) {
257
					return true;
258
				}
259
			break;
260
				// hour, minute, dayofweek, dayofmonth, month, year, julianday
261
			case 'hour':
262
			case 'minute':
263
			case 'month':
264
			case 'year':
265
			case 'dayofweek':
266
			case 'dayofmonth':
267
			case 'dayofyear':
268
				$theEvalTime = $GLOBALS['SIM_EXEC_TIME'];	// In order to simulate time properly in templates.
269
				switch($key) {
270
					case 'hour':		$theTestValue = date('H', $theEvalTime);	break;
271
					case 'minute':		$theTestValue = date('i', $theEvalTime);	break;
272
					case 'month':		$theTestValue = date('m', $theEvalTime);	break;
273
					case 'year':		$theTestValue = date('Y', $theEvalTime);	break;
274
					case 'dayofweek':	$theTestValue = date('w', $theEvalTime);	break;
275
					case 'dayofmonth':	$theTestValue = date('d', $theEvalTime);	break;
276
					case 'dayofyear':	$theTestValue = date('z', $theEvalTime);	break;
277
				}
278
				$theTestValue = intval($theTestValue);
279
					// comp
280
				$values = t3lib_div::trimExplode(',', $value, true);
281
				foreach ($values as $test) {
282
					if (t3lib_div::testInt($test)) {
283
						$test = '=' . $test;
284
					}
285
					if ($this->testNumber($test, $theTestValue)) {
286
						return true;
287
					}
288
				}
289
			break;
290
			case 'usergroup':
291
				if ($GLOBALS['TSFE']->gr_list != '0,-1') {		// '0,-1' is the default usergroups when not logged in!
292
					$values = t3lib_div::trimExplode(',', $value, true);
293
					foreach ($values as $test) {
294
						if ($test == '*' || t3lib_div::inList($GLOBALS['TSFE']->gr_list, $test)) {
295
							return true;
296
						}
297
					}
298
				}
299
			break;
300
			case 'loginUser':
301
				if ($GLOBALS['TSFE']->loginUser) {
302
					$values = t3lib_div::trimExplode(',', $value, true);
303
					foreach ($values as $test) {
304
						if ($test == '*' || !strcmp($GLOBALS['TSFE']->fe_user->user['uid'], $test)) {
305
							return true;
306
						}
307
					}
308
				}
309
			break;
310
			case 'globalVar':
311
				$values = t3lib_div::trimExplode(',', $value, true);
312
				foreach ($values as $test) {
313
					$point = strcspn($test, '!=<>');
314
					$theVarName = substr($test, 0, $point);
315
					$nv = $this->getGP_ENV_TSFE(trim($theVarName));
316
					$testValue = substr($test, $point);
317

  
318
					if ($this->testNumber($testValue, $nv)) {
319
						return true;
320
					}
321
				}
322
			break;
323
			case 'globalString':
324
				$values = t3lib_div::trimExplode(',', $value, true);
325
				foreach ($values as $test) {
326
					$point = strcspn($test, '=');
327
					$theVarName = substr($test, 0, $point);
328
					$nv = $this->getGP_ENV_TSFE(trim($theVarName));
329
					$testValue = substr($test, $point+1);
330

  
331
					if ($this->matchWild($nv, trim($testValue))) {
332
						return true;
333
					}
334
				}
335
			break;
336
			case 'treeLevel':
337
				$values = t3lib_div::trimExplode(',', $value, true);
338
				$theRootLine = is_array($GLOBALS['TSFE']->tmpl->rootLine) ? $GLOBALS['TSFE']->tmpl->rootLine : $this->altRootLine;
339
				$treeLevel = count($theRootLine)-1;
340
				foreach ($values as $test) {
341
					if ($test == $treeLevel) {
342
						return true;
343
					}
344
				}
345
			break;
346
			case 'PIDupinRootline':
347
			case 'PIDinRootline':
348
				$values = t3lib_div::trimExplode(',', $value, true);
349
				if (($key=='PIDinRootline') || (!in_array($GLOBALS['TSFE']->id, $values))) {
350
					$theRootLine = is_array($GLOBALS['TSFE']->tmpl->rootLine) ? $GLOBALS['TSFE']->tmpl->rootLine : $this->altRootLine;
351
					foreach ($values as $test) {
352
						foreach ($theRootLine as $rl_dat) {
353
							if ($rl_dat['uid'] == $test) {
354
								return true;
355
							}
356
						}
357
					}
358
				}
359
			break;
360
			case 'compatVersion':
361
				return t3lib_div::compat_version($value);
362
			break;
363
			case 'userFunc':
364
				$values = preg_split('/\(|\)/', $value);
365
				$funcName = trim($values[0]);
366
				$funcValue = t3lib_div::trimExplode(',', $values[1]);
367
				$pre = $GLOBALS['TSFE']->TYPO3_CONF_VARS['FE']['userFuncClassPrefix'];
368
				if ($pre &&
369
					!t3lib_div::isFirstPartOfStr(trim($funcName),$pre) &&
370
					!t3lib_div::isFirstPartOfStr(trim($funcName),'tx_')
371
				)	{
372
					if (is_object($GLOBALS['TT']))	$GLOBALS['TT']->setTSlogMessage('Match condition: Function "'.$funcName.'" was not prepended with "'.$pre.'"',3);
373
					return false;
374
				}
375
				if (function_exists($funcName) && call_user_func($funcName, $funcValue[0]))	{
376
					return true;
377
				}
378
			break;
379
		}
380

  
381

  
382
		return false;
126
		return parent::evaluateCondition($string);
383 127
	}
384 128

  
385 129
	/**
......
390 134
	 * @return	boolean		If $value is "50" and $test is "< 123" then it will return true.
391 135
	 */
392 136
	function testNumber($test, $leftValue) {
393
		if (preg_match('/^(!?=+|<=?|>=?)\s*([^\s]*)\s*$/', $test, $matches)) {
394
			$operator = $matches[1];
395
			$rightValue = $matches[2];
396

  
397
			switch ($operator) {
398
				case '>=':
399
					return ($leftValue >= doubleval($rightValue));
400
					break;
401
				case '<=':
402
					return ($leftValue <= doubleval($rightValue));
403
					break;
404
				case '!=':
405
					return ($leftValue != doubleval($rightValue));
406
					break;
407
				case '<':
408
					return ($leftValue < doubleval($rightValue));
409
					break;
410
				case '>':
411
					return ($leftValue > doubleval($rightValue));
412
					break;
413
				default:
414
					// nothing valid found except '=', use '='
415
					return ($leftValue == trim($rightValue));
416
					break;
417
			}
418
		}
419

  
420
		return false;
137
		return parent::compareNumber($test, $leftValue);
421 138
	}
422 139

  
423 140
	/**
......
428 145
	 * @return	boolean		Returns true if $needle matches or is found in (according to wildcards) in $haystack. Eg. if $haystack is "Netscape 6.5" and $needle is "Net*" or "Net*ape" then it returns true.
429 146
	 */
430 147
	function matchWild($haystack, $needle) {
431
		$result = false;
432

  
433
		if ($needle) {
434
			if (preg_match('/^\/.+\/$/', $needle)) {
435
				// Regular expression, only "//" is allowed as delimiter
436
				$regex = $needle;
437
			} else {
438
				$needle = str_replace(array('*', '?'), array('###MANY###', '###ONE###'), $needle);
439
				$regex = '/^' . preg_quote($needle, '/') . '$/';
440
				// Replace the marker with .* to match anything (wildcard)
441
				$regex = str_replace(array('###MANY###', '###ONE###'), array('.*' , '.'), $regex);
442
			}
443

  
444
			$result = (boolean)preg_match($regex, (string)$haystack);
445
		}
446

  
447
		return $result;
148
		return parent::searchStringWildcard($haystack, $needle);
448 149
	}
449 150

  
450 151
	/**
......
456 157
	 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=296&cHash=a8ae66c7d6
457 158
	 */
458 159
	function whichDevice($useragent)	{
459
		foreach($this->hookObjectsArr as $hookObj)	{
460
			if (method_exists($hookObj, 'whichDevice')) {
461
				$result = $hookObj->whichDevice($useragent);
462
				if (strlen($result)) {
463
					return $result;
464
				}
465
			}
466
		}
467

  
468
		// deprecated, see above
469
		if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['devices_class']))	{
470
			foreach($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['devices_class'] as $_classRef)	{
471
				$_procObj = t3lib_div::getUserObj($_classRef);
472
				return $_procObj->whichDevice_ext($useragent);
473
			}
474
		}
475
		//
476

  
477
		$deviceType = t3lib_utility_Client::getDeviceType($useragent);
478

  
479
		return $deviceType;
160
		return parent::getDeviceType($useragent);
480 161
	}
481 162

  
482 163
	/**
......
489 170
	 * @see match()
490 171
	 */
491 172
	function browserInfo($useragent)	{
492
		foreach($this->hookObjectsArr as $hookObj)	{
493
			if (method_exists($hookObj, 'browserInfo')) {
494
				$result = $hookObj->browserInfo($useragent);
495
				if (strlen($result)) {
496
					return $result;
497
				}
498
			}
499
		}
500

  
501
		$browserInfo = t3lib_utility_Client::getBrowserInfo($useragent);
502

  
503
		return $browserInfo;
173
		return parent::getBrowserInfo($useragent);
504 174
	}
505 175

  
506 176
	/**
......
525 195
	 * @access private
526 196
	 */
527 197
	function getGlobal($var, $source=NULL)	{
528
		$vars = explode('|',$var);
529
		$c = count($vars);
530
		$k = trim($vars[0]);
531
		$theVar = isset($source) ? $source[$k] : $GLOBALS[$k];
532

  
533
		for ($a=1;$a<$c;$a++)	{
534
			if (!isset($theVar))	{ break; }
535

  
536
			$key = trim($vars[$a]);
537
			if (is_object($theVar))	{
538
				$theVar = $theVar->$key;
539
			} elseif (is_array($theVar))	{
540
				$theVar = $theVar[$key];
541
			} else {
542
				return '';
543
			}
544
		}
545

  
546
		if (!is_array($theVar) && !is_object($theVar))	{
547
			return $theVar;
548
		} else {
549
			return '';
550
		}
198
		return parent::getGlobal($var, $source);
551 199
	}
552 200

  
553 201
	/**
......
559 207
	 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=311&cHash=487cbd5cdf
560 208
	 */
561 209
	function getGP_ENV_TSFE($var) {
562
		$vars = explode(':',$var,2);
563
		if (count($vars)==1)	{
564
			$val = $this->getGlobal($var);
565
		} else {
566
			$splitAgain=explode('|',$vars[1],2);
567
			$k=trim($splitAgain[0]);
568
			if ($k)	{
569
				switch((string)trim($vars[0]))	{
570
					case 'GP':
571
						$val = t3lib_div::_GP($k);
572
					break;
573
					case 'TSFE':
574
						$val = $this->getGlobal('TSFE|'.$vars[1]);
575
						$splitAgain=0;	// getGlobal resolves all parts of the key, so no further splitting is needed
576
					break;
577
					case 'ENV':
578
						$val = getenv($k);
579
					break;
580
					case 'IENV':
581
						$val = t3lib_div::getIndpEnv($k);
582
					break;
583
					case 'LIT':
584
						{ return trim($vars[1]); }	// return litteral value...
585
					break;
586
				}
587
					// If array:
588
				if (count($splitAgain)>1)	{
589
					if (is_array($val) && trim($splitAgain[1]))	{
590
						$val=$this->getGlobal($splitAgain[1],$val);
591
					} else {
592
						$val='';
593
					}
594
				}
595
			}
596
		}
597
		return $val;
210
		return parent::getVariable($var);
598 211
	}
599 212
}
600 213