dbcheck.php3 7.83 KB
Newer Older
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
<?php
#
# Stuff to support checking field data before we insert it into the DB.
#
define("TBDB_CHECKDBSLOT_NOFLAGS",	0x0);
define("TBDB_CHECKDBSLOT_WARN",		0x1);
define("TBDB_CHECKDBSLOT_ERROR",	0x2);

$DBFieldData   = 0;
$DBFieldErrstr = "";
function TBFieldErrorString() { global $DBFieldErrstr; return $DBFieldErrstr; }

#
# Download all data from the DB and store in hash for latter access.
# 
function TBGrabFieldData()
{
    global $DBFieldData;

    $DBFieldData = array();
    
    $query_result =
	DBQueryFatal("select * from table_regex");

    while ($row = mysql_fetch_assoc($query_result)) {
	$table_name  = $row["table_name"];
	$column_name = $row["column_name"];

	$DBFieldData[$table_name . ":" . $column_name] =
	    array("check"       => $row["check"],
		  "check_type"  => $row["check_type"],
		  "column_type" => $row["column_type"],
		  "min"         => $row["min"],
		  "max"         => $row["max"]);
    }
}

#
# Return the field data for a specific table/slot. If none, return the default
# entry.
#
function TBFieldData($table, $column, $flag = 0)
{
    global $DBFieldData;
    
    if (! $DBFieldData) {
	TBGrabFieldData();
    }
    $key = $table . ":" . $column;
    unset($toplevel);
    unset($fielddate);

    while (isset($DBFieldData[$key])) {
	$fielddata = $DBFieldData[$key];

	#
	# See if a redirect to another entry. 
	#
	if ($fielddata["check_type"] == "redirect") {
	    if (!isset($toplevel))
		$toplevel = $fielddata;
	    
	    $key = $fielddata["check"];
	    continue;
	}
	break;
    }
    if (!isset($fielddata)) {
	if ($flag) {
	    if ($flag & TBDB_CHECKDBSLOT_WARN) {
                # Warn TBOPS
		TBERROR("TBFieldData: No slot data for $table/$column!", 0);
	    }
	    if ($flag & TBDB_CHECKDBSLOT_ERROR)
		return 0;
	}
	$fielddata = $DBFieldData["default:default"];
    }
79
    # Return both values.
80
81
    if (isset($toplevel) &&
	($toplevel["min"] || $toplevel["max"])) {
82
	return array($fielddata, $toplevel);
83
    }
84
    return array($fielddata, NULL);
85
86
87
88
89
90
91
92
93
94
}

#
# Generic wrapper to check a slot. It is unfortunate that PHP
# does not allow pass by reference args to be optional. 
#
function TBcheck_dbslot($token, $table, $column, $flag = 0)
{
    global $DBFieldErrstr;

95
    list ($fielddata, $toplevel) = TBFieldData($table, $column, $flag);
96
97
98
99
100
101
102
103

    if (! $fielddata) {
	return 0;
    }
	
    $check       = $fielddata["check"];
    $check_type  = $fielddata["check_type"];
    $column_type = $fielddata["column_type"];
104
105
106
107
    $min         = (empty($toplevel) ? $fielddata["min"] : $toplevel["min"]);
    $max         = (empty($toplevel) ? $fielddata["max"] : $toplevel["max"]);
    $min = intval($min);
    $max = intval($max);
108
109
110
111
112
113
114
115
116

    #
    # Functional checks not implemented yet. 
    #
    if ($check_type == "function") {
	TBERROR("Functional DB checks not implemented! ".
		"$token, $table, $column", 1);
    }

117
118
119
120
121
122
123
    # Make sure the regex is anchored. Its a mistake not to be!
    if (substr($check, 0, 1) != "^")
	$check = "^" . $check;

    if (substr($check, -1, 1) != "\$")
	$check = $check . "\$";
    
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
153
154
155
156
157
158
159
160
    if (!preg_match("/$check/", "$token")) {
	$DBFieldErrstr = "Illegal characters";
	return 0;
    }

    switch ($column_type) {
        case "text":
	    if ((!$min && !max) ||
		(strlen("$token") >= $min && strlen("$token") <= $max))
		return 1;
	    break;
	    
        case "int":
        case "float":
	    # If both min/max are zero, then skip check; allow anything. 
	    if ((!$min && !max) || ($token >= $min && $token <= $max))
		return 1;
	    break;
	    
        default:
	    TBERROR("TBcheck_dbslot: Unrecognized column_type $column_type", 1);
    }

    #
    # Else fill in error string.
    # 
    switch ($column_type) {
        case "text":
	    if (strlen($token) < $min)
		$DBFieldErrstr = "too short - $min chars minimum";
	    else 
		$DBFieldErrstr = "too long - $max chars maximum";
	    break;
	    
        case "int":
        case "float":
	    if ($token < $min)
161
		$DBFieldErrstr = "$token too small - $min minimum value";
162
163
	    else 
		$DBFieldErrstr = "too large - $max maximum value";
164
	    break;
165
166
167
168
169
170
171
	    
        default:
	    TBERROR("TBcheck_dbslot: Unrecognized column_type $column_type", 1);
    }
    return 0;
}

Leigh B. Stoller's avatar
Leigh B. Stoller committed
172
173
174
175
176
177
# Handy default wrapper.
function TBvalid_slot($token, $table, $slot) {
    return TBcheck_dbslot($token, $table, $slot,
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# Handy wrappers for checking various fields.
function TBvalid_uid($token) {
    return TBcheck_dbslot($token, "users", "uid",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
function TBvalid_pid($token) {
    return TBcheck_dbslot($token, "projects", "pid",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
function TBvalid_gid($token) {
    return TBcheck_dbslot($token, "groups", "gid",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
function TBvalid_eid($token) {
    return TBcheck_dbslot($token, "experiments", "eid",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
function TBvalid_phone($token) {
    return TBcheck_dbslot($token, "users", "usr_phone",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
function TBvalid_usrname($token) {
    return TBcheck_dbslot($token, "users", "usr_name",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
function TBvalid_email($token) {
    return TBcheck_dbslot($token, "users", "usr_email",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
function TBvalid_userdata($token) {
    return TBcheck_dbslot($token, "default", "tinytext",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
function TBvalid_title($token) {
    return TBvalid_userdata($token);
}
function TBvalid_affiliation($token) {
    return TBvalid_userdata($token);
}
function TBvalid_addr($token) {
    return TBvalid_userdata($token);
}
function TBvalid_city($token) {
    return TBvalid_userdata($token);
}
function TBvalid_state($token) {
    return TBvalid_userdata($token);
}
function TBvalid_zip($token) {
    return TBvalid_userdata($token);
}
function TBvalid_country($token) {
    return TBvalid_userdata($token);
}
function TBvalid_description($token) {
    return TBvalid_userdata($token);
}
235
236
237
238
function TBvalid_why($token) {
    return TBcheck_dbslot($token, "projects", "why",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
239
240
241
function TBvalid_integer($token) {
    return TBcheck_dbslot($token, "default", "int",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
242
}
243
244
245
246
247
248
249
250
251
252
253
254
function TBvalid_tinyint($token) {
    return TBcheck_dbslot($token, "default", "tinyint",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
function TBvalid_boolean($token) {
    return TBcheck_dbslot($token, "default", "boolean",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
function TBvalid_float($token) {
    return TBcheck_dbslot($token, "default", "float",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
function TBvalid_num_members($token) {
    return TBcheck_dbslot($token, "projects", "num_members",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
function TBvalid_num_pcs($token) {
    return TBcheck_dbslot($token, "projects", "num_pcs",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
function TBvalid_num_pcplab($token) {
    return TBcheck_dbslot($token, "projects", "num_pcplab",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
function TBvalid_num_ron($token) {
    return TBcheck_dbslot($token, "projects", "num_ron",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
271
272
273
274
function TBvalid_osid($token) {
    return TBcheck_dbslot($token, "os_info", "osid",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
Leigh B. Stoller's avatar
Leigh B. Stoller committed
275
276
277
278
function TBvalid_node_id($token) {
    return TBcheck_dbslot($token, "nodes", "node_id",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
279
280
281
282
283
284
285
286
function TBvalid_imageid($token) {
    return TBcheck_dbslot($token, "images", "imageid",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}
function TBvalid_imagename($token) {
    return TBcheck_dbslot($token, "images", "imagename",
			  TBDB_CHECKDBSLOT_WARN|TBDB_CHECKDBSLOT_ERROR);
}