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