From 85f701ab2acb7519ad8b39ea943eaae8aacc3a61 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Wed, 25 Nov 2020 20:38:25 +0900 Subject: [PATCH] Core Class Updates Basic: remove all error handling override for any class vars to avoid exploiting private/public/protected settings Basic: Add MIME lookup table with array. So you can return a File name description (human understandable) to a mime handler. See mimeInitApps for basic list IO: Bug fix for counting prepared statment place holders. If there are $1, $1, $2 then those are TWO and not THREE IO: various wrappers for returning PK, Extended return set, Number for rows Those will be extended to all variables --- www/admin/class_test.php | 22 +++++++ www/configs/config.master.php | 9 +-- www/layout/admin/javascript/edit.jq.js | 15 +++-- www/lib/CoreLibs/Basic.php | 82 +++++++++++++++++++++++--- www/lib/CoreLibs/DB/IO.php | 63 ++++++++++++++++---- 5 files changed, 160 insertions(+), 31 deletions(-) diff --git a/www/admin/class_test.php b/www/admin/class_test.php index cfef0d85..ee74d303 100644 --- a/www/admin/class_test.php +++ b/www/admin/class_test.php @@ -124,6 +124,22 @@ print "DIRECT INSERT STATUS: $status | PRIMARY KEY: ".$basic->insert_id." | PRIM // UPDATE WITH RETURNING $status = $basic->dbExec("UPDATE foo SET test = 'SOMETHING DIFFERENT' WHERE foo_id = 3688452 RETURNING test"); print "UPDATE STATUS: $status | RETURNING EXT: ".print_r($basic->insert_id_ext, true)."
"; +// REEAD PREPARE +if ($basic->dbPrepare('sel_foo', "SELECT foo_id, test, some_bool, string_a, number_a, number_a_numeric, some_time FROM foo ORDER BY foo_id DESC LIMIT 5") === false) { + print "Error in sel_foo prepare
"; +} else { + $max_rows = 6; + // do not run this in dbFetchArray directly as + // dbFetchArray(dbExecute(...)) + // this will end in an endless loop + $cursor = $basic->dbExecute('sel_foo', []); + $i = 1; + while (($res = $basic->dbFetchArray($cursor, true)) !== false) { + print "DB PREP EXEC FETCH ARR: ".$i.":
".print_r($res, true)."

"; + $i ++; + } +} + # db write class test $table = 'foo'; @@ -347,6 +363,12 @@ foreach ($images as $image) { echo "
"; } +// mime test +$mime = 'application/vnd.ms-excel'; +print "App for mime: ".$basic->mimeGetAppName($mime)."
"; +$basic->mimeSetAppName($mime, 'Microsoft Excel'); +print "App for mime changed: ".$basic->mimeGetAppName($mime)."
"; + // print error messages // print $login->printErrorMsg(); print $basic->printErrorMsg(); diff --git a/www/configs/config.master.php b/www/configs/config.master.php index c8cd89cb..8650bf79 100644 --- a/www/configs/config.master.php +++ b/www/configs/config.master.php @@ -165,13 +165,6 @@ define('DEFAULT_ENCODING', 'UTF-8'); // see Basic class constructor define('LOG_FILE_ID', BASE_NAME); -/************* CLASS ERRORS *******************/ -// 0 = default all OFF -// 1 = throw notice on unset class var -// 2 = no notice on unset class var, but do not set undefined class var -// 3 = throw error and do not set class var -define('CLASS_VARIABLE_ERROR_MODE', 3); - /************* QUEUE TABLE *************/ // if we have a dev/live system // set_live is a per page/per item @@ -199,7 +192,7 @@ if (file_exists(BASE.CONFIGS.'config.db.php')) { require BASE.CONFIGS.'config.db.php'; } if (!isset($DB_CONFIG)) { - $DB_CONFIG = array(); + $DB_CONFIG = []; } /************* OTHER PATHS *****************/ if (file_exists(BASE.CONFIGS.'config.path.php')) { diff --git a/www/layout/admin/javascript/edit.jq.js b/www/layout/admin/javascript/edit.jq.js index a03d6fe2..782dc00c 100644 --- a/www/layout/admin/javascript/edit.jq.js +++ b/www/layout/admin/javascript/edit.jq.js @@ -13,8 +13,8 @@ if (!DEBUG) { }*/ // open overlay boxes counter -var GL_OB_S = 10; -var GL_OB_BASE = 10; +var GL_OB_S = 30; +var GL_OB_BASE = 30; /** * opens a popup window with winName and given features (string) @@ -999,9 +999,10 @@ function html_options(name, data, selected = '', options_only = false, return_st * @param {Boolean} [return_string=false] return as string and not as element * @param {String} [sort=''] if empty as is, else allowed 'keys', * 'values' all others are ignored + * @param {String} [onchange=''] onchange trigger call, default unset * @return {String} html with build options block */ -function html_options_block(name, data, selected = '', multiple = 0, options_only = false, return_string = false, sort = '') +function html_options_block(name, data, selected = '', multiple = 0, options_only = false, return_string = false, sort = '', onchange = '') { var content = []; var element_select; @@ -1009,13 +1010,16 @@ function html_options_block(name, data, selected = '', multiple = 0, options_onl var element_option; var data_list = []; // for sorted output var value; - var option; + // var option; if (multiple > 0) { select_options.multiple = ''; if (multiple > 1) { select_options.size = multiple; } } + if (onchange) { + select_options.OnChange = onchange; + } // set outside select, gets stripped on return if options only is true element_select = cel('select', name, '', [], select_options); // console.log('Call for %s, options: %s', name, options_only); @@ -1135,6 +1139,9 @@ function html_options_refill(name, data, sort = '') element_option.label = value; element_option.value = key; element_option.innerHTML = value; + if (key == option_selected) { + element_option.selected = true; + } document.getElementById(name).appendChild(element_option); } } diff --git a/www/lib/CoreLibs/Basic.php b/www/lib/CoreLibs/Basic.php index 60a88818..60bf6021 100644 --- a/www/lib/CoreLibs/Basic.php +++ b/www/lib/CoreLibs/Basic.php @@ -97,18 +97,10 @@ namespace CoreLibs; /** Basic core class declaration */ class Basic { - // define check vars for the flags we can have - const CLASS_STRICT_MODE = 1; - const CLASS_OFF_COMPATIBLE_MODE = 2; // define byteFormat const BYTE_FORMAT_NOSPACE = 1; const BYTE_FORMAT_ADJUST = 2; const BYTE_FORMAT_SI = 4; - // control vars - /** @var bool compatible mode sets variable even if it is not defined */ - private $set_compatible = true; - /** @var bool strict mode throws an error if the variable is not defined */ - private $set_strict_mode = false; // page and host name public $page_name; public $host_name; @@ -188,6 +180,9 @@ class Basic // ajax flag protected $ajax_page_flag = false; + // mime application list + private $mime_apps = []; + /** * main Basic constructor to init and check base settings */ @@ -402,6 +397,9 @@ class Basic // key generation init $this->initRandomKeyData(); + + // init mime apps + $this->mimeInitApps(); } // METHOD: __destruct @@ -981,7 +979,7 @@ class Basic return join( '', array_map( - function ($value) { + function () { return $this->key_range[rand(0, $this->one_key_length - 1)]; }, range(1, $use_key_length) @@ -3396,6 +3394,72 @@ class Basic return false; } } + + // *** MIME PARTS + // to be moved to some mime class + + /** + * init array for mime type to application name lookup + * @return void + */ + private function mimeInitApps(): void + { + // match mime type to some application description + // this is NOT translated + $this->mime_apps = [ + // zip + 'application/zip' => 'Zip File', + // Powerpoint + 'application/vnd.ms-powerpoint' => 'Microsoft Powerpoint', + 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'Microsoft Powerpoint', + // PDF + 'pplication/pdf' => 'PDF', + // JPEG + 'image/jpeg' => 'JPEG', + // PNG + 'image/png' => 'PNG', + // Indesign + 'application/x-indesign' => 'Adobe InDesign', + // Photoshop + 'application/photoshop' => 'Adobe Photoshop', + // Illustrator + 'application/illustrator' => 'Adobe Illustrator', + // Word + 'application/vnd.ms-word' => 'Microsoft Word', + 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'Microsoft Word', + // Excel + 'application/vnd.ms-excel' => 'Microsoft Excel', + 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'Microsoft Excel', + // plain text + 'text/plain' => 'Text file', + // html + 'text/html' => 'HTML', + // mp4 (max 45MB each) + 'video/mpeg' => 'MPEG Video' + ]; + } + + /** + * Sets or updates a mime type + * @param string $mime MIME Name, no validiation + * @param string $app Applicaiton name + * @return void + */ + public function mimeSetAppName(string $mime, string $app): void + { + $this->mime_apps[$mime] = $app; + } + + /** + * get the application name from mime type + * if not set returns "Other file" + * @param string $mime MIME Name + * @return string Application name matching + */ + public function mimeGetAppName(string $mime): string + { + return $this->mime_apps[$mime] ?? 'Other file'; + } } // __END__ diff --git a/www/lib/CoreLibs/DB/IO.php b/www/lib/CoreLibs/DB/IO.php index 59820ee1..b4a878a0 100644 --- a/www/lib/CoreLibs/DB/IO.php +++ b/www/lib/CoreLibs/DB/IO.php @@ -589,8 +589,8 @@ class IO extends \CoreLibs\Basic /** * if there is the 'to_encoding' var set, and the field is in the wrong encoding converts it to the target - * @param array|bool $row array from fetch_row - * @return array|bool convert fetch_row array, or false + * @param array|bool|null $row array from fetch_row + * @return array|bool|null convert fetch_row array, or false */ private function __dbConvertEncoding($row) { @@ -807,7 +807,9 @@ class IO extends \CoreLibs\Basic // if this has only the pk_name, then only return this, else array of all data (but without the position) // example if insert_id[0]['foo'] && insert_id[0]['bar'] it will become insert_id['foo'] & insert_id['bar'] // if only ['foo_id'] and it is the PK then the PK is directly written to the insert_id - if (count($this->insert_id[0]) > 1 || !array_key_exists($this->pk_name, $this->insert_id[0])) { + if (count($this->insert_id[0]) > 1 || + !array_key_exists($this->pk_name, $this->insert_id[0]) + ) { $this->insert_id_ext = $this->insert_id[0]; if (isset($this->insert_id[0][$this->pk_name])) { $this->insert_id = $this->insert_id[0][$this->pk_name]; @@ -1563,8 +1565,9 @@ class IO extends \CoreLibs\Basic } $match = []; // search for $1, $2, in the query and push it into the control array + // skip counts for same eg $1, $1, $2 = 2 and not 3 preg_match_all('/(\$[0-9]{1,})/', $query, $match); - $this->prepare_cursor[$stm_name]['count'] = count($match[1]); + $this->prepare_cursor[$stm_name]['count'] = count(array_unique($match[1])); $this->prepare_cursor[$stm_name]['query'] = $query; $result = $this->db_functions->__dbPrepare($stm_name, $query); if ($result) { @@ -2019,15 +2022,32 @@ class IO extends \CoreLibs\Basic return $value; } + // *************************** + // INTERNAL VARIABLES READ + // *************************** + /** * return current set insert_id as is - * @return string|int|null Primary key value, most likely int - * Empty string for unset - * Null for error + * @return array|string|int|null Primary key value, most likely int + * Array for multiple return set + * Empty string for unset + * Null for error + */ + public function getReturning() + { + return $this->insert_id; + } + + /** + * alternative name, returns insert_id + * @return array|string|int|null Primary key value, most likely int + * Array for multiple return set + * Empty string for unset + * Null for error */ public function getInsertPK() { - return $this->insert_id; + return $this->getReturning(); } /** @@ -2040,7 +2060,7 @@ class IO extends \CoreLibs\Basic * Empty string for unset * Null for error */ - public function getInsertReturn($key = null) + public function getReturningExt($key = null) { if ($key !== null) { if (isset($this->insert_id_ext[$key])) { @@ -2052,12 +2072,24 @@ class IO extends \CoreLibs\Basic return $this->insert_id_ext; } + /** + * old call for getInserReturnExt + * @param string|null $key See above + * @return array|string|null See above + * @deprecated use getInsertReturnExt($key = null) instead + */ + public function getInsertReturn($key = null) + { + trigger_error('Method '.__METHOD__.' is deprecated, use getInsertReturnExt($key = null)', E_USER_DEPRECATED); + return $this->getReturningExt($key); + } + /** * returns the full array for cursor ext * @param string|null $q Query string, if not null convert to md5 * and return set cursor ext for only this * if not found or null return null - * @return array|nul Cursor Extended array + * @return array|null Cursor Extended array * Key is md5 string from query run */ public function getCursorExt($q = null) @@ -2072,6 +2104,17 @@ class IO extends \CoreLibs\Basic } return $this->cursor_ext; } + + /** + * returns current number of rows that where + * affected by UPDATE/SELECT, etc + * null on empty + * @return int|null Number of rows + */ + public function getNumRows() + { + return $this->num_rows ?? null; + } } // end if db class // __END__