function db_connect($host, $user, $passwd, $options = array()) { global $__db; //Assert if (!strlen($user) || !strlen($host)) { return NULL; } if (!($__db = mysqli_init())) { return NULL; } // Setup SSL if enabled if (isset($options['ssl'])) { $__db->ssl_set($options['ssl']['key'], $options['ssl']['cert'], $options['ssl']['ca'], null, null); } elseif (!$passwd) { return NULL; } $port = ini_get("mysqli.default_port"); $socket = ini_get("mysqli.default_socket"); $persistent = stripos($host, 'p:') === 0; if ($persistent) { $host = substr($host, 2); } if (strpos($host, ':') !== false) { list($host, $portspec) = explode(':', $host); // PHP may not honor the port number if connecting to 'localhost' if ($portspec && is_numeric($portspec)) { if (!strcasecmp($host, 'localhost')) { // XXX: Looks like PHP gethostbyname() is IPv4 only $host = gethostbyname($host); } $port = (int) $portspec; } elseif ($portspec) { $socket = $portspec; } } if ($persistent) { $host = 'p:' . $host; } // Connect $start = microtime(true); if (!@$__db->real_connect($host, $user, $passwd, null, $port, $socket)) { return NULL; } //Select the database, if any. if (isset($options['db'])) { $__db->select_db($options['db']); } //set desired encoding just in case mysql charset is not UTF-8 - Thanks to FreshMedia @$__db->query('SET NAMES "utf8"'); @$__db->query('SET CHARACTER SET "utf8"'); @$__db->query('SET COLLATION_CONNECTION=utf8_general_ci'); $__db->set_charset('utf8'); @db_set_variable('sql_mode', ''); $__db->autocommit(true); // Use connection timing to seed the random number generator Misc::__rand_seed((microtime(true) - $start) * 1000000); return $__db; }
function db_connect($host, $user, $passwd, $db = "") { //Assert if (!strlen($user) || !strlen($passwd) || !strlen($host)) { return NULL; } //Connect if (!($dblink = @mysql_connect($host, $user, $passwd))) { return NULL; } //Select the database, if any. if ($db) { db_select_database($db); } //set desired encoding just in case mysql charset is not UTF-8 - Thanks to FreshMedia @mysql_query('SET NAMES "UTF8"'); @mysql_query('SET COLLATION_CONNECTION=utf8_general_ci'); @db_set_variable('sql_mode', ''); return $dblink; }
function db_connect($host, $user, $passwd, $options = array()) { //Assert if (!strlen($user) || !strlen($passwd) || !strlen($host)) { return NULL; } //Connect $start = (double) microtime() * 1000000; if (!($dblink = @mysql_connect($host, $user, $passwd))) { return NULL; } //Select the database, if any. if ($options['db']) { db_select_database($options['db']); } //set desired encoding just in case mysql charset is not UTF-8 - Thanks to FreshMedia @mysql_query('SET NAMES "utf8"'); @mysql_query('SET CHARACTER SET "utf8"'); @mysql_query('SET COLLATION_CONNECTION=utf8_general_ci'); @db_set_variable('sql_mode', ''); // Use connection timing to seed the random number generator Misc::__rand_seed((double) microtime() * 1000000 - $start); return $dblink; }
function save($file) { if (!$file['hash']) { $file['hash'] = MD5(MD5($file['data']) . time()); } if (!$file['size']) { $file['size'] = strlen($file['data']); } //TODO: Do chunked INSERTs - if (($mps = db_get_variable('max_allowed_packet')) && $file['size'] > $mps * 0.7) { @db_set_variable('max_allowed_packet', $file['size'] + $mps); } $sql = 'INSERT INTO ' . FILE_TABLE . ' SET created=NOW() ' . ',type=' . db_input($file['type']) . ',size=' . db_input($file['size']) . ',name=' . db_input($file['name']) . ',hash=' . db_input($file['hash']) . ',filedata=' . db_input($file['data']); return db_query($sql) ? db_insert_id() : 0; }