OAuthStoreMySQLi.php in Lingotek Translation 7.4
File
lib/oauth-php/library/store/OAuthStoreMySQLi.php
View source
<?php
require_once dirname(__FILE__) . '/OAuthStoreMySQL.php';
class OAuthStoreMySQLi extends OAuthStoreMySQL {
public function install() {
$sql = file_get_contents(dirname(__FILE__) . '/mysql/mysql.sql');
$ps = explode('#--SPLIT--', $sql);
foreach ($ps as $p) {
$p = preg_replace('/^\\s*#.*$/m', '', $p);
$this
->query($p);
$this
->sql_errcheck($p);
}
}
function __construct($options = array()) {
if (isset($options['conn'])) {
$this->conn = $options['conn'];
}
else {
if (isset($options['server'])) {
$server = $options['server'];
$username = $options['username'];
if (isset($options['password'])) {
$this->conn = $GLOBALS["___mysqli_ston"] = mysqli_connect($server, $username, $options['password']);
}
else {
$this->conn = $GLOBALS["___mysqli_ston"] = mysqli_connect($server, $username);
}
}
else {
$this->conn = $GLOBALS["___mysqli_ston"] = mysqli_connect();
}
if ($this->conn === false) {
throw new OAuthException2('Could not connect to MySQL database: ' . (is_object($GLOBALS["___mysqli_ston"]) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));
}
if (isset($options['database'])) {
}
$this
->query('set character set utf8');
}
}
protected function query($sql) {
$sql = $this
->sql_printf(func_get_args());
if (!($res = mysqli_query($this->conn, $sql))) {
$this
->sql_errcheck($sql);
}
if (!is_bool($res)) {
mysqli_free_result($res) || is_object($res) && get_class($res) == "mysqli_result" ? true : false;
}
}
protected function query_all_assoc($sql) {
$sql = $this
->sql_printf(func_get_args());
if (!($res = mysqli_query($this->conn, $sql))) {
$this
->sql_errcheck($sql);
}
$rs = array();
while ($row = mysqli_fetch_assoc($res)) {
$rs[] = $row;
}
mysqli_free_result($res) || is_object($res) && get_class($res) == "mysqli_result" ? true : false;
return $rs;
}
protected function query_row_assoc($sql) {
$sql = $this
->sql_printf(func_get_args());
if (!($res = mysqli_query($this->conn, $sql))) {
$this
->sql_errcheck($sql);
}
if ($row = mysqli_fetch_assoc($res)) {
$rs = $row;
}
else {
$rs = false;
}
mysqli_free_result($res) || is_object($res) && get_class($res) == "mysqli_result" ? true : false;
return $rs;
}
protected function query_row($sql) {
$sql = $this
->sql_printf(func_get_args());
if (!($res = mysqli_query($this->conn, $sql))) {
$this
->sql_errcheck($sql);
}
if ($row = mysqli_fetch_array($res)) {
$rs = $row;
}
else {
$rs = false;
}
mysqli_free_result($res) || is_object($res) && get_class($res) == "mysqli_result" ? true : false;
return $rs;
}
protected function query_one($sql) {
$sql = $this
->sql_printf(func_get_args());
if (!($res = mysqli_query($this->conn, $sql))) {
$this
->sql_errcheck($sql);
}
if ($row = mysqli_fetch_assoc($res)) {
$val = array_pop($row);
}
else {
$val = false;
}
mysqli_free_result($res) || is_object($res) && get_class($res) == "mysqli_result" ? true : false;
return $val;
}
protected function query_affected_rows() {
return mysqli_affected_rows($this->conn);
}
protected function query_insert_id() {
return is_null($___mysqli_res = mysqli_insert_id($this->conn)) ? false : $___mysqli_res;
}
protected function sql_printf($args) {
$sql = array_shift($args);
if (count($args) == 1 && is_array($args[0])) {
$args = $args[0];
}
$args = array_map(array(
$this,
'sql_escape_string',
), $args);
return vsprintf($sql, $args);
}
protected function sql_escape_string($s) {
if (is_string($s)) {
return mysqli_real_escape_string($this->conn, $s);
}
else {
if (is_null($s)) {
return NULL;
}
else {
if (is_bool($s)) {
return intval($s);
}
else {
if (is_int($s) || is_float($s)) {
return $s;
}
else {
return mysqli_real_escape_string($this->conn, strval($s));
}
}
}
}
}
protected function sql_errcheck($sql) {
if (is_object($this->conn) ? mysqli_errno($this->conn) : (($___mysqli_res = mysqli_connect_errno()) ? $___mysqli_res : false)) {
$msg = "SQL Error in OAuthStoreMySQL: " . (is_object($this->conn) ? mysqli_error($this->conn) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)) . "\n\n" . $sql;
throw new OAuthException2($msg);
}
}
}