From 3355affd6c11b6ab32015bd1eed4306bd020b56b Mon Sep 17 00:00:00 2001 From: M Date: Sat, 21 Nov 2015 17:37:18 -0500 Subject: + Committing initial framework class definition --- app/class/framework.class.php | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 app/class/framework.class.php (limited to 'app/class') diff --git a/app/class/framework.class.php b/app/class/framework.class.php new file mode 100644 index 0000000..452f0c3 --- /dev/null +++ b/app/class/framework.class.php @@ -0,0 +1,26 @@ + -- cgit v1.2.3 From adade14d9e386797a65f1beb405c21ebbff1ca37 Mon Sep 17 00:00:00 2001 From: M Date: Sat, 21 Nov 2015 18:25:28 -0500 Subject: + Adding abstract controller class --- app/class/controller.class.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 app/class/controller.class.php (limited to 'app/class') diff --git a/app/class/controller.class.php b/app/class/controller.class.php new file mode 100644 index 0000000..4ea40d1 --- /dev/null +++ b/app/class/controller.class.php @@ -0,0 +1,17 @@ + -- cgit v1.2.3 From c50a6be054db3ddb260585865df8341e1347ad73 Mon Sep 17 00:00:00 2001 From: M Date: Sat, 21 Nov 2015 20:53:44 -0500 Subject: * Framework def file is now condifionally including system-level app configuration --- app/class/framework.class.php | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'app/class') diff --git a/app/class/framework.class.php b/app/class/framework.class.php index 452f0c3..e20be7f 100644 --- a/app/class/framework.class.php +++ b/app/class/framework.class.php @@ -1,5 +1,9 @@ Date: Sat, 21 Nov 2015 21:47:03 -0500 Subject: + Defined function to check if scrott.conf.php file exists --- app/class/framework.class.php | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'app/class') diff --git a/app/class/framework.class.php b/app/class/framework.class.php index e20be7f..5232135 100644 --- a/app/class/framework.class.php +++ b/app/class/framework.class.php @@ -9,6 +9,15 @@ is_file("scrott.conf.php") && */ abstract class Framework { + /* + * Check for the existence of Scrott's system-level config + */ + function scrottConfExists() + { + global $_SCROTT; + return isset($_SCROTT['conf']); + } + /* * Get the absolute path on this server for the root of this app */ -- cgit v1.2.3 From dfa67b6059c9657454d3abed2e66ce30ce168960 Mon Sep 17 00:00:00 2001 From: M Date: Sat, 21 Nov 2015 23:17:11 -0500 Subject: + Added abstract model definition --- app/class/model.class.php | 71 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 app/class/model.class.php (limited to 'app/class') diff --git a/app/class/model.class.php b/app/class/model.class.php new file mode 100644 index 0000000..25e34ab --- /dev/null +++ b/app/class/model.class.php @@ -0,0 +1,71 @@ +errorlist = array(); + $this->warninglist = array(); + $this->noticelist = array(); + } + + /* + * Check for error + */ + function isError() + { + return count($this->errorlist) > 0; + } + + /* + * Check for warning + */ + function isWarning() + { + return count($this->warninglist) > 0; + } + + /* + * Check for notice + */ + function isNotice() + { + return count($this->noticelist) > 0; + } + + /* + * Log an error + */ + function logError($str) + { + $this->errorlist[] = $str; + } + + /* + * Log a warning + */ + function logWarning($str) + { + $this->warninglist[] = $str; + } + + /* + * Log a notice + */ + function logNotice($str) + { + $this->noticelist[] = $str; + } +} + +?> -- cgit v1.2.3 From debd679aa4e3d7eb2d216e57df859dd9e6427f5f Mon Sep 17 00:00:00 2001 From: M Date: Sun, 22 Nov 2015 00:24:37 -0500 Subject: * Implemented framework ar (app root) function --- app/class/framework.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/class') diff --git a/app/class/framework.class.php b/app/class/framework.class.php index 5232135..11902d0 100644 --- a/app/class/framework.class.php +++ b/app/class/framework.class.php @@ -23,7 +23,7 @@ abstract class Framework */ function ar() { - /* TODO */ + return substr($_SERVER['PHP_SELF'], 0, -10); // 10 = length of "/index.php" } /* -- cgit v1.2.3 From 2710f0de8d8d900a0997fd72f315c8a6f07329cf Mon Sep 17 00:00:00 2001 From: M Date: Sun, 22 Nov 2015 03:04:06 -0500 Subject: * Derp, default is a reserved word, calling the function 'deflt' instead * Removed explicit call to parent constructor in model class, since that function is not explicitly defined --- app/class/model.class.php | 2 -- 1 file changed, 2 deletions(-) (limited to 'app/class') diff --git a/app/class/model.class.php b/app/class/model.class.php index 25e34ab..4f597f7 100644 --- a/app/class/model.class.php +++ b/app/class/model.class.php @@ -12,8 +12,6 @@ abstract class Model extends Framework */ function __construct() { - parent::__construct(); - $this->errorlist = array(); $this->warninglist = array(); $this->noticelist = array(); -- cgit v1.2.3 From 49e6128951e8d8b340ea6027735c8b3566c44b6b Mon Sep 17 00:00:00 2001 From: M Date: Thu, 3 Dec 2015 22:11:13 -0500 Subject: + Started Form class definition --- app/class/form.class.php | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 app/class/form.class.php (limited to 'app/class') diff --git a/app/class/form.class.php b/app/class/form.class.php new file mode 100644 index 0000000..e398690 --- /dev/null +++ b/app/class/form.class.php @@ -0,0 +1,35 @@ +textFields = array(); + + $this->errorlist = array(); + $this->warninglist = array(); + $this->noticelist = array(); + } + + /* + * Add new text field to the form + */ + function field_text($name, $req = true) + { + if ($req !== true) + $req = false; + + $this->textFields[] = array( + 'name' => $name, + 'req' => $req + ); + } +} + +?> -- cgit v1.2.3 From 59962f7c260aaa0661b0c811e6b553d1a850032b Mon Sep 17 00:00:00 2001 From: M Date: Sat, 5 Dec 2015 15:03:48 -0500 Subject: + Added numeric and enum types to Form class --- app/class/form.class.php | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'app/class') diff --git a/app/class/form.class.php b/app/class/form.class.php index e398690..ffee3d7 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -11,6 +11,8 @@ class Form function __construct() { $this->textFields = array(); + $this->numbFields = array(); + $this->enumFields = array(); $this->errorlist = array(); $this->warninglist = array(); @@ -30,6 +32,41 @@ class Form 'req' => $req ); } + + /* + * Add new numeric field to the form + */ + function field_numeric($name, $req = true, $integer = true, $min = null, $max = null) + { + if ($req !== true) + $req = false; + + if ($integer !== true) + $integer = false; + + $this->numbFields[] = array( + 'name' => $name, + 'req' => $req, + 'int' => $integer, + 'min' => $min, + 'max' => $max + ); + } + + /* + * Add new enumeration field to the form + */ + function field_enum($name, $req = true, $values) + { + if ($req !== true) + $req = false; + + $this->enumFields[] = array( + 'name' => $name, + 'req' => $req, + 'vals' => $values + ); + } } ?> -- cgit v1.2.3 From 91659b121e63735a7620663c0f43f5c5adef77d4 Mon Sep 17 00:00:00 2001 From: M Date: Sat, 5 Dec 2015 18:54:01 -0500 Subject: + Implemented populate function in Form class + Added helper function in Form class, logError ! Finished Form class for now --- app/class/form.class.php | 100 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 3 deletions(-) (limited to 'app/class') diff --git a/app/class/form.class.php b/app/class/form.class.php index ffee3d7..502e348 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -14,9 +14,15 @@ class Form $this->numbFields = array(); $this->enumFields = array(); - $this->errorlist = array(); - $this->warninglist = array(); - $this->noticelist = array(); + $this->errorlist = array(); + } + + /* + * Log an error + */ + function logError($str) + { + $this->errorlist[] = $str; } /* @@ -67,6 +73,94 @@ class Form 'vals' => $values ); } + + /* + * Populate the form with input data from web page + */ + function populate($input) + { + /* detect duplicate names */ + $names = array(); + foreach ($this->textFields as $fld) + $names[] = $fld['name']; + foreach ($this->numbFields as $fld) + $names[] = $fld['name']; + foreach ($this->enumFields as $fld) + $names[] = $fld['name']; + + if (count(array_unique($names)) != count($names)) + { + $this->logError("Internal error: Duplicate field names defined in form"); + return false; + } + + /* init text fields */ + foreach ($this->textFields as $fld) + { + if (isset($input[$fld['name']])) + $this->$fld['name'] = htmlEntities($input[$fld['name']], ENT_QUOTES); + + else if ($fld['req']) + $this->logError($fld['name'] . " is required"); + } + + /* init numeric fields */ + foreach ($this->numbFields as $fld) + { + if (isset($input[$fld['name']])) + { + if (!is_numeric($input[$fld['name']])) + { + $this->logError($fld['name'] . " must be numeric"); + continue; + } + + if ($fld['int'] && (floor($input[$fld['name']]) != $input[$fld['name']])) + { + $this->logError($fld['name'] . " must be an integer"); + continue; + } + + if (!is_null($fld['min']) && ($input[$fld['name']] < $fld['min'])) + { + $this->logError($fld['name'] . " must be no less than " . $fld['min']); + continue; + } + + if (!is_null($fld['max']) && ($input[$fld['name']] > $fld['max'])) + { + $this->logError($fld['name'] . " must be no more than " . $fld['max']); + continue; + } + + $this->$fld['name'] = $input[$fld['name']]; + } + + else if ($fld['req']) + $this->logError($fld['name'] . " is required"); + } + + /* init enum fields */ + foreach ($this->enumFields as $fld) + { + if (isset($input[$fld['name']])) + { + if (array_search($input[$fld['name']], $fld['vals']) === false) + { + $this->logError($fld['name'] . " is not an appropriate value"); + continue; + } + + $this->$fld['name'] = $input[$fld['name']]; + } + + else if ($fld['req']) + $this->logError($fld['name'] . " is required"); + } + + /* return */ + return count($this->errorlist) == 0; + } } ?> -- cgit v1.2.3 From 9bab1e5c3d7dae9603c5f2172b2a620465caab0e Mon Sep 17 00:00:00 2001 From: M Date: Sat, 5 Dec 2015 21:37:03 -0500 Subject: * Form class fields now have the ability to set a default value. Default value is applied if the supplied $input array has no key matching the field name. --- app/class/form.class.php | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) (limited to 'app/class') diff --git a/app/class/form.class.php b/app/class/form.class.php index 502e348..e50876d 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -28,21 +28,22 @@ class Form /* * Add new text field to the form */ - function field_text($name, $req = true) + function field_text($name, $deflt = null, $req = true) { if ($req !== true) $req = false; $this->textFields[] = array( - 'name' => $name, - 'req' => $req + 'name' => $name, + 'deflt' => $deflt, + 'req' => $req ); } /* * Add new numeric field to the form */ - function field_numeric($name, $req = true, $integer = true, $min = null, $max = null) + function field_numeric($name, $min = null, $max = null, $deflt = null, $integer = true, $req = true) { if ($req !== true) $req = false; @@ -51,26 +52,28 @@ class Form $integer = false; $this->numbFields[] = array( - 'name' => $name, - 'req' => $req, - 'int' => $integer, - 'min' => $min, - 'max' => $max + 'name' => $name, + 'min' => $min, + 'max' => $max, + 'deflt' => $deflt, + 'int' => $integer, + 'req' => $req ); } /* * Add new enumeration field to the form */ - function field_enum($name, $req = true, $values) + function field_enum($name, $values, $deflt = null, $req = true) { if ($req !== true) $req = false; $this->enumFields[] = array( - 'name' => $name, - 'req' => $req, - 'vals' => $values + 'name' => $name, + 'vals' => $values, + 'deflt' => $deflt, + 'req' => $req ); } @@ -100,6 +103,9 @@ class Form if (isset($input[$fld['name']])) $this->$fld['name'] = htmlEntities($input[$fld['name']], ENT_QUOTES); + else if (!is_null($fld['deflt'])) + $this->$fld['name'] = $fld['deflt']; + else if ($fld['req']) $this->logError($fld['name'] . " is required"); } @@ -136,6 +142,9 @@ class Form $this->$fld['name'] = $input[$fld['name']]; } + else if (!is_null($fld['deflt'])) + $this->$fld['name'] = $fld['deflt']; + else if ($fld['req']) $this->logError($fld['name'] . " is required"); } @@ -154,6 +163,9 @@ class Form $this->$fld['name'] = $input[$fld['name']]; } + else if (!is_null($fld['deflt'])) + $this->$fld['name'] = $fld['deflt']; + else if ($fld['req']) $this->logError($fld['name'] . " is required"); } -- cgit v1.2.3 From 5a05468fe2d78641d3adb0ba5b83bf526f4f06de Mon Sep 17 00:00:00 2001 From: M Date: Sat, 5 Dec 2015 22:53:34 -0500 Subject: + Added framework function for getting current app path * Changed sysconf view to use new function ($mod->ar()/sysconf -> $mod->ap) --- app/class/framework.class.php | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'app/class') diff --git a/app/class/framework.class.php b/app/class/framework.class.php index 11902d0..151ca8e 100644 --- a/app/class/framework.class.php +++ b/app/class/framework.class.php @@ -26,6 +26,14 @@ abstract class Framework return substr($_SERVER['PHP_SELF'], 0, -10); // 10 = length of "/index.php" } + /* + * Get the absolute path to the current page + */ + function ap() + { + return $this->ar() . $_REQUEST['path']; + } + /* * Redirect to the given URL and die */ -- cgit v1.2.3 From e6f3bf746fbb1d4c768a1d43e2a0233d0fb25f47 Mon Sep 17 00:00:00 2001 From: M Date: Sun, 6 Dec 2015 00:12:16 -0500 Subject: * Bug fix in Form class - populate function -- If a field was set in $input, but equal to "", the isset check would not behave as expected --- app/class/form.class.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'app/class') diff --git a/app/class/form.class.php b/app/class/form.class.php index e50876d..d3af399 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -100,7 +100,7 @@ class Form /* init text fields */ foreach ($this->textFields as $fld) { - if (isset($input[$fld['name']])) + if (isset($input[$fld['name']]) && $input[$fld['name']] != "") $this->$fld['name'] = htmlEntities($input[$fld['name']], ENT_QUOTES); else if (!is_null($fld['deflt'])) @@ -113,7 +113,7 @@ class Form /* init numeric fields */ foreach ($this->numbFields as $fld) { - if (isset($input[$fld['name']])) + if (isset($input[$fld['name']]) && $input[$fld['name']] != "") { if (!is_numeric($input[$fld['name']])) { @@ -152,7 +152,7 @@ class Form /* init enum fields */ foreach ($this->enumFields as $fld) { - if (isset($input[$fld['name']])) + if (isset($input[$fld['name']]) && $input[$fld['name']] != "") { if (array_search($input[$fld['name']], $fld['vals']) === false) { -- cgit v1.2.3 From 9f9d2a9d313122e9cf365e3baf4a8889b611ae28 Mon Sep 17 00:00:00 2001 From: M Date: Sun, 6 Dec 2015 00:16:14 -0500 Subject: + Added function to model class to log all error messages from a Form objects populate call --- app/class/model.class.php | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'app/class') diff --git a/app/class/model.class.php b/app/class/model.class.php index 4f597f7..85bcf54 100644 --- a/app/class/model.class.php +++ b/app/class/model.class.php @@ -64,6 +64,14 @@ abstract class Model extends Framework { $this->noticelist[] = $str; } + + /* + * Log errors from a Form + */ + function logFormErrors($obj) + { + $this->errorlist = array_merge($this->errorlist, $obj->errorlist); + } } ?> -- cgit v1.2.3 From 366e538edd1a63143ddc229679d3d8be285a9ec3 Mon Sep 17 00:00:00 2001 From: M Date: Sun, 6 Dec 2015 03:10:13 -0500 Subject: * Bug fix in framework class - redirectTo function -- http_redirect function I was using is part of an extension for PHP and therefore, non-standard --- app/class/framework.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/class') diff --git a/app/class/framework.class.php b/app/class/framework.class.php index 151ca8e..7244220 100644 --- a/app/class/framework.class.php +++ b/app/class/framework.class.php @@ -39,7 +39,7 @@ abstract class Framework */ function redirectTo($url) { - http_redirect($url); + header("Location: " . $url); exit; } } -- cgit v1.2.3 From 2896ade5e1257045513f871d59e6e4eaac27e317 Mon Sep 17 00:00:00 2001 From: M Date: Tue, 8 Dec 2015 18:51:20 -0500 Subject: + Added bool field type to Form class --- app/class/form.class.php | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'app/class') diff --git a/app/class/form.class.php b/app/class/form.class.php index d3af399..808de27 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -77,6 +77,14 @@ class Form ); } + /* + * Add new boolean field to the form + */ + function field_bool($name) + { + $this->field_enum($name, array("true", "false"), "false"); + } + /* * Populate the form with input data from web page */ -- cgit v1.2.3 From 2dd0900cd5c2adb610fd35e10133dd9fc10ca0f9 Mon Sep 17 00:00:00 2001 From: M Date: Tue, 8 Dec 2015 19:21:46 -0500 Subject: + Added controller security assertions: require_https and forbid_https --- app/class/controller.class.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'app/class') diff --git a/app/class/controller.class.php b/app/class/controller.class.php index 4ea40d1..fabd7e7 100644 --- a/app/class/controller.class.php +++ b/app/class/controller.class.php @@ -12,6 +12,26 @@ abstract class Controller extends Framework * Abstract function for concrete controller to handle the page request */ abstract function handle($argv); + + /* + * Security check + * Assert that the current connection to this server is secure. Redirects if not. + */ + function sec_require_https() + { + if (!isset($_SERVER['HTTPS'])) + $this->redirectTo("https://" . $_SERVER['SERVER_NAME'] . $this->ap()); + } + + /* + * Security check + * Assert that the current connection to this server is NOT secure. Redirects if not. + */ + function sec_forbid_https() + { + if (isset($_SERVER['HTTPS'])) + $this->redirectTo("http://" . $_SERVER['SERVER_NAME'] . $this->ap()); + } } ?> -- cgit v1.2.3 From bdc8790368e2f8b247c8492507d4083ddfbd61c1 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Thu, 17 Dec 2015 00:39:54 -0500 Subject: + Added generic database interface to use throughout the app since I'm planning on supporting multiple database engines + Defined interface for Mysql DBMS for Scrott --- app/class/database.iface.php | 13 +++++++++ app/class/mysql.class.php | 63 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 app/class/database.iface.php create mode 100644 app/class/mysql.class.php (limited to 'app/class') diff --git a/app/class/database.iface.php b/app/class/database.iface.php new file mode 100644 index 0000000..dcd64ba --- /dev/null +++ b/app/class/database.iface.php @@ -0,0 +1,13 @@ + diff --git a/app/class/mysql.class.php b/app/class/mysql.class.php new file mode 100644 index 0000000..b08257f --- /dev/null +++ b/app/class/mysql.class.php @@ -0,0 +1,63 @@ +db = new mysqli($host, $username, $password, $dbName); + + if ($this->db->connect_error) + throw new Exception("Can not connect to Mysql database. Please check your Scrott configuration."); + } + + /* + * Destructor + */ + function __destruct() + { + $this->close(); + } + + /* + * Close connection to DB + */ + function close() + { + $this->db->close(); + } + + /* + * Make a query of the database. Return data as an array of arrays + */ + function query($query) + { + $arr = array(); + $res = $this->db->query($query); + + if ($res === true || $res === false) + return $arr; + + foreach ($res as $r) + $arr[] = $r->fetch_assoc(); + + return $arr; + } + + /* + * Escape a string for use in a query + */ + function esc($string) + { + return $this->db->real_escape_string($string); + } +} + +?> -- cgit v1.2.3 From 886bc202b8debe29f0c3e70b027ad3202e78c263 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Thu, 17 Dec 2015 01:36:09 -0500 Subject: + Added function to framework class for getting (or creating) the app's singleton db connection object. If no connection is established, logic uses system-level configuration to decide how to connect before returning --- app/class/framework.class.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'app/class') diff --git a/app/class/framework.class.php b/app/class/framework.class.php index 7244220..eea6c25 100644 --- a/app/class/framework.class.php +++ b/app/class/framework.class.php @@ -4,11 +4,15 @@ is_file("scrott.conf.php") && require_once "scrott.conf.php"; +require_once "class/mysql.class.php"; + /* * Global functions / operations and access to contextual or session-based information */ abstract class Framework { + static $dbobj = null; + /* * Check for the existence of Scrott's system-level config */ @@ -42,6 +46,34 @@ abstract class Framework header("Location: " . $url); exit; } + + /* + * Get or create the app's database connection object (this is a singleton object and dependent on system-level config) + */ + function getDbConnection() + { + global $_SCROTT; + + if (self::$dbobj != null) + return self::$dbobj; + + switch ($_SCROTT['dbEngine']) + { + case "mysql": + $host = $_SCROTT['dbAddress']; + $username = $_SCROTT['dbUser']; + $password = $_SCROTT['dbPass']; + $dbName = $_SCROTT['dbName']; + self::$dbobj = new Mysql($host, $username, $password, $dbName); + break; + + default: + throw new Exception("Problem with Scrott Configuration. Invalid database engine specified."); + break; + } + + return self::$dbobj; + } } ?> -- cgit v1.2.3 From 0f9b65d812b601c5e047838b07b96098cbe8ad35 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Thu, 17 Dec 2015 13:21:49 -0500 Subject: * Bug fix in Mysql support class -- misuse of Mysql result object and its member function fetch_assoc --- app/class/mysql.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/class') diff --git a/app/class/mysql.class.php b/app/class/mysql.class.php index b08257f..317468c 100644 --- a/app/class/mysql.class.php +++ b/app/class/mysql.class.php @@ -45,8 +45,8 @@ class Mysql implements Database if ($res === true || $res === false) return $arr; - foreach ($res as $r) - $arr[] = $r->fetch_assoc(); + while ($r = $res->fetch_assoc()) + $arr[] = $r; return $arr; } -- cgit v1.2.3 From c31231740866fc31f9f40f9cf53555efec032291 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Thu, 17 Dec 2015 13:25:08 -0500 Subject: + Added abstract base class for Scrott database objects (implemented constructor and loadObj functions) --- app/class/object.class.php | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 app/class/object.class.php (limited to 'app/class') diff --git a/app/class/object.class.php b/app/class/object.class.php new file mode 100644 index 0000000..4d00009 --- /dev/null +++ b/app/class/object.class.php @@ -0,0 +1,71 @@ +db = $this->getDbConnection(); + + $this->table = "object"; + $this->cols = array( + "guid", + "perms", + "owner", + "parent", + "name", + "timeCreated", + "timeUpdated", + "type" + ); + + $this->childTable = $this->db->esc($childTable); + $this->childCols = array(); + + if (is_array($childCols)) + { + foreach ($childCols as $col) + $this->childCols[] = $this->db->esc($col); + } + } + + /* + * Populate this object with data from the DB with a given GUID + */ + function loadObj($guid) + { + if (is_null($guid)) + return; + + $escdGuid = $this->db->esc($guid); + + /* Common fields */ + $query = "SELECT * FROM `" . $this->table . "` WHERE `guid` = '" . $escdGuid . "'"; + $result = $this->db->query($query)[0]; + + foreach ($this->cols as $col) + { + if (isset($result[$col])) + $this->$col = $result[$col]; + } + + /* Child Table fields */ + $query = "SELECT * FROM `" . $this->childTable . "` WHERE `guid` = '" . $escdGuid . "'"; + $result = $this->db->query($query)[0]; + + foreach ($this->childCols as $col) + { + if (isset($result[$col])) + $this->$col = $result[$col]; + } + } +} + +?> -- cgit v1.2.3 From 2d674ddde9b02a5800e7b7004bc7453305e5862c Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Fri, 18 Dec 2015 00:34:15 -0500 Subject: + Added saveObj function to Object class --- app/class/object.class.php | 94 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) (limited to 'app/class') diff --git a/app/class/object.class.php b/app/class/object.class.php index 4d00009..fb38ef7 100644 --- a/app/class/object.class.php +++ b/app/class/object.class.php @@ -46,7 +46,7 @@ abstract class Object extends Framework $escdGuid = $this->db->esc($guid); - /* Common fields */ + /* Base fields */ $query = "SELECT * FROM `" . $this->table . "` WHERE `guid` = '" . $escdGuid . "'"; $result = $this->db->query($query)[0]; @@ -66,6 +66,98 @@ abstract class Object extends Framework $this->$col = $result[$col]; } } + + /* + * Write this object to the database + */ + function saveObj() + { + if (isset($this->guid)) + { + /* Update Base */ + $updateStr = ""; + + foreach ($this->cols as $col) + { + if (!isset($this->$col)) + continue; + + $updateStr .= "`" . $col . "` = '" . $this->db->esc($this->$col) . "', "; + } + + if (strlen($updateStr) > 0) + { + $updateStr = substr($updateStr, 0, -2); // remove ", " from the end + $query = "UPDATE `" . $this->table . "` SET " . $updateStr . " WHERE `guid` = '" . $this->db->esc($this->guid) . "'"; + $this->db->query($query); + } + + /* Update Child */ + $updateStr = ""; + + foreach ($this->childCols as $col) + { + if (!isset($this->$col)) + continue; + + $updateStr .= "`" . $col . "` = '" . $this->db->esc($this->$col) . "', "; + } + + if (strlen($updateStr) > 0) + { + $updateStr = substr($updateStr, 0, -2); // remove ", " from the end + $query = "UPDATE `" . $this->childTable . "` SET " . $updateStr . " WHERE `guid` = '" . $this->db->esc($this->guid) . "'"; + $this->db->query($query); + } + } + + else + { + $this->guid = $this->getNewGUID(); + + /* Insert Base */ + $colsStr = ""; + $valsStr = ""; + + foreach ($this->cols as $col) + { + if (!isset($this->$col)) + continue; + + $colsStr .= "`" . $col . "`, "; + $valsStr .= "'" . $this->db->esc($this->$col) . "', "; + } + + if (strlen($colsStr) > 0) + { + $colsStr = substr($colsStr, 0, -2); // remove ", " + $valsStr = substr($valsStr, 0, -2); + $query = "INSERT INTO `" . $this->table . "` (" . $colsStr . ") VALUES (" . $valsStr . ")"; + $this->db->query($query); + } + + /* Insert Child */ + $colsStr = ""; + $valsStr = ""; + + foreach ($this->childCols as $col) + { + if (!isset($this->$col)) + continue; + + $colsStr .= "`" . $col . "`, "; + $valsStr .= "'" . $this->db->esc($this->$col) . "', "; + } + + if (strlen($colsStr) > 0) + { + $colsStr = substr($colsStr, 0, -2); // remove ", " + $valsStr = substr($valsStr, 0, -2); + $query = "INSERT INTO `" . $this->childTable . "` (" . $colsStr . ") VALUES (" . $valsStr . ")"; + $this->db->query($query); + } + } + } } ?> -- cgit v1.2.3 From 6bc0491af4349a03a2d9f2040f36901aa5497d0d Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Fri, 18 Dec 2015 01:03:40 -0500 Subject: + Added delObj function to object class --- app/class/object.class.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'app/class') diff --git a/app/class/object.class.php b/app/class/object.class.php index fb38ef7..7f73382 100644 --- a/app/class/object.class.php +++ b/app/class/object.class.php @@ -158,6 +158,23 @@ abstract class Object extends Framework } } } + + /* + * Remove this object from the database + */ + function delObj() + { + if (!isset($this->guid)) + return; + + /* Delete Base */ + $query = "DELETE FROM `" . $this->table . "` WHERE `guid` = '" . $this->db->esc($this->guid) . "'"; + $this->db->query($query); + + /* Delete Child */ + $query = "DELETE FROM `" . $this->childTable . "` WHERE `guid` = '" . $this->db->esc($this->guid) . "'"; + $this->db->query($query); + } } ?> -- cgit v1.2.3 From 30c2345e1567832cbaeefcf4db1e559a8a198046 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Fri, 18 Dec 2015 01:52:39 -0500 Subject: * Defined some default values for function parameters for object class -- planning to make a class "RawObject" so that objects may be created in a polymorphic way --- app/class/object.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/class') diff --git a/app/class/object.class.php b/app/class/object.class.php index 7f73382..3622d6a 100644 --- a/app/class/object.class.php +++ b/app/class/object.class.php @@ -10,7 +10,7 @@ abstract class Object extends Framework /* * Constructor */ - function __construct($childTable, $childCols) + function __construct($childTable = "object", $childCols = null) { $this->db = $this->getDbConnection(); @@ -39,7 +39,7 @@ abstract class Object extends Framework /* * Populate this object with data from the DB with a given GUID */ - function loadObj($guid) + function loadObj($guid = null) { if (is_null($guid)) return; -- cgit v1.2.3 From 25947336340ac5bb7f1f9fc762d6e449320069da Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Fri, 18 Dec 2015 02:26:00 -0500 Subject: + Added function "isGUID" to object class for checking whether GUIDs exist --- app/class/object.class.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'app/class') diff --git a/app/class/object.class.php b/app/class/object.class.php index 3622d6a..fe487bc 100644 --- a/app/class/object.class.php +++ b/app/class/object.class.php @@ -44,6 +44,9 @@ abstract class Object extends Framework if (is_null($guid)) return; + if (!$this->isGUID($guid)) + return; + $escdGuid = $this->db->esc($guid); /* Base fields */ @@ -175,6 +178,20 @@ abstract class Object extends Framework $query = "DELETE FROM `" . $this->childTable . "` WHERE `guid` = '" . $this->db->esc($this->guid) . "'"; $this->db->query($query); } + + /* + * Check whether given GUID exists + */ + function isGUID($guid) + { + $query = "SELECT `guid` FROM `object` WHERE `guid` = '" . $this->db->esc($guid) . "'"; + $result = $this->db->query($query); + + if (count($result) > 0) + return true; + + return false; + } } ?> -- cgit v1.2.3 From 877eccf539bfd3a365d8658ed63d096a13e57b00 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Fri, 18 Dec 2015 13:52:19 -0500 Subject: + Implemented Object::getNewGUID function for Object class --- app/class/object.class.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'app/class') diff --git a/app/class/object.class.php b/app/class/object.class.php index fe487bc..7a46e6e 100644 --- a/app/class/object.class.php +++ b/app/class/object.class.php @@ -192,6 +192,21 @@ abstract class Object extends Framework return false; } + + /* + * Get a new, unique GUID for a new system object + */ + function getNewGUID() + { + do + { + $sha = hash("sha256", random_bytes(64)); + $guid = substr($sha, 0, 8); + } + while ($this->isGUID($guid)); + + return $guid; + } } ?> -- cgit v1.2.3 From 00de072a6a90259d20426969ff4d84b2e26959ee Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Fri, 18 Dec 2015 15:07:41 -0500 Subject: * now using rand() instead of random_bytes for numbers --- app/class/object.class.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app/class') diff --git a/app/class/object.class.php b/app/class/object.class.php index 7a46e6e..bae57ea 100644 --- a/app/class/object.class.php +++ b/app/class/object.class.php @@ -200,7 +200,7 @@ abstract class Object extends Framework { do { - $sha = hash("sha256", random_bytes(64)); + $sha = hash("sha256", rand()); $guid = substr($sha, 0, 8); } while ($this->isGUID($guid)); -- cgit v1.2.3 From d508dacd1b5b293df5d0e71cad9cfd87d9f33ff7 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Fri, 18 Dec 2015 16:24:26 -0500 Subject: + Added DBObject class -- A non-abstract version of Object class --- app/class/object.class.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'app/class') diff --git a/app/class/object.class.php b/app/class/object.class.php index bae57ea..bcd8dfa 100644 --- a/app/class/object.class.php +++ b/app/class/object.class.php @@ -209,4 +209,19 @@ abstract class Object extends Framework } } +/* + * Concrete Database Object which can be used in a polymorphic way + */ +class DBObject extends Object +{ + /* + * Constructor + */ + function __construct($guid = null) + { + parent::__construct(); + $this->loadObj($guid); + } +} + ?> -- cgit v1.2.3