11<?php
2- /**
3- * ezSQL class - PDO
4- * Desc..: PDO component (part of ezSQL database abstraction library)
5- *
6- * @author Justin Vincent ([email protected] ) 7- * @author Stefanie Janine Stoelting <[email protected] > 8- * Contributor: Lawrence Stubbs <[email protected] > 9- * @link http://twitter.com/justinvincent
10- * @name ezSQL_pdo
11- * @package ezSQL
12- * @license FREE / Donation (LGPL - You may do what you like with ezSQL - no exceptions.)
13- *
14- */
152
163class ezSQL_pdo extends ezSQLcore
174{
@@ -47,26 +34,30 @@ class ezSQL_pdo extends ezSQLcore
4734 * The array for connection options, MySQL connection charset, for example
4835 * @var array
4936 */
50- private $ _options ;
37+ private static $ _options = array () ;
5138
5239 /**
5340 * Whether it is a file based database connection, for example to a SQLite
5441 * database file, or not
5542 * @var boolean Default is false
5643 */
57- private $ _isFileBased =false ;
44+ private $ _isFileBased = false ;
45+
46+ private static $ isSecure = false ;
47+ private static $ secure = null ;
48+ private $ dbh ;
5849
5950 /**
6051 * Show errors
6152 * @var boolean Default is true
6253 */
6354 public $ show_errors = true ;
6455
65- protected $ preparedvalues = array ();
56+ protected $ preparedValues = array ();
6657
6758 /**
68- * Constructor - allow the user to perform a qucik connect at the same time
69- * as initialising the ezSQL_sqlite class
59+ * Constructor - allow the user to perform a quick connect at the same time
60+ * as initializing the ezSQL_sqlite class
7061 *
7162 * @param string $dsn The connection parameter string
7263 * Default is empty string
@@ -82,12 +73,13 @@ class ezSQL_pdo extends ezSQLcore
8273 * dsn parameter
8374 * Default is false
8475 */
85- public function __construct ($ dsn ='' , $ user ='' , $ password ='' , $ options =array (), $ isFileBased =false ) {
76+ public function __construct ($ dsn = '' , $ user = '' , $ password = '' , $ options = array (), $ isFileBased = false )
77+ {
8678 if ( ! \class_exists ('PDO ' ) ) {
87- throw new Exception ('<b>Fatal Error:</b> ezSQL_pdo requires PDO Lib to be compiled and or linked in to the PHP engine ' );
79+ throw new \ Exception ('<b>Fatal Error:</b> ezSQL_pdo requires PDO Lib to be compiled and or linked in to the PHP engine ' );
8880 }
8981 if ( ! \class_exists ('ezSQLcore ' ) ) {
90- throw new Exception ('<b>Fatal Error:</b> ezSQL_pdo requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used ' );
82+ throw new \ Exception ('<b>Fatal Error:</b> ezSQL_pdo requires ezSQLcore (ez_sql_core.php) to be included/loaded before it can be used ' );
9183 }
9284
9385 parent ::__construct ();
@@ -103,6 +95,33 @@ public function __construct($dsn='', $user='', $password='', $options=array(), $
10395 \setQuery ($ this );
10496 } // __construct
10597
98+ public static function securePDO (
99+ $ vendor = null ,
100+ $ key = 'certificate.key ' ,
101+ $ cert = 'certificate.crt ' ,
102+ $ ca = 'cacert.pem ' ,
103+ $ path = '. ' .\_DS )
104+ {
105+ if (\array_key_exists (\strtolower ($ vendor ), \VENDOR )
106+ && (! \file_exists ($ path .$ cert ) || ! \file_exists ($ path .$ key )))
107+ ezQuery::createCertificate ();
108+
109+ if (($ vendor == 'pgsql ' ) || ($ vendor == 'postgresql ' )) {
110+ self ::$ secure = "sslmode=require;sslcert= " .$ path .$ cert .";sslkey= " .$ path .$ key .";sslrootcert= " .$ path .$ ca ."; " ;
111+ self ::$ isSecure = true ;
112+ } elseif (($ vendor == 'mysql ' ) || ($ vendor == 'mysqli ' )) {
113+ self ::$ _options = array (
114+ \PDO ::MYSQL_ATTR_SSL_KEY => $ path .$ key ,
115+ \PDO ::MYSQL_ATTR_SSL_CERT => $ path .$ cert ,
116+ \PDO ::MYSQL_ATTR_SSL_CA => $ path .$ ca ,
117+ \PDO ::MYSQL_ATTR_SSL_VERIFY_SERVER_CERT => false ,
118+ );
119+ } elseif (($ vendor == 'sqlserver ' ) || ($ vendor == 'mssql ' ) || ($ vendor == 'sqlsrv ' )) {
120+ self ::$ secure = ";Encrypt=true;TrustServerCertificate=true " ;
121+ self ::$ isSecure = true ;
122+ }
123+ }
124+
106125 /**
107126 * Try to connect to the database server in the DSN parameters
108127 *
@@ -121,16 +140,21 @@ public function __construct($dsn='', $user='', $password='', $options=array(), $
121140 * Default is false
122141 * @return boolean
123142 */
124- public function connect ($ dsn ='' , $ dbuser ='' , $ dbpassword ='' , $ options =array (), $ isFileBased =false ) {
143+ public function connect ($ dsn = '' , $ dbuser = '' , $ dbpassword = '' , $ options = array (), $ isFileBased = false )
144+ {
125145 $ this ->_connected = false ;
126146
127- $ this ->_dsn = empty ($ dsn ) ? $ this ->_dsn : $ dsn ;
147+ if (self ::$ isSecure )
148+ $ this ->_dsn = empty ($ dsn ) ? $ this ->_dsn .$ this ->secure : $ dsn .$ this ->secure ;
149+ else
150+ $ this ->_dsn = empty ($ dsn ) ? $ this ->_dsn : $ dsn ;
151+
128152 $ this ->_isFileBased = $ isFileBased ;
129153
130154 if (!$ isFileBased ) {
131155 $ this ->_dbuser = empty ($ dbuser ) ? $ this ->_dbuser : $ dbuser ;
132156 $ this ->_dbpassword = empty ($ dbpassword ) ? $ this ->_dbpassword : $ dbpassword ;
133- $ this ->_options = $ options ;
157+ $ this ->_options = empty ( $ options ) ? $ this -> _options : $ options ;
134158
135159 // Must have a user and a password if not file based
136160 if ( empty ($ this ->_dsn ) || empty ($ this ->_dbuser ) || empty ($ this ->_dbpassword ) ) {
@@ -156,8 +180,7 @@ public function connect($dsn='', $dbuser='', $dbpassword='', $options=array(), $
156180 $ this ->dbh = new \PDO ($ this ->_dsn , $ this ->_dbuser , $ this ->_dbpassword , $ this ->_options );
157181 $ this ->_connected = true ;
158182 }
159- }
160- catch (\PDOException $ e ) {
183+ } catch (\PDOException $ e ) {
161184 $ this ->register_error ($ e ->getMessage ());
162185 $ this ->show_errors ? \trigger_error ($ e ->getMessage () . '- $dsn: ' . $ dsn , \E_USER_WARNING ) : null ;
163186 return false ;
@@ -186,7 +209,8 @@ public function connect($dsn='', $dbuser='', $dbpassword='', $options=array(), $
186209 * Default is false
187210 * @return boolean
188211 */
189- public function quick_connect ($ dsn ='' , $ user ='' , $ password ='' , $ options =array (), $ isFileBased =false ) {
212+ public function quick_connect ($ dsn = '' , $ user = '' , $ password = '' , $ options = array (), $ isFileBased = false )
213+ {
190214 return $ this ->connect ($ dsn , $ user , $ password , $ options , $ isFileBased );
191215 } // quick_connect
192216
@@ -201,7 +225,8 @@ public function quick_connect($dsn='', $user='', $password='', $options=array(),
201225 * @param string $str
202226 * @return string
203227 */
204- public function escape ($ str ) {
228+ public function escape ($ str )
229+ {
205230 // If there is no existing database connection then try to connect
206231 if ( ! isset ($ this ->dbh ) || ! $ this ->dbh ) {
207232 $ this ->connect ($ this ->_dsn , $ this ->user , $ this ->password , $ this ->_options , $ this ->_isFileBased );
@@ -219,7 +244,8 @@ public function escape($str) {
219244 *
220245 * @return string
221246 */
222- public function sysdate () {
247+ public function sysdate ()
248+ {
223249 return "datetime('now') " ;
224250 } // sysdate
225251
@@ -228,7 +254,8 @@ public function sysdate() {
228254 *
229255 * @return string
230256 */
231- public function catch_error (){
257+ public function catch_error ()
258+ {
232259 $ error_str = 'No error info ' ;
233260
234261 $ err_array = $ this ->dbh ->errorInfo ();
@@ -277,7 +304,9 @@ public function query_prepared($query, $param = null, $isselect = false)
277304 * @param type $query
278305 * @return object
279306 */
280- public function query ($ query , $ use_prepare = false ) {
307+ public function query ($ query , $ use_prepare = false )
308+ {
309+ $ param = [];
281310 if ($ use_prepare )
282311 $ param = $ this ->prepareValues ();
283312
@@ -418,11 +447,20 @@ public function query($query, $use_prepare = false) {
418447 /**
419448 * Close the database connection
420449 */
421- public function disconnect (){
450+ public function disconnect ()
451+ {
422452 if ($ this ->dbh ) {
423453 $ this ->dbh = null ;
424454 $ this ->_connected = false ;
425455 }
426456 } // disconnect
427457
458+ /**
459+ * Get connection handle
460+ */
461+ public function connection ()
462+ {
463+ return $ this ->dbh ;
464+ }
465+
428466} // ezSQL_pdo
0 commit comments