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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
|
<?php
/**
* $Header: /cvsroot/bitweaver/_bit_boards/BitBoardPost.php,v 1.33 2008/06/23 21:56:12 squareing Exp $
* $Id: BitBoardPost.php,v 1.33 2008/06/23 21:56:12 squareing Exp $
*
* Messageboards class to illustrate best practices when creating a new bitweaver package that
* builds on core bitweaver functionality, such as the Liberty CMS engine
*
* @author spider <spider@steelsun.com>
* @version $Revision: 1.33 $ $Date: 2008/06/23 21:56:12 $ $Author: squareing $
* @package boards
*/
/**
* required setup
*/
require_once( LIBERTY_PKG_PATH.'LibertyMime.php' );
require_once( BOARDS_PKG_PATH.'BitBoardTopic.php' );
/**
* @package boards
*/
class BitBoardPost extends LibertyComment {
/**
* During initialisation, be sure to call our base constructors
**/
function BitBoardPost($pCommentId = NULL, $pContentId = NULL, $pInfo = NULL) {
LibertyComment::LibertyComment($pCommentId,$pContentId,$pInfo);
// Permission setup
$this->mViewContentPerm = 'p_boards_read';
$this->mEditContentPerm = 'p_boards_post_edit';
$this->mAdminContentPerm = 'p_boards_admin';
}
function verify( &$pParamHash ) {
if( isset( $pParamHash['is_approved'] ) ) {
if( !is_numeric( $pParamHash['is_approved'] ) || $pParamHash['is_approved'] > 1 || $pParamHash['is_approved'] < 0 ) {
$this->mErrors[]=("Invalid post approved state");
} else {
$pParamHash['post_store']['is_approved'] = $pParamHash['is_approved'];
}
}
if( isset( $pParamHash['is_warned'] ) ) {
if( !is_numeric( $pParamHash['warned'] ) || $pParamHash['is_warned'] > 1 || $pParamHash['is_warned'] < 0 ) {
$this->mErrors[]=("Invalid warned state");
} else {
$pParamHash['post_store']['is_warned'] = $pParamHash['is_warned'];
}
}
if( !empty( $pParamHash['warned_message'] ) ) {
$pParamHash['post_store']['warned_message'] = $pParamHash['warned_message'];
}
if( !empty( $pParamHash['warned_message'] ) ) {
$pParamHash['post_store']['warned_message'] = $pParamHash['warned_message'];
}
if( !empty( $pParamHash['migrate_post_id'] ) ) {
$pParamHash['post_store']['migrate_post_id'] = $pParamHash['migrate_post_id'];
}
return( count( $this->mErrors ) == 0 && !empty( $pParamHash['post_store'] ) );
}
/**
* This function stores a post
**/
function store( &$pParamHash ) {
global $gBitSystem;
$ret = FALSE;
if( $this->mCommentId && $this->verify( $pParamHash ) ) {
//$gBitSystem->verifyPermission('p_boards_edit');
//$pParamHash = (($pParamHash + 1)%2);
$query_sel = "SELECT * FROM `".BIT_DB_PREFIX."boards_posts` WHERE `comment_id` = ?";
$isStored = $this->mDb->getOne( $query_sel, array( $this->mCommentId ) );
if( $isStored ) {
$result = $this->mDb->associateUpdate( 'boards_posts', $pParamHash['post_store'], array( 'comment_id' => $this->mCommentId ) );
} else {
$pParamHash['post_store']['comment_id'] = $this->mCommentId;
$result = $this->mDb->associateInsert( 'boards_posts', $pParamHash['post_store'] );
}
$ret = TRUE;
}
return $ret;
}
function loadMetaData() {
if ($this->isValid()) {
if (!isset($this->mInfo['accepted'])) {
$key = array('comment_id' => $this->mCommentId);
$query_sel = "SELECT
post.is_approved,
post.is_warned,
post.warned_message
FROM `".BIT_DB_PREFIX."boards_posts` post WHERE comment_id=?";
$data = $this->mDb->getRow( $query_sel , array_values($key));
if ($data) {
if (!empty($data['warned_message'])) {
$data['warned_message'] = str_replace("\n","<br />\n",$data['warned_message']);
}
$this->mInfo=array_merge($this->mInfo,$data);
}
}
}
}
/**
* This function removes a bitboard entry
**/
function expunge() {
$ret = FALSE;
if( $this->isValid() ) {
$this->mDb->StartTrans();
// parent actually has deletion of rows in boards for constraint reasons
if( parent::expunge() ) {
$this->mDb->CompleteTrans();
$ret = TRUE;
} else {
$this->mDb->RollbackTrans();
}
}
return $ret;
}
function getComments( $pContentId = NULL, $pMaxComments = NULL, $pOffset = NULL, $pSortOrder = NULL, $pDisplayMode = NULL ) {
global $gBitUser, $gBitSystem;
$joinSql = $selectSql = $whereSql = '';
$ret = array();
$contentId = $this->mCommentId;
$mid = 'thread_forward_sequence ASC';
if (!empty($pSortOrder)) {
if ($pSortOrder == 'commentDate_desc') {
$mid = 'last_modified DESC';
} else if ($pSortOrder == 'commentDate_asc') {
$mid = 'last_modified ASC';
} elseif ($pSortOrder == 'thread_asc') {
$mid = 'thread_forward_sequence ASC';
// thread newest first is harder...
} elseif ($pSortOrder == 'thread_desc') {
$mid = 'thread_reverse_sequence ASC';
} else {
$mid = $this->mDb->convertSortmode( $pSortOrder );
}
}
$mid = 'order by ' . $mid;
$bindVars = array();
if (is_array( $contentId ) ) {
$mid2 = 'in ('.implode(',', array_fill(0, count( $pContentId ), '?')).')';
$bindVars = $contentId;
$select1 = ', lcp.content_type_guid as parent_content_type_guid, lcp.title as parent_title ';
$join1 = " LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` lcp ON (lcp.content_id = lcom.parent_id) ";
} elseif ($contentId) {
$mid2 = "lcom.`thread_forward_sequence` LIKE '".sprintf("%09d.",$contentId)."%'";
$select1 = '';
$join1 = '';
}
if ($gBitSystem->isFeatureActive('boards_posts_anon_moderation') && !($gBitUser->hasPermission('p_boards_edit') || $gBitUser->hasPermission('p_boards_post_edit'))) {
$whereSql .= " AND ((post.`is_approved` = 1) OR (lc.`user_id` >= 0))";
}
$pListHash = array( 'content_id' => $contentId, 'max_records' => $pMaxComments, 'offset'=>$pOffset, 'sort_mode'=> $pSortOrder, 'display_mode' => $pDisplayMode, 'has_comment_view_perm' => TRUE );
$this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars, $this, $pListHash );
if ($pContentId) {
$sql = "SELECT lcom.`comment_id`, lcom.`parent_id`, lcom.`root_id`,
lcom.`thread_forward_sequence`, lcom.`thread_reverse_sequence`, lcom.`anon_name`, lc.*, uu.`email`, uu.`real_name`, uu.`login`,
post.is_approved,
post.is_warned,
post.warned_message,
uu.registration_date AS registration_date,
tf_ava.`storage_path` AS `avatar_storage_path`
$selectSql $select1
FROM `".BIT_DB_PREFIX."liberty_comments` lcom
INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lcom.`content_id` = lc.`content_id`)
INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON (lc.`user_id` = uu.`user_id`)
$joinSql $join1
LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_attachments` ta_ava ON ( uu.`avatar_attachment_id`=ta_ava.`attachment_id` )
LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_files` tf_ava ON ( tf_ava.`file_id`=ta_ava.`foreign_id` )
LEFT JOIN `".BIT_DB_PREFIX."boards_posts` post ON (post.`comment_id` = lcom.`comment_id`)
WHERE $mid2 $whereSql $mid";
$flat_comments = array();
if( $result = $this->mDb->query( $sql, $bindVars, $pMaxComments, $pOffset ) ) {
while( $row = $result->FetchRow() ) {
if (empty($row['anon_name'])) $row['anon_name'] = "Anonymous";
$row['user_avatar_url'] = liberty_fetch_thumbnail_url( array(
'storage_path' => $row['avatar_storage_path'],
'size' => 'avatar'
));
unset($row['avatar_storage_path']);
if (!empty($row['warned_message'])) {
$row['warned_message'] = str_replace("\n","<br />\n",$row['warned_message']);
}
$row['data'] = trim($row['data']);
$row['user_url']=BitUser::getDisplayUrl($row['login'],$row);
$row['parsed_data'] = $this->parseData( $row );
$row['level'] = substr_count ( $row['thread_forward_sequence'], '.' ) - 1;
$c = new LibertyComment();
$c->mInfo=$row;
$row['editable'] = $c->userCanEdit();
$flat_comments[$row['content_id']] = $row;
//va($row);
}
}
# now select comments wanted
$ret = $flat_comments;
}
return $ret;
}
function getList( &$pListHash ) {
global $gBitUser, $gBitSystem;
$this->prepGetList( $pListHash );
$joinSql = $selectSql = $whereSql = '';
$ret = array();
$contentId = $this->mCommentId;
// $mid = 'ORDER BY `thread_forward_sequence` ASC';
$bindVars = array();
if( !empty( $pListHash['content_id'] ) ) {
if (is_array( $contentId ) ) {
$mid2 = 'in ('.implode(',', array_fill(0, count( $pListHash['content_id'] ), '?')).')';
$bindVars = $contentId;
$selectSql = ', lcp.content_type_guid as parent_content_type_guid, lcp.title as parent_title ';
$joinSql .= " LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_content` lcp ON (lcp.content_id = lcom.parent_id) ";
} elseif( is_numeric( $contentId ) ) {
$whereSql .= " AND `thread_forward_sequence` LIKE '".sprintf("%09d.",$contentId)."%'";
}
}
if ($gBitSystem->isFeatureActive('boards_posts_anon_moderation') && !($gBitUser->hasPermission('p_boards_edit') || $gBitUser->hasPermission('p_boards_post_edit'))) {
$whereSql .= " AND ((post.`is_approved` = 1) OR (lc.`user_id` >= 0))";
}
$this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars, $this );
if( !empty( $pListHash['board_id'] ) ) {
$joinSql .= "INNER JOIN `".BIT_DB_PREFIX."boards` b ON (b.`content_id` = bm.`board_content_id`)";
$whereSql .= ' AND b.`board_id`=? ';
array_push( $bindVars, $pListHash['board_id'] );
}
if( BitBase::verifyId( $pListHash['user_id'] ) ) {
$whereSql .= ' AND lc.`user_id`=? ';
array_push( $bindVars, $pListHash['user_id'] );
}
if( !empty( $whereSql ) ) {
$whereSql = preg_replace( '/^[\s]*AND\b/i', 'WHERE ', $whereSql );
}
$sql = "SELECT lcom.`comment_id`, lcom.`parent_id`, lcom.`root_id`, lcom.`thread_forward_sequence`, lcom.`thread_reverse_sequence`, lcom.`anon_name`, lc.*, uu.`email`, uu.`real_name`, uu.`login`, post.is_approved, post.is_warned, post.warned_message, uu.registration_date AS registration_date, tf_ava.`storage_path` AS `avatar_storage_path` $selectSql
FROM `".BIT_DB_PREFIX."liberty_comments` lcom
INNER JOIN `".BIT_DB_PREFIX."boards_map` bm ON (lcom.`root_id` = bm.`topic_content_id`)
INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lcom.`content_id` = lc.`content_id`)
INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON (lc.`user_id` = uu.`user_id`)
$joinSql
LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_attachments` ta_ava ON ( uu.`avatar_attachment_id`=ta_ava.`attachment_id` )
LEFT OUTER JOIN `".BIT_DB_PREFIX."liberty_files` tf_ava ON ( tf_ava.`file_id`=ta_ava.`foreign_id` )
LEFT JOIN `".BIT_DB_PREFIX."boards_posts` post ON (post.`comment_id` = lcom.`comment_id`)
$whereSql ORDER BY ".$this->mDb->convertSortmode( $pListHash['sort_mode'] );
$ret = array();
if( $result = $this->mDb->query( $sql, $bindVars, $pListHash['max_records'], $pListHash['offset'] ) ) {
while( $row = $result->FetchRow() ) {
if (empty($row['anon_name'])) $row['anon_name'] = "Anonymous";
if( !empty( $row['avatar_storage_path'] )) {
$row['user_avatar_url'] = liberty_fetch_thumbnail_url( $row['avatar_storage_path'], 'avatar' );
} else {
$row['user_avatar_url'] = FALSE;
}
unset($row['avatar_storage_path']);
if (!empty($row['warned_message'])) {
$row['warned_message'] = str_replace("\n","<br />\n",$row['warned_message']);
}
$row['data'] = trim($row['data']);
$row['user_url']=BitUser::getDisplayUrl($row['login'],$row);
$row['parsed_data'] = $this->parseData( $row );
$row['level'] = substr_count ( $row['thread_forward_sequence'], '.' ) - 1;
$row['display_url'] = $this->getDisplayUrl( $row['comment_id'], boards_get_topic_comment( $row['thread_forward_sequence'] ) );
$c = new LibertyComment();
$c->mInfo=$row;
$row['editable'] = $c->userCanEdit();
$ret[] = $row;
//va($row);
}
}
return $ret;
}
function getNumComments($pContentId = NULL) {
$ret = 0;
$contentId = $this->mCommentId;
$bindVars = array();
$joinSql = $selectSql = $whereSql = '';
$this->getServicesSql( 'content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars, $this, array( 'include_comments' => TRUE ) );
if ($pContentId) {
$sql = "SELECT COUNT(*)
FROM `".BIT_DB_PREFIX."liberty_comments` lcom
INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON (lcom.`content_id` = lc.`content_id`)
INNER JOIN `".BIT_DB_PREFIX."users_users` uu ON (lc.`user_id` = uu.`user_id`) $joinSql
LEFT JOIN `".BIT_DB_PREFIX."boards_posts` post ON (post.`comment_id` = lcom.`comment_id`)
WHERE lcom.`thread_forward_sequence` LIKE '".sprintf("%09d.",$contentId)."%' $whereSql
";
$ret = $this->mDb->getOne( $sql, $bindVars );
}
return $ret;
}
/**
* Generates the URL to the bitboard page
* @param pExistsHash the hash that was returned by LibertyMime::pageExists
* @return the link to display the page.
*/
function getDisplayUrl( $pCommentId=NULL, $pTopicId=NULL ) {
global $gBitSystem;
if( empty( $pCommentId ) || empty( $pTopicId ) ) {
$pCommentId = $this->mCommentId;
$pTopicId = $this->getTopicId();
}
$ret = NULL;
if( @$this->verifyId( $pCommentId ) ) {
if( $gBitSystem->isFeatureActive( 'pretty_urls' ) || $gBitSystem->isFeatureActive( 'pretty_urls_extended' ) ) {
$rewrite_tag = $gBitSystem->isFeatureActive( 'pretty_urls_extended' ) ? 'view/':'';
$ret = BOARDS_PKG_URL.$rewrite_tag."topic/".$pTopicId;
} else {
$ret = BOARDS_PKG_URL."index.php?t=".$pTopicId;
}
if( $pCommentId != $pTopicId ) {
$ret .= '#comment_'.$pCommentId;
}
}
return $ret;
}
function getTopicId() {
return boards_get_topic_comment( $this->getField( 'thread_forward_sequence') );
}
function modApprove() {
$data['is_approved'] = 1;
$this->setMetaData($data);
}
function modReject() {
$this->deleteComment();
}
function modWarn($message) {
global $gBitSystem, $gBitUser;
if (empty($message)) {
$gBitSystem->fatalError("No Warning Message Given. <br />A post cannot be warned without a message");
}
$data['is_warned']=1;
$data['warned_message']=$message;
$this->setMetaData($data);
if ($gBitSystem->isPackageActive('messages')) {
require_once(MESSAGES_PKG_PATH.'messages_lib.php');
$u = new BitUser($this->mInfo['user_id']);
$u->load();
$userInfo = $u->mInfo;
$pm = new Messages();
$message = "Your post \"".$this->mInfo['title']."\" [http://".$_SERVER['HTTP_HOST'].$this->getDisplayUrl()."] has been warned with the following message:\n$message\n";
$msgHash = array(
'to_login' => $userInfo['login'],
'to' => $userInfo['real_name'],
'subject' => tra( 'Warned Post' ).': '.$this->mInfo['title'],
'priority' => 4,
);
$pm->postMessage( $msgHash );
}
}
function setMetaData($data) {
if ($this->isValid()) {
$key = array('comment_id' => $this->mCommentId);
$query_sel = "SELECT COUNT(*) FROM `".BIT_DB_PREFIX."boards_posts` WHERE comment_id=?";
$c = $this->mDb->getOne( $query_sel , array_values($key));
if ($c == 0) {
$data=array_merge($data,$key);
$this->mDb->associateInsert(BIT_DB_PREFIX."boards_posts",$data);
} else {
$this->mDb->associateUpdate(BIT_DB_PREFIX."boards_posts",$data,$key);
}
}
}
}
?>
|