From 9bb847bb07ec017eb8e2ebb2764b7e34acf5e619 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Wed, 12 Oct 2016 18:15:12 -0700 Subject: remove the rest of the backticks from sql queries; replace with TQUOT const which is driver dependent --- include/dba/dba_driver.php | 9 ++++++++- include/dba/dba_postgres.php | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'include/dba') diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index 852dc16af..36353354c 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -72,6 +72,7 @@ class DBA { define('NULL_DATE', self::$dba->get_null_date()); define('ACTIVE_DBTYPE', self::$dbtype); + define('TQUOT', self::$dba->get_table_quote()); return self::$dba; } @@ -88,6 +89,7 @@ abstract class dba_driver { const INSTALL_SCRIPT='install/schema_mysql.sql'; const NULL_DATE = '0001-01-01 00:00:00'; const UTC_NOW = 'UTC_TIMESTAMP()'; + const TQUOT = "`"; protected $db; protected $pdo = array(); @@ -157,6 +159,11 @@ abstract class dba_driver { return static::INSTALL_SCRIPT; } + function get_table_quote() { + return static::TQUOT; + } + + function utcnow() { return static::UTC_NOW; } @@ -313,7 +320,7 @@ function db_concat($fld, $sep) { * queries return true if the command was successful or false if it wasn't. * * Example: - * $r = q("SELECT * FROM `%s` WHERE `uid` = %d", + * $r = q("SELECT * FROM %s WHERE `uid` = %d", * 'user', 1); * * @param string $sql The SQL query to execute diff --git a/include/dba/dba_postgres.php b/include/dba/dba_postgres.php index 03b29d703..ae3e5a76f 100644 --- a/include/dba/dba_postgres.php +++ b/include/dba/dba_postgres.php @@ -7,6 +7,7 @@ class dba_postgres extends dba_driver { const INSTALL_SCRIPT='install/schema_postgres.sql'; const NULL_DATE = '0001-01-01 00:00:00'; const UTC_NOW = "now() at time zone 'UTC'"; + const TQUOT = '"'; function connect($server,$port,$user,$pass,$db) { if(!$port) $port = 5432; -- cgit v1.2.3 From 6532972e61a2aa5e8517ebcca3113adb3c8f336d Mon Sep 17 00:00:00 2001 From: zotlabs Date: Thu, 13 Oct 2016 00:30:41 -0700 Subject: additional array checking --- include/dba/dba_driver.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'include/dba') diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index 36353354c..34597bec4 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -391,9 +391,22 @@ function dbesc_array_cb(&$item, $key) { function dbesc_array(&$arr) { + $bogus_key = false; if(is_array($arr) && count($arr)) { + $matches = false; + foreach($arr as $k => $v) { + if(preg_match('/([^a-zA-Z0-9\-\_\.])/',$k,$matches)) { + logger('bogus key: ' . $k); + $bogus_key = true; + } + } array_walk($arr,'dbesc_array_cb'); + if($bogus_key) { + $arr['BOGUS.KEY'] = 1; + return false; + } } + return true; } function db_getfunc($f) { -- cgit v1.2.3 From 716a83d1f77e2d6f26b25ab03ffe130c11ef7feb Mon Sep 17 00:00:00 2001 From: zotlabs Date: Mon, 17 Oct 2016 16:12:32 -0700 Subject: some pdo work --- include/dba/dba_driver.php | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) (limited to 'include/dba') diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index 34597bec4..4586f6b70 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -29,27 +29,33 @@ class DBA { * @return null|dba_driver A database driver object (dba_mysql|dba_mysqli) or null if no driver found. */ - static public function dba_factory($server, $port,$user,$pass,$db,$dbtype,$install = false) { + static public function dba_factory($server,$port,$user,$pass,$db,$dbtype,$install = false) { self::$dba = null; self::$dbtype = intval($dbtype); - $set_port = $port; if(self::$dbtype == DBTYPE_POSTGRES) { + if(! ($port)) + $port = 5432; + require_once('include/dba/dba_postgres.php'); - if(is_null($port)) $set_port = 5432; - self::$dba = new dba_postgres($server, $set_port, $user, $pass, $db, $install); + self::$dba = new dba_postgres($server, $port, $user, $pass, $db, $install); } else { + if(! ($port)) + $port = 3306; + if($server === 'localhost') + $server = '127.0.0.1'; + + // Highly experimental at the present time. // require_once('include/dba/dba_pdo.php'); -// self::$dba = new dba_pdo($server, $set_port,$user,$pass,$db,$install); +// self::$dba = new dba_pdo($server, $port,$user,$pass,$db,$install); // } if(class_exists('mysqli')) { - if (is_null($port)) $set_port = ini_get("mysqli.default_port"); require_once('include/dba/dba_mysqli.php'); self::$dba = new dba_mysqli($server, $set_port,$user,$pass,$db,$install); } -- cgit v1.2.3 From aee4f8d2fe996f7c35ca90ba19a565bb16a7575f Mon Sep 17 00:00:00 2001 From: zotlabs Date: Wed, 19 Oct 2016 16:58:26 -0700 Subject: pdo changes first cut --- include/dba/dba_driver.php | 54 +++++++++++++++++++++------------------------- include/dba/dba_pdo.php | 3 ++- 2 files changed, 27 insertions(+), 30 deletions(-) (limited to 'include/dba') diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index 4586f6b70..8a0145319 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -12,8 +12,16 @@ class DBA { static public $dba = null; static public $dbtype = null; + static public $scheme = 'mysql'; static public $logging = false; + static public $install_script = 'install/schema_mysql.sql'; + static public $null_date = '0001-01-01 00:00:00'; + static public $utc_now = 'UTC_TIMESTAMP()'; + static public $tquot = "`"; + + + /** * @brief Returns the database driver object. * @@ -35,50 +43,42 @@ class DBA { self::$dbtype = intval($dbtype); + if(self::$dbtype == DBTYPE_POSTGRES) { - if(! ($port)) + if(!($port)) $port = 5432; + self::$install_script = 'install/schema_postgres.sql'; + self::$utc_now = "now() at time zone 'UTC'"; + self::$tquot = '"'; + self::$scheme = 'postgres'; + require_once('include/dba/dba_postgres.php'); self::$dba = new dba_postgres($server, $port, $user, $pass, $db, $install); } else { - if(! ($port)) + if(!($port)) $port = 3306; if($server === 'localhost') $server = '127.0.0.1'; + require_once('include/dba/dba_pdo.php'); + self::$dba = new dba_pdo($server,$port,$user,$pass,$db,$install); - -// Highly experimental at the present time. -// require_once('include/dba/dba_pdo.php'); -// self::$dba = new dba_pdo($server, $port,$user,$pass,$db,$install); -// } - - if(class_exists('mysqli')) { - require_once('include/dba/dba_mysqli.php'); - self::$dba = new dba_mysqli($server, $set_port,$user,$pass,$db,$install); - } } - // Until we have a proper PDO driver, store the DB connection parameters for - // plugins/addons which use PDO natively (such as cdav). This is wasteful as - // it opens a separate connection to the DB, but saves a lot of effort re-writing - // third-party interfaces that are working and well tested. - if(is_object(self::$dba) && self::$dba->connected) { - if($server === 'localhost') - $port = $set_port; + $dns = ((self::$dbtype == DBTYPE_POSTGRES) ? 'postgres' : 'mysql') . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port) . ';dbname=' . $db; self::$dba->pdo_set(array($dns,$user,$pass)); } - define('NULL_DATE', self::$dba->get_null_date()); + define('NULL_DATE', self::$null_date); define('ACTIVE_DBTYPE', self::$dbtype); - define('TQUOT', self::$dba->get_table_quote()); + define('TQUOT', self::$tquot); return self::$dba; } @@ -92,10 +92,6 @@ class DBA { */ abstract class dba_driver { // legacy behavior - const INSTALL_SCRIPT='install/schema_mysql.sql'; - const NULL_DATE = '0001-01-01 00:00:00'; - const UTC_NOW = 'UTC_TIMESTAMP()'; - const TQUOT = "`"; protected $db; protected $pdo = array(); @@ -158,20 +154,20 @@ abstract class dba_driver { } function get_null_date() { - return static::NULL_DATE; + return self::$null_date; } function get_install_script() { - return static::INSTALL_SCRIPT; + return self::$install_script; } function get_table_quote() { - return static::TQUOT; + return self::$tquot; } function utcnow() { - return static::UTC_NOW; + return self::$utc_now; } function install($server,$user,$pass,$db) { diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index 7255a2b66..823349f53 100755 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -9,11 +9,12 @@ class dba_pdo extends dba_driver { function connect($server,$port,$user,$pass,$db) { - $this->driver_dbtype = 'mysql'; // (($dbtype == DBTYPE_POSTGRES) ? 'postgres' : 'mysql'); + $this->driver_dbtype = 'mysql'; $dns = $this->driver_dbtype . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port) . ';dbname=' . $db; + db_logger('dns: ' . $dns); try { $this->db = new PDO($dns,$user,$pass); -- cgit v1.2.3 From 2702b82bc318bde595e4afddb87e4bff300f2dd2 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Wed, 19 Oct 2016 17:23:06 -0700 Subject: pdo fixes --- include/dba/dba_driver.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'include/dba') diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index 8a0145319..11a4d9011 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -154,20 +154,20 @@ abstract class dba_driver { } function get_null_date() { - return self::$null_date; + return DBA::$null_date; } function get_install_script() { - return self::$install_script; + return DBA::$install_script; } function get_table_quote() { - return self::$tquot; + return DBA::$tquot; } function utcnow() { - return self::$utc_now; + return DBA::$utc_now; } function install($server,$user,$pass,$db) { -- cgit v1.2.3 From 3726b546d5e24b2f8ffa3d02346c7a0905d3ca65 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Wed, 19 Oct 2016 19:24:12 -0700 Subject: use pdo for postgres also --- include/dba/dba_driver.php | 23 +++++++++++------------ include/dba/dba_mysql.php | 2 +- include/dba/dba_mysqli.php | 2 +- include/dba/dba_pdo.php | 20 ++++++++++++++++++-- include/dba/dba_postgres.php | 2 +- 5 files changed, 32 insertions(+), 17 deletions(-) (limited to 'include/dba') diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index 11a4d9011..47ec29816 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -54,7 +54,7 @@ class DBA { self::$scheme = 'postgres'; require_once('include/dba/dba_postgres.php'); - self::$dba = new dba_postgres($server, $port, $user, $pass, $db, $install); + self::$dba = new dba_postgres($server, self::$scheme, $port, $user, $pass, $db, $install); } else { if(!($port)) @@ -63,8 +63,7 @@ class DBA { $server = '127.0.0.1'; require_once('include/dba/dba_pdo.php'); - self::$dba = new dba_pdo($server,$port,$user,$pass,$db,$install); - + self::$dba = new dba_pdo($server,self::$scheme,$port,$user,$pass,$db,$install); } @@ -112,7 +111,7 @@ abstract class dba_driver { * @param string $db database name * @return bool */ - abstract function connect($server, $port, $user, $pass, $db); + abstract function connect($server, $scheme, $port, $user, $pass, $db); /** * @brief Perform a DB query with the SQL statement $sql. @@ -146,31 +145,31 @@ abstract class dba_driver { */ abstract function getdriver(); - function __construct($server, $port, $user,$pass,$db,$install = false) { - if(($install) && (! $this->install($server, $port, $user, $pass, $db))) { + function __construct($server, $scheme, $port, $user,$pass,$db,$install = false) { + if(($install) && (! $this->install($server, $scheme, $port, $user, $pass, $db))) { return; } - $this->connect($server, $port, $user, $pass, $db); + $this->connect($server, $scheme, $port, $user, $pass, $db); } function get_null_date() { - return DBA::$null_date; + return \DBA::$null_date; } function get_install_script() { - return DBA::$install_script; + return \DBA::$install_script; } function get_table_quote() { - return DBA::$tquot; + return \DBA::$tquot; } function utcnow() { - return DBA::$utc_now; + return \DBA::$utc_now; } - function install($server,$user,$pass,$db) { + function install($server,$scheme,$port,$user,$pass,$db) { if (!(strlen($server) && strlen($user))){ $this->connected = false; $this->db = null; diff --git a/include/dba/dba_mysql.php b/include/dba/dba_mysql.php index 3cadad6dc..8b51cf578 100755 --- a/include/dba/dba_mysql.php +++ b/include/dba/dba_mysql.php @@ -5,7 +5,7 @@ require_once('include/dba/dba_driver.php'); class dba_mysql extends dba_driver { - function connect($server, $port, $user,$pass,$db) { + function connect($server, $scheme, $port, $user,$pass,$db) { $this->db = mysql_connect($server.":".$port,$user,$pass); if($this->db && mysql_select_db($db,$this->db)) { $this->connected = true; diff --git a/include/dba/dba_mysqli.php b/include/dba/dba_mysqli.php index afd2aa642..165c8e969 100755 --- a/include/dba/dba_mysqli.php +++ b/include/dba/dba_mysqli.php @@ -4,7 +4,7 @@ require_once('include/dba/dba_driver.php'); class dba_mysqli extends dba_driver { - function connect($server,$port,$user,$pass,$db) { + function connect($server,$scheme,$port,$user,$pass,$db) { if($port) $this->db = new mysqli($server,$user,$pass,$db, $port); else diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index 823349f53..44de60d58 100755 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -7,9 +7,9 @@ class dba_pdo extends dba_driver { public $driver_dbtype = null; - function connect($server,$port,$user,$pass,$db) { + function connect($server,$scheme,$port,$user,$pass,$db) { - $this->driver_dbtype = 'mysql'; + $this->driver_dbtype = $scheme; $dns = $this->driver_dbtype . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port) . ';dbname=' . $db; @@ -28,6 +28,9 @@ class dba_pdo extends dba_driver { return false; } + if($this->driver_dbtype === 'postgres') + $this->q("SET standard_conforming_strings = 'off'; SET backslash_quote = 'on';"); + $this->connected = true; return true; @@ -37,6 +40,9 @@ class dba_pdo extends dba_driver { if((! $this->db) || (! $this->connected)) return false; + if($this->driver_dbtype === 'postgres' && (! strpos($sql,';'))) + $sql .= ';'; + $this->error = ''; $select = ((stripos($sql,'select') === 0) ? true : false); @@ -89,6 +95,16 @@ class dba_pdo extends dba_driver { $this->connected = false; } + function concat($fld,$sep) { + if($this->driver_dbtype === 'postgres') { + return 'string_agg(' . $fld . ',\'' . $sep . '\')'; + } + else { + return 'GROUP_CONCAT(DISTINCT '.$fld.' SEPARATOR \''.$sep.'\')'; + } + } + + function getdriver() { return 'pdo'; } diff --git a/include/dba/dba_postgres.php b/include/dba/dba_postgres.php index ae3e5a76f..560d8da60 100644 --- a/include/dba/dba_postgres.php +++ b/include/dba/dba_postgres.php @@ -9,7 +9,7 @@ class dba_postgres extends dba_driver { const UTC_NOW = "now() at time zone 'UTC'"; const TQUOT = '"'; - function connect($server,$port,$user,$pass,$db) { + function connect($server,$scheme,$port,$user,$pass,$db) { if(!$port) $port = 5432; $connstr = 'host=' . $server . ' port='.$port . ' user=' . $user . ' password=' . $pass . ' dbname='. $db; $this->db = pg_connect($connstr); -- cgit v1.2.3 From b4b5eb5babcfd01f41e3df39400758422b39d591 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Thu, 20 Oct 2016 15:45:48 -0700 Subject: pdo testing --- include/dba/dba_driver.php | 9 +++++---- include/dba/dba_pdo.php | 15 ++++++++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) (limited to 'include/dba') diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index 47ec29816..abbacc56e 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -51,20 +51,21 @@ class DBA { self::$install_script = 'install/schema_postgres.sql'; self::$utc_now = "now() at time zone 'UTC'"; self::$tquot = '"'; - self::$scheme = 'postgres'; + self::$scheme = 'pgsql'; - require_once('include/dba/dba_postgres.php'); - self::$dba = new dba_postgres($server, self::$scheme, $port, $user, $pass, $db, $install); +// require_once('include/dba/dba_postgres.php'); +// self::$dba = new dba_postgres($server, self::$scheme, $port, $user, $pass, $db, $install); } else { if(!($port)) $port = 3306; if($server === 'localhost') $server = '127.0.0.1'; + } require_once('include/dba/dba_pdo.php'); self::$dba = new dba_pdo($server,self::$scheme,$port,$user,$pass,$db,$install); - } + if(is_object(self::$dba) && self::$dba->connected) { diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index 44de60d58..b3a1209bc 100755 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -28,7 +28,7 @@ class dba_pdo extends dba_driver { return false; } - if($this->driver_dbtype === 'postgres') + if($this->driver_dbtype === 'pgsql') $this->q("SET standard_conforming_strings = 'off'; SET backslash_quote = 'on';"); $this->connected = true; @@ -40,7 +40,7 @@ class dba_pdo extends dba_driver { if((! $this->db) || (! $this->connected)) return false; - if($this->driver_dbtype === 'postgres' && (! strpos($sql,';'))) + if($this->driver_dbtype === 'pgsql' && (! strpos($sql,';'))) $sql .= ';'; $this->error = ''; @@ -96,7 +96,7 @@ class dba_pdo extends dba_driver { } function concat($fld,$sep) { - if($this->driver_dbtype === 'postgres') { + if($this->driver_dbtype === 'pgsql') { return 'string_agg(' . $fld . ',\'' . $sep . '\')'; } else { @@ -104,6 +104,15 @@ class dba_pdo extends dba_driver { } } + function quote_interval($txt) { + if($this->driver_dbtype === 'pgsql') { + return "'$txt'"; + } + else { + return $txt; + } + } + function getdriver() { return 'pdo'; -- cgit v1.2.3 From e2e3b81f323957495a643a9087b663df03dac2d9 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Thu, 20 Oct 2016 16:13:06 -0700 Subject: more pdo tweaks --- include/dba/dba_pdo.php | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'include/dba') diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index b3a1209bc..bb89df6be 100755 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -14,7 +14,7 @@ class dba_pdo extends dba_driver { . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port) . ';dbname=' . $db; - db_logger('dns: ' . $dns); + // db_logger('dns: ' . $dns); try { $this->db = new PDO($dns,$user,$pass); @@ -40,8 +40,11 @@ class dba_pdo extends dba_driver { if((! $this->db) || (! $this->connected)) return false; - if($this->driver_dbtype === 'pgsql' && (! strpos($sql,';'))) - $sql .= ';'; + if($this->driver_dbtype === 'pgsql') { + if(substr(rtrim($sql),-1,1) !== ';') { + $sql .= ';'; + } + } $this->error = ''; $select = ((stripos($sql,'select') === 0) ? true : false); @@ -113,6 +116,24 @@ class dba_pdo extends dba_driver { } } + function escapebin($str) { + if($this->driver_dbtype === 'pgsql') { + return str_replace([ chr(92), chr(0), chr(39) ], [ '\\\134', '\\\000', '\\\047' ], $str); + } + else { + return $this->escape($str); + } + } + + function unescapebin($str) { + if($this->driver_dbtype === 'pgsql') { + return stripcslashes($str); + } + else { + return $str; + } + } + function getdriver() { return 'pdo'; -- cgit v1.2.3 From 29340152b6a924a27dc62363d623e3dd287b5a2a Mon Sep 17 00:00:00 2001 From: zotlabs Date: Thu, 20 Oct 2016 16:21:15 -0700 Subject: pdo - cleanup --- include/dba/dba_driver.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'include/dba') diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index abbacc56e..e8c79045b 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -53,8 +53,6 @@ class DBA { self::$tquot = '"'; self::$scheme = 'pgsql'; -// require_once('include/dba/dba_postgres.php'); -// self::$dba = new dba_postgres($server, self::$scheme, $port, $user, $pass, $db, $install); } else { if(!($port)) @@ -63,14 +61,12 @@ class DBA { $server = '127.0.0.1'; } - require_once('include/dba/dba_pdo.php'); - self::$dba = new dba_pdo($server,self::$scheme,$port,$user,$pass,$db,$install); + require_once('include/dba/dba_pdo.php'); + self::$dba = new dba_pdo($server,self::$scheme,$port,$user,$pass,$db,$install); - - if(is_object(self::$dba) && self::$dba->connected) { - $dns = ((self::$dbtype == DBTYPE_POSTGRES) ? 'postgres' : 'mysql') + $dns = ((self::$dbtype == DBTYPE_POSTGRES) ? 'pgsql' : 'mysql') . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port) . ';dbname=' . $db; self::$dba->pdo_set(array($dns,$user,$pass)); -- cgit v1.2.3 From 04ac04e0ada6b8dbe3f512379abd2859eb758173 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Thu, 20 Oct 2016 17:04:43 -0700 Subject: allow a dsn override to the database via the server argument. This could be used to allow unix domain sockets and other unusual configurations. --- include/dba/dba_driver.php | 22 ++++++++++++---------- include/dba/dba_pdo.php | 14 +++++++++----- 2 files changed, 21 insertions(+), 15 deletions(-) (limited to 'include/dba') diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index e8c79045b..b4cda6d8e 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -25,16 +25,14 @@ class DBA { /** * @brief Returns the database driver object. * - * If available it will use PHP's mysqli otherwise mysql driver. - * - * @param string $server DB server name + * @param string $server DB server name (or PDO dsn - e.g. mysqli:foobar.com;) * @param string $port DB port * @param string $user DB username * @param string $pass DB password * @param string $db database name * @param string $dbtype 0 for mysql, 1 for postgres * @param bool $install Defaults to false - * @return null|dba_driver A database driver object (dba_mysql|dba_mysqli) or null if no driver found. + * @return null|dba_driver A database driver object (dba_pdo) or null if no driver found. */ static public function dba_factory($server,$port,$user,$pass,$db,$dbtype,$install = false) { @@ -57,8 +55,6 @@ class DBA { else { if(!($port)) $port = 3306; - if($server === 'localhost') - $server = '127.0.0.1'; } require_once('include/dba/dba_pdo.php'); @@ -66,10 +62,16 @@ class DBA { if(is_object(self::$dba) && self::$dba->connected) { - $dns = ((self::$dbtype == DBTYPE_POSTGRES) ? 'pgsql' : 'mysql') - . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port) - . ';dbname=' . $db; - self::$dba->pdo_set(array($dns,$user,$pass)); + if(strpbrk($server,':;')) { + $dsn = $server; + } + else { + $dsn = self::$scheme . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port); + } + $dsn .= ';dbname=' . $db; + + + self::$dba->pdo_set(array($dsn,$user,$pass)); } define('NULL_DATE', self::$null_date); diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index bb89df6be..c2f9c5b09 100755 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -10,14 +10,18 @@ class dba_pdo extends dba_driver { function connect($server,$scheme,$port,$user,$pass,$db) { $this->driver_dbtype = $scheme; - $dns = $this->driver_dbtype - . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port) - . ';dbname=' . $db; - // db_logger('dns: ' . $dns); + if(strpbrk($server,':;')) { + $dsn = $server; + } + else { + $dsn = $this->driver_dbtype . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port); + } + + $dsn .= ';dbname=' . $db; try { - $this->db = new PDO($dns,$user,$pass); + $this->db = new PDO($dsn,$user,$pass); $this->db->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { -- cgit v1.2.3 From d30892ea601e9131c6dc3b25aeee1f258f72a104 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Sun, 23 Oct 2016 17:05:08 -0700 Subject: pdo hacks --- include/dba/dba_driver.php | 9 ++++++++- include/dba/dba_pdo.php | 13 ++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) (limited to 'include/dba') diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index b4cda6d8e..afe209feb 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -53,7 +53,14 @@ class DBA { } else { - if(!($port)) + + // attempt to use the pdo driver compiled-in mysqli socket + // if using 'localhost' with no port configured. + // If this is wrong you'll need to set the socket path specifically + // using a server name of 'mysql:unix_socket=/socket/path', setting /socket/path + // as needed for your platform + + if((!($port)) && ($server !== 'localhost')) $port = 3306; } diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index c2f9c5b09..8ba0adb99 100755 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -107,7 +107,7 @@ class dba_pdo extends dba_driver { return 'string_agg(' . $fld . ',\'' . $sep . '\')'; } else { - return 'GROUP_CONCAT(DISTINCT '.$fld.' SEPARATOR \''.$sep.'\')'; + return 'GROUP_CONCAT(DISTINCT ' . $fld . ' SEPARATOR \'' . $sep . '\')'; } } @@ -120,9 +120,12 @@ class dba_pdo extends dba_driver { } } + // These two functions assume that postgres standard_conforming_strings is set to off; + // which we perform during DB open. + function escapebin($str) { if($this->driver_dbtype === 'pgsql') { - return str_replace([ chr(92), chr(0), chr(39) ], [ '\\\134', '\\\000', '\\\047' ], $str); + return "\\\\x" . bin2hex($str); } else { return $this->escape($str); @@ -131,7 +134,11 @@ class dba_pdo extends dba_driver { function unescapebin($str) { if($this->driver_dbtype === 'pgsql') { - return stripcslashes($str); + + // The initial backslash inserted by escapebin above will have been stripped + // by the postgres server, leaving us with '\x{hexdigits}' + + return hex2bin(substr($str,2)); } else { return $str; -- cgit v1.2.3 From 39f0707201109d7d5784e254aca320af43812ec8 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Sun, 23 Oct 2016 17:53:34 -0700 Subject: fetch bytea as stream --- include/dba/dba_pdo.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'include/dba') diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index 8ba0adb99..9f8deaf17 100755 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -137,8 +137,9 @@ class dba_pdo extends dba_driver { // The initial backslash inserted by escapebin above will have been stripped // by the postgres server, leaving us with '\x{hexdigits}' + // The PDO driver returns bytea fields as streams, so fetch the content with fgets - return hex2bin(substr($str,2)); + return hex2bin(substr(fgets($str),2)); } else { return $str; -- cgit v1.2.3 From 20194bed424d9dac2b9506067adde97bfe83ea0d Mon Sep 17 00:00:00 2001 From: zotlabs Date: Sun, 23 Oct 2016 21:27:10 -0700 Subject: this seems to work, but there are unanswered questions and is still undergoing investigation. It appears that the data stored with os_content = 1 is not being escaped in all circumstances or the scaled image data is being escaped twice. --- include/dba/dba_pdo.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'include/dba') diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index 9f8deaf17..b247728ff 100755 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -134,12 +134,15 @@ class dba_pdo extends dba_driver { function unescapebin($str) { if($this->driver_dbtype === 'pgsql') { + $x = ''; + while(! feof($str)) { + $x .= fread($str,8192); + } + if(substr($x,0,2) === '\\x') { + $x = hex2bin(substr($x,2)); + } + return $x; - // The initial backslash inserted by escapebin above will have been stripped - // by the postgres server, leaving us with '\x{hexdigits}' - // The PDO driver returns bytea fields as streams, so fetch the content with fgets - - return hex2bin(substr(fgets($str),2)); } else { return $str; -- cgit v1.2.3 From 25982f04756beef6677afbda06113f08d3521a5d Mon Sep 17 00:00:00 2001 From: zotlabs Date: Mon, 24 Oct 2016 18:55:11 -0700 Subject: set port if non-zero (instead of non-null) --- include/dba/dba_driver.php | 2 +- include/dba/dba_pdo.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'include/dba') diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index afe209feb..8ae3dccfe 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -73,7 +73,7 @@ class DBA { $dsn = $server; } else { - $dsn = self::$scheme . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port); + $dsn = self::$scheme . ':host=' . $server . (intval($port) ? '' : ';port=' . $port); } $dsn .= ';dbname=' . $db; diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index b247728ff..1d0b142aa 100755 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -15,7 +15,7 @@ class dba_pdo extends dba_driver { $dsn = $server; } else { - $dsn = $this->driver_dbtype . ':host=' . $server . (is_null($port) ? '' : ';port=' . $port); + $dsn = $this->driver_dbtype . ':host=' . $server . (intval($port) ? '' : ';port=' . $port); } $dsn .= ';dbname=' . $db; -- cgit v1.2.3 From 7b713e25768a36ea032b683eb1763578102166d0 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Tue, 25 Oct 2016 16:15:39 -0700 Subject: pdo - fetch assoc only --- include/dba/dba_pdo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/dba') diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index 1d0b142aa..526bf765c 100755 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -54,7 +54,7 @@ class dba_pdo extends dba_driver { $select = ((stripos($sql,'select') === 0) ? true : false); try { - $result = $this->db->query($sql); + $result = $this->db->query($sql, PDO::FETCH_ASSOC); } catch(PDOException $e) { -- cgit v1.2.3 From 084b41fc2c0e8abeec1da5c792ec552b5ae1ce8f Mon Sep 17 00:00:00 2001 From: zotlabs Date: Tue, 25 Oct 2016 16:21:56 -0700 Subject: first cut at edit activities --- include/dba/dba_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/dba') diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index 8ae3dccfe..6fb0e79cc 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -182,7 +182,7 @@ abstract class dba_driver { return false; } - if(strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1')) { + if(strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1') && (! strpbrk($server,':;'))) { if((! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) && (! filter_var($server, FILTER_VALIDATE_IP))) { $this->error = sprintf( t('Cannot locate DNS info for database server \'%s\''), $server); $this->connected = false; -- cgit v1.2.3 From 88a68b941ff2c8295ac5a6f221855bc4940ddb40 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Tue, 25 Oct 2016 18:27:32 -0700 Subject: put all dns checking into one function, allow it to be ignored --- include/dba/dba_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/dba') diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index 6fb0e79cc..0b5f085af 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -183,7 +183,7 @@ abstract class dba_driver { } if(strlen($server) && ($server !== 'localhost') && ($server !== '127.0.0.1') && (! strpbrk($server,':;'))) { - if((! dns_get_record($server, DNS_A + DNS_CNAME + DNS_PTR)) && (! filter_var($server, FILTER_VALIDATE_IP))) { + if(! z_dns_check($server)) { $this->error = sprintf( t('Cannot locate DNS info for database server \'%s\''), $server); $this->connected = false; $this->db = null; -- cgit v1.2.3 From b5c72611b45eba71bdcf6e4afe391fa88fc8a964 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Thu, 27 Oct 2016 15:21:40 -0700 Subject: change log string on pdo log messages --- include/dba/dba_pdo.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'include/dba') diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index 526bf765c..cb244abad 100755 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -60,7 +60,7 @@ class dba_pdo extends dba_driver { $this->error = $e->getMessage(); if($this->error) { - db_logger('dba_mysqli: ERROR: ' . printable($sql) . "\n" . $this->error, LOGGER_NORMAL, LOG_ERR); + db_logger('dba_pdo: ERROR: ' . printable($sql) . "\n" . $this->error, LOGGER_NORMAL, LOG_ERR); if(file_exists('dbfail.out')) { file_put_contents('dbfail.out', datetime_convert() . "\n" . printable($sql) . "\n" . $this->error . "\n", FILE_APPEND); } @@ -69,13 +69,13 @@ class dba_pdo extends dba_driver { if(!($select)) { if($this->debug) { - db_logger('dba_mysqli: DEBUG: ' . printable($sql) . ' returns ' . (($result) ? 'true' : 'false'), LOGGER_NORMAL,(($result) ? LOG_INFO : LOG_ERR)); + db_logger('dba_pdo: DEBUG: ' . printable($sql) . ' returns ' . (($result) ? 'true' : 'false'), LOGGER_NORMAL,(($result) ? LOG_INFO : LOG_ERR)); } return $result; } if($this->debug) { - db_logger('dba_mysqli: DEBUG: ' . printable($sql) . ' returned ' . count($result) . ' results.', LOGGER_NORMAL, LOG_INFO); + db_logger('dba_pdo: DEBUG: ' . printable($sql) . ' returned ' . count($result) . ' results.', LOGGER_NORMAL, LOG_INFO); } $r = array(); -- cgit v1.2.3 From 3b6248cb64bb79b6bf5652bdbe145e76dd755c05 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Thu, 17 Nov 2016 15:15:34 -0800 Subject: dba_pdo: return false on q() DB error like the old driver did --- include/dba/dba_pdo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/dba') diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index cb244abad..e235c467b 100755 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -87,7 +87,7 @@ class dba_pdo extends dba_driver { db_logger('dba_pdo: ' . printable(print_r($r,true)), LOGGER_NORMAL, LOG_INFO); } } - return $r; + return (($this->error) ? false : $r); } function escape($str) { -- cgit v1.2.3 From 793047919d493cd8cd15613f5a0df846113a5e29 Mon Sep 17 00:00:00 2001 From: zotlabs Date: Tue, 29 Nov 2016 02:57:29 -0800 Subject: missed this from the earlier checkin --- include/dba/dba_driver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/dba') diff --git a/include/dba/dba_driver.php b/include/dba/dba_driver.php index 0b5f085af..81a3bd590 100755 --- a/include/dba/dba_driver.php +++ b/include/dba/dba_driver.php @@ -98,7 +98,7 @@ class DBA { abstract class dba_driver { // legacy behavior - protected $db; + public $db; protected $pdo = array(); public $debug = 0; -- cgit v1.2.3 From 83a4999dbee17262e6f14cec6cda220e07b88c0e Mon Sep 17 00:00:00 2001 From: zotlabs Date: Wed, 30 Nov 2016 19:45:46 -0800 Subject: issue #606, postgres binary data handling under PDO and HHVM when passed null --- include/dba/dba_pdo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include/dba') diff --git a/include/dba/dba_pdo.php b/include/dba/dba_pdo.php index e235c467b..f76e6cdd7 100755 --- a/include/dba/dba_pdo.php +++ b/include/dba/dba_pdo.php @@ -133,7 +133,7 @@ class dba_pdo extends dba_driver { } function unescapebin($str) { - if($this->driver_dbtype === 'pgsql') { + if($this->driver_dbtype === 'pgsql' && (! is_null($str))) { $x = ''; while(! feof($str)) { $x .= fread($str,8192); -- cgit v1.2.3