/**
 * Create new database. Existing database will be dropped.
 * Oracle databases must be created manually (create the tcexam user and set the database name equal to user name)
 * @param $host (string) Database server path. It can also include a port number. e.g. "hostname:port" or a path to a local socket e.g. ":/path/to/socket" for the localhost. Note: Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost". If the MySQL client library tries to connect to the wrong local socket, you should set the correct path as mysql.default_host in your PHP configuration and leave the server field blank.
 * @param $dbtype (string) database type ('MYSQL' or 'POSTGREQL')
 * @param $host (string) database host
 * @param $port (string) database port
 * @param $user (string) Name of the user that owns the server process.
 * @param $password (string) Password of the user that owns the server process.
 * @param $database (string) Database name.
 * @param $table_prefix (string) prefix for tables
 * @param $drop (boolean) if true drop existing database
 * @param $create (boolean) if true creates new database
 * @return database link identifier on success, FALSE otherwise.
 */
function F_create_database($dbtype, $host, $port, $user, $password, $database, $table_prefix, $drop, $create)
{
    // open default connection
    if ($drop or $create) {
        if ($db = @F_db_connect($host, $port, $user, $password)) {
            if ($dbtype == 'ORACLE') {
                if ($drop) {
                    $table_prefix = strtoupper($table_prefix);
                    // DROP sequences
                    $sql = 'select \'DROP SEQUENCE \'||sequence_name||\'\' from user_sequences where sequence_name like \'' . $table_prefix . '%\'';
                    if ($r = @F_db_query($sql, $db)) {
                        while ($m = @F_db_fetch_array($r)) {
                            @F_db_query($m[0], $db);
                        }
                    }
                    // DROP triggers
                    $sql = 'select \'DROP TRIGGER \'||trigger_name||\'\' from user_triggers where trigger_name like \'' . $table_prefix . '%\'';
                    if ($r = @F_db_query($sql, $db)) {
                        while ($m = @F_db_fetch_array($r)) {
                            @F_db_query($m[0], $db);
                        }
                    }
                    // DROP tables
                    $sql = 'select \'DROP TABLE \'||table_name||\' CASCADE CONSTRAINTS\' from user_tables where table_name like \'' . $table_prefix . '%\'';
                    if ($r = @F_db_query($sql, $db)) {
                        while ($m = @F_db_fetch_array($r)) {
                            @F_db_query($m[0], $db);
                        }
                    }
                } else {
                    echo '<span style="color:#000080">[SKIP DROP]</span> ';
                }
                // Note: Oracle Database automatically creates a schema when you create a user,
                //       so you have to create a tcexam user before calling this.
            } else {
                if ($drop) {
                    // DROP existing database (if exist)
                    @F_db_query('DROP DATABASE ' . $database . '', $db);
                } else {
                    echo '<span style="color:#000080">[SKIP DROP]</span> ';
                }
                if ($create) {
                    // create database
                    $sql = 'CREATE DATABASE ' . $database . '';
                    if ($dbtype == 'MYSQL') {
                        $sql .= ' CHARACTER SET utf8 COLLATE utf8_unicode_ci';
                    } elseif ($dbtype == 'POSTGRESQL') {
                        $sql .= ' ENCODING=\'UNICODE\'';
                    }
                    if (!($r = @F_db_query($sql, $db))) {
                        return FALSE;
                    }
                } else {
                    echo '<span style="color:#000080">[SKIP CREATE]</span> ';
                }
            }
            @F_db_close($db);
        } else {
            return FALSE;
        }
    } else {
        echo '<span style="color:#000080">[SKIP DROP AND CREATE]</span> ';
    }
    if ($db = @F_db_connect($host, $port, $user, $password, $database)) {
        return $db;
    } else {
        return FALSE;
    }
}
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU Affero General Public License for more details.
//
//    You should have received a copy of the GNU Affero General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
//
//    Additionally, you can't remove, move or hide the original TCExam logo,
//    copyrights statements and links to Tecnick.com and TCExam websites.
//
//    See LICENSE.TXT file for more information.
//============================================================+
/**
 * @file
 * Open a connection to a MySQL Server and select a database.
 * @package com.tecnick.tcexam.shared
 * @author Nicola Asuni
 * @since 2001-09-02
 */
/**
 */
require_once '../../shared/code/tce_db_dal.php';
// Database Abstraction Layer for selected DATABASE type
if (!($db = @F_db_connect(K_DATABASE_HOST, K_DATABASE_PORT, K_DATABASE_USER_NAME, K_DATABASE_USER_PASSWORD, K_DATABASE_NAME))) {
    die('<h2>' . F_db_error() . '</h2>');
}
//============================================================+
// END OF FILE
//============================================================+
/**
 * Insert or Update session.
 * @param $key (string) session ID.
 * @param $val (string) session data.
 * @return resource database query result.
 */
function F_session_write($key, $val)
{
    global $db;
    if (!isset($db) or !$db) {
        // workaround for PHP bug 41230
        if (!($db = @F_db_connect(K_DATABASE_HOST, K_DATABASE_PORT, K_DATABASE_USER_NAME, K_DATABASE_USER_PASSWORD, K_DATABASE_NAME))) {
            return;
        }
    }
    $key = F_escape_sql($key);
    $val = F_escape_sql($val);
    $expiry = date(K_TIMESTAMP_FORMAT, time() + K_SESSION_LIFE);
    // check if this session already exist on database
    $sql = 'SELECT cpsession_id
			FROM ' . K_TABLE_SESSIONS . '
			WHERE cpsession_id=\'' . $key . '\'
			LIMIT 1';
    if ($r = F_db_query($sql, $db)) {
        if ($m = F_db_fetch_array($r)) {
            // SQL to update existing session
            $sqlup = 'UPDATE ' . K_TABLE_SESSIONS . ' SET
				cpsession_expiry=\'' . $expiry . '\',
				cpsession_data=\'' . $val . '\'
				WHERE cpsession_id=\'' . $key . '\'';
        } else {
            // SQL to insert new session
            $sqlup = 'INSERT INTO ' . K_TABLE_SESSIONS . ' (
				cpsession_id,
				cpsession_expiry,
				cpsession_data
				) VALUES (
				\'' . $key . '\',
				\'' . $expiry . '\',
				\'' . $val . '\'
				)';
        }
    }
    return F_db_query($sqlup, $db);
}