From: Janusz Dobrowolski Date: Thu, 28 Oct 2010 09:30:35 +0000 (+0000) Subject: All mysql specific calls moved to connect_db.inc X-Git-Tag: v2.4.2~19^2~533 X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=commitdiff_plain;h=c128e08aea1e33a74bf918578013998a4d8b5c36;p=fa-stable.git All mysql specific calls moved to connect_db.inc --- diff --git a/admin/db/maintenance_db.inc b/admin/db/maintenance_db.inc index ed14e56a..9e0793f1 100644 --- a/admin/db/maintenance_db.inc +++ b/admin/db/maintenance_db.inc @@ -251,45 +251,6 @@ function write_lang() return true; } -function db_create_db($connection) -{ - $db = mysql_connect($connection["host"] , - $connection["dbuser"], $connection["dbpassword"]); - if (!mysql_select_db($connection["dbname"], $db)) - { - $sql = "CREATE DATABASE " . $connection["dbname"] . ""; - if (!mysql_query($sql)) - return 0; - mysql_select_db($connection["dbname"], $db); - } - return $db; -} - -function db_drop_db($connection) -{ - - if ($connection["tbpref"] == "") - { - $sql = "DROP DATABASE " . $connection["dbname"] . ""; - return mysql_query($sql); - } - else - { - $res = db_query("show table status"); - $all_tables = array(); - while($row = db_fetch($res)) - $all_tables[] = $row; - // get table structures - foreach ($all_tables as $table) - { - if (strpos($table['Name'], $connection["tbpref"]) === 0) - db_query("DROP TABLE `".$table['Name'] . "`"); - } - //deleting the tables, how?? - return true; - } -} - function db_import($filename, $connection, $force=true) { global $db, $go_debug; @@ -648,9 +609,6 @@ function db_export($conn, $filename, $zip='no', $comment='', $tbpref = TB_PREF, return false; } - // if (mysql_error()) return "DB_ERROR"; - //@mysql_close($con); - //if ($zip == "zip") // $zip = $time; if (save_to_file($backupfile, $zip, $out)) diff --git a/admin/inst_theme.php b/admin/inst_theme.php index baea29cf..af640e88 100644 --- a/admin/inst_theme.php +++ b/admin/inst_theme.php @@ -32,15 +32,16 @@ function clean_user_themes($id) global $db_connections, $db, $installed_extensions; $theme = $installed_extensions[$id]['package']; - $db_sav = $db; + $comp = user_company(); foreach ($db_connections as $n => $conn) { - $db = $_SESSION["wa_current_user"]->get_db_connection($n); + $db = $_SESSION["wa_current_user"]->set_db_connection($n); $sql = "UPDATE {$conn['tbpref']}users SET theme='default' WHERE theme='$theme'"; if (!db_query($sql, 'Cannot update user theme settings')) return false; } - $db = $db_sav; + $db = $_SESSION["wa_current_user"]->set_db_connection($comp); + $_SESSION['wa_current_user']->prefs->theme = 'default'; return true; } diff --git a/includes/current_user.inc b/includes/current_user.inc index 8b94366f..b97db9f3 100644 --- a/includes/current_user.inc +++ b/includes/current_user.inc @@ -153,19 +153,9 @@ class current_user return $this->can_access($page_level); } - function get_db_connection($id=-1) + function set_db_connection($id = -1) { - global $db_connections; - - $this->cur_con = $id == -1 ? $this->company : $id; - $connection = $db_connections[$this->cur_con]; - - //print_r($connection); - - $db = mysql_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"]); - mysql_select_db($connection["dbname"], $db); - - return $db; + return set_global_connection($id); } function update_prefs($prefs) diff --git a/includes/db/connect_db.inc b/includes/db/connect_db.inc index 1e6c2c4a..5705e3f2 100644 --- a/includes/db/connect_db.inc +++ b/includes/db/connect_db.inc @@ -12,13 +12,20 @@ function set_global_connection($company=-1) { - global $db, $transaction_level; + global $db, $transaction_level, $db_connections; cancel_transaction(); // cancel all aborted transactions if any $transaction_level = 0; - $db = $_SESSION["wa_current_user"]->get_db_connection($company); + if ($company == -1) + $company = $_SESSION["wa_current_user"]->company; + $_SESSION["wa_current_user"]->cur_con = $company; + + $connection = $db_connections[$company]; + + $db = mysql_connect($connection["host"], $connection["dbuser"], $connection["dbpassword"]); + mysql_select_db($connection["dbname"], $db); return $db; } @@ -153,4 +160,47 @@ function db_num_affected_rows() return mysql_affected_rows($db); } -?> +function db_field_name($result, $n) +{ + return mysql_field_name($result, $n); +} + +function db_create_db($connection) +{ + $db = mysql_connect($connection["host"] , + $connection["dbuser"], $connection["dbpassword"]); + if (!mysql_select_db($connection["dbname"], $db)) + { + $sql = "CREATE DATABASE " . $connection["dbname"] . ""; + if (!mysql_query($sql) || !mysql_select_db($connection["dbname"], $db)) + return 0; + } + return $db; +} + +function db_drop_db($connection) +{ + + if ($connection["tbpref"] == "") + { + $sql = "DROP DATABASE " . $connection["dbname"] . ""; + return mysql_query($sql); + } + else + { + $res = db_query("show table status"); + $all_tables = array(); + while($row = db_fetch($res)) + $all_tables[] = $row; + // get table structures + foreach ($all_tables as $table) + { + if (strpos($table['Name'], $connection["tbpref"]) === 0) + db_query("DROP TABLE `".$table['Name'] . "`"); + } + //deleting the tables, how?? + return true; + } +} + +?> \ No newline at end of file diff --git a/includes/db_pager.inc b/includes/db_pager.inc index 9e414aff..1629c47a 100644 --- a/includes/db_pager.inc +++ b/includes/db_pager.inc @@ -159,11 +159,11 @@ class db_pager { $c = 0; // add result field names to column defs for // col value retrieve and sort purposes - $cnt = min(mysql_num_fields($result), count($this->columns)); + $cnt = min(db_num_fields($result), count($this->columns)); for ($c = $i = 0; $c < $cnt; $c++) { if (!(isset($this->columns[$c]['insert']) && $this->columns[$c]['insert'])) { // if (!@($this->columns[$c]['type']=='skip')) - $this->columns[$c]['name']= mysql_field_name($result, $i); + $this->columns[$c]['name']= db_field_name($result, $i); if (!@($this->columns[$c]['type']=='insert')) $i++; } diff --git a/install/index.php b/install/index.php index 2b43068d..590b5b4d 100644 --- a/install/index.php +++ b/install/index.php @@ -117,24 +117,12 @@ function install_connect_db() { global $db; $conn = $_SESSION['inst_set']; - - $db = mysql_connect($conn["host"] , $conn["dbuser"], $conn["dbpassword"]); - if(!$db) { - display_error('Cannot connect to database server. Host name, username and/or password incorrect.'); - return false; - } - if (!defined('TB_PREF')) - define('TB_PREF', '&TB_PREF&'); - if (!mysql_select_db($conn["dbname"], $db)) { - $sql = "CREATE DATABASE " . $conn["dbname"]; - if (!mysql_query($sql)) { - display_error('Cannot create database. Check your permissions to database creation or selct already created database.'); - return false; - } - return mysql_select_db($conn["dbname"], $db); + $db = db_create_db($conn); + if (!$db) { + display_error(_("Cannot connect to database. User or password is invalid or you have no permittions to create database.")); } - return true; + return $db; } function do_install() {