root/trunk/globals/classes/mysql.php @ 1

Revision 1, 7.9 kB (checked in by Winner, 5 years ago)

Initial import

Line 
1<?php
2/*
3 * WiND - Wireless Nodes Database
4 *
5 * Copyright (C) 2005 Nikolaos Nikalexis <winner@cube.gr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 dated June, 1991.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19 *
20 */
21
22class mysql {
23        var $mysql_link;
24        var $error;
25        var $error_report;
26        var $last_query;
27        var $insert_id;
28        var $log=FALSE;
29        var $logs_table='';
30        var $log_insert_id;
31        var $log_last_query;
32       
33        var $total_queries=0;
34        var $total_time=0;
35       
36        function mysql($server, $user, $password, $database) {
37                if (!$this->mysql_link = @mysql_connect($server, $user, $password, TRUE)) {
38                        $this->error();
39                        return;
40                }
41                if (!@mysql_select_db($database, $this->mysql_link)) {
42                        $this->error();
43                        return;
44                }
45        }
46       
47        function close_mysql() {
48                return mysql_close($this->mysql_link);
49        }
50       
51        function query($query) {
52                $this->insert_id = 0;
53                $this->last_query=$query;
54                $this->total_queries += 1;
55                $mt = $this->getmicrotime();
56                #echo $query;
57                $q = mysql_query($query, $this->mysql_link);
58                $this->total_time += ($this->getmicrotime() - $mt);
59                $this->error();
60                if ($q === FALSE) {
61                        return FALSE;
62                } elseif ($q === TRUE) {
63                        $this->insert_id = mysql_insert_id($this->mysql_link);
64                        return TRUE;
65                }
66                $i = 0;
67                while ($ret = mysql_fetch_assoc($q)) {
68                        while (list ($key, $value) = each ($ret)) {
69                                $res[$i][$key] = $value;
70                        }
71                        $i++;
72                }
73                mysql_free_result($q);
74                return $res;
75        }
76       
77        function get_fields($table) {
78                return $this->query("SHOW FIELDS FROM `$table`");
79        }
80
81        function get($select="*", $from, $where="", $group_by="", $order_by="", $limit="") {
82                return $this->query("SELECT $select FROM $from".($where==""?"":" WHERE $where").($group_by==""?"":" GROUP BY $group_by").($order_by==""?"":" ORDER BY $order_by").($limit==""?"":" LIMIT $limit"));
83        }
84       
85        function add($table, $data, $addlog=TRUE) {
86                $table_start = preg_split("/[\s,]+/", $table);
87                $table_start = $table_start[0];
88                $db_fields = $this->query("SHOW FIELDS FROM `$table_start`");
89                for ($i=0;$i<count($db_fields);$i++) {
90                        $nulls[$db_fields[$i]['Field']] = $db_fields[$i]['Null'];
91                        $nulls['`'.$db_fields[$i]['Field'].'`'] = $db_fields[$i]['Null'];
92                }
93                while (list ($key, $value) = each ($data)) {
94                        $key_t = explode(".", $key);
95                        $key_t = $key_t[count($key_t)-1];
96                        if ($value === '' && $nulls[$key_t] != 'YES') {
97                                $not_null_keys .= ($not_null_keys==''?'':', ').$key;
98                        }
99                        $keys .= $key.", ";
100                        $value = str_replace("'", "\\'", $value);
101                        $value = str_replace("\\\\'", "\\'", $value);
102                        $values .= ($value === '' || $value === NULL?'NULL':"'".$value."'").", ";
103                }
104                $keys = substr($keys, 0, -2);
105                $values = substr($values, 0, -2);
106                $query = "INSERT INTO $table ($keys) VALUES ($values)";
107                if ($not_null_keys != '') {
108                        $this->output_error_fields_required($not_null_keys);
109                        if ($addlog) $this->add_log('ADD', $table, $this->insert_id, serialize($data), $query, $this->get_error());
110                        return FALSE;
111                }
112                $res = $this->query($query);
113                if ($addlog) $this->add_log('ADD', $table, $this->insert_id, serialize($data), $query, (!$res?$this->get_error():''));
114                return $res;
115        }
116       
117        function set($table, $data, $where='', $addlog=TRUE) {
118                $table_start = preg_split("/[\s,]+/", $table);
119                $table_start = $table_start[0];
120                if ($addlog && $this->log) $aff = $this->query("SELECT ".$table_start.".id FROM $table WHERE $where");
121                $db_fields = $this->query("SHOW FIELDS FROM `$table_start`");
122                for ($i=0;$i<count($db_fields);$i++) {
123                        $nulls[$db_fields[$i]['Field']] = $db_fields[$i]['Null'];
124                        $nulls['`'.$db_fields[$i]['Field'].'`'] = $db_fields[$i]['Null'];
125                }
126                while (list ($key, $value) = each ($data)) {
127                        $key_t = explode(".", $key);
128                        $key_t = $key_t[count($key_t)-1];
129                        if ($value === '' && $nulls[$key_t] != 'YES') {
130                                $not_null_keys .= ($not_null_keys==''?'':', ').translate($table.'__'.$key, 'db');
131                        }
132                        $value = str_replace("'", "\\'", $value);
133                        $value = str_replace("\\\\'", "\\'", $value);
134                        $sets .= $key."=".($value === '' || $value === NULL?'NULL':"'".$value."'").", ";
135                }
136                $sets = substr($sets, 0, -2);
137                $query = "UPDATE $table SET $sets".($where!=''?" WHERE $where":'');
138                if ($not_null_keys != '') {
139                        $this->output_error_fields_required($not_null_keys);
140                        if ($addlog) {
141                                for ($i=0;$i<count($aff);$i++) {
142                                        $this->add_log('EDIT', $table_start, $aff[$i]['id'], serialize($data), $query, $this->get_error());
143                                }
144                        }
145                        return FALSE;
146                }
147                $res = $this->query($query);
148                if ($addlog) {
149                        for ($i=0;$i<count($aff);$i++) {
150                                $this->add_log('EDIT', $table_start, $aff[$i]['id'], serialize($data), $query, (!$res?$this->get_error():''));
151                        }
152                }
153                return $res;
154        }
155
156        function del($table, $where="", $addlog=TRUE) {
157                $table_start = preg_split("/[\s,]+/", $table);
158                $table_start = $table_start[0];
159                if ($addlog && $this->log) $aff = $this->query("SELECT ".$table_start.".id FROM $table".($where==""?"":" WHERE $where"));
160                $query = "DELETE FROM $table".($where==""?"":" WHERE $where");
161                $res = $this->query($query);
162                if ($addlog) {
163                        for ($i=0;$i<count($aff);$i++) {
164                                $this->add_log('DELETE', $table_start, $aff[$i]['id'], '', $query, (!$res?$this->get_error():''));
165                        }
166                }
167                return $res;
168        }
169
170        function cnt($table, $where="") {
171                $res = $this->query("SELECT COUNT(*) FROM $table".($where==""?"":" WHERE $where"));
172                if ($res[0]['COUNT(*)'] != "") return $res[0]['COUNT(*)'];
173                return $res;
174        }
175       
176        function error() {
177                $this->error = mysql_errno();
178                $this->error_report = mysql_error();
179                if ($this->error > 0) $this->output_error();
180        }
181       
182        function output_error_fields_required($fields_required) {
183                global $main, $lang;
184                $main->message->set($lang['message']['error']['fields_required']['title'], str_replace("##fields_required##", $fields_required, $lang['message']['error']['fields_required']['body']));
185        }
186       
187        function output_error($num='', $report='') {
188                global $main;
189                if ($num !== '') $this->error = $num;
190                if ($num !== '') $this->error_report = $report;
191                if (isset($main)) {
192                        if ($main->userdata->privileges['admin'] === TRUE) $if_admin .= '<br /><br />Last MySQL query:<br />'.$this->last_query;
193                        $main->message->set('MySQL Error', $this->get_error().$if_admin);
194                } else {
195                        echo $this->get_error();
196                }
197        }
198       
199        function get_error() {
200                return $this->error.": ".$this->error_report;
201        }
202       
203        function add_log($type, $table, $affected_id, $data, $query="", $error="") {
204                if (!$this->log) return;
205                global $main;
206                $date = "'".date_now()."'";
207                $user_type = "'".$main->userdata->user_type."'";
208                $user_id = $main->userdata->user_id;
209                $type = "'".$type."'";
210                $ip = "'".get_ip()."'";
211                $dns = "'".get_dns()."'";
212                $table = "'".$table."'";
213                if ($user_type == "''") return;
214                if ($data == '') $data = 'NULL'; else $data = "'".addslashes($data)."'";
215                if ($query == '') $query = 'NULL'; else $query = "'".addslashes($query)."'";
216                if ($error == '') $error = 'NULL'; else $error = "'".addslashes($error)."'";
217                $log_query = "INSERT INTO ".$this->logs_table." (date, user_type, user_id, type, ip, dns, tablename, affected_id, data, query, error) VALUES ($date, $user_type, $user_id, $type, $ip, $dns, $table, $affected_id, $data, $query, $error)";
218                $insert_id_return = $this->insert_id;
219                $last_query_return = $this->last_query;
220                $this->query($log_query);
221                $this->log_insert_id = $this->insert_id;
222                $this->log_last_query = $this->last_query;
223               
224                $this->insert_id = $insert_id_return;
225                $this->last_query = $last_query_return;
226        }
227
228        function getmicrotime(){ 
229                list($usec, $sec) = explode(" ",microtime()); 
230                return ((float)$usec + (float)$sec); 
231        } 
232
233}
234
235?>
Note: See TracBrowser for help on using the browser.