-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUtils.php
249 lines (191 loc) · 5.68 KB
/
Utils.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
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
<?php
class Utils {
static function arrayFind($xs, $f) {
foreach ($xs as $x) {
if (call_user_func($f, $x) === true)
return $x;
}
return null;
}
static function cleanQueryString($removes) {
$get = filter_input_array(INPUT_GET, FILTER_SANITIZE_STRING);
if ($get) {
foreach($removes as $remove) {
unset($get[$remove]);
}
return http_build_query( $get );
} else {
return "";
}
}
static function isValidDecimal($input) {
$decimalPoint = Chain::AMOUNT_DECIMAL_POINT;
$integerLen = Chain::AMOUNT_INTEGER_LEN;
$totalLen = $decimalPoint + $integerLen + 1/* dot */;
if (!preg_match('/^[0-9]{1,'.$integerLen.'}(\.[0-9]{1,'.$decimalPoint.'})?$/', $input)) {
return false;
}
return true;
}
static function leftPadding($hexStr, $len) {
return str_pad($hexStr, $len, "0", STR_PAD_LEFT);
}
static function bchexdec($hex) {
if(strlen($hex) == 1) {
return hexdec($hex);
} else {
$remain = substr($hex, 0, -1);
$last = substr($hex, -1);
return bcadd(bcmul(16, self::bchexdec($remain)), hexdec($last));
}
}
static function bcdechex($dec) {
$last = bcmod($dec, 16);
$remain = bcdiv(bcsub($dec, $last), 16);
if($remain == 0) {
return dechex($last);
} else {
return self::bcdechex($remain).dechex($last);
}
}
static function printOut($content) {
echo "[".date("Y-m-d H:i:s")."] " . $content . "\n";
}
static function config($key) {
return $key ? $GLOBALS['config'][$key] : $GLOBALS['config'];
}
static function world($key = null, $value = null) {
if($value!==null) {
$GLOBALS['world'][$key] = $value;
}
return $key !== null ? $GLOBALS['world'][$key] : $GLOBALS['world'];
}
static function stringEach($value) {
return (string)$value;
}
static function stringDeep($value) {
return (is_array($value)) ? array_map("self::stringDeep", $value) : self::stringEach($value);
}
static function jsonDecode($var) {
return json_decode($var, true);
}
static function jsonEncode($var) {
$var = json_encode($var);
$var = self::jsonDecode($var);
$var = self::stringDeep($var);
$var = json_encode($var);
return $var;
}
static function safeSub($a, $b) {
return bcsub($a,$b,Chain::AMOUNT_DECIMAL_POINT);
}
static function safeAdd($a, $b) {
return bcadd($a,$b,Chain::AMOUNT_DECIMAL_POINT);
}
static function safeMul($a, $b) {
return bcmul($a,$b,Chain::AMOUNT_DECIMAL_POINT);
}
static function safeDiv($a, $b) {
return bcdiv($a,$b,Chain::AMOUNT_DECIMAL_POINT);
}
static function safeComp($a, $b) {
return (int)(bccomp($a,$b,Chain::AMOUNT_DECIMAL_POINT));
}
static function safeMin($a,$b) {
if (Utils::safeComp($a,$b)>=0) {
return $b;
} else {
return $a;
}
}
}
class DB {
public static $conn = null;
public static $host = null;
public static $user = null;
public static $pass = null;
public static $dbname = null;
public static function connect($host, $user, $pass, $dbname = null) {
self::$host = $host;
self::$user = $user;
self::$pass = $pass;
self::$dbname = $dbname;
self::$conn = mysqli_connect($host, $user, $pass, $dbname);
if (mysqli_connect_errno()) {
$errmsg .= ("Failed to connect to MySQL: " . mysqli_connect_error());
} else if (!mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT)) {
$errmsg .= ("Failed to set mysqli report");
} else if (!mysqli_autocommit(self::$conn, TRUE)) {
$errmsg .= ("Failed to set mysqli autocommit");
} else if (!mysqli_set_charset(self::$conn,"utf8")) {
$errmsg .= ("Failed to set charset");
}
if ($errmsg) {
throw new mysqli_sql_exception($errmsg);
}
}
public static function close() {
mysqli_close(self::$conn);
}
public static function result($res,$row=0,$col=0){
$numrows = mysqli_num_rows($res);
if ($numrows && $row <= ($numrows-1) && $row >=0){
mysqli_data_seek($res,$row);
$resrow = (is_numeric($col)) ? mysqli_fetch_row($res) : mysqli_fetch_assoc($res);
if (isset($resrow[$col])){
return $resrow[$col];
}
}
return false;
}
public static function esc($str) {
return mysqli_real_escape_string(self::$conn,$str);
}
public static function errno() {
$errno = mysqli_errno(self::$conn);
return $errno;
}
public static function error($sql) {
$err = mysqli_error(self::$conn);
if ($err) {
return "{$sql}, Error: {$err}";
} else {
return "";
}
}
public static function beginTransaction() {
mysqli_begin_transaction(self::$conn);
}
public static function rollback() {
mysqli_rollback(self::$conn);
}
public static function commit() {
mysqli_commit(self::$conn);
}
public static function affectedRows() {
$r = mysqli_affected_rows(self::$conn);
return $r;
}
public static function query($sql) {
try {
$r = mysqli_query(self::$conn,$sql);
} catch(mysqli_sql_exception $e) {
$errno = self::errno();
if ($errno == 2006 or $errno == 2013 ) {
try {
self::connect(self::$host, self::$user, self::$pass, self::$dbname);
$r = mysqli_query(self::$conn,$sql);
} catch(Exception $e) {
throw new mysqli_sql_exception($e->getMessage()." (Caught Mysqli Errno: {$errno})");
}
} else {
throw new mysqli_sql_exception($e->getMessage()." (Caught Mysqli Errno: {$errno})");
}
}
return $r;
}
public static function insertID() {
$insertId = mysqli_insert_id(self::$conn);
return $insertId;
}
}