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