Update from usntable branch.
[fa-stable.git] / includes / db / connect_db.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12
13 function set_global_connection()
14 {
15         global $db;
16
17         $db = $_SESSION["wa_current_user"]->get_db_connection();
18 }
19
20 $db_duplicate_error_code = 1062;
21
22 //DB wrapper functions to change only once for whole application
23
24 function db_query($sql, $err_msg=null)
25 {
26         global $db, $show_sql, $sql_trail, $select_trail, $go_debug;
27         
28         if ($show_sql)
29         {
30                 echo "<font face=arial size=2 color=000099><b>SQL..</b></font>";
31                 echo "<pre>";
32                 echo $sql;
33                 echo "</pre>\n";
34         }
35         
36
37         $result = mysql_query($sql, $db);
38         if($sql_trail) {
39                 if ($select_trail || (strstr($sql, 'SELECT') === false)) {
40                         mysql_query(
41                         "INSERT INTO ".TB_PREF."sql_trail
42                                 (`sql`, `result`, `msg`)
43                                 VALUES(".db_escape($sql).",".($result ? 1 : 0).",
44                                 ".db_escape($err_msg).")", $db);
45                 }
46         }
47
48         if ($err_msg != null || $go_debug) {
49                 $exit = $err_msg != null;
50                 if (function_exists('xdebug_call_file'))
51                         check_db_error('<br>At file '.xdebug_call_file().':'.xdebug_call_line().':<br>'.$err_msg, $sql, $exit);
52                 else
53                         check_db_error($err_msg, $sql, $exit);
54         }
55         return $result;
56 }
57
58 function db_fetch_row ($result)
59 {
60
61         return mysql_fetch_row($result);
62 }
63
64 function db_fetch_assoc ($result)
65 {
66
67         return mysql_fetch_assoc($result);
68 }
69
70 function db_fetch ($result)
71 {
72
73         return mysql_fetch_array($result);
74 }
75
76 function db_seek (&$result,$record)
77 {
78         return mysql_data_seek($result, $record);
79 }
80
81 function db_free_result ($result)
82 {
83         if ($result)
84                 mysql_free_result($result);
85 }
86
87 function db_num_rows ($result)
88 {
89         return mysql_num_rows($result);
90 }
91
92 function db_num_fields ($result)
93 {
94         return mysql_num_fields($result);
95 }
96
97 function db_escape($value = "", $nullify = false)
98 {
99         $value = @html_entity_decode($value, ENT_QUOTES, $_SESSION['language']->encoding);
100         $value = @htmlspecialchars($value, ENT_QUOTES, $_SESSION['language']->encoding);
101
102         //reset default if second parameter is skipped
103         $nullify = ($nullify === null) ? (false) : ($nullify);
104
105         //check for null/unset/empty strings
106         if ((!isset($value)) || (is_null($value)) || ($value === "")) {
107                 $value = ($nullify) ? ("NULL") : ("''");
108         } else {
109                 if (is_string($value)) {
110                 //value is a string and should be quoted; determine best method based on available extensions
111                         if (function_exists('mysql_real_escape_string')) {
112                                 $value = "'" . mysql_real_escape_string($value) . "'";
113                         } else {
114                           $value = "'" . mysql_escape_string($value) . "'";
115                         }
116                 } else if (!is_numeric($value)) {
117                         //value is not a string nor numeric
118                         display_error("ERROR: incorrect data type send to sql query");
119                         echo '<br><br>';
120                         exit();
121                 }
122         }
123         return $value;
124 }
125
126 function db_error_no ()
127 {
128         global $db;
129         return mysql_errno($db);
130 }
131
132 function db_error_msg($conn)
133 {
134         return mysql_error($conn);
135 }
136
137 function db_insert_id()
138 {
139         global $db;
140         return mysql_insert_id($db);
141 }
142
143 function db_num_affected_rows()
144 {
145         global $db;
146         return mysql_affected_rows($db);
147 }
148
149 ?>