commit | author | age
|
175a52
|
1 |
<?php |
JK |
2 |
if(!extension_loaded('mysql')) { |
|
3 |
error::add('Brak rozszerzenia MySQL. Skrypt nie będzie działał.'); |
|
4 |
} |
|
5 |
|
|
6 |
class sql { |
|
7 |
static $db; |
|
8 |
static $queries = 0; |
|
9 |
|
|
10 |
static function connect() { |
|
11 |
# !!! |
|
12 |
# TUTAJ USTAW DANE LOGOWANIA DO BAZY: |
|
13 |
self::$db = @mysql_connect('host', 'uzytkownik', 'haslo'); |
|
14 |
if(!self::$db) { |
|
15 |
error::add(mysql_error()); |
|
16 |
} |
|
17 |
|
|
18 |
self::query('SET CHARACTER SET \'UTF8\''); |
|
19 |
self::query('SET NAMES \'UTF8\''); |
|
20 |
|
|
21 |
self::$queries = 0; |
|
22 |
|
|
23 |
# !!! |
|
24 |
# TUTAJ USTAW NAZWĘ BAZY |
|
25 |
if(!@mysql_select_db('baza')) { |
|
26 |
error::add(mysql_error()); |
|
27 |
} |
|
28 |
} |
|
29 |
|
|
30 |
static function query($q) { |
|
31 |
if(!self::$db) { |
|
32 |
self::connect(); |
|
33 |
} |
|
34 |
|
|
35 |
if(!@mysql_ping(self::$db)) { |
|
36 |
self::connect(); |
|
37 |
} |
|
38 |
|
|
39 |
$r = @mysql_query($q, self::$db); |
|
40 |
|
|
41 |
self::$queries++; |
|
42 |
|
|
43 |
if($r===FALSE) { |
|
44 |
error::add(mysql_error().' '.$q); |
|
45 |
} |
|
46 |
|
|
47 |
return $r; |
|
48 |
} |
|
49 |
|
|
50 |
static function fetchonea($q) { |
|
51 |
return mysql_fetch_assoc($q); |
|
52 |
} |
|
53 |
|
|
54 |
static function fetchone($q) { |
|
55 |
return mysql_fetch_array($q); |
|
56 |
} |
|
57 |
|
|
58 |
static function fetch($q) { |
|
59 |
while ($entry = mysql_fetch_array($q)) { |
|
60 |
$r[] = $entry; |
|
61 |
} |
|
62 |
|
|
63 |
if(!$r) { |
|
64 |
$r = array(); |
|
65 |
} |
|
66 |
|
|
67 |
return $r; |
|
68 |
} |
|
69 |
|
|
70 |
static function increment_id() { |
|
71 |
return mysql_insert_id(self::$db); |
|
72 |
} |
|
73 |
|
|
74 |
static function affected() { |
|
75 |
return mysql_affected_rows(self::$db); |
|
76 |
} |
|
77 |
|
|
78 |
static function escape($q) { |
|
79 |
if(!self::$db) { |
|
80 |
self::connect(); |
|
81 |
} |
|
82 |
|
|
83 |
if(!@mysql_ping(self::$db)) { |
|
84 |
self::connect(); |
|
85 |
} |
|
86 |
|
|
87 |
return mysql_real_escape_string($q, self::$db); |
|
88 |
} |
|
89 |
|
|
90 |
static function close() { |
|
91 |
mysql_close(self::$db); |
|
92 |
} |
|
93 |
} |
|
94 |
|
|
95 |
class db2 { |
|
96 |
// FALSE - Allows WHERE queries other than "is equal" |
|
97 |
// LIKE, MATCH ... AGAINST, !=, >, <, IS NULL ... |
|
98 |
static $SAFE_MODE_WHERE = FALSE; |
|
99 |
|
|
100 |
// FALSE - Allows SELECT * |
|
101 |
static $SAFE_MODE_SELECT = FALSE; |
|
102 |
|
|
103 |
// FALSE - Skipping empty fields in INSERTs |
|
104 |
static $SAFE_MODE_INSERT = FALSE; |
|
105 |
|
|
106 |
// FALSE - Empty field is stated as NULL |
|
107 |
static $SAFE_MODE_UPDATE = FALSE; |
|
108 |
|
|
109 |
// FALSE - Allows JOINs |
|
110 |
static $SAFE_MODE_TABLE = FALSE; |
|
111 |
|
|
112 |
// FALSE - Allow `table`.`key` using table.key |
|
113 |
static $SAFE_MODE_KEY = FALSE; |
|
114 |
|
|
115 |
// Do htmlspecialchars() on db2::get results? |
|
116 |
static $ESCAPE_DATA = TRUE; |
|
117 |
|
|
118 |
static $revelance = FALSE; |
|
119 |
|
|
120 |
static function revelance() { |
|
121 |
return (bool)self::$revelance; |
|
122 |
} |
|
123 |
|
|
124 |
static function __combine_insert($keys) { |
|
125 |
foreach($keys as $key => $value) { |
|
126 |
if(empty($key)) { |
|
127 |
continue; |
|
128 |
} |
|
129 |
|
|
130 |
if(!self::$SAFE_MODE_INSERT AND $value==='') { |
|
131 |
continue; |
|
132 |
} |
|
133 |
|
|
134 |
$a[] = '`'.sql::escape($key).'`'; |
|
135 |
$b[] = '\''.sql::escape($value).'\''; |
|
136 |
} |
|
137 |
|
|
138 |
return '('.implode(', ', $a).') VALUES ('.implode(', ', $b).')'; |
|
139 |
} |
|
140 |
|
|
141 |
static function __combine_update($keys) { |
|
142 |
foreach($keys as $key => $value) { |
|
143 |
if(empty($key)) { |
|
144 |
continue; |
|
145 |
} |
|
146 |
|
|
147 |
if(!self::$SAFE_MODE_UPDATE AND $value==='') { |
|
148 |
$value = NULL; |
|
149 |
} |
|
150 |
if(is_null($value)) { |
|
151 |
$a[] = '`'.sql::escape($key).'`=NULL'; |
|
152 |
} |
|
153 |
else |
|
154 |
{ |
|
155 |
$a[] = '`'.sql::escape($key).'`=\''.sql::escape($value).'\''; |
|
156 |
} |
|
157 |
} |
|
158 |
|
|
159 |
return implode(', ', $a); |
|
160 |
} |
|
161 |
|
|
162 |
static function __combine_select($keys, $revelance=FALSE) { |
|
163 |
if(!self::$SAFE_MODE_SELECT && $keys=='*') { |
|
164 |
$a[] = '*'; |
|
165 |
} |
|
166 |
else |
|
167 |
{ |
|
168 |
if(!is_array($keys)) { |
|
169 |
$keys = array($keys); |
|
170 |
} |
|
171 |
|
|
172 |
foreach($keys as $v) { |
|
173 |
$a[] = self::__combine_keyn($v); |
|
174 |
if($one) break; |
|
175 |
} |
|
176 |
|
|
177 |
} |
|
178 |
|
|
179 |
if($revelance && self::$revelance) { |
|
180 |
$a[] = self::$revelance; |
|
181 |
} |
|
182 |
|
|
183 |
return implode(', ', $a); |
|
184 |
} |
|
185 |
|
|
186 |
static function __combine_where($keys, $revelance=FALSE) { |
|
187 |
self::$revelance = FALSE; |
|
188 |
|
|
189 |
$implode = ' AND '; |
|
190 |
|
|
191 |
if(!is_array($keys) OR empty($keys)) { |
|
192 |
return ''; |
|
193 |
} |
|
194 |
|
|
195 |
if(self::$SAFE_MODE_WHERE) { |
|
196 |
foreach($keys as $key => $value) { |
|
197 |
if(is_null($value)) { |
|
198 |
$a[] = self::__combine_keyn($key).' IS NULL'; |
|
199 |
} |
|
200 |
else |
|
201 |
{ |
|
202 |
$a[] = self::__combine_keyn($key).'=\''.sql::escape($value).'\''; |
|
203 |
} |
|
204 |
} |
|
205 |
} |
|
206 |
else |
|
207 |
{ |
|
208 |
$a = array(); |
|
209 |
foreach($keys as $key => $v) { |
|
210 |
if(!is_array($v)) { |
|
211 |
$v = array($v); |
|
212 |
} |
|
213 |
|
|
214 |
foreach($v as $value) { |
|
215 |
if($key === 'OR') { |
|
216 |
$implode = ' OR '; |
|
217 |
} |
|
218 |
elseif(substr($key, -1)=='!' AND is_null($value) OR $value==='') { |
|
219 |
$a[] = self::__combine_keyn(substr($key, 0, -1)).' IS NOT NULL'; |
|
220 |
} |
|
221 |
elseif(is_null($value) OR $value==='') { |
|
222 |
$a[] = self::__combine_keyn($key).' IS NULL'; |
|
223 |
} |
|
224 |
elseif(substr($key, -1)=='!') { |
|
225 |
$a[] = self::__combine_keyn(substr($key, 0, -1)).'!=\''.sql::escape($value).'\''; |
|
226 |
} |
|
227 |
elseif($key=='^') { |
|
228 |
$a[] = 'MAX('.self::__combine_keyn($value).')'; |
|
229 |
} |
|
230 |
elseif(substr($key, -2)=='~~') { |
|
231 |
$temp = 'MATCH ('.self::__combine_keyn(substr($key, 0, -2)).') AGAINST (\''.sql::escape($value).'\')'; |
|
232 |
if($revelance) { |
|
233 |
self::$revelance = $temp.' AS `revelance`'; |
|
234 |
} |
|
235 |
|
|
236 |
$a[] = $temp; |
|
237 |
} |
|
238 |
elseif(substr($key, -1)=='~') { |
|
239 |
$a[] = self::__combine_keyn(substr($key, 0, -1)).' LIKE \''.sql::escape($value).'\''; |
|
240 |
} |
|
241 |
elseif(substr($key, -2)=='>=') { |
|
242 |
$a[] = self::__combine_keyn(substr($key, 0, -2)).'>=\''.sql::escape($value).'\''; |
|
243 |
} |
|
244 |
elseif(substr($key, -2)=='<=') { |
|
245 |
$a[] = self::__combine_keyn(substr($key, 0, -2)).'<=\''.sql::escape($value).'\''; |
|
246 |
} |
|
247 |
elseif(substr($key, -1)=='>') { |
|
248 |
$a[] = self::__combine_keyn(substr($key, 0, -1)).'>\''.sql::escape($value).'\''; |
|
249 |
} |
|
250 |
elseif(substr($key, -1)=='<') { |
|
251 |
$a[] = self::__combine_keyn(substr($key, 0, -1)).'<\''.sql::escape($value).'\''; |
|
252 |
} |
|
253 |
else |
|
254 |
{ |
|
255 |
$a[] = self::__combine_keyn($key).'=\''.sql::escape($value).'\''; |
|
256 |
} |
|
257 |
} |
|
258 |
} |
|
259 |
} |
|
260 |
|
|
261 |
return ' WHERE '.implode($implode, $a).$addon; |
|
262 |
} |
|
263 |
|
|
264 |
static function __combine_limit($limit, $stop) { |
|
265 |
if(!ctype_digit($limit) AND !is_int($limit)) { |
|
266 |
return ''; |
|
267 |
} |
|
268 |
elseif(!ctype_digit($stop) AND !is_int($stop)) { |
|
269 |
return ' LIMIT '.$limit; |
|
270 |
} |
|
271 |
else |
|
272 |
{ |
|
273 |
return ' LIMIT '.$limit.', '.$stop; |
|
274 |
} |
|
275 |
} |
|
276 |
|
|
277 |
static function __combine_order($keys, $revelance=FALSE) { |
|
278 |
if(empty($keys)) { |
|
279 |
return ''; |
|
280 |
} |
|
281 |
|
|
282 |
if(!is_array($keys)) { |
|
283 |
$keys = array($keys => 'ASC'); |
|
284 |
} |
|
285 |
|
|
286 |
foreach($keys as $key => $value) { |
|
287 |
$bvalue = strtoupper($value); |
|
288 |
if($bvalue != 'ASC' AND $bvalue != 'DESC') { |
|
289 |
$key = $value; |
|
290 |
$value = 'ASC'; |
|
291 |
} |
|
292 |
else |
|
293 |
{ |
|
294 |
$value = $bvalue; |
|
295 |
} |
|
296 |
|
|
297 |
if((!$revelance OR !self::$revelance) AND $key=='revelance') { |
|
298 |
continue; |
|
299 |
} |
|
300 |
|
|
301 |
$a[] = self::__combine_keyn($key).' '.$value; |
|
302 |
} |
|
303 |
|
|
304 |
return ' ORDER BY '.implode(', ', $a); |
|
305 |
} |
|
306 |
|
|
307 |
private static function __combine_tablelist($key, $value) { |
|
308 |
$as = FALSE; |
|
309 |
if(!is_int($key)) { |
|
310 |
$as = $value; |
|
311 |
$value = $key; |
|
312 |
} |
|
313 |
return '`'.sql::escape($value).'`'.($as ? ' AS `'.sql::escape($as).'`' : ''); |
|
314 |
} |
|
315 |
|
|
316 |
static function __combine_keyn($key) { |
|
317 |
if(!self::$SAFE_MODE_KEY AND strpos($key, '.')!==FALSE) { |
|
318 |
$key = explode('.', $key, 2); |
|
319 |
return '`'.sql::escape($key[0]).'`.`'.sql::escape($key[1]).'`'; |
|
320 |
} |
|
321 |
|
|
322 |
return '`'.sql::escape($key).'`'; |
|
323 |
} |
|
324 |
|
|
325 |
static function __combine_table($table) { |
|
326 |
if(!is_array($table) OR self::$SAFE_MODE_TABLE) { |
|
327 |
return '`'.sql::escape($table).'` '; |
|
328 |
} |
|
329 |
else |
|
330 |
{ |
|
331 |
$ret = array(); |
|
332 |
foreach($table as $key => $value) { |
|
333 |
if(empty($value)) { |
|
334 |
continue; |
|
335 |
} |
|
336 |
|
|
337 |
if(!is_array($value)) { |
|
338 |
$ret[] = self::__combine_tablelist($key, $value); |
|
339 |
} |
|
340 |
else |
|
341 |
{ |
|
342 |
switch(strtoupper($value[0])) { |
|
343 |
case 'L': |
|
344 |
$sub = 'LEFT JOIN '; |
|
345 |
break; |
|
346 |
case 'R': |
|
347 |
$sub = 'RIGHT JOIN '; |
|
348 |
break; |
|
349 |
case 'J': |
|
350 |
$sub = 'JOIN '; |
|
351 |
break; |
|
352 |
case 'S': |
|
353 |
$sub = 'STRAIGHT_JOIN '; |
|
354 |
break; |
|
355 |
default: |
|
356 |
continue 2; |
|
357 |
break; |
|
358 |
} |
|
359 |
|
|
360 |
if(!is_array($value[1])) { |
|
361 |
$value[1] = array($value[1]); |
|
362 |
} |
|
363 |
foreach($value[1] as $k => $v) { |
|
364 |
$sub1[] = self::__combine_tablelist($k, $v); |
|
365 |
} |
|
366 |
$sub .= implode(', ', $sub1).' '; |
|
367 |
|
|
368 |
if(strtoupper($value[2])=='USING') { |
|
369 |
$sub .= 'USING ('.self::__combine_select($value[3]).')'; |
|
370 |
} |
|
371 |
elseif(strtoupper($value[2])=='ON') { |
|
372 |
$sub .= 'ON '.substr(self::__combine_where($value[3]), 7); |
|
373 |
} |
|
374 |
|
|
375 |
if($value[4]) { |
|
376 |
$sub .= ' HAVING '.self::__combine_where($value[4]); |
|
377 |
|
|
378 |
} |
|
379 |
|
|
380 |
$ret[] = $sub; |
|
381 |
} |
|
382 |
} |
|
383 |
|
|
384 |
return implode(' ', $ret).' '; |
|
385 |
} |
|
386 |
} |
|
387 |
|
|
388 |
static function escape_data($data) { |
|
389 |
if(self::$ESCAPE_DATA) { |
|
390 |
if(is_array($data)) { |
|
391 |
foreach($data as &$value) { |
|
392 |
$value = self::escape_data($value); |
|
393 |
} |
|
394 |
} |
|
395 |
elseif(is_string($data)) { |
|
396 |
$data = htmlspecialchars($data); |
|
397 |
} |
|
398 |
} |
|
399 |
|
|
400 |
return $data; |
|
401 |
} |
|
402 |
|
|
403 |
static function get($table, $keys, $where=NULL, $order=NULL, $limit=NULL, $stop=NULL, $revelance=FALSE) { |
|
404 |
$where = self::__combine_where($where, $revelance); |
|
405 |
$keys = self::__combine_select($keys, $revelance); |
|
406 |
|
|
407 |
$q = sql::query('SELECT '.$keys.' FROM '.self::__combine_table($table).$where.self::__combine_order($order, $revelance).self::__combine_limit($limit, $stop)); |
|
408 |
return self::escape_data(sql::fetch($q)); |
|
409 |
} |
|
410 |
|
|
411 |
static function getone($table, $keys, $where=NULL, $order=NULL, $limit=NULL, $stop=NULL, $revelance=FALSE) { |
|
412 |
$where = self::__combine_where($where, $revelance); |
|
413 |
$keys = self::__combine_select($keys, $revelance); |
|
414 |
|
|
415 |
$q = sql::query('SELECT '.$keys.' FROM '.self::__combine_table($table).$where.self::__combine_order($order, $revelance).self::__combine_limit($limit, $stop)); |
|
416 |
return self::escape_data(sql::fetchone($q)); |
|
417 |
} |
|
418 |
|
|
419 |
static function num($table, $key, $where=NULL) { |
|
420 |
$q = sql::query('SELECT COUNT('.self::__combine_select($key, $one).') AS `num` FROM '.self::__combine_table($table).self::__combine_where($where)); |
|
421 |
$r = sql::fetchone($q); |
|
422 |
return $r['num']; |
|
423 |
} |
|
424 |
|
|
425 |
static function add($table, $keys) { |
|
426 |
sql::query('INSERT INTO `'.sql::escape($table).'` '.self::__combine_insert($keys)); |
|
427 |
return sql::affected(); |
|
428 |
} |
|
429 |
|
|
430 |
static function edit($table, $keys, $where=NULL, $order=NULL, $limit=NULL, $stop=NULL) { |
|
431 |
sql::query('UPDATE `'.sql::escape($table).'` SET '.self::__combine_update($keys).self::__combine_where($where).self::__combine_order($order).self::__combine_limit($limit, $stop)); |
|
432 |
return sql::affected(); |
|
433 |
} |
|
434 |
|
|
435 |
static function del($table, $where=NULL, $order=NULL, $limit=NULL, $stop=NULL) { |
|
436 |
sql::query('DELETE FROM `'.sql::escape($table).'`'.self::__combine_where($where).self::__combine_order($order).self::__combine_limit($limit, $stop)); |
|
437 |
return sql::affected(); |
|
438 |
} |
|
439 |
|
|
440 |
static function last_id() { |
|
441 |
return sql::increment_id(); |
|
442 |
} |
|
443 |
} |
|
444 |
?> |