Class namespace change testing

- move auto loader to lib/ folder (so it doesn't exist in document root)
- Fix a lot of old method names in DB\IO and Form\General, etc
- Fix login with non existing DB set (abort -> exit)
- add getDbEncoding call to DB\IO
This commit is contained in:
Clemens Schwaighofer
2018-03-27 18:01:10 +09:00
parent 5c3b45ef08
commit 8151c05d91
9 changed files with 188 additions and 126 deletions

View File

@@ -91,7 +91,11 @@ class Login extends \CoreLibs\DB\IO
$this->log_per_class = 1;
// create db connection and init base class
parent::__construct($db_config, $debug, $db_debug, $echo, $print);
if (!parent::__construct($db_config, $debug, $db_debug, $echo, $print)) {
echo "Could not connect to DB<br>";
// if I can't connect to the DB to auth exit hard. No access allowed
exit;
}
// no session could be found at all
if (!session_id()) {
@@ -104,7 +108,10 @@ class Login extends \CoreLibs\DB\IO
// if we have a search path we need to set it, to use the correct DB to login
// check what schema to use. if there is a login schema use this, else check if there is a schema set in the config, or fall back to DB_SCHEMA if this exists, if this also does not exists use public schema
$SCHEMA = defined('LOGIN_DB_SCHEMA') ? LOGIN_DB_SCHEMA : ($db_config['db_schema'] ? $db_config['db_schema'] : (defined('DB_SCHEMA') ? DB_SCHEMA : 'public'));
$this->dbExec("SET search_path TO ".$SCHEMA);
// set schema if schema differs to schema set in db conneciton
if ($this->dbGetSchema() && $this->dbGetSchema() != $SCHEMA) {
$this->dbExec("SET search_path TO ".$SCHEMA);
}
$this->euid = array_key_exists('EUID', $_SESSION) ? $_SESSION['EUID'] : 0; // if there is none, there is none, saves me POST/GET check
// get login vars, are so, can't be changed
// prepare

View File

@@ -308,15 +308,19 @@ class IO extends \CoreLibs\Basic
{
// start basic class
parent::__construct($debug, $echo, $print);
// dummy init array for db config if not array
if (!is_array($db_config)) {
$db_config = array ();
}
// sets the names (for connect/reconnect)
$this->db_name = $db_config['db_name'];
$this->db_user = $db_config['db_user'];
$this->db_pwd = $db_config['db_pass'];
$this->db_host = $db_config['db_host'];
$this->db_name = $db_config['db_name'] ?? '';
$this->db_user = $db_config['db_user'] ?? '';
$this->db_pwd = $db_config['db_pass'] ?? '';
$this->db_host = $db_config['db_host'] ?? '';
$this->db_port = array_key_exists('db_port', $db_config) ? $db_config['db_port'] : '5432';
$this->db_schema = array_key_exists('db_schema', $db_config) ? $db_config['db_schema'] : ''; // do not set to 'public' if not set, because the default is already public
$this->db_encoding = array_key_exists('db_encoding', $db_config) ? $db_config['db_encoding'] : '';
$this->db_type = 'db_'.$db_config['db_type'];
$this->db_type = $db_config['db_type'] ?? '';
$this->db_ssl = array_key_exists('db_ssl', $db_config) ? $db_config['db_ssl'] : 'allow';
// set the target encoding to the DEFAULT_ENCODING if it is one of them: EUC, Shift_JIS, UTF-8
@@ -326,13 +330,14 @@ class IO extends \CoreLibs\Basic
$this->MAX_QUERY_CALL = 20;
// error & debug stuff, error & warning ids are the same, its just in which var they get written
$this->error_string['10'] = 'Could not load DB interface functions';
$this->error_string['11'] = 'No Querystring given';
$this->error_string['12'] = 'No Cursor given, no correct query perhaps?';
$this->error_string['13'] = 'Query could not be executed without errors';
$this->error_string['14'] = 'Can\'t connect to DB server';
$this->error_string['15'] = 'Can\'t select DB';
$this->error_string['16'] = 'No DB Handler found / connect or reconnect failed';
$this->error_string['17'] = 'All db_return* methods work only with SELECT statements, please use db_exec for everything else';
$this->error_string['17'] = 'All dbReturn* methods work only with SELECT statements, please use dbExec for everything else';
$this->error_string['18'] = 'Query not found in cache. Nothing has been reset';
$this->error_string['19'] = 'Wrong PK name given or no PK name given at all, can\'t get Insert ID';
$this->error_string['20'] = 'Found given Prepare Statement Name in array, Query not prepared, will use existing one';
@@ -361,11 +366,13 @@ class IO extends \CoreLibs\Basic
// How can we do this dynamic? eg for non PgSQL
// OTOH this whole class is so PgSQL specific
// that non PgSQL doesn't make much sense anymore
if ($this->db_type == 'pg_sql') {
if ($this->db_type == 'pgsql') {
$this->db_functions = new \CoreLibs\DB\SQL\PgSQL();
} else {
// abort error
return "Failed to load DB functions for: ".$this->db_type;
$this->error_id = 10;
$this->__dbError();
return false;
}
// connect to DB
@@ -381,6 +388,9 @@ class IO extends \CoreLibs\Basic
'class_created' => '2000-11-23',
'class_author' => 'Clemens Schwaighofer'
);
// all ok return true
return true;
}
// METHOD: __destruct
@@ -451,12 +461,12 @@ class IO extends \CoreLibs\Basic
// WAS : _check_query_for_select
// PARAMS: query
// RETURN: true if matching, false if not
// DESC : checks if query is a SELECT, if not error, 0 return
// NOTE : Query needs to start with SELECT. if starts with "with" it is ignored
// DESC : checks if query is a SELECT or SHOW, if not error, 0 return
// NOTE : Query needs to start with SELECT or SHOW. if starts with "with" it is ignored
private function __checkQueryForSelect($query)
{
// perhaps allow spaces before select ?!?
if (!preg_match("/^select /i", $query)) {
if (!preg_match("/^(select|show) /i", $query)) {
return false;
}
return true;
@@ -538,11 +548,12 @@ class IO extends \CoreLibs\Basic
// NOTE : needed to make public so it can be called from DB.Array.IO too
public function __dbError($cursor = '', $msg = '')
{
$pg_error_string = '';
$where_called = $this->get_caller_method();
if ($cursor) {
$pg_error_string = $this->db_functions->__dbPrintError($cursor);
}
if (!$cursor) {
if (!$cursor && method_exists($this->db_functions, '__dbPrintError')) {
$pg_error_string = $this->db_functions->__dbPrintError();
}
if ($pg_error_string) {
@@ -620,7 +631,7 @@ class IO extends \CoreLibs\Basic
// WAS : _db_prepare_exec
// PARAMS: query, primary key [if set to NULL no returning will be added]
// RETURN: md5 OR boolean false on error
// DESC : sub function for db_exec and db_exec_async
// DESC : sub function for dbExec and dbExecAsync
// * checks query is set
// * checks there is a database handler
// * checks that here is no other query executing
@@ -650,7 +661,7 @@ class IO extends \CoreLibs\Basic
}
}
// check that no other query is running right now
if ($this->db_functions->__dbConnection_busy()) {
if ($this->db_functions->__dbConnectionBusy()) {
$this->error_id = 41;
$this->__dbError();
return false;
@@ -774,7 +785,7 @@ class IO extends \CoreLibs\Basic
// failed to get insert id
$this->insert_id = '';
$this->warning_id = 33;
$this->__dbError($this->cursor, '[db_exec]');
$this->__dbError($this->cursor, '[dbExec]');
}
// if we have multiple, do not set the insert_id different, keep as array
}
@@ -782,7 +793,7 @@ class IO extends \CoreLibs\Basic
// we returned an array of PKs instread of a single one
if (is_array($this->insert_id)) {
$this->warning_id = 32;
$this->__dbError($this->cursor, '[db_exec]');
$this->__dbError($this->cursor, '[dbExec]');
}
}
}
@@ -870,7 +881,7 @@ class IO extends \CoreLibs\Basic
return false;
}
$q = "SET search_path TO '".$this->dbEscapeString($db_schema)."'";
return $this->db_exec($q);
return $this->dbExec($q);
}
// METHOD: dbGetSchema
@@ -897,7 +908,16 @@ class IO extends \CoreLibs\Basic
return false;
}
$q = "SET client_encoding TO '".$this->dbEscapeString($db_encoding)."'";
return $this->db_exec($q);
return $this->dbExec($q);
}
// METHOD: dbGetEncoding
// PARAMS: none
// RETURN: current client encoding
// DESC : returns the current set client encoding from the connected DB
public function dbGetEncoding()
{
return $this->db_return_row('SHOW client_encoding')['client_encoding'];
}
// METHOD: dbInfo
@@ -1009,7 +1029,7 @@ class IO extends \CoreLibs\Basic
}
}
// check that no other query is running right now
if ($this->db_functions->__dbConnection_busy()) {
if ($this->db_functions->__dbConnectionBusy()) {
$this->error_id = 41;
$this->__dbError();
return false;
@@ -1191,7 +1211,7 @@ class IO extends \CoreLibs\Basic
{
// if there is actually a async query there
if ($this->async_running) {
if ($this->db_functions->__dbConnection_busy()) {
if ($this->db_functions->__dbConnectionBusy()) {
return true;
} else {
// get the result/or error
@@ -1250,7 +1270,7 @@ class IO extends \CoreLibs\Basic
$this->__dbError('', $query);
return false;
}
$cursor = $this->db_exec($query);
$cursor = $this->dbExec($query);
$result = $this->dbFetchArray($cursor);
return $result;
}
@@ -1273,7 +1293,7 @@ class IO extends \CoreLibs\Basic
$this->__dbError('', $query);
return false;
}
$cursor = $this->db_exec($query);
$cursor = $this->dbExec($query);
while ($res = $this->dbFetchArray($cursor)) {
for ($i = 0; $i < $this->num_fields; $i ++) {
// cereated mixed, first name
@@ -1361,7 +1381,7 @@ class IO extends \CoreLibs\Basic
}
}
// check that no other query is running right now
if ($this->db_functions->__dbConnection_busy()) {
if ($this->db_functions->__dbConnectionBusy()) {
$this->error_id = 41;
$this->__dbError();
return false;
@@ -1444,7 +1464,7 @@ class IO extends \CoreLibs\Basic
return false;
} else {
if ($this->db_debug) {
$this->__dbDebug('db', $this->__dbDebug_prepare($stm_name, $data), 'dbExecPrep', 'Q');
$this->__dbDebug('db', $this->__dbDebugPrepare($stm_name, $data), 'dbExecPrep', 'Q');
}
$code = $this->db_functions->__dbExecute($stm_name, $data);
if (!$code) {
@@ -1729,7 +1749,7 @@ class IO extends \CoreLibs\Basic
$q .= ')';
$this->temp_sql = $q;
}
if (!$this->db_exec($q)) {
if (!$this->dbExec($q)) {
return false;
}
if (!$primary_key['value']) {

View File

@@ -403,7 +403,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
public function formProcedureLoad($archive_id)
{
if ($this->archive && $archive_id && $this->group_level_user <= $this->security_level["load"]) {
$this->form_load_table_array($archive_id);
$this->formLoadTableArray($archive_id);
$this->yes = 1;
}
}
@@ -417,7 +417,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
{
if ($this->new && $this->group_level_user <= $this->security_level["new"]) {
if ($this->really_new == "yes") {
$this->form_unset_table_array();
$this->formUnsetTablearray();
} else {
$this->msg .= $this->l->__("You have to select the <b>Checkbox for New</b>!<br>");
$this->error = 2;
@@ -434,9 +434,9 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
public function formProcedureSave()
{
if ($this->save && $this->group_level_user <= $this->security_level["save"]) {
$this->form_error_check();
$this->formErrorCheck();
if (!$this->error) {
$this->form_save_table_array();
$this->formSaveTableArray();
}
$this->yes = 1;
}
@@ -456,7 +456,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
$this->error = 2;
}
if ($this->really_delete == "yes") {
$this->form_delete_table_array();
$this->formDeleteTableArray();
} else {
$this->msg .= $this->l->__("You have to select the <b>Checkbox for Delete</b>!<br>");
$this->error = 2;
@@ -492,7 +492,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
if ($_POST[$flag] == "true") {
$q = "DELETE FROM ".$element_list[$i]." WHERE ".$pk_name." = ".$_POST[$id];
$this->db_exec($q);
$this->dbExec($q);
$this->msg .= $this->l->__("Removed entry from list<br>");
$this->warning = 1;
} // post okay true -> delete
@@ -502,7 +502,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
if (!$_POST[$remove_name[$i]][$j]) {
$q = "UPDATE ".$element_list[$i]." WHERE ".$pk_name." = ".$_POST[$prfx.$pk_name][$j];
// $this->debug('edit_db', "UP: $q");
// $this->db_exec($q);
// $this->dbExec($q);
$this->msg .= $this->l->__("Disabled deselected entries from list<br>");
$this->warning = 1;
}
@@ -515,7 +515,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
if (!$_POST[$remove_name[$i]][$j] && $_POST[$prfx.$pk_name][$j]) {
$q = "DELETE FROM ".$element_list[$i]." WHERE ".$pk_name." = ".$_POST[$prfx.$pk_name][$j];
// $this->debug('edit_db', "DEL: $q");
$this->db_exec($q);
$this->dbExec($q);
$this->msg .= $this->l->__("Deleted deselected entries from list<br>");
$this->warning = 1;
}
@@ -539,7 +539,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
$t_pk_name = $this->archive_pk_name;
// lade liste
$this->db_exec($this->load_query);
$this->dbExec($this->load_query);
while ($res = $this->db_fetch_array()) {
$pk_ids[] = $res[$this->int_pk_name];
if ($res[$this->int_pk_name] == $this->table_array[$this->int_pk_name]["value"]) {
@@ -848,7 +848,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
if ($this->table_array[$this->int_pk_name]["value"]) {
$q .= " AND ".$this->int_pk_name." <> ".$this->table_array[$this->int_pk_name]["value"];
}
list($$key) = $this->db_return_row($q);
list($$key) = $this->dbReturnRow($q);
if ($$key) {
$this->msg .= sprintf($this->l->__("The field <b>%s</b> can be used only once!<br>"), $this->table_array[$key]["output_name"]);
}
@@ -1003,7 +1003,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
if ($this->table_array[$this->int_pk_name]["value"]) {
$q .= " AND ".$this->int_pk_name." <> ".$this->table_array[$this->int_pk_name]["value"];
}
list($$key) = $this->db_return_row($q);
list($$key) = $this->dbReturnRow($q);
if ($$key) {
$this->msg .= sprintf($this->l->__("The field <b>%s</b> in row <b>%s</b> can be used only once!<br>"), $reference_array["output_name"], $i);
}
@@ -1046,21 +1046,21 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
public function formSetOrder()
{
// get order name
$order_name = $this->form_get_col_name_from_key("order");
$order_name = $this->formGetColNameFromKey("order");
if ($order_name) {
// first check out of order ...
if (!$this->table_array[$order_name]["value"]) {
// set order (read max)
$q = "SELECT MAX(".$order_name.") + 1 AS max_page_order FROM ".$this->table_name;
list($this->table_array[$order_name]["value"]) = $this->db_return_row($q);
list($this->table_array[$order_name]["value"]) = $this->dbReturnRow($q);
// frist element is 0 because NULL gets returned, set to 1
if (!$this->table_array[$order_name]["value"]) {
$this->table_array[$order_name]["value"] = 1;
}
} elseif ($this->table_array[$this->int_pk_name]["value"]) {
$q = "SELECT $order_name FROM ".$this->table_name." WHERE ".$this->int_pk_name." = ".$this->table_array[$this->int_pk_name]["value"];
list($this->table_array[$order_name]["value"]) = $this->db_return_row($q);
list($this->table_array[$order_name]["value"]) = $this->dbReturnRow($q);
}
}
return $this->table_array;
@@ -1163,20 +1163,20 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
if ($this->table_array[$key]["where"]) {
$q .= " AND ".$this->table_array[$key]["where"];
}
list($pk_name_temp) = $this->db_return_row($q);
list($pk_name_temp) = $this->dbReturnRow($q);
if ($this->num_rows >= 1) {
$this->table_array[$key]["value"] = $pk_name_temp;
} else {
// if a where was given, set this key also [dangerous!]
// postgreSQL compatible insert
$q = "INSERT INTO ".$this->table_array[$key]["table_name"]." (".$this->table_array[$key]["input_name"].") VALUES ('".$this->db_escape_string($this->table_array[$key]["input_value"])."')";
$this->db_exec($q);
$this->dbExec($q);
if ($this->table_array[$key]["where"]) {
// make an update on the just inseted data with the where data als update values
$q = "UPDATE ".$this->table_array[$key]["table_name"]." SET ";
$q .= $this->table_array[$key]["where"]." ";
$q .= "WHERE ".$this->table_array[$key]["pk_name"]." = ".$this->insert_id;
$this->db_exec($q);
$this->dbExec($q);
}
$this->table_array[$key]["value"] = $this->insert_id;
} // set value from DB through select or insert
@@ -1192,7 +1192,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
if ($this->table_array[$key]["where"]) {
$q .= " AND ".$this->table_array[$key]["where"];
}
list($temp) = $this->db_return_row($q);
list($temp) = $this->dbReturnRow($q);
// nothing found in table, use new inserted key
if (!$temp) {
$this->table_array[$key]["value"] = $this->table_array[$key]["input_value"];
@@ -1250,9 +1250,9 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
} // go through each field
// set object order (if necessary)
$this->form_set_order();
$this->formSetOrder();
// write the object
$this->db_write($addslashes);
$this->dbWrite($addslashes);
// write reference array(s) if necessary
if (is_array($this->reference_array)) {
if (!is_array($this->reference_array)) {
@@ -1261,11 +1261,11 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
reset($this->reference_array);
foreach ($this->reference_array as $reference_array) {
$q = "DELETE FROM ".$reference_array["table_name"]." WHERE ".$this->int_pk_name."=".$this->table_array[$this->int_pk_name]["value"];
$this->db_exec($q);
$this->dbExec($q);
$q = "INSERT INTO ".$reference_array["table_name"]." (".$reference_array["other_table_pk"].", ".$this->int_pk_name.") VALUES ";
for ($i = 0; $i < count($reference_array["selected"]); $i ++) {
$t_q = "(".$reference_array["selected"][$i].", ".$this->table_array[$this->int_pk_name]["value"].")";
$this->db_exec($q.$t_q);
$this->dbExec($q.$t_q);
}
} // foreach reference arrays
} // if reference arrays
@@ -1383,7 +1383,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
$this->debug('edit', "Pos[$i] => ".$type[$i]." Q: ".$q."<br>");
// write the dataset
if ($q) {
$this->db_exec($q);
$this->dbExec($q);
}
}
} // for each created query
@@ -1408,7 +1408,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
reset($this->reference_array);
foreach ($this->reference_array as $reference_array) {
$q = "DELETE FROM ".$reference_array["table_name"]." WHERE ".$this->int_pk_name." = ".$this->table_array[$this->int_pk_name]["value"];
$this->db_exec($q);
$this->dbExec($q);
}
}
// remove any element list references
@@ -1419,7 +1419,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
reset($this->element_list);
while (list($table_name, $data_array) = each($this->element_list)) {
$q = "DELETE FROM ".$table_name." WHERE ".$this->int_pk_name." = ".$this->table_array[$this->int_pk_name]["value"];
$this->db_exec($q);
$this->dbExec($q);
}
}
// unlink ALL files
@@ -1486,7 +1486,7 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
}
// METHOD: formCreateElementListTable
// WAS : form_create_element_list
// WAS : form_create_element_list_table
// PARAMS: show which element list
// RETURN: array for output
// DESC : create list of elements next to each other for a group of data in an input field
@@ -1684,4 +1684,4 @@ class Generate extends \CoreLibs\DB\Extended\ArrayIO
}
} // end of class
# __END__
# __END__

57
www/lib/autoloader.php Executable file
View File

@@ -0,0 +1,57 @@
<?php
namespace Autoloader;
// shall implement an auto loader
if (class_exists('Autoload', false) === false) {
// define the auto loader class
class Autoload
{
// we do it simple here
// passes on the class to load and we search here in namespace
// to load that class
public static function load($class)
{
// print "(1) Class: $class / DIR: ".__DIR__."<br>";
// set directory seperator (we need to replace from namespace)
$ds = DS ?? DIRECTORY_SEPARATOR;
// base lib
$LIB = LIB ?? 'lib';
// if lib is in path, do not add lib again
if (strpos(__DIR__, $LIB) !== false) {
$LIB .= DS;
} else {
$LIB = '';
}
// default path is unset
$path = false;
// set path on full dir
// if we have the namespace in the class, strip it out
$len = 0;
if (strpos($class, __NAMESPACE__) !== false) {
$len = strlen(__NAMESPACE__);
}
// set default extension
$extension = '.inc';
// set full include path
$path = __DIR__.$ds.$LIB.substr($class, $len);
// replace namespace \ with dir sepeator
$path = str_replace('\\', $ds, $path).$extension;
// print "(2) Class clean: $path<br>";
// if path is set and a valid file
if ($path !== false && is_file($path)) {
// echo "<b>(3)</b> Load Path: $path<br>";
// we should sub that
// self::loadFile($path);
include $path;
return true;
}
return false;
}
} // end class define
spl_autoload_register('Autoloader\Autoload::load', true, true);
} // end check for already defined
# __END__