Some security fixes backported from unstable code.
[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     if (isset($_SESSION["wa_current_user"]) && $_SESSION["wa_current_user"]->company !='')
18         $db = $_SESSION["wa_current_user"]->get_db_connection();
19     else
20         $db = null;
21 }
22
23 $db_duplicate_error_code = 1062;
24
25 //DB wrapper functions to change only once for whole application
26
27 function db_query($sql, $err_msg=null)
28 {
29         global $db, $show_sql, $sql_trail, $select_trail;
30
31         //echo "<br>$sql<br>";
32         if ($show_sql)
33         {
34                 echo "<font face=arial size=2 color=000099><b>SQL..</b></font>";
35                 echo "<pre>";
36                 echo $sql;
37                 echo "</pre>\n";
38         }
39         
40
41         $result = mysql_query($sql, $db);
42         if($sql_trail) {
43                 if ($select_trail || (strstr($sql, 'SELECT') === false)) {
44                         mysql_query(
45                         "INSERT INTO ".TB_PREF."sql_trail
46                                 (`sql`, `result`, `msg`)
47                                 VALUES(".db_escape($sql).",".($result ? 1 : 0).",
48                                 ".db_escape($err_msg).")", $db);
49                 }
50         }
51
52         if ($err_msg != null)
53           if (function_exists('xdebug_call_file'))
54                 check_db_error('<br>At file '.xdebug_call_file().':'.xdebug_call_line().':<br>'.$err_msg, $sql);
55           else
56                 check_db_error($err_msg, $sql);
57
58         return $result;
59 }
60
61 function db_fetch_row ($result)
62 {
63
64         return mysql_fetch_row($result);
65 }
66
67 function db_fetch_assoc ($result)
68 {
69
70         return mysql_fetch_assoc($result);
71 }
72
73 function db_fetch ($result)
74 {
75
76         return mysql_fetch_array($result);
77 }
78
79 function db_seek (&$result,$record)
80 {
81         return mysql_data_seek($result, $record);
82 }
83
84 function db_free_result ($result)
85 {
86         if ($result)
87                 mysql_free_result($result);
88 }
89
90 function db_num_rows (&$result)
91 {
92         return mysql_num_rows($result);
93 }
94
95 function db_num_fields ($result)
96 {
97         return mysql_num_fields($result);
98 }
99
100 function db_escape($value = "", $nullify = false)
101 {
102         $value = @htmlspecialchars($value, ENT_QUOTES, $_SESSION['language']->encoding);
103
104         //reset default if second parameter is skipped
105         $nullify = ($nullify === null) ? (false) : ($nullify);
106
107         //check for null/unset/empty strings
108         if ((!isset($value)) || (is_null($value)) || ($value === "")) {
109                 $value = ($nullify) ? ("NULL") : ("''");
110         } else {
111                 if (is_string($value)) {
112                 //value is a string and should be quoted; determine best method based on available extensions
113                         if (function_exists('mysql_real_escape_string')) {
114                                 $value = "'" . mysql_real_escape_string($value) . "'";
115                         } else {
116                           $value = "'" . mysql_escape_string($value) . "'";
117                         }
118                 } else if (!is_numeric($value)) {
119                         //value is not a string nor numeric
120                         display_error("ERROR: incorrect data type send to sql query");
121                         echo '<br><br>';
122                         exit();
123                 }
124         }
125         return $value;
126 }
127
128 function db_error_no ()
129 {
130         global $db;
131         return mysql_errno($db);
132 }
133
134 function db_error_msg($conn)
135 {
136         return mysql_error($conn);
137 }
138
139 function db_insert_id()
140 {
141         global $db;
142         return mysql_insert_id($db);
143 }
144
145 function db_num_affected_rows()
146 {
147         global $db;
148         return mysql_affected_rows($db);
149 }
150
151 ?>