Skip to content

Commit 11950e1

Browse files
committed
rework additional method to setup for secure connections
1 parent 27ce43e commit 11950e1

File tree

7 files changed

+129
-102
lines changed

7 files changed

+129
-102
lines changed

lib/Database/ez_mysqli.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99
use ezsql\DatabaseInterface;
1010

1111
class ez_mysqli extends ezsqlModel implements DatabaseInterface
12-
{
13-
private static $isSecure = false;
14-
private static $secure = null;
15-
12+
{
1613
/**
1714
* Database connection handle
1815
* @var resource

lib/Database/ez_pdo.php

Lines changed: 51 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111

1212
class ez_pdo extends ezsqlModel implements DatabaseInterface
1313
{
14-
private static $isSecure = false;
15-
private static $secure = null;
16-
private static $_options = [];
17-
1814
/**
1915
* Database connection handle
2016
* @var resource
@@ -56,84 +52,73 @@ public function settings()
5652
return $this->database;
5753
}
5854

59-
public static function securePDO(
60-
$vendor = null,
61-
$key = 'certificate.key',
62-
$cert = 'certificate.crt',
63-
$ca = 'cacert.pem',
64-
$path = '.'.\_DS)
65-
{
66-
if (\array_key_exists(\strtolower($vendor), \VENDOR)
67-
&& (! \file_exists($path.$cert) || ! \file_exists($path.$key)))
68-
$path = ezQuery::createCertificate();
69-
elseif ($path == '.'.\_DS) {
70-
$ssl_path = \getcwd();
71-
$path = \preg_replace('/\\\/', \_DS, $ssl_path). \_DS;
72-
}
73-
74-
if (($vendor == \PGSQL) || ($vendor == \POSTGRESQL)) {
75-
self::$secure = "sslmode=require;sslcert=".$path.$cert.";sslkey=".$path.$key.";sslrootcert=".$path.$ca.";";
76-
self::$isSecure = true;
77-
} elseif (($vendor == \MYSQL) || ($vendor == \MYSQLI)) {
78-
self::$_options = array(
79-
\PDO::MYSQL_ATTR_SSL_KEY => $path.$key,
80-
\PDO::MYSQL_ATTR_SSL_CERT => $path.$cert,
81-
\PDO::MYSQL_ATTR_SSL_CA => $path.$ca,
82-
\PDO::MYSQL_ATTR_SSL_CAPATH => $path,
83-
\PDO::MYSQL_ATTR_SSL_CIPHER => 'DHE-RSA-AES256-SHA',
84-
\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
85-
);
86-
} elseif (($vendor == \SQLSERVER) || ($vendor == \MSSQL) || ($vendor == \SQLSRV)) {
87-
self::$secure = ";Encrypt=true;TrustServerCertificate=true";
88-
self::$isSecure = true;
89-
}
90-
}
91-
9255
/**
93-
* Try to connect to the database server in the DSN parameters
94-
*
95-
* @param string $dsn The connection parameter string
96-
* Default is empty string
97-
* @param string $user The database user name
98-
* Default is empty string
99-
* @param string $password The database password
100-
* Default is empty string
101-
* @param array $options Array for setting connection options as MySQL
102-
* charset for example
103-
* Default is an empty array
104-
* @param boolean $isFileBased File based databases like SQLite don't need user and password,
105-
* they work with path in the dsn parameter
106-
* Default is false
107-
* @return boolean
108-
*/
56+
* Try to connect to the database server in the DSN parameters
57+
*
58+
* @param string $dsn The connection parameter string
59+
* Default is empty string
60+
* @param string $user The database user name
61+
* Default is empty string
62+
* @param string $password The database password
63+
* Default is empty string
64+
* @param array $options Array for setting connection options
65+
* Default is an empty array
66+
* @param boolean $isFileBased File based databases like SQLite don't need user and password,
67+
* Default is false
68+
* @return boolean
69+
*/
10970
public function connect(
11071
$dsn = '',
11172
$user = '',
11273
$password = '',
11374
$options = array(),
11475
$isFile = false)
11576
{
116-
$this->_connected = false;
117-
if (self::$isSecure)
118-
$setDsn = empty($dsn) ? $this->database->getDsn().$this->secure : $dsn.$this->secure;
77+
$this->_connected = false;
78+
$key = $this->sslKey;
79+
$cert = $this->sslCert;
80+
$ca = $this->sslCa;
81+
$path = $this->sslPath;
82+
83+
$vendor = $this->database->getDsn();
84+
if ($this->isSecure) {
85+
if (\strpos($vendor, \PGSQL) !== false) {
86+
$this->secureOptions = 'sslmode=require;sslcert='.$path.$cert.';sslkey='.$path.$key.';sslrootcert='.$path.$ca.';';
87+
} elseif (\strpos($vendor, 'mysql') !== false) {
88+
$this->secureOptions = array(
89+
\PDO::MYSQL_ATTR_SSL_KEY => $path.$key,
90+
\PDO::MYSQL_ATTR_SSL_CERT => $path.$cert,
91+
\PDO::MYSQL_ATTR_SSL_CA => $path.$ca,
92+
\PDO::MYSQL_ATTR_SSL_CAPATH => $path,
93+
\PDO::MYSQL_ATTR_SSL_CIPHER => 'DHE-RSA-AES256-SHA',
94+
\PDO::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false
95+
);
96+
} elseif (\strpos($vendor, \MSSQL) !== false) {
97+
$this->secureOptions = ';Encrypt=true;TrustServerCertificate=true';
98+
}
99+
}
100+
101+
if ($this->isSecure && \is_string($this->secureOptions))
102+
$dsn = empty($dsn) ? $vendor.$this->secureOptions : $dsn.$this->secureOptions;
119103
else
120-
$setDsn = empty($dsn) ? $this->database->getDsn() : $dsn;
104+
$dsn = empty($dsn) ? $vendor : $dsn;
121105

122-
if (!empty(self::$_options))
123-
$this->database->setOptions(self::$_options);
106+
if ($this->isSecure && \is_array($this->secureOptions))
107+
$options = $this->secureOptions;
108+
else
109+
$options = empty($options) ? $this->database->getOptions() : $options;
124110

125-
$setUser = empty($user) ? $this->database->getUser() : $user;
126-
$setPassword = empty($password) ? $this->database->getPassword() : $password;
127-
$setOptions = empty($options) ? $this->database->getOptions() : $options;
128-
$IsFile = empty($isFile) ? $this->database->getIsFile() : $isFile;
111+
$user = empty($user) ? $this->database->getUser() : $user;
112+
$password = empty($password) ? $this->database->getPassword() : $password;
113+
$isFile = empty($isFile) ? $this->database->getIsFile() : $isFile;
129114

130115
// Establish PDO connection
131116
try {
132-
if ($IsFile) {
133-
$this->dbh = new \PDO($setDsn, null, null, null);
117+
if ($isFile) {
118+
$this->dbh = new \PDO($dsn, null, null, null);
134119
$this->_connected = true;
135120
} else {
136-
$this->dbh = new \PDO($setDsn, $setUser, $setPassword, $setOptions);
121+
$this->dbh = new \PDO($dsn, $user, $password, $options);
137122
$this->_connected = true;
138123
}
139124
} catch (\PDOException $e) {

lib/Database/ez_pgsql.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
class ez_pgsql extends ezsqlModel implements DatabaseInterface
1212
{
13-
private static $isSecure = false;
14-
private static $secure = null;
15-
1613
/**
1714
* Database connection handle
1815
* @var resource

lib/Database/ez_sqlsrv.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ class ez_sqlsrv extends ezsqlModel implements DatabaseInterface
2020
-11 => 'uniqueidentifier', -3 => 'varbinary', 12 => 'varchar', -152 => 'xml',
2121
);
2222

23-
private static $isSecure = false;
24-
2523
/**
2624
* Database connection handle
2725
* @var resource
@@ -86,7 +84,7 @@ public function connect($user = '', $password = '', $name = '', $host = 'localho
8684
$host = ($host != 'localhost') ? $this->database->getHost() : $host;
8785

8886
// Blank user assumes Windows Authentication
89-
if (self::$isSecure) {
87+
if ($this->isSecure) {
9088
$connectionOptions = array(
9189
"UID" => $user,
9290
"PWD" => $password,

lib/ezFunctions.php

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,12 @@ function createCertificate(
102102
array $details = ["commonName" => "localhost"]
103103
)
104104
{
105-
ezQuery::createCertificate($privatekeyFile, $certificateFile, $signingFile, $ssl_path, $details);
106-
}
107-
108-
function securePDO(
109-
$vendor = null,
110-
$key = 'certificate.key',
111-
$cert = 'certificate.crt',
112-
$ca = 'cacert.pem',
113-
$path = '.'.\_DS)
114-
{
115-
ez_pdo::securePDO($vendor, $key, $cert, $ca, $path);
116-
}
117-
118-
function secureSQL(
119-
$key = 'certificate.key',
120-
$cert = 'certificate.crt',
121-
$ca = 'cacert.pem',
122-
$path = '.'.\_DS)
123-
{
124-
// todo
105+
return ezQuery::createCertificate($privatekeyFile, $certificateFile, $signingFile, $ssl_path, $details);
125106
}
126107

127108
/**
128109
* Creates an array from expressions in the following format
110+
*
129111
* @param strings $x, - The left expression.
130112
* @param strings $operator, - One of
131113
* '<', '>', '=', '!=', '>=', '<=', '<>', 'IN',, 'NOT IN', 'LIKE',

lib/ezsqlModel.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
*/
1212
class ezsqlModel extends ezQuery implements ezsqlModelInterface
1313
{
14+
protected $isSecure = false;
15+
protected $secureOptions = null;
16+
protected $sslKey = null;
17+
protected $sslCert = null;
18+
protected $sslCa = null;
19+
protected $sslPath = null;
20+
1421
/**
1522
* If set to true (i.e. $db->debug_all = true;) Then it will print out ALL queries and ALL results of your script.
1623
* @var boolean
@@ -642,7 +649,39 @@ public function count($all = true, $increase = false)
642649
return ($all) ? $this->num_queries : $this->conn_queries;
643650
}
644651

645-
/**
652+
public function secureSetup(
653+
string $key = 'certificate.key',
654+
string $cert = 'certificate.crt',
655+
string $ca = 'cacert.pem',
656+
string $path = '.'.\_DS)
657+
{
658+
if (! \file_exists($path.$cert) || ! \file_exists($path.$key)) {
659+
$vendor = \getVendor();
660+
if (($vendor != \SQLITE) || ($vendor != \MSSQL))
661+
$path = ezQuery::createCertificate();
662+
} elseif ($path == '.'.\_DS) {
663+
$ssl_path = \getcwd();
664+
$path = \preg_replace('/\\\/', \_DS, $ssl_path). \_DS;
665+
}
666+
667+
$this->isSecure = true;
668+
$this->sslKey = $key;
669+
$this->sslCert = $cert;
670+
$this->sslCa = $ca;
671+
$this->sslPath = $path;
672+
}
673+
674+
public function secureReset()
675+
{
676+
$this->isSecure = false;
677+
$this->sslKey = null;
678+
$this->sslCert = null;
679+
$this->sslCa = null;
680+
$this->sslPath = null;
681+
$this->secureOptions = null;
682+
}
683+
684+
/**
646685
* Returns, whether a database connection is established, or not
647686
*
648687
* @return boolean

tests/pdo/pdo_mysqlTest.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,43 @@ public function testMySQLQuery() {
109109

110110
$this->assertEquals(0, $this->object->query('DROP TABLE unit_test'));
111111
}
112-
112+
113+
/**
114+
* @covers ezsql\ezsqlModel::secureSetup
115+
* @covers ezsql\ezsqlModel::secureReset
116+
* @covers ezsql\Database\ez_pdo::handle
117+
* @covers ezsql\ezQuery::drop
118+
* @covers \primary
119+
*/
120+
public function testSecureSetup()
121+
{
122+
$this->object->secureSetup();
123+
$this->object->connect('mysql:host=' . self::TEST_DB_HOST . ';dbname=' . self::TEST_DB_NAME . ';port=' . self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD);
124+
125+
$this->assertEquals(0, $this->object->drop('new_create_test2'));
126+
$this->assertEquals(0, $this->object->create('new_create_test2',
127+
column('id', INTR, 11, notNULL, AUTO),
128+
column('create_key', VARCHAR, 50),
129+
primary('id_pk', 'id'))
130+
);
131+
132+
$this->assertEquals(1, $this->object->insert('new_create_test2',
133+
['create_key' => 'test 2'])
134+
);
135+
136+
$conn = $this->object->handle();
137+
$res = $conn->query("SHOW STATUS LIKE 'Ssl_cipher';")->fetchAll();
138+
$this->assertEquals('Ssl_cipher', $res[0]['Variable_name']);
139+
$this->assertEquals(0, $this->object->drop('new_create_test2'));
140+
$this->object->secureReset();
141+
}
113142

114143
/**
115-
* @covers ezsql\ezQuery::create
144+
* @covers ezsql\ezQuery::create
116145
*/
117146
public function testCreate()
118147
{
119-
$this->assertTrue($this->object->connect('mysql:host=' . self::TEST_DB_HOST . ';dbname=' . self::TEST_DB_NAME . ';port=' . self::TEST_DB_PORT, self::TEST_DB_USER, self::TEST_DB_PASSWORD));
148+
$this->assertTrue($this->object->connect());
120149

121150
$this->assertEquals($this->object->create('new_create_test',
122151
column('id', INTR, 11, notNULL, AUTO),
@@ -132,7 +161,7 @@ public function testCreate()
132161
}
133162

134163
/**
135-
* @covers ezsql\ezQuery::drop
164+
* @covers ezsql\ezQuery::drop
136165
*/
137166
public function testDrop()
138167
{
@@ -142,7 +171,7 @@ public function testDrop()
142171
}
143172

144173
/**
145-
* @covers ezsql\ezQuery::insert
174+
* @covers ezsql\ezQuery::insert
146175
*/
147176
public function testInsert()
148177
{

0 commit comments

Comments
 (0)