summaryrefslogtreecommitdiffstats
path: root/app/class/obj.class.php
blob: 353d6173c96af9f4f43232c3c0ea4dcf7fe7e215 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?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",
        "image/gif",
        "image/bmp",
        "image/x-ms-bmp",
    );

    /*
     * 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);
    }
}

?>