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