summaryrefslogtreecommitdiff
path: root/web/include/Tools.inc
blob: 430fe54eb527d90d015ccd1ac530b1400cdcf36f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?
	# vim:set ts=4:
	# vim:set shiftwidth=4:

	/*
	 * Backslide - 3node BTS
	 * (c) 2002 Florian Reitmeir/3node
	 *          Peter Palfrader/3node
	 *
	 * $Id: Tools.inc,v 1.2 2002/05/28 14:31:24 weasel Exp $
	 */
#
# Todo:
#	* Dokumemtation
#
function error($msg)
{

	print "Error: $msg\n";
	exit;
}

function redirect($ns, $name='', $vars = array())
{
	if (sizeof($vars)>0) {
		foreach ($vars as $key=>$value) {
			$param[] = $key.'='.urlencode($value);
		}
		$pars = '?' . implode('&', $param);
	} else {
		$pars = '';
	}

	if ($name != '') {
		Header('Location: '.$ns->config->baseurl.'/'.$name . $pars);
	} elseif (isset ($GLOBALS['followup_to'])) {
		Header('Location: '.$GLOBALS['followup_to'] . $pars);
	} elseif (isset ($GLOBALS['HTTP_REFERER'])) {
		Header('Location: '.$GLOBALS['HTTP_REFERER'] . $pars);
	} else {
		error('Fehler: Weiss nicht mehr wohin jetzt.');
	};
}

function param_check(&$parameters, &$new_check_fields, $optionals=array())
{
	$check_parameters = array();

	$params_required = isset($optionals['params_required']) ? $optionals['params_required']: true;
	$form_id = isset($optionals['form_id']) ? $optionals['form_id'] : $parameters['form_id'];

	if ( ! isset($form_id) )
		if ( $params_required )
			error('Formular ID: not set');
		else {
			$form_id='default';
		}

	if ( $form_id == 'default' && ! isset($new_check_fields[$form_id]) )
		return $check_parameters;

	if ( ! isset($new_check_fields[$form_id]) ) 
		error("Formular ID: is set ('$form_id') but no ruleset defined");

	assert($form_id !== false );
	assert($new_check_fields[$form_id] !== false);
	
	$check_fields = $new_check_fields[$form_id];
	foreach( $check_fields as $field_name => $field ) {

		if ( !isset($parameters[$field_name]) && $field['type'] != 'boolean' ) {
			if (!$field['optional'] )
				error('Parameter '.$field_name.' not found'); 
		} else {
			if ( isset($field['length']) && strlen($parameters[$field_name])>$field['length'] )
				error('Parameter '.$field_name.' to long');

			unset($result);
			switch ($field['type']) {
				case 'integer':
					if (!(is_numeric($parameters[$field_name]) && is_int($parameters[$field_name] + 0)))
						error($field_name . ' is not an Integer');
					$result=$parameters[$field_name] + 0;
					break;
				case 'decimal':
					if (! isset($field['scale']) )
						$field['scale'] = 0;
					assert(isset($field['precision']));
					assert(isset($field['scale']));
					$vorkomma = '[0-9]{0,'.($field['precision'] - $field['scale']).'}';
					$nachkomma = '[0-9]{0,'.($field['scale']).'}';
					$regex = $vorkomma . '(\.'.$nachkomma.')?';
					$matches = preg_match('/^'.$regex.'$/', $parameters[$field_name]);
					if (! $matches)
						error($field_name . " is not Decimal (matching $regex)");
					$result=$parameters[$field_name];
					if ($result === '')
						$result = 0;
					break;
				case 'string':
					if (!is_string($parameters[$field_name]))
						error($field_name . ' is not a String');
					$result=trim($parameters[$field_name]);
					break;
				case 'boolean':
					if ($parameters[$field_name])
						$result=true;
					else
						$result=false;
					break;
				case 'anything':
					$result=$parameters[$field_name];
					break;
				default:
					error('unkown Type '.$field['type'].' in Ruleset '.$form_id.' for field '.$field_name);
			}

			assert(isset($result));
			if ( isset( $field['modifier'] ) ) 
				if ( is_array($field['modifier']) ) 
					foreach ($field['modifier'] as $modifier) {
						assert(is_callable($modifier));
						$result = $modifier($result);	
					}
				else {
					assert(is_callable($field['modifier']));
					$result = $field['modifier']($result);
				}
					
			if (! $field['discard'])
				$check_parameters[$field_name] = $result;
		}
	}
	$check_parameters['form_id']=$form_id;
	return $check_parameters;
}

function print_array(&$fields, $die = 0)
{
	echo "<pre>";
	print_r($fields);
	echo "XXXXXXXXXXXX</pre>";

	if ($die)
		exit;
}

# vim:set ts=4:
# vim:set syntax=php:
# vim:set shiftwidth=4:

?>