Projet

Général

Profil

Paste
Statistiques
| Branche: | Révision:

ryxeo-glpi-git / htdocs / inc / dbmysql.class.php @ 1c14bcc4

Historique | Voir | Annoter | Télécharger (8,29 ko)

1
<?php
2
/*
3
 * @version $Id: dbmysql.class.php 7876 2009-01-23 15:50:37Z moyo $
4
 -------------------------------------------------------------------------
5
 GLPI - Gestionnaire Libre de Parc Informatique
6
 Copyright (C) 2003-2009 by the INDEPNET Development Team.
7

8
 http://indepnet.net/   http://glpi-project.org
9
 -------------------------------------------------------------------------
10

11
 LICENSE
12

13
 This file is part of GLPI.
14

15
 GLPI is free software; you can redistribute it and/or modify
16
 it under the terms of the GNU General Public License as published by
17
 the Free Software Foundation; either version 2 of the License, or
18
 (at your option) any later version.
19

20
 GLPI is distributed in the hope that it will be useful,
21
 but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 GNU General Public License for more details.
24

25
 You should have received a copy of the GNU General Public License
26
 along with GLPI; if not, write to the Free Software
27
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28
 --------------------------------------------------------------------------
29
 */
30

    
31
if (!defined('GLPI_ROOT')){
32
        die("Sorry. You can't access directly to this file");
33
        }
34

    
35
/**
36
 *  Database class for Mysql
37
 */
38
class DBmysql {
39

    
40
        //! Database Host
41
        var $dbhost        = ""; 
42
        //! Database User
43
        var $dbuser = "";
44
        //! Database Password 
45
        var $dbpassword        = "";
46
        //! Default Database
47
        var $dbdefault        = "";
48
        //! Database Handler
49
        var $dbh;
50
        //! Database Error
51
        var $error = 0;
52

    
53
        /// Slave management
54
        var $slave = false;
55
        /** Is it a first connection ?
56
        * Indicates if the first connection attempt is successful or not
57
        * if first attempt fail -> display a warning which indicates
58
        * that glpi is in readonly
59
        */
60
        var $first_connection = true;
61
        /// Is connected to the DB ?
62
        var $connected = false;
63
        
64
        /**
65
         * Constructor / Connect to the MySQL Database
66
         *
67
         * Use dbhost, dbuser, dbpassword and dbdefault
68
         * Die if connection or database selection failed
69
         *
70
         * @return nothing 
71
         */
72
        function DBmysql(){
73
                $this->connected=false;
74
                $this->dbh = @mysql_connect($this->dbhost, $this->dbuser, rawurldecode($this->dbpassword)) or $this->error = 1;
75
                if ($this->dbh){
76
                        @mysql_query("SET NAMES '" . (isset($this->dbenc) ? $this->dbenc : "utf8") . "'",$this->dbh);
77
                        $select= mysql_select_db($this->dbdefault) or $this->error = 1;
78
                        if ($select){ // select ok
79
                                $this->connected=true;
80
                        }else{ // select wrong
81
                                $this->connected=false;
82
                        }
83
                } else {
84
                        $this->connected=false;
85
                }
86
        }
87
        /**
88
         * Execute a MySQL query
89
         * @param $query Query to execute
90
         * @return Query result handler
91
         */
92
        function query($query) {
93
                global $CFG_GLPI,$DEBUG_SQL,$SQL_TOTAL_REQUEST;
94

    
95
                if ($CFG_GLPI["debug"]) {
96
                        if ($CFG_GLPI["debug_sql"]){                
97
                                $SQL_TOTAL_REQUEST++;
98
                                $DEBUG_SQL["queries"][$SQL_TOTAL_REQUEST]=$query;
99

    
100
                                if ($CFG_GLPI["debug_profile"]){                
101
                                        $TIMER=new Script_Timer;
102
                                        $TIMER->Start_Timer();
103
                                }
104
                        }
105
                }
106

    
107
                //mysql_ping($this->dbh);
108
                $res=@mysql_query($query,$this->dbh);
109
                
110
                if (!$res) {
111
                        $this->DBmysql();
112
                        $res=mysql_query($query,$this->dbh);
113
                        
114
                        if (!$res) {
115
                                $error = "*** MySQL query error : \n***\nSQL: ".addslashes($query)."\nError: ". mysql_error()."\n";
116
                                if (function_exists("debug_backtrace")) {
117
                                        $error .= "Backtrace :\n";
118
                                        $traces=debug_backtrace();
119
                                        foreach ($traces as $trace) {
120
                                                $error .= $trace["file"] . ":" . $trace["line"] . "\t\t"
121
                                                                . (isset($trace["class"]) ? $trace["class"] : "")
122
                                                                . (isset($trace["type"]) ? $trace["type"] : "")
123
                                                                . (isset($trace["function"]) ? $trace["function"]."()" : "")
124
                                                                . "\n";
125
                                        }
126
                                } else {
127
                                        $error .= "Script: " ; 
128
                                }
129
                                $error .= $_SERVER["SCRIPT_FILENAME"]. "\n";
130
                                logInFile("sql-errors",$error);
131
                
132
                                if ($CFG_GLPI["debug"]&&$CFG_GLPI["debug_sql"]){
133
                                        $DEBUG_SQL["errors"][$SQL_TOTAL_REQUEST]=$this->error();
134
                                }
135
                        }
136
                }
137

    
138
                if ($CFG_GLPI["debug"]) {
139
                        if ($CFG_GLPI["debug_profile"]&&$CFG_GLPI["debug_sql"]){                
140
                                $TIME=$TIMER->Get_Time();
141
                                $DEBUG_SQL["times"][$SQL_TOTAL_REQUEST]=$TIME;
142
                        }
143
                }
144

    
145
                return $res;
146
        }
147
        /**
148
         * Give result from a mysql result
149
         * @param $result MySQL result handler
150
         * @param $i Row to give
151
         * @param $field Field to give
152
         * @return Value of the Row $i and the Field $field of the Mysql $result
153
         */
154
        function result($result, $i, $field) {
155
                $value=get_magic_quotes_runtime()?stripslashes_deep(mysql_result($result, $i, $field)):mysql_result($result, $i, $field);
156
                return $value;
157
        }
158
        /**
159
         * Give number of rows of a Mysql result
160
         * @param $result MySQL result handler
161
         * @return number of rows
162
         */
163
        function numrows($result) {
164
                return mysql_num_rows($result);
165
        }
166
        /**
167
         * Fetch array of the next row of a Mysql query
168
         * @param $result MySQL result handler
169
         * @return result array
170
         */
171
        function fetch_array($result) {
172
                $value=get_magic_quotes_runtime()?stripslashes_deep(mysql_fetch_array($result)):mysql_fetch_array($result);
173
                return $value;
174
        }
175
        /**
176
         * Fetch row of the next row of a Mysql query
177
         * @param $result MySQL result handler
178
         * @return result row
179
         */
180
        function fetch_row($result) {
181
                $value=get_magic_quotes_runtime()?stripslashes_deep(mysql_fetch_row($result)):mysql_fetch_row($result);
182
                return $value;
183
        }
184
        /**
185
         * Fetch assoc of the next row of a Mysql query
186
         * @param $result MySQL result handler
187
         * @return result associative array
188
         */
189
        function fetch_assoc($result) {
190
                $value=get_magic_quotes_runtime()?stripslashes_deep(mysql_fetch_assoc($result)):mysql_fetch_assoc($result);
191
                return $value;
192
        }
193
        /**
194
         * Move current pointer of a Mysql result to the specific row
195
         * @param $result MySQL result handler
196
         * @param $num row to move current pointer
197
         * @return boolean
198
         */
199
        function data_seek($result,$num){
200
                return mysql_data_seek ($result,$num);
201
        }
202
        /**
203
         * Give ID of the last insert item by Mysql
204
         * @return item ID
205
         */
206
        function insert_id() {
207
                return mysql_insert_id($this->dbh);
208
        }
209
        /**
210
         * Give number of fields of a Mysql result
211
         * @param $result MySQL result handler
212
         * @return number of fields
213
         */
214
        function num_fields($result) {
215
                return mysql_num_fields($result);
216
        }
217
        /**
218
         * Give name of a field of a Mysql result
219
         * @param $result MySQL result handler
220
         * @param $nb number of column of the field
221
         * @return name of the field
222
         */
223
        function field_name($result,$nb){
224
                return mysql_field_name($result,$nb);
225
        }
226
        /**
227
         * Get flags of a field of a mysql result 
228
         * @param $result MySQL result handler
229
         * @param $field field name
230
         * @return flags of the field
231
         */
232
        function field_flags($result,$field){
233
                return mysql_field_flags($result,$field);
234
        }
235
        /**
236
         * List tables in database 
237
         * @param $table table name condition (glpi_% as default to retrieve only glpi tables)
238
         * @return list of tables
239
         */
240
        function list_tables($table="glpi_%") {
241
                return $this->query("SHOW TABLES LIKE '".$table."'");
242
        }
243
        /**
244
         * List fields of a table
245
         * @param $table table name condition
246
         * @return list of fields
247
         */
248
        function list_fields($table) {
249
                $result = $this->query("SHOW COLUMNS FROM `$table`");
250
                if ($result) {
251
                        if ($this->numrows($result) > 0) {
252
                                while ($data = mysql_fetch_assoc($result)){
253
                                        $ret[$data["Field"]]= $data;
254
                                }
255
                                return $ret;
256
                        } else return array();
257
                } else return false;
258

    
259

    
260
        }
261
        /**
262
         * Get number of affected rows in previous MySQL operation
263
         * @return number of affected rows on success, and -1 if the last query failed.
264
         */
265
        function affected_rows() {
266
                return mysql_affected_rows($this->dbh);
267
        }
268
        /**
269
         * Free result memory
270
         * @param $result MySQL result handler
271
         * @return Returns TRUE on success or FALSE on failure.
272
         */
273
        function free_result($result) {
274
                return mysql_free_result($result);
275
        }
276
        /**
277
         * Returns the numerical value of the error message from previous MySQL operation
278
         * @return error number from the last MySQL function, or 0 (zero) if no error occurred.
279
         */
280
        function errno(){
281
                return mysql_errno($this->dbh);
282
        }
283

    
284
        /**
285
         * Returns the text of the error message from previous MySQL operation
286
         * @return error text from the last MySQL function, or '' (empty string) if no error occurred.
287
         */
288
        function error(){
289
                return mysql_error($this->dbh);
290
        }
291
        /**
292
         * Close MySQL connection
293
         * @return TRUE on success or FALSE on failure.
294
         */
295
        function close(){
296
                return @mysql_close($this->dbh);
297
        }
298
        
299
        /**
300
         * is a slave database ?
301
         * @return boolean 
302
         */
303
        function isSlave(){
304
                return $this->slave;
305
        }
306
}
307
?>
Redmine Appliance - Powered by TurnKey Linux