1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Forum Post Object. Contains a single post by the user. A thread is generated |
5
|
|
|
* with multiple posts. |
6
|
|
|
* |
7
|
|
|
* @package forum |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
class Post extends DataObject |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
private static $db = array( |
|
|
|
|
14
|
|
|
"Content" => "Text", |
15
|
|
|
"Status" => "Enum('Awaiting, Moderated, Rejected, Archived', 'Moderated')", |
16
|
|
|
); |
17
|
|
|
|
18
|
|
|
private static $casting = array( |
|
|
|
|
19
|
|
|
"Updated" => "SS_Datetime", |
20
|
|
|
"RSSContent" => "HTMLText", |
21
|
|
|
"RSSAuthor" => "Varchar", |
22
|
|
|
"Content" => "HTMLText" |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
private static $has_one = array( |
|
|
|
|
26
|
|
|
"Author" => "Member", |
27
|
|
|
"Thread" => "ForumThread", |
28
|
|
|
"Forum" => "Forum" // denormalized data but used for read speed |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
private static $has_many = array( |
|
|
|
|
32
|
|
|
"Attachments" => "Post_Attachment" |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
private static $summary_fields = array( |
|
|
|
|
36
|
|
|
"Content.LimitWordCount" => "Summary", |
37
|
|
|
"Created" => "Created", |
38
|
|
|
"Status" => "Status", |
39
|
|
|
"Thread.Title" => "Thread", |
40
|
|
|
"Forum.Title" => "Forum" |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Update all the posts to have a forum ID of their thread ID. |
45
|
|
|
*/ |
46
|
|
|
public function requireDefaultRecords() |
47
|
|
|
{ |
48
|
|
|
$posts = Post::get()->filter(array('ForumID' => 0, 'ThreadID:GreaterThan' => 0)); |
49
|
|
|
|
50
|
|
|
if ($posts->exists()) { |
51
|
|
|
foreach ($posts as $post) { |
52
|
|
|
if ($post->ThreadID) { |
53
|
|
|
$post->ForumID = $post->Thread()->ForumID; |
54
|
|
|
$post->write(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
DB::alteration_message(_t('Forum.POSTSFORUMIDUPDATED', 'Forum posts forum ID added'), 'created'); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Before deleting a post make sure all attachments are also deleted |
64
|
|
|
*/ |
65
|
|
|
public function onBeforeDelete() |
66
|
|
|
{ |
67
|
|
|
parent::onBeforeDelete(); |
68
|
|
|
|
69
|
|
|
if ($attachments = $this->Attachments()) { |
|
|
|
|
70
|
|
|
foreach ($attachments as $file) { |
71
|
|
|
$file->delete(); |
72
|
|
|
$file->destroy(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Check if user can see the post |
79
|
|
|
*/ |
80
|
|
|
public function canView($member = null) |
81
|
|
|
{ |
82
|
|
|
if (!$member) { |
83
|
|
|
$member = Member::currentUser(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($this->Author()->ForumStatus != 'Normal') { |
|
|
|
|
87
|
|
|
if ($this->AuthorID != $member->ID || $member->ForumStatus != 'Ghost') { |
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->Thread()->canView($member); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Check if user can edit the post (only if it's his own, or he's an admin user) |
97
|
|
|
*/ |
98
|
|
|
public function canEdit($member = null) |
99
|
|
|
{ |
100
|
|
|
if (!$member) { |
101
|
|
|
$member = Member::currentUser(); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if ($member) { |
105
|
|
|
// Admins can always edit, regardless of thread/post ownership |
106
|
|
|
if (Permission::checkMember($member, 'ADMIN')) { |
107
|
|
|
return true; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Otherwise check for thread permissions and ownership |
111
|
|
|
if ($this->Thread()->canPost($member) && $member->ID == $this->AuthorID) { |
|
|
|
|
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Follow edit permissions for this, but additionally allow moderation even |
121
|
|
|
* if the thread is marked as readonly. |
122
|
|
|
*/ |
123
|
|
|
public function canDelete($member = null) |
124
|
|
|
{ |
125
|
|
|
if (!$member) { |
126
|
|
|
$member = Member::currentUser(); |
127
|
|
|
} |
128
|
|
|
if ($this->canEdit($member)) { |
129
|
|
|
return true; |
130
|
|
|
} else { |
131
|
|
|
return $this->Thread()->canModerate($member); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Check if user can add new posts - hook up into canPost. |
137
|
|
|
*/ |
138
|
|
|
public function canCreate($member = null) |
139
|
|
|
{ |
140
|
|
|
if (!$member) { |
141
|
|
|
$member = Member::currentUser(); |
142
|
|
|
} |
143
|
|
|
return $this->Thread()->canPost($member); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Returns the absolute url rather then relative. Used in Post RSS Feed |
148
|
|
|
* |
149
|
|
|
* @return String |
150
|
|
|
*/ |
151
|
|
|
public function AbsoluteLink() |
152
|
|
|
{ |
153
|
|
|
return Director::absoluteURL($this->Link()); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Return the title of the post. Because we don't have to have the title |
158
|
|
|
* on individual posts check with the topic |
159
|
|
|
* |
160
|
|
|
* @return String |
161
|
|
|
*/ |
162
|
|
|
public function getTitle() |
163
|
|
|
{ |
164
|
|
|
return ($this->isFirstPost()) ? $this->Thread()->Title : sprintf(_t('Post.RESPONSE', "Re: %s", 'Post Subject Prefix'), $this->Thread()->Title); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Return the last edited date, if it's different from created |
169
|
|
|
*/ |
170
|
|
|
public function getUpdated() |
171
|
|
|
{ |
172
|
|
|
if ($this->LastEdited != $this->Created) { |
173
|
|
|
return $this->LastEdited; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Is this post the first post in the thread. Check if their is a post with an ID less |
179
|
|
|
* than the one of this post in the same thread |
180
|
|
|
* |
181
|
|
|
* @return bool |
182
|
|
|
*/ |
183
|
|
|
public function isFirstPost() |
184
|
|
|
{ |
185
|
|
|
if (empty($this->ThreadID) || empty($this->ID)) { |
|
|
|
|
186
|
|
|
return false; |
187
|
|
|
} |
188
|
|
|
$earlierPosts = DB::query(sprintf( |
189
|
|
|
'SELECT COUNT("ID") FROM "Post" WHERE "ThreadID" = \'%d\' and "ID" < \'%d\'', |
190
|
|
|
$this->ThreadID, |
|
|
|
|
191
|
|
|
$this->ID |
192
|
|
|
))->value(); |
193
|
|
|
return empty($earlierPosts); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Return a link to edit this post. |
198
|
|
|
* |
199
|
|
|
* @return String |
200
|
|
|
*/ |
201
|
|
|
public function EditLink() |
202
|
|
|
{ |
203
|
|
|
if ($this->canEdit()) { |
204
|
|
|
$url = Controller::join_links($this->Link('editpost'), $this->ID); |
205
|
|
|
return '<a href="' . $url . '" class="editPostLink">' . _t('Post.EDIT', 'Edit') . '</a>'; |
206
|
|
|
} |
207
|
|
|
return false; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Return a link to delete this post. |
212
|
|
|
* |
213
|
|
|
* If the member is an admin of this forum, (ADMIN permissions |
214
|
|
|
* or a moderator) then they can delete the post. |
215
|
|
|
* |
216
|
|
|
* @return String |
217
|
|
|
*/ |
218
|
|
|
public function DeleteLink() |
219
|
|
|
{ |
220
|
|
|
if ($this->canDelete()) { |
221
|
|
|
$url = Controller::join_links($this->Link('deletepost'), $this->ID); |
222
|
|
|
$token = SecurityToken::inst(); |
223
|
|
|
$url = $token->addToUrl($url); |
224
|
|
|
|
225
|
|
|
$firstPost = ($this->isFirstPost()) ? ' firstPost' : ''; |
226
|
|
|
|
227
|
|
|
return '<a class="deleteLink' . $firstPost . '" href="' . $url . '">' . _t('Post.DELETE', 'Delete') . '</a>'; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return false; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Return a link to the reply form. Permission checking is handled on the actual URL |
235
|
|
|
* and not on this function |
236
|
|
|
* |
237
|
|
|
* @return String |
238
|
|
|
*/ |
239
|
|
|
public function ReplyLink() |
240
|
|
|
{ |
241
|
|
|
$url = $this->Link('reply'); |
242
|
|
|
|
243
|
|
|
return '<a href="' . $url . '" class="replyLink">' . _t('Post.REPLYLINK', 'Post Reply') . '</a>'; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Return a link to the post view. |
248
|
|
|
* |
249
|
|
|
* @return String |
250
|
|
|
*/ |
251
|
|
|
public function ShowLink() |
252
|
|
|
{ |
253
|
|
|
$url = $this->Link('show'); |
254
|
|
|
|
255
|
|
|
return '<a href="' . $url . '" class="showLink">' . _t('Post.SHOWLINK', 'Show Thread') . "</a>"; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Return a link to mark this post as spam. |
260
|
|
|
* used for the spamprotection module |
261
|
|
|
* |
262
|
|
|
* @return String |
263
|
|
|
*/ |
264
|
|
|
public function MarkAsSpamLink() |
265
|
|
|
{ |
266
|
|
|
if ($this->Thread()->canModerate()) { |
|
|
|
|
267
|
|
|
$member = Member::currentUser(); |
268
|
|
|
if ($member->ID != $this->AuthorID) { |
|
|
|
|
269
|
|
|
$url = Controller::join_links($this->Forum()->Link('markasspam'), $this->ID); |
|
|
|
|
270
|
|
|
$token = SecurityToken::inst(); |
271
|
|
|
$url = $token->addToUrl($url); |
272
|
|
|
|
273
|
|
|
$firstPost = ($this->isFirstPost()) ? ' firstPost' : ''; |
274
|
|
|
|
275
|
|
|
return '<a href="' . $url .'" class="markAsSpamLink' . $firstPost . '" rel="' . $this->ID . '">'. _t('Post.MARKASSPAM', 'Mark as Spam') . '</a>'; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
return false; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
View Code Duplication |
public function BanLink() |
|
|
|
|
282
|
|
|
{ |
283
|
|
|
$thread = $this->Thread(); |
|
|
|
|
284
|
|
|
if ($thread->canModerate()) { |
285
|
|
|
$link = $thread->Forum()->Link('ban') .'/'. $this->AuthorID; |
|
|
|
|
286
|
|
|
return "<a class='banLink' href=\"$link\" rel=\"$this->AuthorID\">". _t('Post.BANUSER', 'Ban User') ."</a>"; |
|
|
|
|
287
|
|
|
} |
288
|
|
|
return false; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
View Code Duplication |
public function GhostLink() |
|
|
|
|
292
|
|
|
{ |
293
|
|
|
$thread = $this->Thread(); |
|
|
|
|
294
|
|
|
if ($thread->canModerate()) { |
295
|
|
|
$link = $thread->Forum()->Link('ghost') .'/'. $this->AuthorID; |
|
|
|
|
296
|
|
|
return "<a class='ghostLink' href=\"$link\" rel=\"$this->AuthorID\">". _t('Post.GHOSTUSER', 'Ghost User') ."</a>"; |
|
|
|
|
297
|
|
|
} |
298
|
|
|
return false; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* Return the parsed content and the information for the |
303
|
|
|
* RSS feed |
304
|
|
|
*/ |
305
|
|
|
public function getRSSContent() |
306
|
|
|
{ |
307
|
|
|
return $this->renderWith('Includes/Post_rss'); |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
|
311
|
|
|
public function getRSSAuthor() |
312
|
|
|
{ |
313
|
|
|
$author = $this->Author(); |
|
|
|
|
314
|
|
|
|
315
|
|
|
return $author->Nickname; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Return a link to show this post |
320
|
|
|
* |
321
|
|
|
* @return String |
322
|
|
|
*/ |
323
|
|
|
public function Link($action = "show") |
324
|
|
|
{ |
325
|
|
|
// only include the forum thread ID in the URL if we're showing the thread either |
326
|
|
|
// by showing the posts or replying therwise we only need to pass a single ID. |
327
|
|
|
$includeThreadID = ($action == "show" || $action == "reply") ? true : false; |
328
|
|
|
$link = $this->Thread()->Link($action, $includeThreadID); |
|
|
|
|
329
|
|
|
|
330
|
|
|
// calculate what page results the post is on |
331
|
|
|
// the count is the position of the post in the thread |
332
|
|
|
$count = DB::query(" |
333
|
|
|
SELECT COUNT(\"ID\") |
334
|
|
|
FROM \"Post\" |
335
|
|
|
WHERE \"ThreadID\" = '$this->ThreadID' AND \"Status\" = 'Moderated' AND \"ID\" < $this->ID |
|
|
|
|
336
|
|
|
")->value(); |
337
|
|
|
|
338
|
|
|
$start = ($count >= Forum::$posts_per_page) ? floor($count / Forum::$posts_per_page) * Forum::$posts_per_page : 0; |
|
|
|
|
339
|
|
|
$pos = ($start == 0 ? '' : "?start=$start") . ($count == 0 ? '' : "#post{$this->ID}"); |
340
|
|
|
|
341
|
|
|
return ($action == "show") ? $link . $pos : $link; |
342
|
|
|
} |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
/** |
346
|
|
|
* Attachments for posts (one post can have many attachments) |
347
|
|
|
* |
348
|
|
|
* @package forum |
349
|
|
|
*/ |
350
|
|
|
class Post_Attachment extends File |
351
|
|
|
{ |
352
|
|
|
|
353
|
|
|
private static $has_one = array( |
|
|
|
|
354
|
|
|
"Post" => "Post" |
355
|
|
|
); |
356
|
|
|
|
357
|
|
|
private static $defaults = array( |
|
|
|
|
358
|
|
|
'ShowInSearch' => 0 |
359
|
|
|
); |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Can a user delete this attachment |
363
|
|
|
* |
364
|
|
|
* @return bool |
365
|
|
|
*/ |
366
|
|
|
public function canDelete($member = null) |
367
|
|
|
{ |
368
|
|
|
if (!$member) { |
369
|
|
|
$member = Member::currentUser(); |
370
|
|
|
} |
371
|
|
|
return ($this->Post()) ? $this->Post()->canDelete($member) : true; |
|
|
|
|
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* Can a user edit this attachement |
376
|
|
|
* |
377
|
|
|
* @return bool |
378
|
|
|
*/ |
379
|
|
|
public function canEdit($member = null) |
380
|
|
|
{ |
381
|
|
|
if (!$member) { |
382
|
|
|
$member = Member::currentUser(); |
383
|
|
|
} |
384
|
|
|
return ($this->Post()) ? $this->Post()->canEdit($member) : true; |
|
|
|
|
385
|
|
|
} |
386
|
|
|
|
387
|
|
|
/** |
388
|
|
|
* Allows the user to download a file without right-clicking |
389
|
|
|
*/ |
390
|
|
|
public function download() |
391
|
|
|
{ |
392
|
|
|
if (isset($this->urlParams['ID'])) { |
|
|
|
|
393
|
|
|
$SQL_ID = Convert::raw2sql($this->urlParams['ID']); |
|
|
|
|
394
|
|
|
|
395
|
|
|
if (is_numeric($SQL_ID)) { |
396
|
|
|
$file = DataObject::get_by_id("Post_Attachment", $SQL_ID); |
397
|
|
|
$response = SS_HTTPRequest::send_file(file_get_contents($file->getFullPath()), $file->Name); |
398
|
|
|
$response->output(); |
399
|
|
|
} |
400
|
|
|
} |
401
|
|
|
|
402
|
|
|
return $this->redirectBack(); |
|
|
|
|
403
|
|
|
} |
404
|
|
|
} |
405
|
|
|
|