From e77c9bb97ed56d30c08f7d7ea9c06dd80ac9467b Mon Sep 17 00:00:00 2001 From: josh4trunks Date: Wed, 4 Jun 2014 22:50:23 -0700 Subject: [PATCH 01/11] Work with MySQL Sockets This passes anything that is not a valid port (065535) { + $socket=true; + } } else { $port=false; } @@ -89,7 +92,11 @@ class OC_DB { 'dbname' => $name, ); if (!empty($port)) { - $connectionParams['port'] = $port; + if ($socket) { + $connectionParams['unix_socket'] = $port; + } else { + $connectionParams['port'] = $port; + } } } From ea162c8a39188450636548f4613d96c39cac8a04 Mon Sep 17 00:00:00 2001 From: josh4trunks Date: Wed, 4 Jun 2014 23:03:13 -0700 Subject: [PATCH 02/11] Fix variable not always being defined. --- lib/private/db.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/private/db.php b/lib/private/db.php index 16030a20f8..f5fd9fb6ad 100644 --- a/lib/private/db.php +++ b/lib/private/db.php @@ -67,6 +67,8 @@ class OC_DB { list($host, $port)=explode(':', $host, 2); if(!is_int($port)||$port<1||$port>65535) { $socket=true; + } else { + $socket=false; } } else { $port=false; From 55ccd6da51c10ad3403fbdf32c4de09edaf102db Mon Sep 17 00:00:00 2001 From: josh4trunks Date: Thu, 5 Jun 2014 20:17:50 -0700 Subject: [PATCH 03/11] Update notes on dbhost --- config/config.sample.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/config.sample.php b/config/config.sample.php index 0a81543589..066b369efa 100755 --- a/config/config.sample.php +++ b/config/config.sample.php @@ -26,7 +26,7 @@ $CONFIG = array( /* Password to access the ownCloud database */ "dbpassword" => "", -/* Host running the ownCloud database */ +/* Host running the ownCloud database. To specify a port use "HOSTNAME:####"; to specify a unix sockets use "localhost:/path/to/socket". */ "dbhost" => "", /* Prefix for the ownCloud tables in the database */ From 1b02991a1dfd3d8cb8a7992609e0235bb97876ea Mon Sep 17 00:00:00 2001 From: josh4trunks Date: Wed, 11 Jun 2014 21:47:45 -0700 Subject: [PATCH 04/11] Fixes based on suggestions I use the term socket for any extension, either unix socket, or internet socket (port). I check if the socket is all digits * only integers 0 and larger would pass this test. I then check if the string is less than or equal to the maximum port number. By using "if($socket)" I make sure socket isn't false, empty, or the string '0'. I don't believe I need to initialize $port because $port will always be set if $socket is true. Please show me if I am wrong here. Thanks --- lib/private/db.php | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/private/db.php b/lib/private/db.php index f5fd9fb6ad..eec2b9220e 100644 --- a/lib/private/db.php +++ b/lib/private/db.php @@ -64,14 +64,10 @@ class OC_DB { $pass = OC_Config::getValue( "dbpassword", "" ); $type = OC_Config::getValue( "dbtype", "sqlite" ); if(strpos($host, ':')) { - list($host, $port)=explode(':', $host, 2); - if(!is_int($port)||$port<1||$port>65535) { - $socket=true; - } else { - $socket=false; - } + list($host, $socket)=explode(':', $host, 2); + $port = ctype_digit($socket) && $socket<=65535; } else { - $port=false; + $socket=FALSE; } $factory = new \OC\DB\ConnectionFactory(); @@ -93,11 +89,11 @@ class OC_DB { 'host' => $host, 'dbname' => $name, ); - if (!empty($port)) { - if ($socket) { - $connectionParams['unix_socket'] = $port; + if ($socket) { + if ($port) { + $connectionParams['port'] = $socket; } else { - $connectionParams['port'] = $port; + $connectionParams['unix_socket'] = $socket; } } } From 6da2beeaff40be812fa1a5fcb125156c6a3a33c3 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 12 Jun 2014 19:48:37 +0200 Subject: [PATCH 05/11] Inline $port expression, getting rid of $port variable. --- lib/private/db.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/private/db.php b/lib/private/db.php index eec2b9220e..d3bf8c0016 100644 --- a/lib/private/db.php +++ b/lib/private/db.php @@ -65,7 +65,6 @@ class OC_DB { $type = OC_Config::getValue( "dbtype", "sqlite" ); if(strpos($host, ':')) { list($host, $socket)=explode(':', $host, 2); - $port = ctype_digit($socket) && $socket<=65535; } else { $socket=FALSE; } @@ -90,7 +89,7 @@ class OC_DB { 'dbname' => $name, ); if ($socket) { - if ($port) { + if (ctype_digit($socket) && $socket <= 65535) { $connectionParams['port'] = $socket; } else { $connectionParams['unix_socket'] = $socket; From 4b87586487888e0d9fcc5700eefccc6ed031ce96 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 12 Jun 2014 19:53:33 +0200 Subject: [PATCH 06/11] Extract common variables of $connectionParams. --- lib/private/db.php | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/private/db.php b/lib/private/db.php index d3bf8c0016..6f0596f328 100644 --- a/lib/private/db.php +++ b/lib/private/db.php @@ -74,20 +74,17 @@ class OC_DB { return false; } + $connectionParams = array( + 'user' => $user, + 'password' => $pass, + ); + if ($factory->normalizeType($type) === 'sqlite3') { $datadir = OC_Config::getValue("datadirectory", OC::$SERVERROOT.'/data'); - $connectionParams = array( - 'user' => $user, - 'password' => $pass, - 'path' => $datadir.'/'.$name.'.db', - ); + $connectionParams['path'] = $datadir.'/'.$name.'.db'; } else { - $connectionParams = array( - 'user' => $user, - 'password' => $pass, - 'host' => $host, - 'dbname' => $name, - ); + $connectionParams['host'] = $host; + $connectionParams['dbname'] = $name; if ($socket) { if (ctype_digit($socket) && $socket <= 65535) { $connectionParams['port'] = $socket; From 17c2e63449ed4c3afa31e6a424eb4e208596b906 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 12 Jun 2014 19:55:26 +0200 Subject: [PATCH 07/11] Move check for : to where it belongs, getting rid of $socket = false; --- lib/private/db.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/lib/private/db.php b/lib/private/db.php index 6f0596f328..df558dfce1 100644 --- a/lib/private/db.php +++ b/lib/private/db.php @@ -63,11 +63,6 @@ class OC_DB { $user = OC_Config::getValue( "dbuser", "" ); $pass = OC_Config::getValue( "dbpassword", "" ); $type = OC_Config::getValue( "dbtype", "sqlite" ); - if(strpos($host, ':')) { - list($host, $socket)=explode(':', $host, 2); - } else { - $socket=FALSE; - } $factory = new \OC\DB\ConnectionFactory(); if (!$factory->isValidType($type)) { @@ -83,15 +78,17 @@ class OC_DB { $datadir = OC_Config::getValue("datadirectory", OC::$SERVERROOT.'/data'); $connectionParams['path'] = $datadir.'/'.$name.'.db'; } else { - $connectionParams['host'] = $host; - $connectionParams['dbname'] = $name; - if ($socket) { + if (strpos($host, ':')) { + // Host variable may carry a port or socket. + list($host, $socket) = explode(':', $host, 2); if (ctype_digit($socket) && $socket <= 65535) { $connectionParams['port'] = $socket; } else { $connectionParams['unix_socket'] = $socket; } } + $connectionParams['host'] = $host; + $connectionParams['dbname'] = $name; } $connectionParams['tablePrefix'] = OC_Config::getValue('dbtableprefix', 'oc_'); From 4ca1e3cc0295203f2d1bdca083b73ed56b8fa978 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 12 Jun 2014 19:59:40 +0200 Subject: [PATCH 08/11] Move getValue() to where required. This actually is not required "global data". --- lib/private/db.php | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/lib/private/db.php b/lib/private/db.php index df558dfce1..a35a4f53e8 100644 --- a/lib/private/db.php +++ b/lib/private/db.php @@ -57,27 +57,23 @@ class OC_DB { return true; } - // The global data we need - $name = OC_Config::getValue( "dbname", "owncloud" ); - $host = OC_Config::getValue( "dbhost", "" ); - $user = OC_Config::getValue( "dbuser", "" ); - $pass = OC_Config::getValue( "dbpassword", "" ); - $type = OC_Config::getValue( "dbtype", "sqlite" ); - + $type = OC_Config::getValue('dbtype', 'sqlite'); $factory = new \OC\DB\ConnectionFactory(); if (!$factory->isValidType($type)) { return false; } $connectionParams = array( - 'user' => $user, - 'password' => $pass, + 'user' => OC_Config::getValue('dbuser', ''), + 'password' => OC_Config::getValue('dbpassword', ''), ); + $name = OC_Config::getValue('dbname', 'owncloud'); if ($factory->normalizeType($type) === 'sqlite3') { $datadir = OC_Config::getValue("datadirectory", OC::$SERVERROOT.'/data'); $connectionParams['path'] = $datadir.'/'.$name.'.db'; } else { + $host = OC_Config::getValue('dbhost', ''); if (strpos($host, ':')) { // Host variable may carry a port or socket. list($host, $socket) = explode(':', $host, 2); From 3d8eabedbdb130206f5a0f9d88ec8ee25f0e9a2a Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 12 Jun 2014 20:17:30 +0200 Subject: [PATCH 09/11] No need to check the port number as this will fail anyway. --- lib/private/db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/private/db.php b/lib/private/db.php index a35a4f53e8..e911abd5f8 100644 --- a/lib/private/db.php +++ b/lib/private/db.php @@ -77,7 +77,7 @@ class OC_DB { if (strpos($host, ':')) { // Host variable may carry a port or socket. list($host, $socket) = explode(':', $host, 2); - if (ctype_digit($socket) && $socket <= 65535) { + if (ctype_digit($socket)) { $connectionParams['port'] = $socket; } else { $connectionParams['unix_socket'] = $socket; From 09327603045566f51ece009a43a74fc4987edd93 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 12 Jun 2014 20:18:38 +0200 Subject: [PATCH 10/11] Rename variable to indicate that it can be port and socket. --- lib/private/db.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/private/db.php b/lib/private/db.php index e911abd5f8..bc9137fefd 100644 --- a/lib/private/db.php +++ b/lib/private/db.php @@ -76,11 +76,11 @@ class OC_DB { $host = OC_Config::getValue('dbhost', ''); if (strpos($host, ':')) { // Host variable may carry a port or socket. - list($host, $socket) = explode(':', $host, 2); - if (ctype_digit($socket)) { - $connectionParams['port'] = $socket; + list($host, $portOrSocket) = explode(':', $host, 2); + if (ctype_digit($portOrSocket)) { + $connectionParams['port'] = $portOrSocket; } else { - $connectionParams['unix_socket'] = $socket; + $connectionParams['unix_socket'] = $portOrSocket; } } $connectionParams['host'] = $host; From 73062040e68212ac6e92d36211ca0bef91777589 Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 12 Jun 2014 20:22:45 +0200 Subject: [PATCH 11/11] Don't specify host when using a socket. --- lib/private/db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/private/db.php b/lib/private/db.php index bc9137fefd..0e18686ae9 100644 --- a/lib/private/db.php +++ b/lib/private/db.php @@ -78,12 +78,12 @@ class OC_DB { // Host variable may carry a port or socket. list($host, $portOrSocket) = explode(':', $host, 2); if (ctype_digit($portOrSocket)) { + $connectionParams['host'] = $host; $connectionParams['port'] = $portOrSocket; } else { $connectionParams['unix_socket'] = $portOrSocket; } } - $connectionParams['host'] = $host; $connectionParams['dbname'] = $name; }