From 977e4271c6f27ed2d42fab6bb2706f4a59bf0237 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Wed, 7 Feb 2018 21:28:08 -0500 Subject: Rename object.class.php to reflect name of its class --- app/class/obj.class.php | 286 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 286 insertions(+) create mode 100644 app/class/obj.class.php (limited to 'app/class/obj.class.php') diff --git a/app/class/obj.class.php b/app/class/obj.class.php new file mode 100644 index 0000000..5be9ac3 --- /dev/null +++ b/app/class/obj.class.php @@ -0,0 +1,286 @@ +fields['objects'] = array( + "guid", + "owner", + "parent", + "name", + "created", + "updated", + + "membModify", + "membMemb", + "membAccs", + "membCres", + "membModifys", + "membMembs", + "pubAcc", + "pubAccs", + "pubCres", + + "objtype", + ); + + parent::__construct($guid); + } + + /* + * Get the object type for the given GUID + */ + public static function typeOf(string $guid) : string + { + $obj = new obj($guid); + return $obj->objtype; + } + + /* + * Remove duplicate elements from an array of Scrott objects. This + * function compares object GUIDs to check for uniqueness. Array + * keys are preserved. NULL elements are removed. Resulting array + * is returned. + */ + public static function arrayUnique(array $arr) : array + { + $guids = array(); + $ret = array(); + + foreach ($arr as $k => $v) + { + if ($v === NULL) + continue; + + if (in_array($v->guid, $guids)) + continue; + + $guids[] = $v->guid; + $ret[$k] = $v; + } + + return $ret; + } + + /* + * Get the owner of this object. Either a user object or a group + * object will be returned. If this object does not have an owner, + * NULL will be returned. + */ + public function getOwner() : ?agent + { + if (!isset($this->owner) || $this->owner == "") + return NULL; + + if (self::typeOf($this->owner) == "group") + return new group($this->owner); + + return new user($this->owner); + } + + /* + * Update the owner of this object + */ + public function setOwner(agent $owner) : void + { + $this->owner = $owner->guid; + $this->saveObj(); + } + + /* + * Get the parent of this object. If this object does not have a + * parent, NULL will be returned. + */ + public function getParent() : ?obj + { + if (!isset($this->parent) || $this->parent == "") + return NULL; + + $parent = new obj($this->parent); + return new $parent->objtype($parent->guid); + } + + /* + * Update the parent of this object + */ + public function setParent(obj $parent) : void + { + $this->parent = $parent->guid; + $this->saveObj(); + } + + /* + * Get an array of all members of this object + */ + public function getMembers() : array + { + $memb = array(); + $query = "SELECT member FROM members WHERE guid = '" . database::esc($this->guid) . "'"; + $res = database::query($query); + + foreach ($res as $m) + $memb[] = new user($m['member']); + + return $memb; + } + + /* + * Add a user as a member of this object. Returns false if user is + * already a member, or if another error occurs; true otherwise. + */ + public function addMember(user $user) : bool + { + if ($user->isMemberOf($this) || !isset($user->guid)) + return false; + + $query = "INSERT INTO members (guid, member) VALUES ('" . database::esc($this->guid) . "', '" . + database::esc($user->guid) . "')"; + database::query($query); + return true; + } + + /* + * Remove a user as a member of this object. Returns false if user + * is not a member, or if another error occurs; true otherwise. + */ + public function remMember(user $user) : bool + { + if (!$user->isMemberOf($this) || !isset($user->guid)) + return false; + + $query = "DELETE FROM members WHERE guid = '" . database::esc($this->guid) . "' AND " . + "member = '" . database::esc($user->guid) . "'"; + database::query($query); + return true; + } + + /* + * Get all messages on this object. Messages are sorted by date + * created. + */ + public function getMesgs_ordByDatetime() : array + { + $query = "SELECT guid FROM objects WHERE objtype = 'mesg' AND " . + "parent = '" . database::esc($this->guid) . "' ORDER BY created"; + $res = database::query($query); + + $mesgs = array(); + + foreach ($res as $m) + $mesgs[] = new mesg($m['guid']); + + return $mesgs; + } + + /* + * Get the URL to the head image resource for this object + */ + public function getHeadImg() : string + { + return ar() . "/df.php?d=heads&f=" . $this->guid; + } + + /* + * Set the head image for this object, overwriting any existing + * image. $image should be an uploaded file to PHP, still + * unhandled. + */ + public function setHeadImg(array $image) : bool + { + $path = "dynmic/heads/" . $this->guid; + + if (!saveFile($image, $path, self::HEAD_MAXSIZE, self::IMAGE_MIME)) + return false; + + if (!imageSquareCrop($path)) + { + $this->rmHeadImg(); + return false; + } + + return true; + } + + /* + * Remove the head image for this object. This deletes the image + * on disk. + */ + public function rmHeadImg() : bool + { + if (!is_file("dynmic/heads/" . $this->guid)) + return true; + + return unlink("dynmic/heads/" . $this->guid); + } + + /* + * Get the URL to the background image resource for this + * object. If no image is set, NULL is returned. + */ + public function getBgImg() : ?string + { + if (!is_file("dynmic/bgs/" . $this->guid)) + return NULL; + + return ar() . "/df.php?d=bgs&f=" . $this->guid; + } + + /* + * Set the background image for this object, overwriting any + * existing image. $image should be an uploaded file to PHP, + * still unhandled. + */ + public function setBgImg(array $image) : bool + { + $path = "dynmic/bgs/" . $this->guid; + return saveFile($image, $path, self::BG_MAXSIZE, self::IMAGE_MIME); + } + + /* + * Remove the background image for this object. This deletes + * the image on disk. + */ + public function rmBgImg() : bool + { + if (!is_file("dynmic/bgs/" . $this->guid)) + return true; + + return unlink("dynmic/bgs/" . $this->guid); + } +} + +?> -- cgit v1.2.3 From 84b7c560ffc09a65df896116ccd63b2f132b92b0 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Thu, 26 Jul 2018 03:52:21 -0400 Subject: Update function obj::getMembers() Added a $limit argument to specify a maximum number of results to return. --- app/class/obj.class.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'app/class/obj.class.php') diff --git a/app/class/obj.class.php b/app/class/obj.class.php index 5be9ac3..4e44649 100644 --- a/app/class/obj.class.php +++ b/app/class/obj.class.php @@ -144,12 +144,17 @@ class obj extends table } /* - * Get an array of all members of this object + * Get an array of all members of this object. Limit of zero + * returns all members. */ - public function getMembers() : array + public function getMembers(int $limit = 0) : array { $memb = array(); $query = "SELECT member FROM members WHERE guid = '" . database::esc($this->guid) . "'"; + + if ($limit != 0) + $query .= " LIMIT " . database::esc($limit); + $res = database::query($query); foreach ($res as $m) -- cgit v1.2.3 From 6b49c22dde73d3770f0f49f075ec86ff28919674 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Sun, 16 Sep 2018 16:00:19 -0400 Subject: Add function obj::hasHeadImg() We can check for the existence of an object's background image by calling getBgImg(), since it returns NULL when there is no such image. But getHeadImg() behaves differently, returning a path to 'static/img/null.jpg' (via df.php) when there is no image, making it more difficult to tell. This function addresses this concern. --- app/class/obj.class.php | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'app/class/obj.class.php') diff --git a/app/class/obj.class.php b/app/class/obj.class.php index 4e44649..003738b 100644 --- a/app/class/obj.class.php +++ b/app/class/obj.class.php @@ -211,6 +211,16 @@ class obj extends table return $mesgs; } + /* + * Check whether object has a custom head set. This is necessary, since + * getHeadImg() will return a path to a default if the object doesn't + * have its own. + */ + public function hasHeadImg() : bool + { + return is_file("dynmic/heads/" . $this->guid); + } + /* * Get the URL to the head image resource for this object */ -- cgit v1.2.3 From 866c16abdce264362edb7f5a3c35e7bab9ddf2a5 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Wed, 19 Sep 2018 15:46:26 -0400 Subject: Update all usage of saveFile() Update all usage of saveFile() to use added saveIfFile() function, forwarding on the convenience to model code. Model code can pass in file field names, rather than $_FILES arrays directly. --- app/class/obj.class.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'app/class/obj.class.php') diff --git a/app/class/obj.class.php b/app/class/obj.class.php index 003738b..199529b 100644 --- a/app/class/obj.class.php +++ b/app/class/obj.class.php @@ -231,14 +231,13 @@ class obj extends table /* * Set the head image for this object, overwriting any existing - * image. $image should be an uploaded file to PHP, still - * unhandled. + * image. $image should be the name of the file formctrl field. */ - public function setHeadImg(array $image) : bool + public function setHeadImg(string $image) : bool { $path = "dynmic/heads/" . $this->guid; - if (!saveFile($image, $path, self::HEAD_MAXSIZE, self::IMAGE_MIME)) + if (!saveIfFile($image, $path, self::HEAD_MAXSIZE, self::IMAGE_MIME)) return false; if (!imageSquareCrop($path)) @@ -276,13 +275,13 @@ class obj extends table /* * Set the background image for this object, overwriting any - * existing image. $image should be an uploaded file to PHP, - * still unhandled. + * existing image. $image should be the name of the file + * formctrl field. */ - public function setBgImg(array $image) : bool + public function setBgImg(string $image) : bool { $path = "dynmic/bgs/" . $this->guid; - return saveFile($image, $path, self::BG_MAXSIZE, self::IMAGE_MIME); + return saveIfFile($image, $path, self::BG_MAXSIZE, self::IMAGE_MIME); } /* -- cgit v1.2.3 From 1bb6077c7c74c0b65be906cbeed417f911498e87 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Wed, 24 Oct 2018 03:26:05 -0400 Subject: obj: Fix bug in function getMesgs_ordByDatetime() The SQL query here, as written, was omitting 'log' type messages from the results. Signed-off-by: Malf Furious --- app/class/obj.class.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'app/class/obj.class.php') diff --git a/app/class/obj.class.php b/app/class/obj.class.php index 199529b..850184c 100644 --- a/app/class/obj.class.php +++ b/app/class/obj.class.php @@ -199,8 +199,8 @@ class obj extends table */ public function getMesgs_ordByDatetime() : array { - $query = "SELECT guid FROM objects WHERE objtype = 'mesg' AND " . - "parent = '" . database::esc($this->guid) . "' ORDER BY created"; + $query = "SELECT guid FROM objects WHERE (objtype = 'mesg' OR objtype = 'log') " . + "AND parent = '" . database::esc($this->guid) . "' ORDER BY created"; $res = database::query($query); $mesgs = array(); -- cgit v1.2.3