From 05aefcfdfc50b41fa8fa1aa11c5855d41b504753 Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 20 Jun 2026 01:27:02 -0400 Subject: [PATCH] Fun w/ DB --- src/Framework/Arrays/CountriesCommon.php | 211 ++++++++++++++++++++ src/Framework/Assets.php | 21 +- src/Framework/Common.php | 46 ++++- src/Framework/Database/Model.php | 14 +- src/Framework/Trait/Database/RunSql.php | 63 ++++-- src/Framework/Trait/Database/Validation.php | 12 +- 6 files changed, 324 insertions(+), 43 deletions(-) create mode 100644 src/Framework/Arrays/CountriesCommon.php diff --git a/src/Framework/Arrays/CountriesCommon.php b/src/Framework/Arrays/CountriesCommon.php new file mode 100644 index 0000000..f14c0cc --- /dev/null +++ b/src/Framework/Arrays/CountriesCommon.php @@ -0,0 +1,211 @@ + 'Afghanistan', + 'ALB' => 'Albania', + 'ALG' => 'Algeria', + 'AND' => 'Andorra', + 'ANG' => 'Angola', + 'ANT' => 'Antigua and Barbuda', + 'ARG' => 'Argentina', + 'ARM' => 'Armenia', + 'AUS' => 'Australia', + 'AUT' => 'Austria', + 'AZE' => 'Azerbaijan', + 'BAH' => 'Bahamas', + 'BAH' => 'Bahrain', + 'BAN' => 'Bangladesh', + 'BAR' => 'Barbados', + 'BEL' => 'Belgium', + 'BEL' => 'Belize', + 'BEN' => 'Benin', + 'BHU' => 'Bhutan', + 'BOL' => 'Bolivia', + 'BIH' => 'Bosnia and Herzegovina', + 'BOT' => 'Botswana', + 'BRA' => 'Brazil', + 'BRU' => 'Brunei', + 'BUL' => 'Bulgaria', + 'BUR' => 'Burkina Faso', + 'BUR' => 'Burundi', + 'CAV' => 'Cabo Verde', + 'CAM' => 'Cambodia', + 'CMR' => 'Cameroon', + 'CAN' => 'Canada', + 'CAF' => 'Central African Republic', + 'CHA' => 'Chad', + 'CHI' => 'Chile', + 'CHN' => 'China', + 'COL' => 'Colombia', + 'COM' => 'Comoros', + 'CGO' => 'Congo, Republic of the', + 'COD' => 'Congo, Democratic Republic of the', + 'CRI' => 'Costa Rica', + 'HRV' => 'Croatia', + 'CUB' => 'Cuba', + 'CYP' => 'Cyprus', + 'CZE' => 'Czech Republic', + 'DEN' => 'Denmark', + 'DJB' => 'Djibouti', + 'DMA' => 'Dominica', + 'DOM' => 'Dominican Republic', + 'ECU' => 'Ecuador', + 'EGY' => 'Egypt', + 'SLV' => 'El Salvador', + 'GEQ' => 'Equatorial Guinea', + 'ERI' => 'Eritrea', + 'EST' => 'Estonia', + 'SWZ' => 'Eswatini', + 'ETH' => 'Ethiopia', + 'FIJ' => 'Fiji', + 'FIN' => 'Finland', + 'FRA' => 'France', + 'GAB' => 'Gabon', + 'GMB' => 'Gambia', + 'GEO' => 'Georgia', + 'DEU' => 'Germany', + 'GHA' => 'Ghana', + 'GRC' => 'Greece', + 'GRD' => 'Grenada', + 'GTM' => 'Guatemala', + 'GIN' => 'Guinea', + 'GNB' => 'Guinea-Bissau', + 'GUY' => 'Guyana', + 'HAI' => 'Haiti', + 'HND' => 'Honduras', + 'HUN' => 'Hungary', + 'ISL' => 'Iceland', + 'IND' => 'India', + 'IDN' => 'Indonesia', + 'IRN' => 'Iran', + 'IRQ' => 'Iraq', + 'IRL' => 'Ireland', + 'ISR' => 'Israel', + 'ITA' => 'Italy', + 'JAM' => 'Jamaica', + 'JPN' => 'Japan', + 'JOR' => 'Jordan', + 'KAZ' => 'Kazakhstan', + 'KEN' => 'Kenya', + 'KIR' => 'Kiribati', + 'KOR' => 'Korea, North', + 'KRS' => 'Korea, South', + 'KOS' => 'Kosovo', + 'KWT' => 'Kuwait', + 'KGZ' => 'Kyrgyzstan', + 'LAO' => 'Laos', + 'LVA' => 'Latvia', + 'LBN' => 'Lebanon', + 'LSO' => 'Lesotho', + 'LBR' => 'Liberia', + 'LBY' => 'Libya', + 'LIE' => 'Liechtenstein', + 'LTU' => 'Lithuania', + 'LUX' => 'Luxembourg', + 'MDG' => 'Madagascar', + 'MWI' => 'Malawi', + 'MYS' => 'Malaysia', + 'MDV' => 'Maldives', + 'MLI' => 'Mali', + 'MLT' => 'Malta', + 'MHL' => 'Marshall Islands', + 'MRT' => 'Mauritania', + 'MUS' => 'Mauritius', + 'MEX' => 'Mexico', + 'FSM' => 'Micronesia', + 'MDA' => 'Moldova', + 'MCO' => 'Monaco', + 'MNG' => 'Mongolia', + 'MNE' => 'Montenegro', + 'MAR' => 'Morocco', + 'MOZ' => 'Mozambique', + 'MYA' => 'Myanmar', + 'NAM' => 'Namibia', + 'NRU' => 'Nauru', + 'NPL' => 'Nepal', + 'NLD' => 'Netherlands', + 'NZL' => 'New Zealand', + 'NIC' => 'Nicaragua', + 'NER' => 'Niger', + 'NGA' => 'Nigeria', + 'MKD' => 'North Macedonia', + 'NOR' => 'Norway', + 'OMN' => 'Oman', + 'PAK' => 'Pakistan', + 'PLW' => 'Palau', + 'PAN' => 'Panama', + 'PNG' => 'Papua New Guinea', + 'PRY' => 'Paraguay', + 'PER' => 'Peru', + 'PHL' => 'Philippines', + 'POL' => 'Poland', + 'PRT' => 'Portugal', + 'QAT' => 'Qatar', + 'ROU' => 'Romania', + 'RUS' => 'Russia', + 'RWA' => 'Rwanda', + 'KNA' => 'Saint Kitts and Nevis', + 'LCA' => 'Saint Lucia', + 'VCT' => 'Saint Vincent and the Grenadines', + 'WSM' => 'Samoa', + 'SMR' => 'San Marino', + 'STP' => 'Sao Tome and Principe', + 'SAU' => 'Saudi Arabia', + 'SEN' => 'Senegal', + 'SRB' => 'Serbia', + 'SYC' => 'Seychelles', + 'SLE' => 'Sierra Leone', + 'SGP' => 'Singapore', + 'SVK' => 'Slovakia', + 'SVN' => 'Slovenia', + 'SLB' => 'Solomon Islands', + 'SOM' => 'Somalia', + 'ZAF' => 'South Africa', + 'SSD' => 'South Sudan', + 'ESP' => 'Spain', + 'LKA' => 'Sri Lanka', + 'SDN' => 'Sudan', + 'SUR' => 'Suriname', + 'SWE' => 'Sweden', + 'CHE' => 'Switzerland', + 'SYR' => 'Syria', + 'TAI' => 'Taiwan', + 'TJK' => 'Tajikistan', + 'TZA' => 'Tanzania', + 'THA' => 'Thailand', + 'TGO' => 'Togo', + 'TON' => 'Tonga', + 'TTO' => 'Trinidad and Tobago', + 'TUN' => 'Tunisia', + 'TUR' => 'Turkey', + 'TKM' => 'Turkmenistan', + 'TUV' => 'Tuvalu', + 'UGA' => 'Uganda', + 'UKR' => 'Ukraine', + 'ARE' => 'United Arab Emirates', + 'GBR' => 'United Kingdom', + 'USA' => 'United States', + 'URY' => 'Uruguay', + 'UZB' => 'Uzbekistan', + 'VAN' => 'Vanuatu', + 'VAT' => 'Vatican City', + 'VEN' => 'Venezuela', + 'VNM' => 'Vietnam', + 'YEM' => 'Yemen', + 'ZMB' => 'Zambia', + 'ZWE' => 'Zimbabwe', +]; + } + +} \ No newline at end of file diff --git a/src/Framework/Assets.php b/src/Framework/Assets.php index dcdb71a..5cb9df1 100644 --- a/src/Framework/Assets.php +++ b/src/Framework/Assets.php @@ -201,11 +201,12 @@ class Assets } else { $more .= " media=\"all\""; } - - $ver = self::projectPaths($file, $scope); - $wrap_file = self::wrapAsset($file, $scope); - if ($ver === false || $wrap_file === false) { - return ""; + if (strtolower($scope) !== "cdn") { + $ver = self::projectPaths($file, $scope); + $wrap_file = self::wrapAsset($file, $scope); + if ($ver === false || $wrap_file === false) { + return ""; + } } return "\r\n"; } @@ -224,10 +225,12 @@ class Assets } } - $ver = self::projectPaths($file, $scope); - $wrap_file = self::wrapAsset($file, $scope); - if ($ver === false || $wrap_file === false) { - return ""; + if (strtolower($scope) !== "cdn") { + $ver = self::projectPaths($file, $scope); + $wrap_file = self::wrapAsset($file, $scope); + if ($ver === false || $wrap_file === false) { + return ""; + } } return "\r\n"; //return ""; diff --git a/src/Framework/Common.php b/src/Framework/Common.php index 4474b35..20e539e 100644 --- a/src/Framework/Common.php +++ b/src/Framework/Common.php @@ -9,11 +9,52 @@ use IOcornerstone\Framework\{ Logger, String\StringFacade as F, Enum\ExitOnDump as endDump, + TimeZones, }; final class Common { + /** + * Walks through all variables given to it, checking for false, + * if false found it Failed, so save Failed.... + * @param type $args + * @return array + */ + public static function hasSaved(...$args): array + { + if (self::isArrayWithData($args)) { + if (self::hasKeys($args)) { + foreach($args as $key => $value){ + if ($value === false) { + return self::saveFailed(); + } + } + } else { + foreach($args as $item){ + if ($item === false) { + return self::saveFailed(); + } + } + } + } + if (is_bool($args) && $args === false) { + return self::saveFailed(); + } + return self::saved(); + } + + public static function saved(bool $worked = true): array { + $time = TimeZones::convertTimeZone(array('format' => 'g:i a')); + return ['time' => $time, 'saved' => $worked]; + } + /** + * JSON API inform AJAX that save failed + */ + public static function saveFailed($msg = 'Save Failed!'): array { + return ['saved' => false, 'msg' => $msg]; + } + /** * Clear out from memory given variable by Reference! * @param type $sensitive_data @@ -103,11 +144,12 @@ final class Common return false; // Empty Data..., return false } - public static function getDigitsAndLettersOnly(false|null|string $input): ?string { + public static function getAlphaNumbric(false|null|string $input, $allow_dashes = false): ?string { if ($input === false || $input === null) { return null; } - return preg_replace('/[^a-zA-Z0-9]/', '', $input); + $allowed = ($allow_dashes) ? "_-" : ""; + return preg_replace('/[^a-zA-Z0-9'.$allowed.']/', '', $input); } public static function safeFlipArray(?array $data): array diff --git a/src/Framework/Database/Model.php b/src/Framework/Database/Model.php index 727bc5b..36dcab9 100644 --- a/src/Framework/Database/Model.php +++ b/src/Framework/Database/Model.php @@ -11,14 +11,16 @@ declare(strict_types=1); namespace IOcornerstone\Framework\Database; use IOcornerstone\Framework\{ + Trait\Database\RunSql, + Trait\Database\Validation, TimeZones, Common, }; class Model { - use \IOCornerstone\Framework\Trait\Database\RunSql; - use \IOCornerstone\Framework\Trait\Database\Validation; + use RunSql; + use Validation; const SUCCESSFUL_SAVE = 1; const DUPLICATE_FOUND = 2; @@ -29,20 +31,19 @@ class Model { private $members = []; private $validationMembers = []; private $missing = []; - private $errorMessage; + protected $errorMessage; private $primaryKey = 'id'; private $dbSkiped = []; public function saved(bool $worked = true): array { - $time = TimeZones::convertTimeZone(array('format' => 'g:i a')); - return ['time' => $time, 'saved' => $worked]; + return Common::saved($worked); } /** * JSON API inform AJAX that save failed */ public function saveFailed($msg = 'Save Failed!'): array { - return ['saved' => false, 'msg' => $msg]; + return Common::saveFailed($msg); } public function dumpTableFields(string $table) { @@ -262,7 +263,6 @@ class Model { /** * Save * - * @param object $lcoal_model Should be $this from the Model * * Insert if primary key not set * Update if primary key set diff --git a/src/Framework/Trait/Database/RunSql.php b/src/Framework/Trait/Database/RunSql.php index b92c555..6cc386f 100644 --- a/src/Framework/Trait/Database/RunSql.php +++ b/src/Framework/Trait/Database/RunSql.php @@ -1,37 +1,49 @@ pdo->prepare(trim($sql)); if (Common::getCount($bind) > 0) { $exec = $pdostmt->execute($bind); } else { $exec = $pdostmt->execute(); } + if ($exec === false) { + return 0; + } return $pdostmt->rowCount(); } - - public function runSelect($sql) { + + public function runSelect($sql): array + { $pdostmt = $this->pdo->prepare(trim($sql)); $exec = $pdostmt->execute(); + if ($exec === false) { + return []; + } return $pdostmt->fetchAll(\PDO::FETCH_ASSOC); - } + } - private function describeTable(string $table) { - $driver = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); - $tickTable = (strrpos($table, "`") === false) ? "`{$table}`" : $table; + private function describeTable(string $table) + { + $safer_table = Common::getAlphaNumbric($table, allow_dashes: true); + $driver = $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); + $tickTable = (strrpos($safer_table, "`") === false) ? "`{$safer_table}`" : $safer_table; if ($driver == 'sqlite') { $sql = "PRAGMA table_info({$tickTable});"; $key = "name"; @@ -39,41 +51,54 @@ class RunSql $sql = "DESCRIBE {$tickTable};"; $key = "Field"; } elseif ($driver == 'pgsql') { - $sql = "SELECT column_name FROM information_schema.columns WHERE table_name = '{$table}';"; + $sql = "SELECT column_name FROM information_schema.columns WHERE table_name = '{$safer_table}';"; $key = "column_name"; } else { return false; } - return ['sql'=>$sql, 'key'=>$key]; + return ['sql' => $sql, 'key' => $key]; } - public function getFields(string $table) { + /** + * Uses the private helper method describeTable to get all fields in a Table + * @param string $table + * @return array + */ + public function getFields(string $table = ""): array + { + if (empty($table)) { + $table = $this->table ?? false; + } + if ($table === false) { + return []; + } $describe = $this->describeTable($table); if (false !== ($list = $this->runSelect($describe['sql']))) { - $fields = array(); + $fields = []; if (count($list) == 0) { - return array(); + return []; } foreach ($list as $record) { $fields[] = $record[$describe['key']]; } return $fields; } - return array(); + return []; } - - private function filter(string $table, array $info): array { + + protected function filter(string $table, array $info): array + { $describe = $this->describeTable($table); if ($describe === false) { return array_keys($info); } if (false !== ($list = $this->runSelect($describe['sql']))) { - $fields = array(); + $fields = []; foreach ($list as $record) { $fields[] = $record[$describe['key']]; } return array_values(array_intersect($fields, array_keys($info))); } - return array(); + return []; } } diff --git a/src/Framework/Trait/Database/Validation.php b/src/Framework/Trait/Database/Validation.php index 0a0465e..b1986f0 100644 --- a/src/Framework/Trait/Database/Validation.php +++ b/src/Framework/Trait/Database/Validation.php @@ -12,7 +12,8 @@ namespace IOCornerstone\Framework\Trait\Database; use IOcornerstone\Framework\{ Configure, Common, - Registry + Registry, + Logger, }; trait Validation @@ -198,11 +199,10 @@ trait Validation * @param string $msg error message */ private function do_verr(string $msg): void { - $this->error_message = $msg; - $exists = Registry::get('di')->exists('log'); - if ($exists && Configure::get('database', 'log_validation_errors') === true) { - $log = Registry::get('di')->getService('log', ['validation_errors']); - $log->write($msg); + if (Configure::get('database', 'log_validation_errors') === true) { + $log = new Logger(); + $log->alert($msg); } + $this->errorMessage = $msg; } }