/** * function that will be called on every static call * which connects to the database if necessary * * @param bool $root * * @return object */ private static function getDB() { if (!extension_loaded('pdo') || in_array("mysql", PDO::getAvailableDrivers()) == false) { self::_showerror(new Exception("The php PDO extension or PDO-MySQL driver is not available")); } // do we got a connection already? if (self::$_link) { // return it return self::$_link; } // include userdata.inc.php require FROXLOR_INSTALL_DIR . "/lib/userdata.inc.php"; // le format if (self::$_needroot == true && isset($sql['root_user']) && isset($sql['root_password']) && (!isset($sql_root) || !is_array($sql_root))) { $sql_root = array(0 => array('caption' => 'Default', 'host' => $sql['host'], 'socket' => isset($sql['socket']) ? $sql['socket'] : null, 'user' => $sql['root_user'], 'password' => $sql['root_password'])); unset($sql['root_user']); unset($sql['root_password']); } // either root or unprivileged user if (self::$_needroot) { $caption = $sql_root[self::$_dbserver]['caption']; $user = $sql_root[self::$_dbserver]['user']; $password = $sql_root[self::$_dbserver]['password']; $host = $sql_root[self::$_dbserver]['host']; $socket = isset($sql_root[self::$_dbserver]['socket']) ? $sql_root[self::$_dbserver]['socket'] : null; $port = isset($sql_root[self::$_dbserver]['port']) ? $sql_root[self::$_dbserver]['port'] : '3306'; } else { $caption = 'localhost'; $user = $sql["user"]; $password = $sql["password"]; $host = $sql["host"]; $socket = isset($sql['socket']) ? $sql['socket'] : null; $port = isset($sql['port']) ? $sql['port'] : '3306'; } // save sql-access-data if needed if (self::$_needsqldata) { self::$_sqldata = array('user' => $user, 'passwd' => $password, 'host' => $host, 'port' => $port, 'socket' => $socket, 'db' => $sql["db"], 'caption' => $caption); } // build up connection string $driver = 'mysql'; $dsn = $driver . ":"; $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'set names utf8'); $attributes = array('ATTR_ERRMODE' => 'ERRMODE_EXCEPTION'); $dbconf["dsn"] = array('dbname' => $sql["db"], 'charset' => 'utf8'); if ($socket != null) { $dbconf["dsn"]['unix_socket'] = makeCorrectFile($socket); } else { $dbconf["dsn"]['host'] = $host; $dbconf["dsn"]['port'] = $port; } self::$_dbname = $sql["db"]; // add options to dsn-string foreach ($dbconf["dsn"] as $k => $v) { $dsn .= $k . "=" . $v . ";"; } // clean up unset($dbconf); // try to connect try { self::$_link = new PDO($dsn, $user, $password, $options); } catch (PDOException $e) { self::_showerror($e); } // set attributes foreach ($attributes as $k => $v) { self::$_link->setAttribute(constant("PDO::" . $k), constant("PDO::" . $v)); } // return PDO instance return self::$_link; }
/** * function that will be called on every static call * which connects to the database if necessary * * * @return object */ private static function getDB() { if (!extension_loaded('pdo') || in_array("mysql", PDO::getAvailableDrivers()) == false) { self::_showerror(new Exception("The php PDO extension or PDO-MySQL driver is not available")); } // do we got a connection already? if (self::$_link) { // return it return self::$_link; } // get the data from user data file $sql_root = self::getSqlRootArrayFromUserDataFile(); $sql = self::getSqlArrayFromUserDataFile(); if (self::$_needroot && !is_array($sql_root)) { self::_showerror(new Exception("No data available in array sql_root.")); } // either root or unprivileged user if (self::$_needroot) { $caption = $sql_root[self::$_dbserver]['caption']; $user = $sql_root[self::$_dbserver]['user']; $password = $sql_root[self::$_dbserver]['password']; $host = $sql_root[self::$_dbserver]['host']; $socket = isset($sql_root[self::$_dbserver]['socket']) ? $sql_root[self::$_dbserver]['socket'] : null; $port = isset($sql_root[self::$_dbserver]['port']) ? $sql_root[self::$_dbserver]['port'] : '3306'; } else { $caption = 'localhost'; $user = $sql["user"]; $password = $sql["password"]; $host = $sql["host"]; $socket = isset($sql['socket']) ? $sql['socket'] : null; $port = isset($sql['port']) ? $sql['port'] : '3306'; } // save sql-access-data if needed if (self::$_needsqldata) { self::$_sqldata = array('user' => $user, 'passwd' => $password, 'host' => $host, 'port' => $port, 'socket' => $socket, 'db' => $sql["db"], 'caption' => $caption); } // build up connection string $driver = 'mysql'; $dsn = $driver . ":"; $options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'set names utf8'); $attributes = array('ATTR_ERRMODE' => 'ERRMODE_EXCEPTION'); $dbconf["dsn"] = array('dbname' => $sql["db"], 'charset' => 'utf8'); if ($socket != null) { $dbconf["dsn"]['unix_socket'] = makeCorrectFile($socket); } else { $dbconf["dsn"]['host'] = $host; $dbconf["dsn"]['port'] = $port; } self::$_dbname = $sql["db"]; // for a connection to an external database server e.g. to create a new database // we must not specify the froxlor database if (self::$_linkWithoutDatabase == true) { unset(self::$_sqldata['db']); unset($dbconf["dsn"]['dbname']); self::$_dbname = null; } // add options to dsn-string foreach ($dbconf["dsn"] as $k => $v) { $dsn .= $k . "=" . $v . ";"; } // clean up unset($dbconf); // try to connect try { self::$_link = new PDO($dsn, $user, $password, $options); } catch (PDOException $e) { self::_showerror($e); } // set attributes foreach ($attributes as $k => $v) { self::$_link->setAttribute(constant("PDO::" . $k), constant("PDO::" . $v)); } // return PDO instance return self::$_link; }