Aleksander Machniak
2015-03-12 8f74b23dd23d2cdbe723158dc0666bff6bd62887
program/lib/Roundcube/rcube_db.php
@@ -1,6 +1,6 @@
<?php
/**
/*
 +-----------------------------------------------------------------------+
 | This file is part of the Roundcube Webmail client                     |
 | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
@@ -70,6 +70,7 @@
            'dblib'   => 'mssql',
            'mysqli'  => 'mysql',
            'oci'     => 'oracle',
            'oci8'    => 'oracle',
        );
        $driver = isset($driver_map[$driver]) ? $driver_map[$driver] : $driver;
@@ -130,6 +131,20 @@
            return $this->dbh;
        }
        // connect to database
        if ($dbh = $this->conn_create($dsn)) {
            $this->dbh          = $dbh;
            $this->dbhs[$mode]  = $dbh;
            $this->db_mode      = $mode;
            $this->db_connected = true;
        }
    }
    /**
     * Create PDO connection
     */
    protected function conn_create($dsn)
    {
        // Get database specific connection options
        $dsn_string  = $this->dsn_string($dsn);
        $dsn_options = $this->dsn_options($dsn);
@@ -151,6 +166,8 @@
            // don't throw exceptions or warnings
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT);
            $this->conn_configure($dsn, $dbh);
        }
        catch (Exception $e) {
            $this->db_error     = true;
@@ -163,11 +180,7 @@
            return null;
        }
        $this->dbh          = $dbh;
        $this->dbhs[$mode]  = $dbh;
        $this->db_mode      = $mode;
        $this->db_connected = true;
        $this->conn_configure($dsn, $dbh);
        return $dbh;
    }
    /**
@@ -344,7 +357,7 @@
    public function get_variable($varname, $default = null)
    {
        // to be implemented by driver class
        return $default;
        return rcube::get_instance()->config->get('db_' . $varname, $default);
    }
    /**
@@ -435,15 +448,28 @@
            }
        }
        // replace escaped '?' back to normal, see self::quote()
        $query = str_replace('??', '?', $query);
        $query = rtrim($query, " \t\n\r\0\x0B;");
        // replace escaped '?' and quotes back to normal, see self::quote()
        $query = str_replace(
            array('??', self::DEFAULT_QUOTE.self::DEFAULT_QUOTE),
            array('?', self::DEFAULT_QUOTE),
            $query
        );
        // log query
        $this->debug($query);
        return $this->query_execute($query);
    }
    /**
     * Query execution
     */
    protected function query_execute($query)
    {
        // destroy reference to previous result, required for SQLite driver (#1488874)
        $this->last_result = null;
        $this->last_result  = null;
        $this->db_error_msg = null;
        // send query
@@ -453,9 +479,7 @@
            $result = $this->handle_error($query);
        }
        $this->last_result = $result;
        return $result;
        return $this->last_result = $result;
    }
    /**
@@ -497,9 +521,6 @@
            }
        }
        // replace escaped quote back to normal, see self::quote()
        $query = str_replace($quote.$quote, $quote, $query);
        return $query;
    }
@@ -537,7 +558,9 @@
    public function affected_rows($result = null)
    {
        if ($result || ($result === null && ($result = $this->last_result))) {
            return $result->rowCount();
            if ($result !== true) {
                return $result->rowCount();
            }
        }
        return 0;
@@ -553,7 +576,7 @@
     */
    public function num_rows($result = null)
    {
        if ($result || ($result === null && ($result = $this->last_result))) {
        if (($result || ($result === null && ($result = $this->last_result))) && $result !== true) {
            // repeat query with SELECT COUNT(*) ...
            if (preg_match('/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/ims', $result->queryString, $m)) {
                $query = $this->dbh->query('SELECT COUNT(*) FROM ' . $m[1], PDO::FETCH_NUM);
@@ -629,7 +652,9 @@
    protected function _fetch_row($result, $mode)
    {
        if ($result || ($result === null && ($result = $this->last_result))) {
            return $result->fetch($mode);
            if ($result !== true) {
                return $result->fetch($mode);
            }
        }
        return false;
@@ -696,6 +721,63 @@
        }
        return array();
    }
    /**
     * Start transaction
     *
     * @return bool True on success, False on failure
     */
    public function startTransaction()
    {
        $this->db_connect('w', true);
        // check connection before proceeding
        if (!$this->is_connected()) {
            return $this->last_result = false;
        }
        $this->debug('BEGIN TRANSACTION');
        return $this->last_result = $this->dbh->beginTransaction();
    }
    /**
     * Commit transaction
     *
     * @return bool True on success, False on failure
     */
    public function endTransaction()
    {
        $this->db_connect('w', true);
        // check connection before proceeding
        if (!$this->is_connected()) {
            return $this->last_result = false;
        }
        $this->debug('COMMIT TRANSACTION');
        return $this->last_result = $this->dbh->commit();
    }
    /**
     * Rollback transaction
     *
     * @return bool True on success, False on failure
     */
    public function rollbackTransaction()
    {
        $this->db_connect('w', true);
        // check connection before proceeding
        if (!$this->is_connected()) {
            return $this->last_result = false;
        }
        $this->debug('ROLLBACK TRANSACTION');
        return $this->last_result = $this->dbh->rollBack();
    }
    /**
@@ -983,6 +1065,10 @@
     */
    public function table_name($table, $quoted = false)
    {
        // let plugins alter the table name (#1489837)
        $plugin = rcube::get_instance()->plugins->exec_hook('db_table_name', array('table' => $table));
        $table = $plugin['table'];
        // add prefix to the table name if configured
        if (($prefix = $this->options['table_prefix']) && strpos($table, $prefix) !== 0) {
            $table = $prefix . $table;