From 33766e1e2d0d41b42c6534094a4a1f65891a2447 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 19 Jan 2022 15:20:46 +0900 Subject: [PATCH] Update PostgreSQL DB libs for PHP 8.1, update PHP version check PHP version check now works with only max too, if called with (null, '1.2.3') then php version will be checked <= 1.2.3 Debug Support has a to string print method printToString which tries to print all types of var content as string (bool, int, float, resource, object, etc) Debug Support printAr supports ##HTMLPRE## style too (flag true) DB/IO + PgSQL is changed from is_resource check to "false" check to be compatible with new PHP 8.1 pgsql connect interface where all resources are changed to objects PgSql\Connect|Resource|... --- 4dev/tests/CoreLibsCheckPhpVersionTest.php | 25 ++++-- 4dev/tests/CoreLibsDebugSupportTest.php | 98 +++++++++++++++++++++- www/admin/class_test.db.php | 22 +++-- www/admin/class_test.php | 10 +-- www/admin/class_test.phpv.php | 4 + www/lib/CoreLibs/Check/PhpVersion.php | 43 +++++++--- www/lib/CoreLibs/DB/IO.php | 38 ++++----- www/lib/CoreLibs/DB/SQL/PgSQL.php | 66 ++++++++------- www/lib/CoreLibs/Debug/Support.php | 40 ++++++++- www/lib/CoreLibs/Template/SmartyExtend.php | 6 +- 10 files changed, 262 insertions(+), 90 deletions(-) diff --git a/4dev/tests/CoreLibsCheckPhpVersionTest.php b/4dev/tests/CoreLibsCheckPhpVersionTest.php index 7db6e7bc..b6b75273 100644 --- a/4dev/tests/CoreLibsCheckPhpVersionTest.php +++ b/4dev/tests/CoreLibsCheckPhpVersionTest.php @@ -29,14 +29,23 @@ final class CoreLibsCheckPHPVersionTest extends TestCase 'min 10' => ['10', '', false], 'min 10.0' => ['10.0', '', false], 'min 10.0.0' => ['10.0.0', '', false], - // max version, NOTE: update if php version bigger than 10 - 'max 10' => ['7', '10', true], - 'max 10.0' => ['7', '10.0', true], - 'max 10.0.0' => ['7', '10.0.0', true], - // max version - 'max 7' => ['5', '7', false], - 'max 7.4' => ['5', '7.4', false], - 'max 7.4.1' => ['5', '7.4.1', false], + // min/max version, NOTE: update if php version bigger than 10 + 'min 7/max 10' => ['7', '10', true], + 'min 7/max 10.0' => ['7', '10.0', true], + 'min 7/max 10.0.0' => ['7', '10.0.0', true], + // min/max version + 'min 5/max 7' => ['5', '7', false], + 'min 5/max 7.4' => ['5', '7.4', false], + 'min 5/max 7.4.1' => ['5', '7.4.1', false], + // max only + 'max 7' => ['', '7', false], + 'max 7.4' => ['', '7.4', false], + 'max 7.4.1' => ['', '7.4.1', false], + // max over + 'max 10' => ['', '10', true], + 'max 10.0' => ['', '10.0', true], + 'max 10.0.0' => ['', '10.0.0', true], + // TODO: add null tests ]; } diff --git a/4dev/tests/CoreLibsDebugSupportTest.php b/4dev/tests/CoreLibsDebugSupportTest.php index 0538ddac..069223cc 100644 --- a/4dev/tests/CoreLibsDebugSupportTest.php +++ b/4dev/tests/CoreLibsDebugSupportTest.php @@ -62,6 +62,67 @@ final class CoreLibsDebugSupportTest extends TestCase ]; } + public function printToStringProvider(): array + { + return [ + 'string' => [ + 'a string', + null, + 'a string', + ], + 'a number' => [ + 1234, + null, + '1234', + ], + 'a float number' => [ + 1234.5678, + null, + '1234.5678', + ], + 'bool true' => [ + true, + null, + 'TRUE', + ], + 'bool false' => [ + false, + null, + 'FALSE', + ], + 'an array default' => [ + ['a', 'b'], + null, + "
Array\n(\n"
+					. "    [0] => a\n"
+					. "    [1] => b\n"
+					. ")\n
", + ], + 'an array, no html' => [ + ['a', 'b'], + true, + "##HTMLPRE##" + . "Array\n(\n" + . " [0] => a\n" + . " [1] => b\n" + . ")\n" + . "##/HTMLPRE##", + ], + // resource + 'a resource' => [ + tmpfile(), + null, + '/^Resource id #\d+$/', + ], + // object + 'an object' => [ + new \CoreLibs\Debug\Support(), + null, + 'CoreLibs\Debug\Support', + ] + ]; + } + /** * Undocumented function * @@ -119,7 +180,7 @@ final class CoreLibsDebugSupportTest extends TestCase * * @cover ::printAr * @dataProvider printArProvider - * @testdox printAr $input will be $expected[$_dataName] + * @testdox printAr $input will be $expected [$_dataName] * * @param array $input * @param string $expected @@ -133,6 +194,41 @@ final class CoreLibsDebugSupportTest extends TestCase ); } + /** + * Undocumented function + * + * @cover ::printToString + * @dataProvider printToStringProvider + * @testdox printToString $input with $flag will be $expected [$_dataName] + * + * @param mixed $input + * @param boolean|null $flag + * @param string $expected + * @return void + */ + public function testPrintToString($input, ?bool $flag, string $expected): void + { + if ($flag === null) { + // if expected starts with / and ends with / then this is a regex compare + if (substr($expected, 0, 1) == '/' && substr($expected, -1, 1) == '/') { + $this->assertMatchesRegularExpression( + $expected, + \CoreLibs\Debug\Support::printToString($input) + ); + } else { + $this->assertEquals( + $expected, + \CoreLibs\Debug\Support::printToString($input) + ); + } + } else { + $this->assertEquals( + $expected, + \CoreLibs\Debug\Support::printToString($input, $flag) + ); + } + } + /** * Undocumented function * diff --git a/www/admin/class_test.db.php b/www/admin/class_test.db.php index 154c55ef..7d2bb913 100644 --- a/www/admin/class_test.db.php +++ b/www/admin/class_test.db.php @@ -33,6 +33,7 @@ ob_end_flush(); use CoreLibs\Debug\Support as DgS; use CoreLibs\DB\IO as DbIo; +use CoreLibs\Debug\Support; $log = new CoreLibs\Debug\Logging([ 'log_folder' => BASE . LOG, @@ -46,6 +47,7 @@ $log = new CoreLibs\Debug\Logging([ ]); $basic = new CoreLibs\Basic($log); $db = new CoreLibs\Admin\Backend(DB_CONFIG, $log); +$db->log->debug('START', '=============================>'); // NEXT STEP // $basic = new CoreLibs\Basic(); @@ -89,7 +91,8 @@ $query = "TRUNCATE test_foobar"; $db->dbExec($query); $status = $db->dbExec("INSERT INTO test_foo (test) VALUES ('FOO TEST " . time() . "') RETURNING test"); -print "DIRECT INSERT STATUS: $status | " +print "DIRECT INSERT STATUS: " . Support::printToString($status) . "| " + . "DB OBJECT:
" . print_r($status, true) . "
| " . "PRIMARY KEY: " . $db->dbGetInsertPK() . " | " . "RETURNING EXT: " . print_r($db->dbGetReturningExt(), true) . " | " . "RETURNING EXT[test]: " . print_r($db->dbGetReturningExt('test'), true) . " | " @@ -104,7 +107,7 @@ print "DIRECT INSERT PREVIOUS INSERTED: " // PREPARED INSERT $db->dbPrepare("ins_test_foo", "INSERT INTO test_foo (test) VALUES ($1) RETURNING test"); $status = $db->dbExecute("ins_test_foo", array('BAR TEST ' . time())); -print "PREPARE INSERT[ins_test_foo] STATUS: $status | " +print "PREPARE INSERT[ins_test_foo] STATUS: " . Support::printToString($status) . " | " . "PRIMARY KEY: " . $db->dbGetInsertPK() . " | " . "RETURNING EXT: " . print_r($db->dbGetReturningExt(), true) . " | " . "RETURNING RETURN: " . print_r($db->dbGetReturningArray(), true) . "
"; @@ -127,7 +130,7 @@ $status = $db->dbExec( . "('BAR 3 " . time() . "') " . "RETURNING test_foo_id, test" ); -print "DIRECT MULTIPLE INSERT WITH RETURN STATUS: $status | " +print "DIRECT MULTIPLE INSERT WITH RETURN STATUS: " . Support::printToString($status) . " | " . "PRIMARY KEYS: " . print_r($db->dbGetInsertPK(), true) . " | " . "RETURNING EXT: " . print_r($db->dbGetReturningExt(), true) . " | " . "RETURNING EXT[test]: " . print_r($db->dbGetReturningExt('test'), true) . " | " @@ -135,7 +138,7 @@ print "DIRECT MULTIPLE INSERT WITH RETURN STATUS: $status | " // no returning, but not needed ; $status = $db->dbExec("INSERT INTO test_foo (test) VALUES ('FOO; TEST " . time() . "');"); -print "DIRECT INSERT NO RETURN STATUS: $status | " +print "DIRECT INSERT NO RETURN STATUS: " . Support::printToString($status) . " | " . "PRIMARY KEY: " . $db->dbGetInsertPK() . " | " . "RETURNING EXT: " . print_r($db->dbGetReturningExt(), true) . " | " . "RETURNING ARRAY: " . print_r($db->dbGetReturningArray(), true) . "
"; @@ -150,20 +153,21 @@ if (is_array($s_res = $db->dbReturnRow($q)) && !empty($s_res['test'])) { // UPDATE WITH RETURNING $status = $db->dbExec("UPDATE test_foo SET test = 'SOMETHING DIFFERENT' " . "WHERE test_foo_id = " . $last_insert_pk . " RETURNING test"); -print "UPDATE WITH PK " . $last_insert_pk . " RETURN STATUS: $status | " +print "UPDATE WITH PK " . $last_insert_pk + . " RETURN STATUS: " . Support::printToString($status) . " | " . "RETURNING EXT: " . print_r($db->dbGetReturningExt(), true) . " | " . "RETURNING ARRAY: " . print_r($db->dbGetReturningArray(), true) . "
"; // INSERT WITH NO RETURNING $status = $db->dbExec("INSERT INTO test_foobar (type, integer) VALUES ('WITH DATA', 123)"); -print "INSERT WITH NO PRIMARY KEY NO RETURNING STATUS: $status | " +print "INSERT WITH NO PRIMARY KEY NO RETURNING STATUS: " . Support::printToString($status) . " | " . "PRIMARY KEY: " . $db->dbGetInsertPK() . " | " . "RETURNING EXT: " . print_r($db->dbGetReturningExt(), true) . " | " . "RETURNING ARRAY: " . print_r($db->dbGetReturningArray(), true) . "
"; $status = $db->dbExec("INSERT INTO test_foobar (type, integer) VALUES ('WITH DATA', 123) RETURNING type, integer"); -print "INSERT WITH NO PRIMARY KEY WITH RETURNING STATUS: $status | " +print "INSERT WITH NO PRIMARY KEY WITH RETURNING STATUS: " . Support::printToString($status) . " | " . "PRIMARY KEY: " . $db->dbGetInsertPK() . " | " . "RETURNING EXT: " . print_r($db->dbGetReturningExt(), true) . " | " . "RETURNING ARRAY: " . print_r($db->dbGetReturningArray(), true) . "
"; @@ -327,7 +331,9 @@ $status = $db->dbExec( . "('TIME: " . time() . "', " . rand(1, 10) . ")" ); print "OTHER SCHEMA INSERT STATUS: " - . $status . " | PK NAME: " . $db->dbGetInsertPKName() . ", PRIMARY KEY: " . $db->dbGetInsertPK() . "
"; + . Support::printToString($status) + . " | PK NAME: " . $db->dbGetInsertPKName() + . ", PRIMARY KEY: " . $db->dbGetInsertPK() . "
"; print "NULL TEST DB READ
"; $q = "SELECT uid, null_varchar, null_int FROM test_null_data WHERE uid = 'A'"; diff --git a/www/admin/class_test.php b/www/admin/class_test.php index dca655aa..b0bf55d5 100644 --- a/www/admin/class_test.php +++ b/www/admin/class_test.php @@ -48,12 +48,12 @@ print "TEST CLASS"; print ""; print '
Class Test: DB
'; -print '
Class Test: Colors
'; +print '
Class Test: COLORS
'; print '
Class Test: MIME
'; -print '
Class Test: Json
'; -print '
Class Test: Form Token
'; -print '
Class Test: Password
'; -print '
Class Test: Math
'; +print '
Class Test: JSON
'; +print '
Class Test: FORM TOKEN
'; +print '
Class Test: PASSWORD
'; +print '
Class Test: MATH
'; print '
Class Test: HTML/ELEMENTS
'; print '
Class Test: EMAIL
'; print '
Class Test: UIDS
'; diff --git a/www/admin/class_test.phpv.php b/www/admin/class_test.phpv.php index 1a5275e1..74fe189a 100644 --- a/www/admin/class_test.phpv.php +++ b/www/admin/class_test.phpv.php @@ -55,6 +55,9 @@ $min_version_s = '7'; $min_version_ss = '7.1'; $min_version = '7.1.0'; $max_version = '7.3.1'; +$max_version_ss = '8.1'; + +print "CURRENT: " . phpversion() . "
"; // class print "MIN: $min_version: " . (string)$_phpv->checkPHPVersion($min_version) . "
"; print "MIN/MAX: $min_version/$max_version: " . (string)$_phpv->checkPHPVersion($min_version, $max_version) . "
"; @@ -66,6 +69,7 @@ print "S::MIN/MAX: $min_version/$max_version: " . (string)$phpv_class::checkPHPVersion($min_version, $max_version) . "
"; print "S::MIN/S: $min_version_s: " . (string)$phpv_class::checkPHPVersion($min_version_s) . "
"; print "S::MIN/SS: $min_version_ss: " . (string)$phpv_class::checkPHPVersion($min_version_ss) . "
"; +print "S::MAX $max_version_ss: " . (string)$phpv_class::checkPHPVersion(null, $max_version_ss) . "
"; // use stats print "U-S::MIN: $min_version: " . (string)PhpVersion::checkPHPVersion($min_version) . "
"; diff --git a/www/lib/CoreLibs/Check/PhpVersion.php b/www/lib/CoreLibs/Check/PhpVersion.php index 2f70abdc..b77ca792 100644 --- a/www/lib/CoreLibs/Check/PhpVersion.php +++ b/www/lib/CoreLibs/Check/PhpVersion.php @@ -8,28 +8,38 @@ class PhpVersion { /** * checks if running PHP version matches given PHP version (min or max) - * @param string $min_version minimum version as string (x, x.y, x.y.x) - * @param string $max_version optional maximum version as string (x, x.y, x.y.x) + * if either is empty or null it will be ignored + * if no min version (null or empty) + * @param string|null $min_version minimum version as string (x, x.y, x.y.x) + * @param string|null $max_version optional maximum version as string (x, x.y, x.y.x) * @return bool true if ok, false if not matching version */ - public static function checkPHPVersion(string $min_version, string $max_version = ''): bool + public static function checkPHPVersion(?string $min_version, ?string $max_version = null): bool { // exit with false if the min/max strings are wrong - if (!preg_match("/^\d{1,2}(\.\d{1,2})?(\.\d{1,2})?$/", $min_version)) { + if ( + !empty($min_version) && + !preg_match("/^\d{1,2}(\.\d{1,2})?(\.\d{1,2})?$/", $min_version) + ) { return false; } // max is only chcked if it is set - if ($max_version && !preg_match("/^\d{1,2}(\.\d{1,2})?(\.\d{1,2})?$/", $max_version)) { + if ( + !empty($max_version) && + !preg_match("/^\d{1,2}(\.\d{1,2})?(\.\d{1,2})?$/", $max_version) + ) { return false; } // split up the version strings to calc the compare number - $version = explode('.', $min_version); - $min_version = (int)$version[0] * 10000 + (int)($version[1] ?? 0) * 100 + (int)($version[2] ?? 0); - if ($max_version) { + if (!empty($min_version)) { + $version = explode('.', $min_version); + $min_version = (int)$version[0] * 10000 + (int)($version[1] ?? 0) * 100 + (int)($version[2] ?? 0); + } + if (!empty($max_version)) { $version = explode('.', $max_version); $max_version = (int)$version[0] * 10000 + (int)($version[1] ?? 0) * 100 + (int)($version[2] ?? 0); // drop out if min is bigger max, equal size is okay, that would be only THIS - if ($min_version > $max_version) { + if (!empty($min_version) && $min_version > $max_version) { return false; } } @@ -40,9 +50,20 @@ class PhpVersion define('PHP_VERSION_ID', (int)$version[0] * 10000 + (int)$version[1] * 100 + (int)$version[2]); } // check if matching for version - if (!$max_version && PHP_VERSION_ID >= $min_version) { + if ( + !empty($min_version) && empty($max_version) && + PHP_VERSION_ID >= $min_version + ) { return true; - } elseif (PHP_VERSION_ID >= $min_version && PHP_VERSION_ID <= $max_version) { + } elseif ( + empty($min_version) && !empty($max_version) && + PHP_VERSION_ID <= $max_version + ) { + return true; + } elseif ( + !empty($min_version) && !empty($max_version) && + PHP_VERSION_ID >= $min_version && PHP_VERSION_ID <= $max_version + ) { return true; } // if no previous return, fail diff --git a/www/lib/CoreLibs/DB/IO.php b/www/lib/CoreLibs/DB/IO.php index 594b9f36..2fc80b98 100644 --- a/www/lib/CoreLibs/DB/IO.php +++ b/www/lib/CoreLibs/DB/IO.php @@ -276,7 +276,7 @@ class IO public $query; // the query string at the moment // only inside // basic vars - /** @var resource|bool|int|null */ + /** @var PgSql\Connection|resource|bool|int|null */ private $dbh; // the dbh handler, if disconnected by command is null, bool:false/int:-1 on error, /** @var bool */ public $db_debug = false; // DB_DEBUG ... (if set prints out debug msgs) @@ -303,7 +303,7 @@ class IO /** @var array */ public $cursor_ext; // hash of hashes // per query vars - /** @var resource */ + /** @var PgSql\Result|resource */ public $cursor; // actual cursor (DBH) /** @var int */ public $num_rows; // how many rows have been found @@ -312,7 +312,7 @@ class IO /** @var array */ public $field_names = []; // array with the field names of the current query /** @var array */ - private $insert_id_arr; // always return as array, even if only one + private $insert_id_arr = []; // always return as array, even if only one /** @var string */ private $insert_id_pk_name; // primary key name for insert recovery from insert_id_arr // other vars @@ -506,7 +506,7 @@ class IO */ private function __closeDB(): void { - if (isset($this->dbh) && $this->dbh) { + if (!empty($this->dbh) && $this->dbh !== false) { $this->db_functions->__dbClose(); $this->dbh = null; } @@ -624,7 +624,7 @@ class IO * if error_id set, writes long error string into error_msg * NOTE: * needed to make public so it can be called from DB.Array.IO too - * @param resource|bool $cursor current cursor for pg_result_error, mysql uses dbh, + * @param PgSql\Result|resource|bool $cursor current cursor for pg_result_error, mysql uses dbh, * pg_last_error too, but pg_result_error is more accurate * @param string $msg optional message * @return void has no return @@ -633,10 +633,10 @@ class IO { $pg_error_string = ''; $where_called = (string)\CoreLibs\Debug\Support::getCallerMethod(); - if (is_resource($cursor)) { + if ($cursor !== false) { $pg_error_string = $this->db_functions->__dbPrintError($cursor); } - if (!$cursor && method_exists($this->db_functions, '__dbPrintError')) { + if ($cursor === false && method_exists($this->db_functions, '__dbPrintError')) { $pg_error_string = $this->db_functions->__dbPrintError(); } if ($pg_error_string) { @@ -850,7 +850,7 @@ class IO { // if FALSE returned, set error stuff // if either the cursor is false - if (!is_resource($this->cursor) || $this->db_functions->__dbLastErrorQuery()) { + if ($this->cursor === false || $this->db_functions->__dbLastErrorQuery()) { // printout Query if debug is turned on if ($this->db_debug) { $this->__dbDebug('db', $this->query, 'dbExec', 'Q[nc]'); @@ -907,7 +907,7 @@ class IO * @param boolean $returning_id * @param string $query * @param string|null $pk_name - * @param resource $cursor + * @param PgSql\Result|resource $cursor * @param string|null $stm_name If not null, is dbExecutre run * @return void */ @@ -1518,7 +1518,7 @@ class IO * if pk name is table name and _id, pk_name * is not needed to be set * if NULL is given here, no RETURNING will be auto added - * @return resource|false cursor for this query or false on error + * @return PgSql\Result|resource|false cursor for this query or false on error */ public function dbExec(string $query = '', string $pk_name = '') { @@ -1529,7 +1529,7 @@ class IO } // ** actual db exec call $cursor = $this->db_functions->__dbQuery($this->query); - if (!is_resource($cursor)) { + if ($cursor === false) { return false; } $this->cursor = $cursor; @@ -1576,7 +1576,7 @@ class IO /** * checks a previous async query and returns data if finished * NEEDS : dbExecAsync - * @return bool|resource cursor resource if the query is still running, + * @return bool|PgSql\Result|resource cursor resource if the query is still running, * false if an error occured or cursor of that query */ public function dbCheckAsync() @@ -1587,7 +1587,7 @@ class IO return true; } else { $cursor = $this->db_functions->__dbGetResult(); - if (!is_resource($cursor)) { + if ($cursor === false) { return false; } // get the result/or error @@ -1615,7 +1615,7 @@ class IO /** * executes a cursor and returns the data, if no more data 0 will be returned - * @param resource|false $cursor the cursor from db_exec or + * @param PgSql\Result|resource|false $cursor the cursor from db_exec or * pg_query/pg_exec/mysql_query * if not set will use internal cursor, * if not found, stops with 0 (error) @@ -1625,10 +1625,10 @@ class IO public function dbFetchArray($cursor = false, bool $assoc_only = false) { // return false if no query or cursor set ... - if (!is_resource($cursor)) { + if ($cursor === false) { $cursor = $this->cursor; } - if (!is_resource($cursor)) { + if ($cursor === false) { $this->error_id = 12; $this->__dbError(); return false; @@ -1752,7 +1752,7 @@ class IO * @param string $stm_name statement name * @param string $query queryt string to run * @param string $pk_name optional primary key - * @return bool|resource false on error, true on warning or result on full ok + * @return bool|PgSql\Result|resource false on error, true on warning or result on full ok */ public function dbPrepare(string $stm_name, string $query, string $pk_name = '') { @@ -1887,7 +1887,7 @@ class IO $this->__dbDebug('db', $this->__dbDebugPrepare($stm_name, $data), 'dbExecPrep', 'Q'); } $result = $this->db_functions->__dbExecute($stm_name, $data); - if (!is_resource($result)) { + if ($result === false) { $this->log->debug('ExecuteData', 'ERROR in STM[' . $stm_name . '|' . $this->prepare_cursor[$stm_name]['result'] . ']: ' . $this->log->prAr($data)); @@ -2340,7 +2340,7 @@ class IO * Either as single array level for single insert * Or nested array for multiple insert values * - * If key was set only returns those values directly or as array + * If key was set only returns tghose values directly or as array * * On multiple insert return the position for which to return can be set too * diff --git a/www/lib/CoreLibs/DB/SQL/PgSQL.php b/www/lib/CoreLibs/DB/SQL/PgSQL.php index 8f4589ff..5f71a9c6 100644 --- a/www/lib/CoreLibs/DB/SQL/PgSQL.php +++ b/www/lib/CoreLibs/DB/SQL/PgSQL.php @@ -51,7 +51,8 @@ class PgSQL { /** @var string */ private $last_error_query; - /** @var resource|bool */ + // NOTE for PHP 8.1 this is no longer a resource + /** @var PgSql\Connection|resource|bool */ private $dbh; /** @@ -77,17 +78,17 @@ class PgSQL /** * wrapper for gp_query, catches error and stores it in class var * @param string $query query string - * @return resource|bool query result + * @return PgSql\Result|resource|bool query result */ public function __dbQuery(string $query) { $this->last_error_query = ''; - if (!is_resource($this->dbh)) { + if ($this->dbh === false) { return false; } // read out the query status and save the query if needed $result = pg_query($this->dbh, $query); - if (!$result) { + if ($result === false) { $this->last_error_query = $query; } return $result; @@ -100,7 +101,7 @@ class PgSQL */ public function __dbSendQuery(string $query): bool { - if (!is_resource($this->dbh)) { + if ($this->dbh === false) { return false; } $result = pg_send_query($this->dbh, $query); @@ -109,16 +110,16 @@ class PgSQL /** * wrapper for pg_get_result - * @return resource|bool resource handler or false for error + * @return PgSql\Result|resource|bool resource handler or false for error */ public function __dbGetResult() { $this->last_error_query = ''; - if (!is_resource($this->dbh)) { + if ($this->dbh === false) { return false; } $result = pg_get_result($this->dbh); - if (!is_resource($result)) { + if ($result === false) { return false; } if ($error = pg_result_error($result)) { @@ -133,11 +134,12 @@ class PgSQL */ public function __dbClose(): void { - if (!is_resource($this->dbh)) { + if ($this->dbh === false) { return; } if (pg_connection_status($this->dbh) === PGSQL_CONNECTION_OK) { - pg_close($this->dbh); + // in 8.1 this throws an error, and we don't need that anyway + // pg_close($this->dbh); } } @@ -145,11 +147,11 @@ class PgSQL * wrapper for pg_prepare * @param string $name statement name * @param string $query query string - * @return resource|bool prepare statement handler or false for error + * @return PgSql\Result|resource|bool prepare statement handler or false for error */ public function __dbPrepare(string $name, string $query) { - if (!is_resource($this->dbh)) { + if ($this->dbh === false) { return false; } $result = pg_prepare($this->dbh, $name, $query); @@ -163,11 +165,11 @@ class PgSQL * wrapper for pg_execute for running a prepared statement * @param string $name statement name * @param array $data data array - * @return resource|bool returns status or false for error + * @return PgSql\Result|resource|bool returns status or false for error */ public function __dbExecute(string $name, array $data) { - if (!is_resource($this->dbh)) { + if ($this->dbh === false) { return false; } $result = pg_execute($this->dbh, $name, $data); @@ -179,7 +181,7 @@ class PgSQL /** * wrapper for pg_num_rows - * @param resource $cursor cursor resource + * @param PgSql\Result|resource $cursor cursor PgSql\Result (former resource) * @return int number of rows, -1 on error */ public function __dbNumRows($cursor): int @@ -189,7 +191,7 @@ class PgSQL /** * wrapper for pg_num_fields - * @param resource $cursor cursor resource + * @param PgSql\Result|resource $cursor cursor PgSql\Result (former resource) * @return int number for fields in result, -1 on error */ public function __dbNumFields($cursor): int @@ -199,7 +201,7 @@ class PgSQL /** * wrapper for pg_field_name - * @param resource $cursor cursor resource + * @param PgSql\Result|resource $cursor cursor PgSql\Result (former resource) * @param int $i field position * @return string|bool name or false on error */ @@ -211,7 +213,7 @@ class PgSQL /** * wrapper for pg_fetch_array * if through/true false, use __dbResultType(true) - * @param resource $cursor cursor resource + * @param PgSql\Result|resource $cursor cursor PgSql\Result (former resource) * @param int $result_type result type as int number * @return array|bool array result data or false on end/error */ @@ -237,7 +239,7 @@ class PgSQL /** * wrapper for pg_fetch_all - * @param resource $cursor cursor resource + * @param PgSql\Result|resource $cursor cursor PgSql\Result (former resource) * @return array|bool data array or false for end/error */ public function __dbFetchAll($cursor) @@ -247,7 +249,7 @@ class PgSQL /** * wrapper for pg_affected_rows - * @param resource $cursor cursor resource + * @param PgSql\Result|resource $cursor cursor PgSql\Result (former resource) * @return int affected rows, 0 for none */ public function __dbAffectedRows($cursor): int @@ -291,7 +293,7 @@ class PgSQL // I have to do manually or I overwrite the original insert internal vars ... if ($q = $this->__dbQuery($q)) { // abort if this is not an resource - if (!is_resource($q)) { + if ($q === false) { return false; } if (is_array($res = $this->__dbFetchArray($q))) { @@ -323,7 +325,7 @@ class PgSQL if ($schema) { $q = "SHOW search_path"; $cursor = $this->__dbQuery($q); - if (!is_resource($cursor)) { + if ($cursor === false) { return false; } $__db_fetch_array = $this->__dbFetchArray($cursor); @@ -355,7 +357,7 @@ class PgSQL . "pg_attribute.attnum = any(pg_index.indkey) " . "AND indisprimary"; $cursor = $this->__dbQuery($q); - if (is_resource($cursor)) { + if ($cursor !== false) { $__db_fetch_array = $this->__dbFetchArray($cursor); if (!is_array($__db_fetch_array)) { return false; @@ -377,7 +379,7 @@ class PgSQL * @param string $db_name databse name * @param integer $db_port port (int, 5432 is default) * @param string $db_ssl SSL (allow is default) - * @return resource|bool db handler resource or false on error + * @return PgSql\Connection|resource|bool db handler PgSql\Connection or false on error */ public function __dbConnect( string $db_host, @@ -402,12 +404,12 @@ class PgSQL /** * reads the last error for this cursor and returns * html formatted string with error name - * @param ?resource $cursor cursor resource or null + * @param null|PgSql\Result|resource $cursor cursor PgSql\Result (former resource) or null * @return string error string */ public function __dbPrintError($cursor = null): string { - if (!is_resource($this->dbh)) { + if ($this->dbh === false) { return ''; } // run the query again for the error result here @@ -431,7 +433,7 @@ class PgSQL */ public function __dbMetaData(string $table, $extended = false) { - if (!is_resource($this->dbh)) { + if ($this->dbh === false) { return false; } // needs to prefixed with @ or it throws a warning on not existing table @@ -445,7 +447,7 @@ class PgSQL */ public function __dbEscapeString($string): string { - if (!is_resource($this->dbh)) { + if ($this->dbh === false) { return ''; } return pg_escape_string($this->dbh, (string)$string); @@ -460,7 +462,7 @@ class PgSQL */ public function __dbEscapeLiteral($string): string { - if (!is_resource($this->dbh)) { + if ($this->dbh === false) { return ''; } return pg_escape_string($this->dbh, (string)$string); @@ -473,7 +475,7 @@ class PgSQL */ public function __dbEscapeBytea($bytea): string { - if (!is_resource($this->dbh)) { + if ($this->dbh === false) { return ''; } return pg_escape_bytea($this->dbh, $bytea); @@ -485,7 +487,7 @@ class PgSQL */ public function __dbConnectionBusy(): bool { - if (!is_resource($this->dbh)) { + if ($this->dbh === false) { return false; } return pg_connection_busy($this->dbh); @@ -499,7 +501,7 @@ class PgSQL */ public function __dbVersion(): string { - if (!is_resource($this->dbh)) { + if ($this->dbh === false) { return ''; } // array has client, protocol, server diff --git a/www/lib/CoreLibs/Debug/Support.php b/www/lib/CoreLibs/Debug/Support.php index 82f15239..71682df8 100644 --- a/www/lib/CoreLibs/Debug/Support.php +++ b/www/lib/CoreLibs/Debug/Support.php @@ -31,12 +31,44 @@ class Support /** * prints a html formatted (pre) array - * @param array $array any array - * @return string formatted array for output with
 tag added
+	 * @param  array $array   any array
+	 * @param  bool         $no_html set to true to use ##HTMLPRE##
+	 * @return string                formatted array for output with 
 tag added
 	 */
-	public static function printAr(array $array): string
+	public static function printAr(array $array, bool $no_html = false): string
 	{
-		return "
" . print_r($array, true) . "
"; + if ($no_html === false) { + return "
" . print_r($array, true) . "
"; + } else { + return '##HTMLPRE##' . print_r($array, true) . '##/HTMLPRE##'; + } + } + + /** + * print out any data as string + * will convert boolean to TRUE/FALSE + * if object return get_class + * for array use printAr function, can be controlled with no_html for + * Debug\Logging compatible output + * @param mixed $mixed + * @param bool $no_html set to true to use ##HTMLPRE## + * @return string + */ + public static function printToString($mixed, bool $no_html = false): string + { + if (is_bool($mixed)) { + return $mixed ? 'TRUE' : 'FALSE'; + } elseif (is_resource($mixed)) { + return (string)$mixed; + } elseif (is_object($mixed)) { + return get_class($mixed); + } elseif (is_array($mixed)) { + // use the pre one OR debug one + return self::printAr($mixed, $no_html); + } else { + // should be int/float/string + return (string)$mixed; + } } /** diff --git a/www/lib/CoreLibs/Template/SmartyExtend.php b/www/lib/CoreLibs/Template/SmartyExtend.php index e8223245..fd363ca5 100644 --- a/www/lib/CoreLibs/Template/SmartyExtend.php +++ b/www/lib/CoreLibs/Template/SmartyExtend.php @@ -25,10 +25,14 @@ namespace CoreLibs\Template; // HARD CODED path: // __DIR__: lib/CoreLibs/Template/ // smarty located in lib/Smarty/ +// require_once(__DIR__ . '/../../Smarty/Smarty.class.php'); require_once(__DIR__ . '/../../Smarty/SmartyBC.class.php'); // So it doesn't start looking around in the wrong naemspace as smarty doesn't have one +// use Smarty; use SmartyBC; +// technically this can be Smarty +// class SmartyExtend extends Smarty class SmartyExtend extends SmartyBC { // internal translation engine @@ -170,8 +174,6 @@ class SmartyExtend extends SmartyBC $this->setLangEncoding(); // iinit lang $this->l10n = new \CoreLibs\Language\L10n($this->lang); - // variable variable register - // $this->register_modifier('getvar', [&$this, 'get_template_vars']); /** @phpstan-ignore-next-line */ $this->registerPlugin('modifier', 'getvar', [&$this, 'get_template_vars']);