Remove all __set/__get class variable check

It ultimate failed for the following reason.

If base class is passed on to some other class as object parameter
then accessing protected/private variables will be possible because the
__get method will interfer.
Also __set of protected/private variables is possible.

I rather run check for setting variables without defining them than
haveing open protected/private var access
This commit is contained in:
Clemens Schwaighofer
2020-09-08 11:24:15 +09:00
parent 0ec0007569
commit d5fdb22e93
7 changed files with 86 additions and 143 deletions

View File

@@ -114,16 +114,15 @@ class Login extends \CoreLibs\DB\IO
/**
* constructor, does ALL, opens db, works through connection checks, closes itself
* @param array $db_config db config array
* @param int $set_control_flag class variable check flags
* @param array $db_config db config array
*/
public function __construct(array $db_config, int $set_control_flag = 0)
public function __construct(array $db_config)
{
// log login data for this class only
$this->log_per_class = 1;
// create db connection and init base class
parent::__construct($db_config, $set_control_flag);
parent::__construct($db_config);
if ($this->db_init_error === true) {
echo 'Could not connect to DB<br>';
// if I can't connect to the DB to auth exit hard. No access allowed

View File

@@ -68,17 +68,16 @@ class Backend extends \CoreLibs\DB\IO
// CONSTRUCTOR / DECONSTRUCTOR |====================================>
/**
* main class constructor
* @param array $db_config db config array
* @param int|integer $set_control_flag class variable check flag
* @param array $db_config db config array
*/
public function __construct(array $db_config, int $set_control_flag = 0)
public function __construct(array $db_config)
{
$this->setLangEncoding();
// get the language sub class & init it
$this->l = new \CoreLibs\Language\L10n($this->lang);
// init the database class
parent::__construct($db_config, $set_control_flag);
parent::__construct($db_config);
// set the action ids
foreach ($this->action_list as $_action) {

View File

@@ -188,19 +188,11 @@ class Basic
// ajax flag
protected $ajax_page_flag = false;
// METHOD: __construct
// PARAMS: set_control_flag [current sets set/get var errors]
// RETURN: none
// DESC : class constructor
/**
* main Basic constructor to init and check base settings
* @param int $set_control_flag 0/1/2/3 to set internal class parameter check
*/
public function __construct(int $set_control_flag = 0)
public function __construct()
{
// init flags
$this->__setControlFlag($set_control_flag);
// set per run UID for logging
$this->running_uid = hash($this->hash_algo, uniqid((string)rand(), true));
// running time start for script
@@ -425,81 +417,6 @@ class Basic
// $this->fdebugFP('c');
}
// *************************************************************
// INTERAL VARIABLE ERROR HANDLER
// *************************************************************
/**
* sets internal control flags for class variable check
* 0 -> turn of all, works like default php class
* CLASS_STRICT_MODE: 1 -> if set throws error on unset class variable
* CLASS_OFF_COMPATIBLE_MODE: 2 -> if set turns of auto set for unset variables
* 3 -> sets error on unset and does not set variable (strict)
* @param int $set_control_flag control flag as 0/1/2/3
* @return void
*/
private function __setControlFlag(int $set_control_flag): void
{
// is there either a constant or global set to override the control flag
if (defined('CLASS_VARIABLE_ERROR_MODE')) {
$set_control_flag = CLASS_VARIABLE_ERROR_MODE;
}
if (isset($GLOBALS['CLASS_VARIABLE_ERROR_MODE'])) {
$set_control_flag = $GLOBALS['CLASS_VARIABLE_ERROR_MODE'];
}
// bit wise check of int and set
if ($set_control_flag & self::CLASS_OFF_COMPATIBLE_MODE) {
$this->set_compatible = false;
} else {
$this->set_compatible = true;
}
if ($set_control_flag & self::CLASS_STRICT_MODE) {
$this->set_strict_mode = true;
} else {
$this->set_strict_mode = false;
}
}
/**
* if strict mode is set, throws an error if the class variable is not set
* if compatible mode is set, also auto sets variable even if not declared
* default is strict mode false and compatible mode on
* @param mixed $name class variable name
* @return void
*/
public function __set($name, $value): void
{
if ($this->set_strict_mode === true && !property_exists($this, $name)) {
trigger_error('Undefined property via __set(): '.$name, E_USER_NOTICE);
}
// use this for fallback as to work like before to set unset
if ($this->set_compatible === true) {
$this->{$name} = $value;
}
}
/**
* if strict mode is set, throws an error if the class variable is not set
* default is strict mode false
* @param mixed $name class variable name
* @return mixed return set variable content
*/
public function &__get($name)
{
if ($this->set_strict_mode === true && !property_exists($this, $name)) {
trigger_error('Undefined property via __get(): '.$name, E_USER_NOTICE);
}
// on set return
if (property_exists($this, $name)) {
return $this->$name;
} elseif ($this->set_compatible === true && !property_exists($this, $name)) {
// if it is not set, and we are in compatible mode we need to init.
// This is so that $class->array['key'] = 'bar'; works
$this->{$name} = null;
return $this->$name;
}
}
// *************************************************************
// GENERAL METHODS
// *************************************************************

View File

@@ -51,15 +51,14 @@ class ArrayIO extends \CoreLibs\DB\IO
/**
* constructor for the array io class, set the
* primary key name automatically (from array)
* @param array $db_config db connection config
* @param array $table_array table array config
* @param string $table_name table name string
* @param int|integer $set_control_flag set basic class set/get variable error flags
* @param array $db_config db connection config
* @param array $table_array table array config
* @param string $table_name table name string
*/
public function __construct(array $db_config, array $table_array, string $table_name, int $set_control_flag = 0)
public function __construct(array $db_config, array $table_array, string $table_name)
{
// instance db_io class
parent::__construct($db_config, $set_control_flag);
parent::__construct($db_config);
// more error vars for this class
$this->error_string['91'] = 'No Primary Key given';
$this->error_string['92'] = 'Could not run Array Query';

View File

@@ -128,13 +128,13 @@
* - returns an hashed array of table column data
* function db_prepare($stm_name, $query)
* - prepares a query with the given stm name, returns false on error
* function db_execute($stm_name, $data = array())
* function db_execute($stm_name, $data = [])
* - execute a query that was previously prepared
* $string db_escape_string($string)
* - correctly escapes string for db insert
* $string db_boolean(string)
* - if the string value is 't' or 'f' it returns correct TRUE/FALSE for php
* $primary_key db_write_data($write_array, $not_write_array, $primary_key, $table, $data = array())
* $primary_key db_write_data($write_array, $not_write_array, $primary_key, $table, $data = [])
* - writes into one table based on arrays of columns to write and not write, reads data from global vars or optional array
* $boolean db_set_schema(schema)
* - sets search path to a schema
@@ -270,7 +270,7 @@ class IO extends \CoreLibs\Basic
public $cursor; // actual cursor (DBH)
public $num_rows; // how many rows have been found
public $num_fields; // how many fields has the query
public $field_names = array(); // array with the field names of the current query
public $field_names = []; // array with the field names of the current query
public $insert_id; // last inserted ID
public $insert_id_ext; // extended insert ID (for data outside only primary key)
private $temp_sql;
@@ -289,14 +289,14 @@ class IO extends \CoreLibs\Basic
// endless loop protection
private $MAX_QUERY_CALL;
private $DEFAULT_MAX_QUERY_CALL = 20; // default
private $query_called = array();
private $query_called = [];
// error string
protected $error_string = array();
protected $error_string = [];
// prepared list
public $prepare_cursor = array();
public $prepare_cursor = [];
// primary key per table list
// format is 'table' => 'pk_name'
public $pk_name_table = array();
public $pk_name_table = [];
// internal primary key name, for cross calls in async
public $pk_name;
// if we use RETURNING in the INSERT call
@@ -307,15 +307,14 @@ class IO extends \CoreLibs\Basic
/**
* main DB concstructor with auto connection to DB and failure set on failed connection
* @param array $db_config DB configuration array
* @param int $set_control_flag 0/1/2/3 to set internal class parameter check
*/
public function __construct(array $db_config, int $set_control_flag = 0)
public function __construct(array $db_config)
{
// start basic class
parent::__construct($set_control_flag);
parent::__construct();
// dummy init array for db config if not array
if (!is_array($db_config)) {
$db_config = array();
$db_config = [];
}
// sets the names (for connect/reconnect)
$this->db_name = $db_config['db_name'] ?? '';
@@ -511,7 +510,7 @@ class IO extends \CoreLibs\Basic
{
$string = '';
if (!is_array($array)) {
$array = array();
$array = [];
}
foreach ($array as $key => $value) {
$string .= $this->nbsp.'<b>'.$key.'</b> => ';
@@ -617,7 +616,7 @@ class IO extends \CoreLibs\Basic
* @param array $data the data array
* @return string string of query with data inside
*/
private function __dbDebugPrepare(string $stm_name, array $data = array()): string
private function __dbDebugPrepare(string $stm_name, array $data = []): string
{
// get the keys from data array
$keys = array_keys($data);
@@ -773,7 +772,7 @@ class IO extends \CoreLibs\Basic
// count the fields
$this->num_fields = $this->db_functions->__dbNumFields($this->cursor);
// set field names
$this->field_names = array();
$this->field_names = [];
for ($i = 0; $i < $this->num_fields; $i ++) {
$this->field_names[] = $this->db_functions->__dbFieldName($this->cursor, $i);
}
@@ -789,8 +788,8 @@ class IO extends \CoreLibs\Basic
if (!$this->returning_id) {
$this->insert_id = $this->db_functions->__dbInsertId($this->query, $this->pk_name);
} else {
$this->insert_id = array();
$this->insert_id_ext = array();
$this->insert_id = [];
$this->insert_id_ext = [];
// echo "** PREPARE RETURNING FOR CURSOR: ".$this->cursor."<br>";
// we have returning, now we need to check if we get one or many returned
// we'll need to loop this, if we have multiple insert_id returns
@@ -1211,7 +1210,7 @@ class IO extends \CoreLibs\Basic
$return = false;
} else {
// unset return value ...
$return = array();
$return = [];
for ($i = 0; $i < $this->cursor_ext[$md5]['num_fields']; $i ++) {
// create mixed return array
if ($assoc_only === false && isset($this->cursor_ext[$md5]['data'][$this->cursor_ext[$md5]['pos']][$i])) {
@@ -1247,7 +1246,7 @@ class IO extends \CoreLibs\Basic
$this->cursor_ext[$md5]['read_rows'] ++;
// if reset is <3 caching is done, else no
if ($reset < 3) {
$temp = array();
$temp = [];
foreach ($return as $field_name => $data) {
$temp[$field_name] = $data;
}
@@ -1437,9 +1436,9 @@ class IO extends \CoreLibs\Basic
return false;
}
$cursor = $this->dbExec($query);
$rows = array();
$rows = [];
while ($res = $this->dbFetchArray($cursor, $assoc_only)) {
$data = array();
$data = [];
for ($i = 0; $i < $this->num_fields; $i ++) {
$data[$this->field_names[$i]] = $res[$this->field_names[$i]];
}
@@ -1590,7 +1589,7 @@ class IO extends \CoreLibs\Basic
* @param array $data data to run for this query, empty array for none
* @return ?mixed false on error, or result on OK
*/
public function dbExecute(string $stm_name, array $data = array())
public function dbExecute(string $stm_name, array $data = [])
{
// if we do not have no prepare cursor array entry for this statement name, abort
if (!is_array($this->prepare_cursor[$stm_name])) {
@@ -1625,8 +1624,8 @@ class IO extends \CoreLibs\Basic
if (!$this->prepare_cursor[$stm_name]['returning_id']) {
$this->insert_id = $this->db_functions->__dbInsertId($this->prepare_cursor[$stm_name]['query'], $this->prepare_cursor[$stm_name]['pk_name']);
} elseif ($result) {
$this->insert_id = array();
$this->insert_id_ext = array();
$this->insert_id = [];
$this->insert_id_ext = [];
// we have returning, now we need to check if we get one or many returned
// we'll need to loop this, if we have multiple insert_id returns
while ($_insert_id = $this->db_functions->__dbFetchArray(
@@ -1817,18 +1816,18 @@ class IO extends \CoreLibs\Basic
* @param array $data data array to override _POST data
* @return int|bool primary key
*/
public function dbWriteData(array $write_array, array $not_write_array, $primary_key, string $table, $data = array())
public function dbWriteData(array $write_array, array $not_write_array, $primary_key, string $table, $data = [])
{
if (!is_array($write_array)) {
$write_array = array();
$write_array = [];
}
if (!is_array($not_write_array)) {
$not_write_array = array();
$not_write_array = [];
}
if (is_array($table)) {
return false;
}
$not_write_update_array = array();
$not_write_update_array = [];
return $this->dbWriteDataExt($write_array, $primary_key, $table, $not_write_array, $not_write_update_array, $data);
}
@@ -1849,9 +1848,9 @@ class IO extends \CoreLibs\Basic
array $write_array,
$primary_key,
string $table,
array $not_write_array = array(),
array $not_write_update_array = array(),
array $data = array()
array $not_write_array = [],
array $not_write_update_array = [],
array $data = []
) {
if (!is_array($primary_key)) {
$primary_key = array(
@@ -1988,7 +1987,7 @@ class IO extends \CoreLibs\Basic
*/
public function dbArrayParse(string $text): array
{
$output = array();
$output = [];
return $this->db_functions->__dbArrayParse($text, $output);
}
@@ -2034,14 +2033,45 @@ class IO extends \CoreLibs\Basic
/**
* return the extended insert return string set
* Most likely Array
* @return array|string|null RETURNING values as array
* Empty string for unset
* Null for error
* @param string|null $key Optional key for insert_id_ext array
* if found will return only this element,
* else will return null
* @return array|string|null RETURNING values as array
* Empty string for unset
* Null for error
*/
public function getInsertReturn()
public function getInsertReturn($key = null)
{
if ($key !== null) {
if (isset($this->insert_id_ext[$key])) {
return $this->insert_id_ext[$key];
} else {
return null;
}
}
return $this->insert_id_ext;
}
/**
* 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
* Key is md5 string from query run
*/
public function getCursorExt($q = null)
{
if ($q !== null) {
$q_md5 = md5($q);
if (isset($this->cursor_ext[$q_md5])) {
return $this->cursor_ext[$q_md5];
} else {
return null;
}
}
return $this->cursor_ext;
}
} // end if db class
// __END__

View File

@@ -37,13 +37,13 @@ class L10n extends \CoreLibs\Basic
/**
* class constructor call for language getstring
* @param string $lang language name (optional), fallback is en
* @param string $path path, if empty fallback on default internal path
* @param int|integer $set_control_flag control flags for Basic class set/get checks
* @param string $lang language name (optional), fallback is en
* @param string $path path, if empty fallback on default internal path
*/
public function __construct(string $lang = '', string $path = '', int $set_control_flag = 0)
public function __construct(string $lang = '', string $path = ''
)
{
parent::__construct($set_control_flag);
parent::__construct();
if (!$lang) {
$this->lang = 'en';
} else {

View File

@@ -255,11 +255,10 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
/**
* construct form generator
* @param array $db_config db config array
* @param int|integer $table_width table/div width (default 750)
* @param int|integer $set_control_flag basic class set/get variable error flags
* @param array $db_config db config array
* @param int|integer $table_width table/div width (default 750)
*/
public function __construct(array $db_config, int $table_width = 750, int $set_control_flag = 0)
public function __construct(array $db_config, int $table_width = 750)
{
$this->my_page_name = $this->getPageName(1);
$this->setLangEncoding();
@@ -289,7 +288,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
}
// start the array_io class which will start db_io ...
parent::__construct($db_config, $config_array['table_array'], $config_array['table_name'], $set_control_flag);
parent::__construct($db_config, $config_array['table_array'], $config_array['table_name']);
// here should be a check if the config_array is correct ...
if (isset($config_array['show_fields']) && is_array($config_array['show_fields'])) {
$this->field_array = $config_array['show_fields'];