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/object.class.php') 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/object.class.php') 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/object.class.php') 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/object.class.php') 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/object.class.php') 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/object.class.php') 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/object.class.php') 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/object.class.php') 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