summaryrefslogtreecommitdiffstats
path: root/app/class/obj.class.php
diff options
context:
space:
mode:
authorMalf Furious <m@lfurio.us>2018-10-27 18:28:22 -0400
committerMalf Furious <m@lfurio.us>2018-10-27 18:28:22 -0400
commit0421aa1b60f4fe6bf140888159c58059c1013588 (patch)
treec3285276f6c53b6789e2f6dc82cb3b0fd17b38a4 /app/class/obj.class.php
parent495157341d60522084dcc9f6219877b6ba497312 (diff)
parent6512655aee73d3d295daa4de0e4ef25c08cfec9e (diff)
downloadscrott-05af670f00a270ee69f487891741116ef5b82de8.tar.gz
scrott-05af670f00a270ee69f487891741116ef5b82de8.zip
Merge branch 'rel/v0.1'v0.1
Diffstat (limited to 'app/class/obj.class.php')
-rw-r--r--app/class/obj.class.php300
1 files changed, 300 insertions, 0 deletions
diff --git a/app/class/obj.class.php b/app/class/obj.class.php
new file mode 100644
index 0000000..850184c
--- /dev/null
+++ b/app/class/obj.class.php
@@ -0,0 +1,300 @@
+<?php
+
+/*
+ * SCROTT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
+ *
+ * For more information, please refer to UNLICENSE
+ */
+
+require_once "class/table.class.php";
+require_once "class/image.php";
+
+/*
+ * This is a generic database object. This is a supertype of all Scrott
+ * datatypes and defines fields common to all of them.
+ */
+class obj extends table
+{
+ /*
+ * Constants used for uploading images
+ */
+ public const HEAD_MAXSIZE = 1048576; // 1Mb
+ public const BG_MAXSIZE = 1048576; // 1Mb
+ public const IMAGE_MIME = array(
+ "image/jpeg",
+ "image/jpg",
+ "image/png",
+ );
+
+ /*
+ * Constructor
+ */
+ public function __construct(?string $guid = NULL)
+ {
+ $this->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. Limit of zero
+ * returns all members.
+ */
+ 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)
+ $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' OR objtype = 'log') " .
+ "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;
+ }
+
+ /*
+ * 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
+ */
+ 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 the name of the file formctrl field.
+ */
+ public function setHeadImg(string $image) : bool
+ {
+ $path = "dynmic/heads/" . $this->guid;
+
+ if (!saveIfFile($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 the name of the file
+ * formctrl field.
+ */
+ public function setBgImg(string $image) : bool
+ {
+ $path = "dynmic/bgs/" . $this->guid;
+ return saveIfFile($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);
+ }
+}
+
+?>