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
This commit is contained in:
Clemens Schwaighofer
2020-11-25 20:38:25 +09:00
parent 519de8a23c
commit 85f701ab2a
5 changed files with 160 additions and 31 deletions

View File

@@ -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__

View File

@@ -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__