aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/grid/tests/support/data.php
blob: 1beb6f044f5f4c5ae5d57d253294647494e0e981 (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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
	// db settings
	$dbserver = 'localhost';
	$dbuser = 'root';
	$dbpassword = 'root';
	
	error_reporting(E_ALL);
	
	/*
		Simple protocol:
			- Inputs via POST variables. 
			- Output is a string that can be evaluated into a JSON
			  First element of the array contains return status.
				
		This simplified tutorial code should not be deployed without a security review.
	*/
	
	@include "json.php";
	
	// set up response encoding 
	header("Content-Type: text/html; charset=utf-8");

	// util
	function getPostString($inName) {
		// make sure input strings are 'clean'
		return mysql_real_escape_string(@$_POST[$inName]);
	}
		
	// used for json encoding
	$json = new Services_JSON();
	
	function echoJson($inData) {
		global $json;
		// delay in ms
		$delay = getPostString('delay');
		if (!empty($delay))
			usleep($delay * 1000);
		echo '/* ' . $json->encode($inData) . ' */';
	}
	
	function error($inMessage) {
		$inMessage = str_replace('"', '\\"', $inMessage);
		error_log($inMessage);
		//echo '/* ({error: true, message: "' . $inMessage . '"}) */';
		echoJson(array('error' => true, 'message' => $inMessage));
		exit;
	}


	function getArray($inResult, $inArray="true") {
		$o = Array();
		while ($row = ($inArray ? mysql_fetch_row($inResult) : mysql_fetch_object($inResult)))
			$o[] = $row;
		return $o;	
	}
	
	// connect to DB
	mysql_connect($dbserver, $dbuser, $dbpassword);

	// select DB
	$database = getPostString("database");
	$database = ($database ? $database : $db);
	if (!mysql_select_db($database))
		error('failed to select db: ' . mysql_error());

	// select table
	$table = getPostString("table");
	$table = ($table ? $table : $dbtable);

	// cache
	$colCache = NULL;
	$pkCache = NULL;

	// set UTF8 output (MySql > 4.0)
	mysql_query("SET NAMES UTF8");
	
	// server, database, table meta data
	function getDatabases() {
		$result = mysql_query("SHOW DATABASES");
		$output = Array();
		while ($row = mysql_fetch_row($result)) {
			$r = strtolower($row[0]);
			if ($r != 'mysql' && $r != 'information_schema')
				$output[] = $row[0];
		}	
		return $output;	
	}
	
	function getTables() {
		global $database;
		$result = mysql_query("SHOW TABLES FROM $database");
		$output = Array();
		while ($row = mysql_fetch_row($result))
			$output[] = $row[0];
		return $output;	
	}
	
	function getColumns() {
		global $table, $colCache;
		if (!$colCache) {
			$result = mysql_query("SHOW COLUMNS FROM `$table`");
			return getArray($result, false);
			$colCache = getArray($result, false);
		}
		return $colCache;	
	}
	
	// returns object: $this->name, $this->index
	function getPk() {
		global $pkCache;
		if (!$pkCache) {
			$k = '';
			$columns = getColumns();
			for ($i=0; $i < count($columns); $i++) {
				$c = $columns[$i];
				if ($c->Key == 'PRI') {
					$k = $c->Field;
					break;
				}	
			}
			$pkCache->index = $i;
			$pkCache->name = $k;
		}	
		return $pkCache;
	}
	
	function getTableInfo() {
		global $table, $database;
		$c = getColumns();
		$r = rowcount();
		return array("count" => $r, "columns" => $c, "database" => $database, "table" => $table);
	}
	
	function getOldPostPkValue() {
		$pk = getPk();
		return getPostString('_o' . $pk->index);
	}
	
	function getNewPostPkValue() {
		$pk = getPk();
		return getPostString('_' . $pk->index);
	}
	
	function getPostColumns() {
		$columns = getColumns();
		for ($i=0, $a=array(), $p; (($p=getPostString("_".$i)) != ''); $i++) {
			$r = new stdClass();
			$r->name = $columns[$i]->Field;
			$r->value = $p;
			$a[] = $r;
		}	
		return $a;
	}
	
	function getOrderBy() {
		$ob = getPostString("orderby");
		if (is_numeric($ob)) {
			$columns = getColumns();
			$ob = $columns[intval($ob)-1]->Field;
		}
		return $ob;
	}
	
	function getWhere() {
		$w = getPostString("where");
		return ($w ? " WHERE $w" : "");
	}
	
	// basic operations
	function rowcount()	{
		global $table;
		$query = "SELECT COUNT(*) FROM `$table`" . getWhere();
		$result = mysql_query($query);
		if (!$result)
			error("failed to perform query: $query. " . mysql_error());
		if ($row = mysql_fetch_row($result))
			return $row[0];
		else
			return 0;
	}
	
	function select($inQuery = '') {
		global $table;
		// built limit clause
		$lim = (int)getPostString("limit");
		$off = (int)getPostString("offset");
		$limit = ($lim || $off ? " LIMIT $off, $lim" : "");
		// build order by clause
		$desc = (boolean)getPostString("desc");
		$ob = getOrderBy();
		$orderby = ($ob ? " ORDER BY `" . $ob . "`" . ($desc ? " DESC" : "") : "");
		// build query
		$query = ($inQuery ? $inQuery : "SELECT * FROM `$table`" . getWhere() . $orderby . $limit);
		// execute query
		if (!$result = mysql_query($query))
			error("failed to perform query: $query. " . mysql_error());
		// fetch each result row 
		return getArray($result);
	}

	function reflectRow() {
		global $table;
		$pk = getPk();
		$key = getNewPostPkValue();			
		$where = "`$pk->name`=\"$key\"";
		return select("SELECT * FROM `$table` WHERE $where LIMIT 1");
	}
	
	function update() {
		// build set clause
		for ($i=0, $set = array(), $cols = getPostColumns(), $v; ($v=$cols[$i]); $i++)
			$set[] = "`$v->name` = '$v->value'";
		$set = implode(', ', $set);
		// our table
		global $table;
		// build query
		$pk = getPk();
		$pkValue = getOldPostPkValue();
		$query = "UPDATE `$table` SET $set WHERE `$pk->name` = '$pkValue' LIMIT 1";
		// execute query
		if (!mysql_query($query))
			error("failed to perform query: [$query]. " .
					"MySql says: [" . mysql_error() ."]");
		else {
			return reflectRow();
		}	
	}
	
	function insert() {
		global $table;
		// build values clause
		for ($i=0, $values = array(), $cols = getPostColumns(), $v; ($v=$cols[$i]); $i++)
			$values[] = $v->value;
		$values = '"' . implode('", "', $values) . '"';			
		// build query
		$query = "INSERT INTO `$table` VALUES($values)";
		// execute query
		if (!mysql_query($query))
			error("failed to perform query: [$query]. " .
					"MySql says: [" . mysql_error() ."]");
		else {
			return reflectRow();
		}
	}
	
	function delete() {
		global $table;
		// build query
		$n = getPostString("count");
		$pk = getPk();
		for ($i = 0, $deleted=array(); $i < $n; $i++) {
			$key = getPostString("_$i");
			array_push($deleted, $key);
			$query = "DELETE FROM `$table` WHERE `$pk->name`=\"$key\" LIMIT 1";
			// execute query
			if (!mysql_query($query) || mysql_affected_rows() != 1)
				error("failed to perform query: [$query]. " .
					"Affected rows: " . mysql_affected_rows() .". " . 
					"MySql says: [" . mysql_error() ."]");
		}	
		return $deleted;			
	}
	
	// find (full text search)
	function findData($inFindCol, $inFind, $inOrderBy, $inFullText) {
		global $table;
		$where = ($inFullText ? "WHERE MATCH(`$inFindCol`) AGAINST ('$inFind')" : "WHERE $inFindCol LIKE '$inFind'");
		$query = "SELECT * FROM $table $where $inOrderBy";
		$result = mysql_query($query);
		// return rows
		return getArray($result);
	}
	
	// binary search through sorted data, supports start point ($inFindFrom) and direction ($inFindForward)
	function findRow($inData, $inFindFrom=-1, $inFindForward) {
		$b = -1;
		$l = count($inData);
		if (!$inData)
			return $b;
		if (!$inFindFrom==-1 || $l < 2)
			$b = 0;
		else {
			// binary search
			$t = $l-1;
			$b = 0;
			while ($b <= $t) {
				$p = floor(($b+$t)/2);
				$d = $inData[$p][0];
				if ($d < $inFindFrom)
					$b = $p + 1;
				else if ($d > $inFindFrom)
					$t = $p - 1;
				else {
					$b = $p;
					break;
				}	
			}	
			if ($inFindFrom == $inData[$b][0]) {
				// add or subtract 1
				$b = ($inFindForward ? ($b+1 > $l-1 ? 0 : $b+1) : ($b-1 < 0 ? $l-1 : $b-1) );
			}	
			else if (!$inFindForward)
				// subtract 1
				$b = ($b-1 < 0 ? $l-1 : $b-1);
		}	
		return $inData[$b][0];
	}
	
	function buildFindWhere($inFindData, $inKey, $inCol) {
		$o = Array();
		foreach($inFindData as $row)
			$o[] = $inCol . "='" . $row[$inKey] . "'";
		return (count($o) ? ' WHERE ' . implode(' OR ', $o) : '');
	}
		
	function find($inFindCol, $inFind='', $inOb='', $inFindFrom=0, $inFindForward=true, $inFullText=true) {
		global $table;
		// build order by clause
		$desc = (boolean)getPostString("desc");
		if (!$inOb)
			$inOb = getOrderBy();
		if ($inOb)
			$inOb = "`" . $inOb . "`"	;
		$orderby = ($inOb ? " ORDER BY $inOb " . ($desc ? " DESC" : "") : "");
		// update inputs from post
		if (!$inFind)
			$inFind = getPostString('findText');
		if (!$inFindCol)
			$inFindCol = getPostString('findCol');	
		if (empty($inFindFrom))
			$inFindFrom = getPostString('findFrom');
		$ff = getPostString('findForward');
		if ($ff)
			$inFindForward = (strtolower($ff) == 'true' ? true : false);
		$ft = getPostString('findFullText');
		if ($ft)
			$inFullText = (strtolower($ft) == 'true' ? true : false);	
		
		// get find data
		$f = findData($inFindCol, $inFind, $orderby,  $inFullText);
		$pk = getPk();

		// execute query
		$where = buildFindWhere($f, $pk->index, 'f');
		$query = "SELECT Row, f FROM (SELECT @row := @row + 1 AS Row, $pk->name as f FROM `$table` $orderby) AS tempTable $where";
		mysql_query('SET @row = -1;');
		if (!$result = mysql_query($query))
			error("failed to perform query: $query. " . mysql_error());
		
		// return row number 
		return findRow(getArray($result), $inFindFrom, $inFindForward);
	}
	
	// our command list
	$cmds = array( 
		"count" => "rowcount", 
		"select" => "select",
		"update" => "update",
		"insert" => "insert",
		"delete" => "delete",
		"find" => "find",
		"databases" => "getDatabases",
		"tables" => "getTables",
		"columns" => "getColumns",
		"info" => "getTableInfo"
	);
		
	// process input params
	$cmd = @$_POST["command"];
	
	//$cmd="select";
	
	// dispatch command
	$func = @$cmds[$cmd];
	if (function_exists($func)) 
		echoJson(call_user_func($func));
	else
		error("bad command");
?>