-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.php
151 lines (132 loc) · 3.57 KB
/
tools.php
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
<?php
require_once "config.php";
/**
* Drops the log table
* @returns nothing
*/
function clear_log(){
$query="DROP TABLE ".DB_TABLE_LOG;
$result = mysql_query($query);
log_this("new log");
}
/**
* Checks a table named $table exists
* @params
* $table :string, name of the table to look for
* @returns true if the table exists, false otherwise
*/
function table_exists($table){
$table = mysql_escape_string($table);
$query = "SELECT * FROM $table";
$result = mysql_query($query);
return(mysql_errno() != 1146);
}
/**
* Creates a table named $table with columns $colums
* @params
* $table :string, name of the table
* $colums :string, columns of the table e.g. "name, age, id"
* @returns true on success, dies otherwiese
*/
function create_table($table, $columns){
$table = mysql_escape_string($table);
$columns = mysql_escape_string($columns);
$query = "CREATE TABLE $table ($columns)";
$result = mysql_query($query);
$error = mysql_errno();
if($error != 0){
if($table!=DB_TABLE_LOG){ //prevent infinite loop: must not try to log that logging doesn't work
log_this("error: tried to create table $table", mysql_error());
}else{
echo "Unable to log.\n";
}
die("MySQL error: ".$error);
}else{
log_this("success: created table $table", "($columns)");
return(true);
}
}
/**
* Appends a row to the log table
* @params
* $key :string, a short description of the data to be stored
* $data :the data to be stored
* @returns true on success, dies otherwise
*/
function log_this($key, $data=''){
$key = mysql_escape_string($key);
$data = mysql_escape_string($data);
if(!table_exists(DB_TABLE_LOG)){
create_table(DB_TABLE_LOG, "id INT AUTO_INCREMENT, time INT, label TEXT, data TEXT, PRIMARY KEY (id)");
}else{ // nothing is kept older than 3 days
$time=time()-60*60*24*3;
$query = "DELETE from ".DB_TABLE_LOG." WHERE time<$time";
$result = mysql_query($query);
if(mysql_errno != 0){
die("MySQL error: ".mysql_errno());
}
}
$query = "INSERT INTO ".DB_TABLE_LOG."(time, label, data) VALUES ( ".time().", \"$key\", \"$data\")";
$result = mysql_query($query);
if(mysql_errno != 0){
die("MySQL error: ".mysql_errno());
}else{
return(true);
}
}
/**
* Logs the output buffer and flushes it afterwards
*
*/
function log_and_flush(){
global $log;
$log .= ob_get_contents();
ob_flush();
}
/**
* Outputs a table
* @params
* $table :string, the name of the table
* @returns nothing
*/
function print_table($table){
$query = "SELECT * FROM ".$table;
$result = mysql_query($query);
while($result and $row = mysql_fetch_row($result)){
foreach($row as $key => $column){
$column = substr($column, 0, 180);
echo "$column\t\t\t\t\t";
}
echo "\n";
}
}
/**
* Decodes a JSON string
* @params
* $json :string, JSON to decode
* @returns an array corresponding to the JSON string
*/
function my_json_decode($json){ // found on php.net (and modified)
$comment = false;
$out = '$x=';
for ($i=0; $i<strlen($json); $i++)
{
if (!$comment){
if (in_array($json[$i], array('{', '['))) $out .= ' array(';
else if (in_array($json[$i], array('}', ']'))) $out .= ')';
else if ($json[$i] == ':') $out .= '=>';
else $out .= $json[$i];
}else{
$out .= $json[$i];
}
if ($json[$i] == '"' and ($json[$i-1] != "\\" )) $comment = !$comment;
}
if(eval($out . ';')===false){
log_this("error: my_json_decode", "$out");
return(array());
}else{
log_this("success: my_json_decode", $out);
return $x;
}
}
?>