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 ***********************************************************************/
14 Converts encoding name to mysql standard.
16 function get_mysql_encoding_name($encoding)
20 'ISO-8859-1' => 'latin1',
21 'ISO-8859-2' => 'latin2',
22 'ISO-8859-7' => 'greek',
23 'ISO-8859-8' => 'hebrew',
24 'ISO-8859-9' => 'latin5',
25 'ISO-8859-13' => 'latin7',
41 'SHIFT_JIS' => 'sjis',
42 'TIS-620' => 'tis620',
45 $encoding = strtoupper($encoding);
47 return isset($db_encoding[$encoding]) ? $db_encoding[$encoding] : null;
51 Returns 'best' collation for various locale language codes
53 function get_mysql_collation($lang=null)
56 $lang = substr($_SESSION['language']->code, 0, 2);
58 $db_collation = array(
65 'es' => 'spanish', // or 'spanish2',
79 return 'utf8_'.(isset($db_collation[$lang]) ? $db_collation[$lang] : 'general').'_ci';
82 Later we assume that database with version less than 2.4 is old database,
83 which is subject to invalid encodings on text columns,
84 so no SET NAMES or equivalent should be used.
88 $result = db_query("SELECT value FROM ".TB_PREF."sys_prefs WHERE name='version_id'");
89 $data = db_fetch($result);
90 return !db_num_rows($result) // new database is fixed by default
91 || ($data[0] > "2.3rc");
94 Check database default charset.
96 function db_get_charset()
98 $result = db_query("SELECT @@character_set_database");
99 $var = db_fetch($result);
104 Set mysql client encoding.
105 Default is is encoding used by default language.
107 function db_set_encoding($ui_encoding=null)
109 global $dflt_lang, $installed_languages;
111 if (!isset($ui_encoding))
113 $lang = array_search_value($dflt_lang, $installed_languages, 'code');
114 $ui_encoding = strtoupper($lang['encoding']);
117 if ($mysql_enc = get_mysql_encoding_name($ui_encoding))
118 mysql_set_charset($mysql_enc);
122 Connects application to company database.
124 function set_global_connection($company=-1)
126 global $db, $transaction_level, $path_to_root;
127 global $db_connections;
129 include ($path_to_root . "/config_db.php");
131 $company = $_SESSION["wa_current_user"]->company ? $_SESSION["wa_current_user"]->company : 0;
133 cancel_transaction(); // cancel all aborted transactions if any
134 $transaction_level = 0;
136 $_SESSION["wa_current_user"]->cur_con = $company;
138 $connection = $db_connections[$company];
140 $db = mysql_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"]);
141 mysql_select_db($connection["dbname"], $db);
146 $db_duplicate_error_code = 1062;
148 function db_query($sql, $err_msg=null)
150 global $db, $show_sql, $sql_trail, $select_trail, $go_debug, $sql_queries, $Ajax,
151 $db_connections, $db_last_inserted_id;
153 // set current db prefix
154 $comp = isset($_SESSION["wa_current_user"]->cur_con) ? $_SESSION["wa_current_user"]->cur_con : 0;
155 $cur_prefix = $db_connections[$comp]['tbpref'];
156 $sql = str_replace(TB_PREF, $cur_prefix, $sql);
160 $Ajax->activate('footer_debug');
161 $sql_queries .= "<pre>$sql</pre>\n<hr>";
166 if (@$profile_sql) get_usec();
167 $result = mysql_query($sql, $db);
171 _vd($sql.'<br>:'.db_num_rows($result).'rows, '.get_usec());
175 $db_last_inserted_id = mysql_insert_id($db); // preserve in case trail insert is done
176 if ($select_trail || (strstr($sql, 'SELECT') === false)) {
178 "INSERT INTO ".$cur_prefix."sql_trail
179 (`sql`, `result`, `msg`)
180 VALUES(".db_escape($sql).",".($result ? 1 : 0).",
181 ".db_escape($err_msg).")", $db);
185 if ($err_msg != null || $go_debug) {
186 $exit = $err_msg != null;
187 if (function_exists('xdebug_call_file'))
188 check_db_error('<br>At file '.xdebug_call_file().':'.xdebug_call_line().':<br>'.$err_msg, $sql, $exit);
190 check_db_error($err_msg, $sql, $exit);
195 function db_fetch_row ($result)
198 return mysql_fetch_row($result);
201 function db_fetch_assoc ($result)
204 return mysql_fetch_assoc($result);
207 function db_fetch ($result)
210 return mysql_fetch_array($result);
213 function db_seek (&$result,$record)
215 return mysql_data_seek($result, $record);
218 function db_free_result ($result)
221 mysql_free_result($result);
224 function db_num_rows ($result)
226 return mysql_num_rows($result);
229 function db_num_fields ($result)
231 return mysql_num_fields($result);
234 function db_escape($value = "", $nullify = false)
236 $value = @html_entity_decode($value, ENT_QUOTES, $_SESSION['language']->encoding);
237 $value = @htmlspecialchars($value, ENT_QUOTES, $_SESSION['language']->encoding);
239 //reset default if second parameter is skipped
240 $nullify = ($nullify === null) ? (false) : ($nullify);
242 //check for null/unset/empty strings
243 if ((!isset($value)) || (is_null($value)) || ($value === "")) {
244 $value = ($nullify) ? ("NULL") : ("''");
246 if (is_string($value)) {
247 //value is a string and should be quoted; determine best method based on available extensions
248 if (function_exists('mysql_real_escape_string')) {
249 $value = "'" . mysql_real_escape_string($value) . "'";
251 $value = "'" . mysql_escape_string($value) . "'";
253 } else if (!is_numeric($value)) {
254 //value is not a string nor numeric
255 display_error("ERROR: incorrect data type send to sql query");
263 function db_error_no ()
266 return mysql_errno($db);
269 function db_error_msg($conn)
271 return mysql_error($conn);
274 function db_insert_id()
276 global $db_last_inserted_id, $sql_trail, $db;
278 return $sql_trail ? $db_last_inserted_id : mysql_insert_id($db);
281 function db_num_affected_rows()
284 return mysql_affected_rows($db);
287 function db_field_name($result, $n)
289 return mysql_field_name($result, $n);
292 function db_create_db($connection)
294 $db = mysql_connect($connection["host"] ,
295 $connection["dbuser"], $connection["dbpassword"]);
296 if (!mysql_select_db($connection["dbname"], $db))
298 $sql = "CREATE DATABASE " . $connection["dbname"] . " COLLATE ".get_mysql_collation();
300 if (!mysql_query($sql) || !mysql_select_db($connection["dbname"], $db))
306 function db_drop_db($connection)
309 if ($connection["tbpref"] == "")
311 $sql = "DROP DATABASE " . $connection["dbname"] . "";
312 return mysql_query($sql);
316 $res = db_query("show table status");
317 $all_tables = array();
318 while($row = db_fetch($res))
319 $all_tables[] = $row;
320 // get table structures
321 foreach ($all_tables as $table)
323 if (strpos($table['Name'], $connection["tbpref"]) === 0)
324 db_query("DROP TABLE `".$table['Name'] . "`");
326 //deleting the tables, how??
331 function db_close($dbase = null)
337 return mysql_close($dbase);