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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
<?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/obj.class.php";
require_once "class/user.class.php";
require_once "class/pad.class.php";
require_once "class/stage.class.php";
require_once "class/issue.class.php";
/*
* This class models issue activity, private messaging, pad discussions,
* and system and object log messages.
*/
class mesg extends obj
{
/*
* Constants used for uploading attachments
*/
public const ATTACH_MAXSIZE = 536870912; // 512Mb
/*
* Constructor
*/
public function __construct(?string $guid = NULL)
{
$this->fields['mesgs'] = array(
"guid",
"author",
"mesg",
"attachment",
);
parent::__construct($guid);
try
{
$this->expectType("log");
}
catch (Exception $e)
{
$this->expectType("mesg");
}
}
/*
* Initialize a new regular message.
*/
public static function initNew(string $message, user $author, obj $parent) : mesg
{
$mesg = new mesg();
$mesg->setOwner($author);
$mesg->setParent($parent);
$mesg->name = "message " . $mesg->guid;
$mesg->objtype = "mesg";
$mesg->setAuthor($author);
$mesg->mesg = $message;
$mesg->saveObj();
return $mesg;
}
/*
* Initialize a new pad discussion thread.
*/
public static function initNewDiscussion(string $name, string $message, user $author,
pad $parent) : mesg
{
$mesg = new mesg();
$mesg->setOwner($author);
$mesg->setParent($parent);
$mesg->name = $name;
$mesg->objtype = "mesg";
$mesg->setAuthor($author);
$mesg->mesg = $message;
$mesg->saveObj();
return $mesg;
}
/*
* Initialize a new private message (user-to-user).
*/
public static function initNewPM(string $name, string $message, user $author,
user $rcpt) : mesg
{
$mesg = new mesg();
$mesg->setOwner($rcpt);
$mesg->setParent($rcpt);
$mesg->name = $name;
$mesg->objtype = "mesg";
$mesg->setAuthor($author);
$mesg->mesg = $message;
$mesg->saveObj();
return $mesg;
}
/*
* Initialize a new log message.
*/
public static function initNewLog(string $message, user $author, obj $parent) : mesg
{
$owner = $parent->getOwner();
if (!$owner)
$owner = $parent;
$mesg = new mesg();
$mesg->setOwner($owner);
$mesg->setParent($parent);
$mesg->name = "log " . $mesg->guid;
$mesg->objtype = "log";
$mesg->setAuthor($author);
$mesg->mesg = $message;
$mesg->saveObj();
return $mesg;
}
/*
* Initialize a new admin log message.
*/
public static function initNewAdminLog(string $message, user $author) : mesg
{
$mesg = new mesg();
$mesg->saveObj(); // need to get a guid
$mesg->name = "admin log " . $mesg->guid;
$mesg->objtype = "log";
$mesg->setAuthor($author);
$mesg->mesg = $message;
$mesg->saveObj();
return $mesg;
}
/*
* Get an array of messages in the admin log, between two timestamps.
* With no arguments, the entire log is returned. Results are sorted
* in reverse chronological order.
*/
public static function getAdminLog_ordByDatetime(?string $after = NULL,
?string $before = NULL) : array
{
$query = "SELECT guid FROM objects WHERE objtype = 'log' AND " .
"parent = '' ";
if ($after)
$query += "AND created >= '" . database::esc($after) . "' ";
if ($before)
$query += "AND created < '" . database::esc($before) . "' ";
$query += "ORDER BY created DESC";
$res = database::query($query);
$log = array();
foreach ($res as $l)
$log[] = new mesg($l['guid']);
return $log;
}
/*
* Get the author of this message. This user either actually wrote
* this message, or caused it to be generated.
*/
public function getAuthor() : user
{
if (!isset($this->author) || $this->author == "")
return NULL;
return new user($this->author);
}
/*
* Set the author of this message. This should usually only be done
* while constructing a new message or to clear out references to
* a user that got removed.
*/
public function setAuthor(user $author) : void
{
$this->author = $author->guid;
$this->saveObj();
}
/*
* Check whether this message has been seen by the given user
*/
public function seenBy(user $viewer) : bool
{
$query = "SELECT * FROM views WHERE guid = '" . database::esc($this->guid) . "' " .
"AND viewer = '" . database::esc($viewer->guid) . "'";
$res = database::query($query);
return count($res) > 0;
}
/*
* Mark this message as seen by the given user
*/
public function markSeen(user $viewer) : void
{
if (!$this->seenBy($viewer))
{
$query = "INSERT INTO views (guid, viewer) VALUES ('" .
database::esc($this->guid) . "', '" . database::esc($viewer->guid) . "')";
database::query($query);
}
}
/*
* Format message (if auto generated), and fixup content for display in HTML
*/
public function renderMesg() : string
{
if ($this->objtype == "log")
{
$mesg = sprintf($this->mesg, $this->getAuthor()->getDisplayName());
}
else
{
$mesg = nl2br(str_replace(" ", " ", $this->mesg));
$mesg = preg_replace('/^(\s*>.*)$/m', '<span class="text-success">${1}</span>', $mesg);
}
return $mesg;
}
/*
* Get the URL to this message's attachment. If no file is attached,
* NULL is returned.
*/
public function getAttachment() : ?string
{
if (!is_file("dynmic/attach/" . $this->guid))
return NULL;
return ar() . "/df.php?d=attach&f=" . $this->guid;
}
/*
* Set the attached file for this message. Should typically be done
* during new message creation and not changed afterward. Returns
* false if there is a problem saving the attachment.
*/
public function setAttachment(string $file) : bool
{
$path = "dynmic/attach/" . $this->guid;
$origName = "";
$ret = saveIfFile($file, $path, self::ATTACH_MAXSIZE, NULL, $origName);
$this->attachment = $origName;
$this->saveObj();
return $ret;
}
/*
* Promote a pad discussion thread to an issue. This message object
* must be the top-level message (op) of the discussion thread (ie:
* its parent must be a pad). All reply messages to this one are
* retained and will be messages left on the new issue. A new issue
* object is created and this message object will be destroyed. If
* this is not an eligible message for promotion, NULL is returned.
*/
public function makeIssue(stage $parent) : ?issue
{
if ($this->getParent()->objtype != "pad")
return NULL;
$issue = issue::initNew($this->name, $this->getOwner(), $parent);
$issue->created = $this->created;
$issue->description = $this->mesg;
$issue->saveObj();
foreach ($this->getMesgs_ordByDatetime() as $mesg)
$mesg->setParent($issue);
$this->delObj();
return $issue;
}
/*
* Email this message to parents, owners, members. In the case that
* this is an issue message or a reply message, the assignee or original
* author is also included. Attachments are included in mailing. Any
* duplicates in the rcpt list are removed before sending. Success
* or failure is returned.
*/
public function emailMesg() : bool
{
$parent = $this->getParent();
if (!$parent)
return true;
$rcpt = $parent->getMembers();
$rcpt[] = $parent->getOwner();
switch ($parent->objtype)
{
case "user":
$rcpt[] = $parent;
$subj = $this->author . " " . $this->getAuthor()->getDisplayName() . " // " . $this->name;
break;
case "issue":
$rcpt[] = $parent->getAssignee();
$pad = $parent->getPad();
$subj = $parent->guid . " " . $pad->name . " [#" . $parent->numb . "] " . $parent->name;
break;
case "mesg":
case "log":
$rcpt[] = $parent->getAuthor();
$pad = $parent->getParent();
$subj = $parent->guid . " " . $pad->name . " // " . $parent->name;
break;
}
$rcpt = obj::arrayUnique($rcpt);
$author = $this->author;
$rcpt = array_filter($rcpt, function ($val) use($author) { return $val->guid != $author; });
$attachPath = ($this->getAttachment() ? "dynmic/attach/" . $this->guid : NULL);
$mesg = $this->getAuthor()->getDisplayName() . " wrote on " . $this->created . "\n\n";
$mesg .= $this->renderMesg();
foreach ($rcpt as $r)
{
if (!$r->sendEmail($subj, $mesg, $attachPath, $this->attachment))
return false;
}
return true;
}
}
?>
|