Esempio n. 1
0
 function getConnection(array $credentials)
 {
     // Set the host string for PDO dsn.
     $db_hoststring = "host={$credentials['host']}";
     // If using mysql and it is set to use sockets instead of hostname,
     // you can only use one method to connect, not both.  If $socket_tiki
     // is set in local.php, then it will override the hostname method
     // of connecting to the database.
     if ($credentials['socket']) {
         $db_hoststring = "unix_socket={$credentials['socket']}";
     }
     $conn = false;
     $pdo_options = array();
     $pdo_post_queries = array();
     if ($credentials['charset']) {
         $charset_query = "SET NAMES {$credentials['charset']}";
         if (defined('PDO::MYSQL_ATTR_INIT_COMMAND')) {
             $pdo_options[PDO::MYSQL_ATTR_INIT_COMMAND] = $charset_query;
         } else {
             $pdo_post_queries[] = $charset_query;
         }
         unset($charset_query);
     }
     $dbTiki = new PDO("mysql:{$db_hoststring};dbname={$credentials['dbs']}", $credentials['user'], $credentials['pass'], $pdo_options);
     $dbTiki->setAttribute(PDO::ATTR_CASE, PDO::CASE_NATURAL);
     $dbTiki->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
     //	$dbTiki->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING);
     $db = new TikiDb_Pdo($dbTiki);
     foreach ($pdo_post_queries as $query) {
         $db->query($query);
     }
     return $db;
 }
Esempio n. 2
0
function initTikiDB(&$api, &$driver, $host, $user, $pass, $dbname, $client_charset, &$dbTiki)
{
    global $tikifeedback;
    $dbcon = false;
    // This section handles the case of adodb (not the preferred case)
    if (isset($api) && $api == 'adodb' || !extension_loaded('pdo')) {
        $api = 'adodb';
        $dbTiki = ADONewConnection($driver);
        $db = new TikiDb_Adodb($dbTiki);
        if (!($dbcon = (bool) @$dbTiki->Connect($host, $user, $pass, $dbname))) {
            $tikifeedback[] = array('num' => 1, 'mes' => $dbTiki->ErrorMsg());
        }
        // Attempt to create database. This might work if the $user has create database permissions.
        // First check that suggested database name will not cause issues
        $dbname_clean = preg_replace('/[^a-z0-9$_-]/', "", $dbname);
        if ($dbname_clean != $dbname) {
            $tikifeedback[] = array('num' => 1, 'mes' => tra("Some invalid characters were detected in database name. Please use alphanumeric characters or _ or -.", '', false, array($dbname_clean)));
            $attempt_creation = false;
        } else {
            $attempt_creation = true;
        }
        if (!$dbcon && $attempt_creation == true) {
            $dbh = ADONewConnection($driver);
            if (@$dbh->Connect($host, $user, $pass)) {
                $sql = "CREATE DATABASE IF NOT EXISTS `{$dbname_clean}` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;";
                $dbcon = $dbh->Execute($sql);
                if ($dbcon) {
                    $tikifeedback[] = array('num' => 1, 'mes' => tra("Database `%0` was created.", '', false, array($dbname_clean)));
                } else {
                    $tikifeedback[] = array('num' => 1, 'mes' => tra("Database `%0` creation failed. You need to create the database.", '', false, array($dbname_clean)));
                }
            } else {
                $tikifeedback[] = array('num' => 1, 'mes' => $dbh->ErrorMsg());
            }
            if ($dbcon) {
                $dbTiki = ADONewConnection($driver);
                $db = new TikiDb_Adodb($dbTiki);
                if (!($dbcon = (bool) @$dbTiki->Connect($host, $user, $pass, $dbname))) {
                    $tikifeedback[] = array('num' => 1, 'mes' => $dbTiki->ErrorMsg());
                }
            }
        }
        // This section handles the case of PDO (preferred case)
    } else {
        $db_hoststring = "host={$host}";
        if ($driver == 'mysqli') {
            $driver = 'mysql';
            if (isset($socket_tiki)) {
                $db_hoststring = "unix_socket={$socket_tiki}";
            }
        }
        try {
            $dbTiki = new PDO("{$driver}:{$db_hoststring};dbname={$dbname}", $user, $pass);
            $db = new TikiDb_Pdo($dbTiki);
            $dbcon = true;
        } catch (PDOException $e) {
            $dbcon = false;
            $tikifeedback[] = array('num' => 1, 'mes' => $e->getMessage());
        }
        // Attempt to create database. This might work if the $user has create database permissions.
        // First check that suggested database name will not cause issues
        $dbname_clean = preg_replace('/[^a-z0-9$_-]/', "", $dbname);
        if ($dbname_clean != $dbname) {
            $tikifeedback[] = array('num' => 1, 'mes' => tra("Some invalid characters were detected in database name. Please use alphanumeric characters or _ or -.", '', false, array($dbname_clean)));
            $attempt_creation = false;
        } else {
            $attempt_creation = true;
        }
        if (!$dbcon && $attempt_creation == true) {
            try {
                $dbh = new PDO("{$driver}:{$db_hoststring}", $user, $pass);
                $sql = "CREATE DATABASE IF NOT EXISTS `{$dbname_clean}` DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;";
                $dbcon = $dbh->exec($sql);
                if ($dbcon) {
                    $tikifeedback[] = array('num' => 1, 'mes' => tra("Database `%0` was created.", '', false, array($dbname_clean)));
                } else {
                    $tikifeedback[] = array('num' => 1, 'mes' => tra("Database `%0` creation failed. You need to create the database.", '', false, array($dbname_clean)));
                }
            } catch (PDOException $e) {
                $dbcon = false;
                $tikifeedback[] = array('num' => 1, 'mes' => $e->getMessage());
            }
            if ($dbcon) {
                try {
                    $dbTiki = new PDO("{$driver}:{$db_hoststring};dbname={$dbname}", $user, $pass);
                    $db = new TikiDb_Pdo($dbTiki);
                } catch (PDOException $e) {
                    $dbcon = false;
                    $tikifeedback[] = array('num' => 1, 'mes' => $e->getMessage());
                }
            }
        }
    }
    if ($dbcon) {
        $db->setErrorHandler(new InstallerDatabaseErrorHandler());
        if (!empty($client_charset)) {
            $db->query("SET CHARACTER SET {$client_charset}");
        }
        TikiDb::set($db);
    }
    return $dbcon;
}