5b4293eb10ed8d001b803fd2e6ef5a7b4426cba1
[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($company=-1)
14 {
15         global $db, $transaction_level, $db_connections;
16
17         cancel_transaction(); // cancel all aborted transactions if any
18         $transaction_level = 0;
19
20         if ($company == -1) 
21                 $company = $_SESSION["wa_current_user"]->company;
22
23         $_SESSION["wa_current_user"]->cur_con = $company;
24
25         $connection = $db_connections[$company];
26
27         $db = mysql_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"]);
28                 mysql_select_db($connection["dbname"], $db);
29         ///// From MySql release 5.6.6 the sql_mode is no longer empty as it was prior to
30         ///// this release. Just for safety we set it empty for all 5.6 release and higher.
31         ///// This non empty sql_mode values can interphere with FA, so all is set empty during
32         ///// our sessions.
33         ///// We are, however, investigating the existing code to be compatible in the future.
34         ///// We are also working on a mysql/mysqli solution to go to release 2.4.
35         if (strncmp(mysql_get_server_info(), "5.6", 3) >= 0) 
36                 db_query("SET sql_mode = ''");
37         /////
38         return $db;
39 }
40
41 $db_duplicate_error_code = 1062;
42
43 //DB wrapper functions to change only once for whole application
44
45 function db_query($sql, $err_msg=null)
46 {
47         global $db, $show_sql, $sql_trail, $select_trail, $go_debug, $sql_queries, $Ajax,
48                 $db_connections, $db_last_inserted_id;
49         
50         // set current db prefix
51         $cur_prefix = $db_connections[$_SESSION["wa_current_user"]->cur_con]['tbpref'];
52         $sql = str_replace(TB_PREF, $cur_prefix, $sql);
53
54         if ($show_sql)
55         {
56                 $Ajax->activate('footer_debug');
57                 $sql_queries .= "<pre>$sql</pre>\n<hr>";
58         }
59
60         $result = mysql_query($sql, $db);
61         
62         if($sql_trail) {
63                 $db_last_inserted_id = mysql_insert_id($db);    // preserve in case trail insert is done
64                 if ($select_trail || (strstr($sql, 'SELECT') === false)) {
65                         mysql_query(
66                         "INSERT INTO ".$cur_prefix."sql_trail
67                                 (`sql`, `result`, `msg`)
68                                 VALUES(".db_escape($sql).",".($result ? 1 : 0).",
69                                 ".db_escape($err_msg).")", $db);
70                 }
71         }
72
73         if ($err_msg != null || $go_debug) {
74                 $exit = $err_msg != null;
75                 if (function_exists('xdebug_call_file'))
76                         check_db_error('<br>At file '.xdebug_call_file().':'.xdebug_call_line().':<br>'.$err_msg, $sql, $exit);
77                 else
78                         check_db_error($err_msg, $sql, $exit);
79         }
80         return $result;
81 }
82
83 function db_fetch_row ($result)
84 {
85
86         return mysql_fetch_row($result);
87 }
88
89 function db_fetch_assoc ($result)
90 {
91
92         return mysql_fetch_assoc($result);
93 }
94
95 function db_fetch ($result)
96 {
97
98         return mysql_fetch_array($result);
99 }
100
101 function db_seek (&$result,$record)
102 {
103         return mysql_data_seek($result, $record);
104 }
105
106 function db_free_result ($result)
107 {
108         if ($result)
109                 mysql_free_result($result);
110 }
111
112 function db_num_rows ($result)
113 {
114         return mysql_num_rows($result);
115 }
116
117 function db_num_fields ($result)
118 {
119         return mysql_num_fields($result);
120 }
121
122 function db_escape($value = "", $nullify = false)
123 {
124         $value = @html_entity_decode($value, ENT_QUOTES, $_SESSION['language']->encoding);
125         $value = @htmlspecialchars($value, ENT_QUOTES, $_SESSION['language']->encoding);
126
127         //reset default if second parameter is skipped
128         $nullify = ($nullify === null) ? (false) : ($nullify);
129
130         //check for null/unset/empty strings
131         if ((!isset($value)) || (is_null($value)) || ($value === "")) {
132                 $value = ($nullify) ? ("NULL") : ("''");
133         } else {
134                 if (is_string($value)) {
135                 //value is a string and should be quoted; determine best method based on available extensions
136                         if (function_exists('mysql_real_escape_string')) {
137                                 $value = "'" . mysql_real_escape_string($value) . "'";
138                         } else {
139                           $value = "'" . mysql_escape_string($value) . "'";
140                         }
141                 } else if (!is_numeric($value)) {
142                         //value is not a string nor numeric
143                         display_error("ERROR: incorrect data type send to sql query");
144                         echo '<br><br>';
145                         exit();
146                 }
147         }
148         return $value;
149 }
150
151 function db_error_no ()
152 {
153         global $db;
154         return mysql_errno($db);
155 }
156
157 function db_error_msg($conn)
158 {
159         return mysql_error($conn);
160 }
161
162 function db_insert_id()
163 {
164         global $db_last_inserted_id, $sql_trail, $db;
165
166         return $sql_trail ? $db_last_inserted_id : mysql_insert_id($db);
167 }
168
169 function db_num_affected_rows()
170 {
171         global $db;
172         return mysql_affected_rows($db);
173 }
174
175 function db_field_name($result, $n)
176 {
177         return mysql_field_name($result, $n);
178 }
179
180 function db_create_db($connection)
181 {
182         $db = mysql_connect($connection["host"] ,
183                 $connection["dbuser"], $connection["dbpassword"]);
184         if (!mysql_select_db($connection["dbname"], $db))
185         {
186                 $sql = "CREATE DATABASE IF NOT EXISTS " . $connection["dbname"] . "";
187                 if (!mysql_query($sql) || !mysql_select_db($connection["dbname"], $db))
188                         return 0;
189         }
190         return $db;
191 }
192
193 function db_drop_db($connection)
194 {
195
196         if ($connection["tbpref"] == "")
197         {
198                 $sql = "DROP DATABASE IF EXISTS " . $connection["dbname"] . "";
199                 return mysql_query($sql);
200         }
201         else
202         {
203         $res = db_query("show table status");
204         $all_tables = array();
205         while($row = db_fetch($res))
206                 $all_tables[] = $row;
207         // get table structures
208                 foreach ($all_tables as $table)
209                 {
210                         if (strpos($table['Name'], $connection["tbpref"]) === 0)
211                                 db_query("DROP TABLE `".$table['Name'] . "`");
212                 }
213                 //deleting the tables, how??
214                 return true;
215         }
216 }
217
218 function db_close($dbase = null)
219 {
220         global $db;
221         
222         if (!$dbase)
223                 $dbase = $db;
224         return mysql_close($dbase);
225 }
226
227 ?>