xref: /plugin/condition/base_tester.php.orig.php (revision 09b43617b7cf414344497ee7ebf8cd1b31efdab4)
1*09b43617SGerry Weißbach<?php
2*09b43617SGerry Weißbach	/*
3*09b43617SGerry Weißbach	 * The condition_plugin_custom_tester class defined in <tpl>/condition_plugin_custom_tester.php
4*09b43617SGerry Weißbach	 * MUST implements this class
5*09b43617SGerry Weißbach	 *
6*09b43617SGerry Weißbach	 * To add a custom test in condition_plugin_custom_tester you just have to add a method like :
7*09b43617SGerry Weißbach	 *
8*09b43617SGerry Weißbach	 *	function test_dummy($b, &$bug, $lop=false) { if($lop) return array(); return true; }
9*09b43617SGerry Weißbach	 *		this test will react to <if dummy></if> of <if dummy=3></if>
10*09b43617SGerry Weißbach	 *
11*09b43617SGerry Weißbach	 * or
12*09b43617SGerry Weißbach	 *
13*09b43617SGerry Weißbach	 *	function test_IP($b, &$bug, $lop=false) {
14*09b43617SGerry Weißbach	 *		if($lop) return array('\=\=?', 'eq', '\!\=', 'neq?', '\~\='); // pcre regexp list of allowed test operators
15*09b43617SGerry Weißbach	 *		$ip = clientIP(true);
16*09b43617SGerry Weißbach	 *		if(!$b['test'] || ($b['test'] == '') || !$b['value'] || ($b['value'] == '') || ($ip == '0.0.0.0')) {
17*09b43617SGerry Weißbach	 *			$bug = true;
18*09b43617SGerry Weißbach	 *			return false;
19*09b43617SGerry Weißbach	 *		}
20*09b43617SGerry Weißbach	 *		switch($b['test']) {
21*09b43617SGerry Weißbach	 *			case '=' :
22*09b43617SGerry Weißbach	 *			case 'eq' :
23*09b43617SGerry Weißbach	 *			case '==' :
24*09b43617SGerry Weißbach	 *				return ($ip == $b['value']); break;
25*09b43617SGerry Weißbach	 *			case '!=' :
26*09b43617SGerry Weißbach	 *			case 'ne' :
27*09b43617SGerry Weißbach	 *			case 'neq' :
28*09b43617SGerry Weißbach	 *				return ($ip != $b['value']); break;
29*09b43617SGerry Weißbach	 *			case '~=' : // such new test operators must be added in syntax.php
30*09b43617SGerry Weißbach	 *				return (strpos($ip, $b['value']) !== false); break;
31*09b43617SGerry Weißbach	 *			default: // non allowed operators for the test must lead to a bug flag raise
32*09b43617SGerry Weißbach	 *				$bug = true;
33*09b43617SGerry Weißbach	 *				return false;
34*09b43617SGerry Weißbach	 *		}
35*09b43617SGerry Weißbach	 *	}
36*09b43617SGerry Weißbach	 *		this test will react to <if IP=127.0.0.1></if>
37*09b43617SGerry Weißbach	 */
38*09b43617SGerry Weißbach
39*09b43617SGerry Weißbach	class condition_plugin_base_tester {
40*09b43617SGerry Weißbach		function __construct() {}
41*09b43617SGerry Weißbach
42*09b43617SGerry Weißbach		// Wrapper for all tests
43*09b43617SGerry Weißbach		function run($b, &$bug) {
44*09b43617SGerry Weißbach			if(method_exists($this, 'test_'.$b['key'])) {
45*09b43617SGerry Weißbach				return call_user_func(array($this, 'test_'.$b['key']), $b, $bug);
46*09b43617SGerry Weißbach			}
47*09b43617SGerry Weißbach			$bug = true;
48*09b43617SGerry Weißbach			return false;
49*09b43617SGerry Weißbach		}
50*09b43617SGerry Weißbach
51*09b43617SGerry Weißbach		// Get allowed keys
52*09b43617SGerry Weißbach		function getkeys() {
53*09b43617SGerry Weißbach			$keys = array();
54*09b43617SGerry Weißbach			foreach(get_class_methods($this) as $m) {
55*09b43617SGerry Weißbach				if(preg_match('`^test_(.+)$`', $m, $r)) $keys[] = $r[1];
56*09b43617SGerry Weißbach			}
57*09b43617SGerry Weißbach			return $keys;
58*09b43617SGerry Weißbach		}
59*09b43617SGerry Weißbach
60*09b43617SGerry Weißbach		// Get test operators
61*09b43617SGerry Weißbach		function getops() {
62*09b43617SGerry Weißbach			$ops = array();
63*09b43617SGerry Weißbach			foreach($this->getkeys() as $m) $ops = array_merge($ops, call_user_func(array($this, 'test_'.$m), null, $dummy, true));
64*09b43617SGerry Weißbach			return array_unique($ops);
65*09b43617SGerry Weißbach		}
66*09b43617SGerry Weißbach
67*09b43617SGerry Weißbach		// Tests follows
68*09b43617SGerry Weißbach		// -------------
69*09b43617SGerry Weißbach
70*09b43617SGerry Weißbach		// user based tests
71*09b43617SGerry Weißbach		function test_user($b, &$bug, $lop=false) {
72*09b43617SGerry Weißbach			if($lop) return array('\=\=?', 'eq', '\!\=', 'neq?');
73*09b43617SGerry Weißbach			$rh = isset($_SERVER['REMOTE_USER']) ? $_SERVER['REMOTE_USER'] : '';
74*09b43617SGerry Weißbach			if(!$b['test'] || ($b['test'] == '')) return ($rh && ($rh != ''));
75*09b43617SGerry Weißbach			switch($b['test']) {
76*09b43617SGerry Weißbach				case '=' :
77*09b43617SGerry Weißbach				case 'eq' :
78*09b43617SGerry Weißbach				case '==' :
79*09b43617SGerry Weißbach					return $rh == $b['value']; break;
80*09b43617SGerry Weißbach				case '!=' :
81*09b43617SGerry Weißbach				case 'ne' :
82*09b43617SGerry Weißbach				case 'neq' :
83*09b43617SGerry Weißbach					return $rh != $b['value']; break;
84*09b43617SGerry Weißbach				default:
85*09b43617SGerry Weißbach					$bug = true;
86*09b43617SGerry Weißbach					return false;
87*09b43617SGerry Weißbach			}
88*09b43617SGerry Weißbach		}
89*09b43617SGerry Weißbach
90*09b43617SGerry Weißbach		function test_group($b, &$bug, $lop=false) {
91*09b43617SGerry Weißbach			if($lop) return array('\=\=?', 'eq', '\!\=', 'neq?');
92*09b43617SGerry Weißbach			global $INFO;
93*09b43617SGerry Weißbach			$grps = isset($INFO['userinfo']) ? $INFO['userinfo']['grps'] : array();
94*09b43617SGerry Weißbach			if(!$b['test'] || ($b['test'] == '')) return (count($grps) != 0);
95*09b43617SGerry Weißbach			switch($b['test']) {
96*09b43617SGerry Weißbach				case '=' :
97*09b43617SGerry Weißbach				case 'eq' :
98*09b43617SGerry Weißbach				case '==' :
99*09b43617SGerry Weißbach					if(!$b['value'] || ($b['value'] == '')) return (count($grps) == 0);
100*09b43617SGerry Weißbach					return in_array($b['value'], $grps); break;
101*09b43617SGerry Weißbach				case '!=' :
102*09b43617SGerry Weißbach				case 'ne' :
103*09b43617SGerry Weißbach				case 'neq' :
104*09b43617SGerry Weißbach					if(!$b['value'] || ($b['value'] == '')) return (count($grps) != 0);
105*09b43617SGerry Weißbach					return !in_array($b['value'], $grps); break;
106*09b43617SGerry Weißbach				default:
107*09b43617SGerry Weißbach					$bug = true;
108*09b43617SGerry Weißbach					return false;
109*09b43617SGerry Weißbach			}
110*09b43617SGerry Weißbach		}
111*09b43617SGerry Weißbach
112*09b43617SGerry Weißbach		// namespace based tests
113*09b43617SGerry Weißbach		function test_nsread($b, &$bug, $lop=false) {
114*09b43617SGerry Weißbach			if($lop) return array('\=\=?', 'eq', '\!\=', 'neq?');
115*09b43617SGerry Weißbach			if(!$b['test'] || ($b['test'] == '')) {
116*09b43617SGerry Weißbach				$bug = true;
117*09b43617SGerry Weißbach				return false;
118*09b43617SGerry Weißbach			}
119*09b43617SGerry Weißbach			if(!$b['value'] || ($b['value'] == '')) $b['value'] = '.';
120*09b43617SGerry Weißbach			switch($b['test']) {
121*09b43617SGerry Weißbach				case '=' :
122*09b43617SGerry Weißbach				case 'eq' :
123*09b43617SGerry Weißbach				case '==' :
124*09b43617SGerry Weißbach					return (auth_quickaclcheck($b['value']) >= AUTH_READ); break;
125*09b43617SGerry Weißbach				case '!=' :
126*09b43617SGerry Weißbach				case 'ne' :
127*09b43617SGerry Weißbach				case 'neq' :
128*09b43617SGerry Weißbach					return (auth_quickaclcheck($b['value']) < AUTH_READ); break;
129*09b43617SGerry Weißbach				default:
130*09b43617SGerry Weißbach					$bug = true;
131*09b43617SGerry Weißbach					return false;
132*09b43617SGerry Weißbach			}
133*09b43617SGerry Weißbach		}
134*09b43617SGerry Weißbach
135*09b43617SGerry Weißbach		function test_nsedit($b, &$bug, $lop=false) {
136*09b43617SGerry Weißbach			if($lop) return array('\=\=?', 'eq', '\!\=', 'neq?');
137*09b43617SGerry Weißbach			if(!$b['test'] || ($b['test'] == '')) {
138*09b43617SGerry Weißbach				$bug = true;
139*09b43617SGerry Weißbach				return false;
140*09b43617SGerry Weißbach			}
141*09b43617SGerry Weißbach			if(!$b['value'] || ($b['value'] == '')) $b['value'] = '.';
142*09b43617SGerry Weißbach			switch($b['test']) {
143*09b43617SGerry Weißbach				case '=' :
144*09b43617SGerry Weißbach				case 'eq' :
145*09b43617SGerry Weißbach				case '==' :
146*09b43617SGerry Weißbach					return (auth_quickaclcheck($b['value']) >= AUTH_EDIT); break;
147*09b43617SGerry Weißbach				case '!=' :
148*09b43617SGerry Weißbach				case 'ne' :
149*09b43617SGerry Weißbach				case 'neq' :
150*09b43617SGerry Weißbach					return (auth_quickaclcheck($b['value']) < AUTH_EDIT); break;
151*09b43617SGerry Weißbach				default:
152*09b43617SGerry Weißbach					$bug = true;
153*09b43617SGerry Weißbach					return false;
154*09b43617SGerry Weißbach			}
155*09b43617SGerry Weißbach		}
156*09b43617SGerry Weißbach
157*09b43617SGerry Weißbach		// time based tests
158*09b43617SGerry Weißbach		function test_time($b, &$bug, $lop=false) {
159*09b43617SGerry Weißbach			if($lop) return array('\=\=?', 'eq', '\!\=', 'neq?', '\<\=?', 'lt', '\>\=?', 'gt');
160*09b43617SGerry Weißbach			global $INFO;
161*09b43617SGerry Weißbach			if(!$b['test'] || ($b['test'] == '') || !$b['value'] || ($b['value'] == '')) {
162*09b43617SGerry Weißbach				$bug = true;
163*09b43617SGerry Weißbach				return false;
164*09b43617SGerry Weißbach			}
165*09b43617SGerry Weißbach			switch($b['test']) {
166*09b43617SGerry Weißbach				case '=' :
167*09b43617SGerry Weißbach				case 'eq' :
168*09b43617SGerry Weißbach				case '==' :
169*09b43617SGerry Weißbach					return $this->_bt_cmptimeandstr($b['value']); break;
170*09b43617SGerry Weißbach				case '!=' :
171*09b43617SGerry Weißbach				case 'ne' :
172*09b43617SGerry Weißbach				case 'neq' :
173*09b43617SGerry Weißbach					return !$this->_bt_cmptimeandstr($b['value']); break;
174*09b43617SGerry Weißbach				case '<' :
175*09b43617SGerry Weißbach				case 'lt' :
176*09b43617SGerry Weißbach					$t = time();
177*09b43617SGerry Weißbach					return ($t < $this->_bt_strtotime($b['value'], $t)); break;
178*09b43617SGerry Weißbach				case '>' :
179*09b43617SGerry Weißbach				case 'gt' :
180*09b43617SGerry Weißbach					$t = time();
181*09b43617SGerry Weißbach					return ($t > $this->_bt_strtotime($b['value'], $t)); break;
182*09b43617SGerry Weißbach				case '<=' :
183*09b43617SGerry Weißbach					$t = time();
184*09b43617SGerry Weißbach					return ($t <= $this->_bt_strtotime($b['value'], $t)); break;
185*09b43617SGerry Weißbach				case '>=' :
186*09b43617SGerry Weißbach					$t = time();
187*09b43617SGerry Weißbach					return ($t >= $this->_bt_strtotime($b['value'], $t)); break;
188*09b43617SGerry Weißbach				default:
189*09b43617SGerry Weißbach					$bug = true;
190*09b43617SGerry Weißbach					return false;
191*09b43617SGerry Weißbach			}
192*09b43617SGerry Weißbach		}
193*09b43617SGerry Weißbach		function _bt_strtotime($value, $default) {
194*09b43617SGerry Weißbach			if(preg_match('`^([0-9]{4})(-|/)([0-9]{2})(-|/)([0-9]{2})(\s+([0-9]{2}):([0-9]{2})(:([0-9]{2}))?)?$`', $value, $reg)) $value = mktime($reg[7], $reg[8], $reg[10], $reg[3], $reg[5], $reg[1]); // YYYY(-|/)MM(-|/)DD (HH:II(:SS)?)?
195*09b43617SGerry Weißbach			if(preg_match('`^(([0-9]{2}):([0-9]{2})(:([0-9]{2}))?\s+)?([0-9]{4})(-|/)([0-9]{2})(-|/)([0-9]{2})$`', $value, $reg)) $value = mktime($reg[2], $reg[3], $reg[5], $reg[8], $reg[10], $reg[6]); // (HH:II(:SS)?)? YYYY(-|/)MM(-|/)DD
196*09b43617SGerry Weißbach			if(preg_match('`^([0-9]{2})(-|/)([0-9]{2})(-|/)([0-9]{4})(\s+([0-9]{2}):([0-9]{2})(:([0-9]{2}))?)?$`', $value, $reg)) $value = mktime($reg[7], $reg[8], $reg[10], $reg[3], $reg[1], $reg[5]); // DD(-|/)MM(-|/)YYYY (HH:II(:SS)?)?
197*09b43617SGerry Weißbach			if(preg_match('`^(([0-9]{2}):([0-9]{2})(:([0-9]{2}))?\s+)?([0-9]{2})(-|/)([0-9]{2})(-|/)([0-9]{4})$`', $value, $reg)) $value = mktime($reg[2], $reg[3], $reg[5], $reg[8], $reg[6], $reg[10]); // (HH:II(:SS)?)? DD(-|/)MM(-|/)YYYY
198*09b43617SGerry Weißbach			if(!is_numeric($value)) $value = $default;
199*09b43617SGerry Weißbach			return $value;
200*09b43617SGerry Weißbach		}
201*09b43617SGerry Weißbach		function _bt_cmptimeandstr($str) {
202*09b43617SGerry Weißbach			$matched = false;
203*09b43617SGerry Weißbach			$t = time();
204*09b43617SGerry Weißbach			$time = array('y' => date('Y', $t), 'm' => date('m', $t), 'd' => date('d', $t), 'h' => date('H', $t), 'i' => date('i', $t), 's' => date('s', $t));
205*09b43617SGerry Weißbach			$d = array('y' => '', 'm' => '', 'd' => '', 'h' => '', 'i' => '', 's' => '');
206*09b43617SGerry Weißbach
207*09b43617SGerry Weißbach			// full date y, m and d, time is optionnal
208*09b43617SGerry Weißbach			if(preg_match('`^([0-9]{4})(-|/)([0-9]{2})(-|/)([0-9]{2})(\s+([0-9]{2}):([0-9]{2})(:([0-9]{2}))?)?$`', $str, $reg)) {
209*09b43617SGerry Weißbach				$d = array('y' => $reg[1], 'm' => $reg[3], 'd' => $reg[5], 'h' => $reg[7], 'i' => $reg[8], 's' => $reg[10]); // YYYY(-|/)MM(-|/)DD (HH:II(:SS)?)?
210*09b43617SGerry Weißbach				$matched = true;
211*09b43617SGerry Weißbach			}
212*09b43617SGerry Weißbach			if(preg_match('`^(([0-9]{2}):([0-9]{2})(:([0-9]{2}))?\s+)?([0-9]{4})(-|/)([0-9]{2})(-|/)([0-9]{2})$`', $str, $reg)) {
213*09b43617SGerry Weißbach				$d = array('y' => $reg[6], 'm' => $reg[8], 'd' => $reg[10], 'h' => $reg[2], 'i' => $reg[3], 's' => $reg[5]); // (HH:II(:SS)?)? YYYY(-|/)MM(-|/)DD
214*09b43617SGerry Weißbach				$matched = true;
215*09b43617SGerry Weißbach			}
216*09b43617SGerry Weißbach			if(preg_match('`^([0-9]{2})(-|/)([0-9]{2})(-|/)([0-9]{4})(\s+([0-9]{2}):([0-9]{2})(:([0-9]{2}))?)?$`', $str, $reg)) {
217*09b43617SGerry Weißbach				$d = array('y' => $reg[5], 'm' => $reg[3], 'd' => $reg[1], 'h' => $reg[7], 'i' => $reg[8], 's' => $reg[10]); // DD(-|/)MM(-|/)YYYY (HH:II(:SS)?)?
218*09b43617SGerry Weißbach				$matched = true;
219*09b43617SGerry Weißbach			}
220*09b43617SGerry Weißbach			if(preg_match('`^(([0-9]{2}):([0-9]{2})(:([0-9]{2}))?\s+)?([0-9]{2})(-|/)([0-9]{2})(-|/)([0-9]{4})$`', $str, $reg)) {
221*09b43617SGerry Weißbach				$d = array('y' => $reg[10], 'm' => $reg[8], 'd' => $reg[6], 'h' => $reg[2], 'i' => $reg[3], 's' => $reg[5]); // (HH:II(:SS)?)? DD(-|/)MM(-|/)YYYY
222*09b43617SGerry Weißbach				$matched = true;
223*09b43617SGerry Weißbach			}
224*09b43617SGerry Weißbach
225*09b43617SGerry Weißbach			// only month and year
226*09b43617SGerry Weißbach			if(preg_match('`^([0-9]{2})(-|/)([0-9]{4})$`', $str, $reg)) {
227*09b43617SGerry Weißbach				$d = array('y' => $reg[3], 'm' => $reg[1], 'd' => '', 'h' => '', 'i' => '', 's' => '');
228*09b43617SGerry Weißbach				$matched = true;
229*09b43617SGerry Weißbach			}
230*09b43617SGerry Weißbach			if(preg_match('`^([0-9]{4})(-|/)([0-9]{2})$`', $str, $reg)) {
231*09b43617SGerry Weißbach				$d = array('y' => $reg[1], 'm' => $reg[3], 'd' => '', 'h' => '', 'i' => '', 's' => '');
232*09b43617SGerry Weißbach				$matched = true;
233*09b43617SGerry Weißbach			}
234*09b43617SGerry Weißbach
235*09b43617SGerry Weißbach			// only year
236*09b43617SGerry Weißbach			if(preg_match('`^([0-9]{4})$`', $str, $reg)) {
237*09b43617SGerry Weißbach				$d = array('y' => $reg[1], 'm' => '', 'd' => '', 'h' => '', 'i' => '', 's' => '');
238*09b43617SGerry Weißbach				$matched = true;
239*09b43617SGerry Weißbach			}
240*09b43617SGerry Weißbach
241*09b43617SGerry Weißbach			// full time hours, minutes (opt) and seconds (opt)
242*09b43617SGerry Weißbach			// 11 : 11h
243*09b43617SGerry Weißbach			// 11:30 : 11h30min
244*09b43617SGerry Weißbach			// 11:30:27 : 11h30min27sec
245*09b43617SGerry Weißbach			if(preg_match('`^([0-9]{2})(:([0-9]{2})(:([0-9]{2}))?)?$`', $str, $reg)) {
246*09b43617SGerry Weißbach				$d = array('y' => '', 'm' => '', 'd' => '', 'h' => $reg[7], 'i' => $reg[8], 's' => $reg[10]); // YYYY(-|/)MM(-|/)DD (HH:II(:SS)?)?
247*09b43617SGerry Weißbach				$matched = true;
248*09b43617SGerry Weißbach			}
249*09b43617SGerry Weißbach
250*09b43617SGerry Weißbach			// custom datetime format : (XX(XX)?i\s?)+
251*09b43617SGerry Weißbach			if(preg_match('`^[0-9]{2}([0-9]{2})?\s?[ymdhis](\s?[0-9]{2}([0-9]{2})?\s?[ymdhis])*$`', $str, $reg)) {
252*09b43617SGerry Weißbach				while(preg_match('`^(([0-9]{2}([0-9]{2})?)\s?([ymdhis]))`', $str, $reg)) {
253*09b43617SGerry Weißbach					$v = $reg[2];
254*09b43617SGerry Weißbach					$i = $reg[4];
255*09b43617SGerry Weißbach					$str = substr($str, strlen($reg[1]));
256*09b43617SGerry Weißbach					if(($i != 'y') || (strlen($v) == 4)) $d[$i] = $v;
257*09b43617SGerry Weißbach				}
258*09b43617SGerry Weißbach				$matched = true;
259*09b43617SGerry Weißbach			}
260*09b43617SGerry Weißbach
261*09b43617SGerry Weißbach			if(!$matched) return false;
262*09b43617SGerry Weißbach			$same = true;
263*09b43617SGerry Weißbach			foreach($time as $k => $v) if(($d[$k] != '') && ($d[$k] != $v)) $same = false;
264*09b43617SGerry Weißbach			return $same;
265*09b43617SGerry Weißbach		}
266*09b43617SGerry Weißbach
267*09b43617SGerry Weißbach		// test IP
268*09b43617SGerry Weißbach		function test_IP($b, &$bug, $lop=false) {
269*09b43617SGerry Weißbach			if($lop) return array('\=\=?', 'eq', '\!\=', 'neq?', '\~\=');
270*09b43617SGerry Weißbach			$ip = clientIP(true);
271*09b43617SGerry Weißbach			if(!$b['test'] || ($b['test'] == '') || !$b['value'] || ($b['value'] == '') || ($ip == '0.0.0.0')) {
272*09b43617SGerry Weißbach				$bug = true;
273*09b43617SGerry Weißbach				return false;
274*09b43617SGerry Weißbach			}
275*09b43617SGerry Weißbach			switch($b['test']) {
276*09b43617SGerry Weißbach				case '=' :
277*09b43617SGerry Weißbach				case 'eq' :
278*09b43617SGerry Weißbach				case '==' :
279*09b43617SGerry Weißbach					return ($ip == $b['value']); break;
280*09b43617SGerry Weißbach				case '!=' :
281*09b43617SGerry Weißbach				case 'ne' :
282*09b43617SGerry Weißbach				case 'neq' :
283*09b43617SGerry Weißbach					return ($ip != $b['value']); break;
284*09b43617SGerry Weißbach				case '~=' :
285*09b43617SGerry Weißbach					return (strpos($ip, $b['value']) !== false); break;
286*09b43617SGerry Weißbach				default:
287*09b43617SGerry Weißbach					$bug = true;
288*09b43617SGerry Weißbach					return false;
289*09b43617SGerry Weißbach			}
290*09b43617SGerry Weißbach		}
291*09b43617SGerry Weißbach	}
292*09b43617SGerry Weißbach?>
293