function connect($server, $db, $user, $password, $socketPath, $charset = null, $port = false)
 {
     $connection = false;
     if ($socketPath !== false) {
         ini_set("mysqli.default_socket", $socketPath);
     }
     if ($this->UsePersistentConnection == true) {
         // Only supported on PHP 5.3 (mysqlnd)
         if (version_compare(PHP_VERSION, '5.3') > 0) {
             $this->Server = 'p:' . $this->Server;
         } else {
             eZDebug::writeWarning('mysqli only supports persistent connections when using php 5.3 and higher', 'eZMySQLiDB::connect');
         }
     }
     eZPerfLogger::accumulatorStart('mysqli_connection', 'mysqli_total', 'Database connection');
     $connection = mysqli_connect($server, $user, $password, null, (int) $port, $socketPath);
     $dbErrorText = mysqli_connect_error();
     eZPerfLogger::accumulatorStop('mysqli_connection');
     $maxAttempts = $this->connectRetryCount();
     $waitTime = $this->connectRetryWaitTime();
     $numAttempts = 1;
     while (!$connection && $numAttempts <= $maxAttempts) {
         sleep($waitTime);
         eZPerfLogger::accumulatorStart('mysqli_connection', 'mysqli_total', 'Database connection');
         $connection = mysqli_connect($this->Server, $this->User, $this->Password, null, (int) $this->Port, $this->SocketPath);
         eZPerfLogger::accumulatorStop('mysqli_connection');
         $numAttempts++;
     }
     $this->setError();
     $this->IsConnected = true;
     if (!$connection) {
         eZDebug::writeError("Connection error: Couldn't connect to database. Please try again later or inform the system administrator.\n{$dbErrorText}", __CLASS__);
         $this->IsConnected = false;
         throw new eZDBNoConnectionException($server);
     }
     if ($this->IsConnected && $db != null) {
         eZPerfLogger::accumulatorStart('mysqli_connection', 'mysqli_total', 'Database connection');
         $ret = mysqli_select_db($connection, $db);
         eZPerfLogger::accumulatorStop('mysqli_connection');
         if (!$ret) {
             //$this->setError();
             eZDebug::writeError("Connection error: " . mysqli_errno($connection) . ": " . mysqli_error($connection), "eZMySQLiDB");
             $this->IsConnected = false;
         }
     }
     if ($charset !== null) {
         $originalCharset = $charset;
         $charset = eZCharsetInfo::realCharsetCode($charset);
     }
     if ($this->IsConnected and $charset !== null) {
         eZPerfLogger::accumulatorStart('mysqli_connection', 'mysqli_total', 'Database connection');
         $status = mysqli_set_charset($connection, eZMySQLCharset::mapTo($charset));
         eZPerfLogger::accumulatorStop('mysqli_connection');
         if (!$status) {
             $this->setError();
             eZDebug::writeWarning("Connection warning: " . mysqli_errno($connection) . ": " . mysqli_error($connection), "eZMySQLiDB");
         }
     }
     return $connection;
 }
예제 #2
0
 /**
  * Reimplement parent's method to make use of a tracing dfs backend class
  */
 public function _connect()
 {
     $siteINI = eZINI::instance('site.ini');
     // DB Connection setup
     // This part is not actually required since _connect will only be called
     // once, but it is useful to run the unit tests. So be it.
     // @todo refactor this using eZINI::setVariable in unit tests
     if (parent::$dbparams === null) {
         $fileINI = eZINI::instance('file.ini');
         parent::$dbparams = array();
         parent::$dbparams['host'] = $fileINI->variable('eZDFSClusteringSettings', 'DBHost');
         $dbPort = $fileINI->variable('eZDFSClusteringSettings', 'DBPort');
         parent::$dbparams['port'] = $dbPort !== '' ? $dbPort : null;
         parent::$dbparams['socket'] = $fileINI->variable('eZDFSClusteringSettings', 'DBSocket');
         parent::$dbparams['dbname'] = $fileINI->variable('eZDFSClusteringSettings', 'DBName');
         parent::$dbparams['user'] = $fileINI->variable('eZDFSClusteringSettings', 'DBUser');
         parent::$dbparams['pass'] = $fileINI->variable('eZDFSClusteringSettings', 'DBPassword');
         parent::$dbparams['max_connect_tries'] = $fileINI->variable('eZDFSClusteringSettings', 'DBConnectRetries');
         parent::$dbparams['max_execute_tries'] = $fileINI->variable('eZDFSClusteringSettings', 'DBExecuteRetries');
         parent::$dbparams['sql_output'] = $siteINI->variable("DatabaseSettings", "SQLOutput") == "enabled";
         parent::$dbparams['cache_generation_timeout'] = $siteINI->variable("ContentSettings", "CacheGenerationTimeout");
     }
     $serverString = parent::$dbparams['host'];
     if (parent::$dbparams['socket']) {
         $serverString .= ':' . parent::$dbparams['socket'];
     } elseif (parent::$dbparams['port']) {
         $serverString .= ':' . parent::$dbparams['port'];
     }
     $maxTries = parent::$dbparams['max_connect_tries'];
     $tries = 0;
     eZPerfLogger::accumulatorStart('mysql_cluster_connect', 'MySQL Cluster', 'Cluster database connection');
     while ($tries < $maxTries) {
         if ($this->db = mysqli_connect(parent::$dbparams['host'], parent::$dbparams['user'], parent::$dbparams['pass'], parent::$dbparams['dbname'], parent::$dbparams['port'])) {
             break;
         }
         ++$tries;
     }
     eZPerfLogger::accumulatorStop('mysql_cluster_connect');
     if (!$this->db) {
         throw new eZClusterHandlerDBNoConnectionException($serverString, parent::$dbparams['user'], parent::$dbparams['pass']);
     }
     /*if ( !mysql_select_db( parent::$dbparams['dbname'], $this->db ) )
       throw new eZClusterHandlerDBNoDatabaseException( parent::$dbparams['dbname'] );*/
     // DFS setup
     if ($this->dfsbackend === null) {
         $this->dfsbackend = new eZDFSFileHandlerTracing46DFSBackend();
     }
     $charset = trim($siteINI->variable('DatabaseSettings', 'Charset'));
     if ($charset === '') {
         $charset = eZTextCodec::internalCharset();
     }
     if ($charset) {
         if (!mysqli_set_charset($this->db, eZMySQLCharset::mapTo($charset))) {
             $this->_fail("Failed to set Database charset to {$charset}.");
         }
     }
 }
예제 #3
0
파일: mysqli.php 프로젝트: legende91/ez
 function _connect()
 {
     $siteINI = eZINI::instance('site.ini');
     if (!isset($GLOBALS['eZDBFileHandlerMysqlBackend_dbparams'])) {
         $fileINI = eZINI::instance('file.ini');
         $params['host'] = $fileINI->variable('ClusteringSettings', 'DBHost');
         $params['port'] = $fileINI->variable('ClusteringSettings', 'DBPort');
         $params['socket'] = $fileINI->variable('ClusteringSettings', 'DBSocket');
         $params['dbname'] = $fileINI->variable('ClusteringSettings', 'DBName');
         $params['user'] = $fileINI->variable('ClusteringSettings', 'DBUser');
         $params['pass'] = $fileINI->variable('ClusteringSettings', 'DBPassword');
         $params['chunk_size'] = $fileINI->variable('ClusteringSettings', 'DBChunkSize');
         $params['max_connect_tries'] = $fileINI->variable('ClusteringSettings', 'DBConnectRetries');
         $params['max_execute_tries'] = $fileINI->variable('ClusteringSettings', 'DBExecuteRetries');
         $params['sql_output'] = $siteINI->variable("DatabaseSettings", "SQLOutput") == "enabled";
         $params['cache_generation_timeout'] = $siteINI->variable("ContentSettings", "CacheGenerationTimeout");
         $GLOBALS['eZDBFileHandlerMysqlBackend_dbparams'] = $params;
     } else {
         $params = $GLOBALS['eZDBFileHandlerMysqlBackend_dbparams'];
     }
     $this->dbparams = $params;
     $maxTries = $params['max_connect_tries'];
     $tries = 0;
     eZDebug::accumulatorStart('mysql_cluster_connect', 'mysql_cluster_total', 'Cluster_database_connection');
     while ($tries < $maxTries) {
         /// @todo what if port is null, '' ??? to be tested
         if ($this->db = mysqli_connect($params['host'], $params['user'], $params['pass'], $params['dbname'], $params['port'])) {
             break;
         }
         ++$tries;
     }
     eZDebug::accumulatorStop('mysql_cluster_connect');
     if (!$this->db) {
         return $this->_die("Unable to connect to storage server");
     }
     $charset = trim($siteINI->variable('DatabaseSettings', 'Charset'));
     if ($charset === '') {
         $charset = eZTextCodec::internalCharset();
     }
     if ($charset) {
         if (!mysqli_set_charset($this->db, eZMySQLCharset::mapTo($charset))) {
             return $this->_die("Failed to set Database charset to {$charset}.");
         }
     }
 }
예제 #4
0
파일: mysql.php 프로젝트: rmiguel/ezpublish
 function _connect($newLink = false)
 {
     $siteINI = eZINI::instance('site.ini');
     if (!isset($GLOBALS['eZDBFileHandlerMysqlBackend_dbparams'])) {
         $fileINI = eZINI::instance('file.ini');
         $params['host'] = $fileINI->variable('ClusteringSettings', 'DBHost');
         $params['port'] = $fileINI->variable('ClusteringSettings', 'DBPort');
         $params['socket'] = $fileINI->variable('ClusteringSettings', 'DBSocket');
         $params['dbname'] = $fileINI->variable('ClusteringSettings', 'DBName');
         $params['user'] = $fileINI->variable('ClusteringSettings', 'DBUser');
         $params['pass'] = $fileINI->variable('ClusteringSettings', 'DBPassword');
         $params['chunk_size'] = $fileINI->variable('ClusteringSettings', 'DBChunkSize');
         $params['max_connect_tries'] = $fileINI->variable('ClusteringSettings', 'DBConnectRetries');
         $params['max_execute_tries'] = $fileINI->variable('ClusteringSettings', 'DBExecuteRetries');
         $params['sql_output'] = $siteINI->variable("DatabaseSettings", "SQLOutput") == "enabled";
         $params['cache_generation_timeout'] = $siteINI->variable("ContentSettings", "CacheGenerationTimeout");
         $GLOBALS['eZDBFileHandlerMysqlBackend_dbparams'] = $params;
     } else {
         $params = $GLOBALS['eZDBFileHandlerMysqlBackend_dbparams'];
     }
     $this->dbparams = $params;
     $serverString = $params['host'];
     if ($params['socket']) {
         $serverString .= ':' . $params['socket'];
     } elseif ($params['port']) {
         $serverString .= ':' . $params['port'];
     }
     $maxTries = $params['max_connect_tries'];
     $tries = 0;
     while ($tries < $maxTries) {
         if ($this->db = mysql_connect($serverString, $params['user'], $params['pass'], $newLink)) {
             break;
         }
         ++$tries;
     }
     if (!$this->db) {
         return $this->_die("Unable to connect to storage server");
     }
     if (!mysql_select_db($params['dbname'], $this->db)) {
         return $this->_die("Unable to select database {$params['dbname']}");
     }
     $charset = trim($siteINI->variable('DatabaseSettings', 'Charset'));
     if ($charset === '') {
         $charset = eZTextCodec::internalCharset();
     }
     if ($charset) {
         if (!mysql_query("SET NAMES '" . eZMySQLCharset::mapTo($charset) . "'", $this->db)) {
             return $this->_die("Failed to set Database charset to {$charset}.");
         }
     }
 }
예제 #5
0
    function checkCharsetPriv( $charset, &$currentCharset )
    {
        $query = "SHOW CREATE DATABASE `{$this->DB}`";
        $status = mysql_query( $query, $this->DBConnection );
        $this->reportQuery( __CLASS__, $query, false, false );
        if ( !$status )
        {
            $this->setError();
            eZDebug::writeWarning( "Connection warning: " . mysql_errno( $this->DBConnection ) . ": " . mysql_error( $this->DBConnection ), "eZMySQLDB" );
            return false;
        }

        $numRows = mysql_num_rows( $status );
        if ( $numRows == 0 )
            return false;

        for ( $i = 0; $i < $numRows; ++$i )
        {
            $tmpRow = mysql_fetch_array( $status, MYSQL_ASSOC );
            if ( $tmpRow['Database'] == $this->DB )
            {
                $createText = $tmpRow['Create Database'];
                if ( preg_match( '#DEFAULT CHARACTER SET ([a-zA-Z0-9_-]+)#', $createText, $matches ) )
                {
                    $currentCharset = $matches[1];
                    $currentCharset = eZCharsetInfo::realCharsetCode( $currentCharset );
                    // Convert charset names into something MySQL will understand
                    $unmappedCurrentCharset = eZMySQLCharset::mapFrom( $currentCharset );

                    if ( is_array( $charset ) )
                    {
                        if ( in_array( $unmappedCurrentCharset, $charset ) )
                        {
                            return $unmappedCurrentCharset;
                        }
                    }
                    else if ( $unmappedCurrentCharset == $charset )
                    {
                        return true;
                    }
                    return false;
                }
                break;
            }
        }
        return true;
    }
예제 #6
0
 public function testUnknownUppercaseCharsetMapFrom()
 {
     $this->assertSame( "uNknOwN", eZMySQLCharset::mapFrom( "uNknOwN" ) );
 }
예제 #7
0
    /**
     * Connects to the database.
     *
     * @return void
     * @throw eZClusterHandlerDBNoConnectionException
     * @throw eZClusterHandlerDBNoDatabaseException
     */
    public function _connect()
    {
        $siteINI = eZINI::instance( 'site.ini' );
        // DB Connection setup
        // This part is not actually required since _connect will only be called
        // once, but it is useful to run the unit tests. So be it.
        // @todo refactor this using eZINI::setVariable in unit tests
        if ( self::$dbparams === null )
        {
            $fileINI = eZINI::instance( 'file.ini' );

            self::$dbparams = array();
            self::$dbparams['host']       = $fileINI->variable( 'eZDFSClusteringSettings', 'DBHost' );
            self::$dbparams['port']       = $fileINI->variable( 'eZDFSClusteringSettings', 'DBPort' );
            self::$dbparams['socket']     = $fileINI->variable( 'eZDFSClusteringSettings', 'DBSocket' );
            self::$dbparams['dbname']     = $fileINI->variable( 'eZDFSClusteringSettings', 'DBName' );
            self::$dbparams['user']       = $fileINI->variable( 'eZDFSClusteringSettings', 'DBUser' );
            self::$dbparams['pass']       = $fileINI->variable( 'eZDFSClusteringSettings', 'DBPassword' );

            self::$dbparams['max_connect_tries'] = $fileINI->variable( 'eZDFSClusteringSettings', 'DBConnectRetries' );
            self::$dbparams['max_execute_tries'] = $fileINI->variable( 'eZDFSClusteringSettings', 'DBExecuteRetries' );

            self::$dbparams['sql_output'] = $siteINI->variable( "DatabaseSettings", "SQLOutput" ) == "enabled";

            self::$dbparams['cache_generation_timeout'] = $siteINI->variable( "ContentSettings", "CacheGenerationTimeout" );
        }

        $serverString = self::$dbparams['host'];
        if ( self::$dbparams['socket'] )
            $serverString .= ':' . self::$dbparams['socket'];
        elseif ( self::$dbparams['port'] )
            $serverString .= ':' . self::$dbparams['port'];

        $maxTries = self::$dbparams['max_connect_tries'];
        $tries = 0;
        while ( $tries < $maxTries )
        {
            if ( $this->db = mysql_connect( $serverString, self::$dbparams['user'], self::$dbparams['pass'] ) )
                break;
            ++$tries;
        }
        if ( !$this->db )
            throw new eZClusterHandlerDBNoConnectionException( $serverString, self::$dbparams['user'], self::$dbparams['pass'] );

        if ( !mysql_select_db( self::$dbparams['dbname'], $this->db ) )
            throw new eZClusterHandlerDBNoDatabaseException( self::$dbparams['dbname'] );

        // DFS setup
        if ( $this->dfsbackend === null )
        {
            $this->dfsbackend = new eZDFSFileHandlerDFSBackend();
        }

        $charset = trim( $siteINI->variable( 'DatabaseSettings', 'Charset' ) );
        if ( $charset === '' )
        {
            $charset = eZTextCodec::internalCharset();
        }

        if ( $charset )
        {
            if ( !mysql_query( "SET NAMES '" . eZMySQLCharset::mapTo( $charset ) . "'", $this->db ) )
            {
                $this->_fail( "Failed to set Database charset to $charset." );
            }
        }
    }