File: ForumMessage.class

package info (click to toggle)
gforge 4.5.14-22etch13
  • links: PTS
  • area: main
  • in suites: etch
  • size: 13,004 kB
  • ctags: 11,918
  • sloc: php: 36,047; sql: 29,050; sh: 10,538; perl: 6,496; xml: 3,810; makefile: 341; python: 263; ansic: 256
file content (439 lines) | stat: -rw-r--r-- 12,596 bytes parent folder | download | duplicates (2)
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/**
 * GForge Forums Facility
 *
 * Copyright 2002 GForge, LLC
 * http://gforge.org/
 *
 * @version   $Id: ForumMessage.class 5632 2006-08-16 14:35:25Z federicot $
 *
 * This file is part of GForge.
 *
 * GForge is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GForge; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


/*
	Message Forums
	By Tim Perdue, Sourceforge, 11/99

	Massive rewrite by Tim Perdue 7/2000 (nested/views/save)

	Complete OO rewrite by Tim Perdue 12/2002
*/

require_once('common/include/Error.class');

class ForumMessage extends Error {

	/**
	 * Associative array of data from db.
	 *
	 * @var	 array   $data_array.
	 */
	var $data_array;

	/**
	 * The Forum object.
	 *
	 * @var	 object  $Forum.
	 */
	var $Forum;
	
		/**
	 *  Constructor.
	 *
	 *	@param	object	The Forum object to which this ForumMessage is associated.
	 *  @param  int	 The message_id.
	 *  @param  array   The associative array of data.
	 *	@return	boolean	success.
	 */
	function ForumMessage(&$Forum, $msg_id=false, $arr=false) {
		global $Language;
		$this->Error();
		if (!$Forum || !is_object($Forum)) {
			$this->setError($Language->getText('forum_common_forummessage','no_valid_forum_object'));
			return false;
		}
		if ($Forum->isError()) {
			$this->setError('ForumMessage:: '.$Forum->getErrorMessage());
			return false;
		}
		$this->Forum =& $Forum;

		if ($msg_id) {
			if (!$arr || !is_array($arr)) {
				if (!$this->fetchData($msg_id)) {
					return false;
				}
			} else {
				$this->data_array =& $arr;
				//
				//	Verify this message truly belongs to this Forum
				//
				if ($this->data_array['group_forum_id'] != $this->Forum->getID()) {
					$this->setError($Language->getText('forum_common_forummessage','error_group_forum_id'));
					$this->data_array=null;
					return false;
				}
			}
		}
		return true;
	}

	/**
	 *	create - use this function to create a new message in the database.
	 *
	 *	@param	string	The subject of the message.
	 *	@param	string	The body of the message.
	 *	@param	int	The thread_id of the message, if known.
	 *	@param	int	The message_id of the parent message, if any.
	 *	@return	boolean success.
	 */
	function create($subject, $body, $thread_id='', $is_followup_to='') {
		global $Language;
		if (!$body || !$subject) {
			$this->setError($Language->getText('forum_common_forummessage','error_required_fields'));
			return false;
		}
		if (!$this->Forum->userCanPost()) {
			$this->setPermissionDeniedError();
			return false;
		}
		if (!session_loggedin()) {
			$user_id=100;
		} else {
			$user_id=user_getid();
		}

		if ($is_follow_up_to) {
			$ParentMessage=new ForumMessage($this->Forum,$is_followup_to);
			if (!$ParentMessage || !is_object($ParentMessage)) {
				$this->setError("ForumMessage::create()".$Language->getText('forum_common_forummessage','error_no_valid_parent_message'));
				return false;
			}
			if ($ParentMessage->isError()) {
				$this->setError('ForumMessage::create() '.$ParentMessage->getErrorMessage());
				return false;
			}
		}
		if (!$is_followup_to) { 
			$is_followup_to=0; 
		}

		//see if that message has been posted already for all the idiots that double-post
		$res3=db_query("SELECT * FROM forum 
			WHERE is_followup_to='$is_followup_to' 
			AND body='".  htmlspecialchars($body) ."'
			AND subject='".  htmlspecialchars($subject) ."' 
			AND group_forum_id='". $this->Forum->getId() ."' 
			AND posted_by='$user_id'");

		if (db_numrows($res3) > 0) {
			//already posted this message
			$this->setError($Language->getText('forum_utils','doublepost'));
			return false;
		} else {
			echo db_error();
		}
		db_begin();
		if (!$thread_id) {
			$thread_id=$this->Forum->getNextThreadID();
			$is_followup_to=0;
			if (!$thread_id) {
				$this->setError('ForumMessage::create() '.$Language->getText('forum_common_forummessage','error_getting_next_thread'));
				db_rollback();
				return false;
			}
		} else {
			//
			//  increment the parent's followup count if necessary
			//
			$res4=db_query("UPDATE forum SET most_recent_date='". time() ."' 
				WHERE thread_id='$thread_id' AND is_followup_to='0'");
			if (!$res4 || db_affected_rows($res4) < 1) {
				$this->setError($Language->getText('forum_common_forummessage','error_cannot_update_master_thread'));
				db_rollback();
				return false;
			} else {
				//
				//  mark the parent with followups as an optimization later
				//
				$res3=db_query("UPDATE forum SET has_followups='1',most_recent_date='". time() ."' 
					WHERE msg_id='$is_followup_to'");
				if (!$res3) {
					$this->setError($Language->getText('forum_common_forummessage','error_cannot_update_parent'));
					db_rollback();
					return false;
				}
			}
		}

		$sql="INSERT INTO forum (group_forum_id,posted_by,subject,
			body,post_date,is_followup_to,thread_id,most_recent_date) 
			VALUES ('". $this->Forum->getID() ."', '$user_id', '". htmlspecialchars($subject) ."', 
			'". htmlspecialchars($body) ."', '". time() ."','$is_followup_to','$thread_id','". time() ."')";

		$result=db_query($sql);
		if (!$result || db_affected_rows($result) < 1) {
			$this->setError($Language->getText('forum_common_forummessage','error_posting_failed').' '.db_error());
			db_rollback();
			return false;
		} else {
			$msg_id=db_insertid($result,'forum','msg_id');
			if (!$this->fetchData($msg_id)) {
				db_rollback();
				return false;
			}
			if (!$msg_id) {
				db_rollback();
				$this->setError($Language->getText('forum_common_forummessage','error_get_message_id'));
				return false;
			} else {
				if (!$this->sendNotice()) {
					db_rollback();
					return false;
				}
//echo "Committing";
				db_commit();
//echo "db_error()".db_error();
				return true;
			}
		}
	}

	/**
	 *  fetchData - re-fetch the data for this forum_message from the database.
	 *
	 *  @param  int	 The message ID.
	 *  @return boolean	success.
	 */
	function fetchData($msg_id) {
		global $Language;
		$res=db_query("SELECT * FROM forum_user_vw
			WHERE msg_id='$msg_id'
			AND group_forum_id='". $this->Forum->getID() ."'");
		if (!$res || db_numrows($res) < 1) {
			$this->setError($Language->getText('forum_common_forummessage','error_invalid_message_id').db_error());
			return false;
		}
		$this->data_array =& db_fetch_array($res);
		db_free_result($res);
		return true;
	}

	/**
	 *	getForum - get the Forum object this ForumMessage is associated with.
	 *
	 *	@return	object	The Forum object.
	 */
	function &getForum() {
		return $this->Forum;
	}

	/**
	 *	getID - get this message_id.
	 *
	 *	@return	int	The message_id.
	 */
	function getID() {
		return $this->data_array['msg_id'];
	}

	/**
	 *	getPosterName - get the unix user_name of this message's poster.
	 *
	 *	@return	string	The poster's unix name.
	 */
	function getPosterName() {
		return $this->data_array['user_name'];
	}

	/**
	 *	getPosterID - get this user_id of this message's poster.
	 *
	 *	@return	int	The user_id.
	 */
	function getPosterID() {
		return $this->data_array['posted_by'];
	}

	/**
	 *	getPosterRealName - get the real name of this message's poster.
	 *
	 *	@return	string	The real name.
	 */
	function getPosterRealName() {
		return $this->data_array['realname'];
	}

	/**
	 *	getSubject - get the subject of this message.
	 *
	 *	@return	string	The subject.
	 */
	function getSubject() {
		return $this->data_array['subject'];
	}

	/**
	 *	getBody - get the body of this message.
	 *
	 *	@return	String	The body.
	 */
	function getBody() {
		return $this->data_array['body'];
	}

	/**
	 *	getPostDate - get the post date of this message.
	 *
	 *	@return	int	The post date.
	 */
	function getPostDate() {
		return $this->data_array['post_date'];
	}

	/**
	 *	getParentID - get the id of the parent message, if this is a followup.
	 *
	 *	@return	int	The parent id.
	 */
	function getParentID() {
		return $this->data_array['is_followup_to'];
	}

	/**
	 *	getThreadID - get the thread_id of the message.
	 *
	 *	@return	int	The thread_id.
	 */
	function getThreadID() {
		return $this->data_array['thread_id'];
	}

	/**
	 *	getMostRecentDate - get the date of the most recent followup.
	 *
	 *	@return	int	The date of the most recent followup.
	 */
	function getMostRecentDate() {
		return $this->data_array['most_recent_date'];
	}

	/**
	 *	hasFollowups - whether this message has any followups.
	 *
	 *	@return boolean has_followups.
	 */
	function hasFollowups() {
		return $this->data_array['has_followups'];
	}

	/**
	 *	delete - Delete this message and its followups.
	 *
	 *	@return	int	The count of deleted messages.
	 */
	function delete() {
		$msg_id=$this->getID();
		$perm =& $this->Forum->Group->getPermission( session_get_user() );

		if (!$perm || !is_object($perm) || !$perm->isForumAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}

		$sql="SELECT msg_id FROM forum 
			WHERE is_followup_to='$msg_id' 
			AND group_forum_id='".$this->Forum->getID()."'";
		$result=db_query($sql);
		$rows=db_numrows($result);
		$count=1;

		for ($i=0;$i<$rows;$i++) {
			$msg = new ForumMessage($this->Forum,db_result($result,$i,'msg_id'));
			$count += $msg->delete();
		}
		$sql="DELETE FROM forum 
			WHERE msg_id='$msg_id' 
			AND group_forum_id='".$this->Forum->getID()."'";
		$toss=db_query($sql);

		return $count;

	}

	/**
	 *	sendNotice - contains the logic to send out email followups when a message is posted.
	 *
	 *	@return boolean success.
	 */
	function sendNotice() {
		global $Language;
		$ids =& $this->Forum->getMonitoringIDs();

		//
		//	See if there is anyone to send messages to
		//
		if (!count($ids) > 0 && !$this->Forum->getSendAllPostsTo()) {
			return true;
		}

		$body = "\nRead and respond to this message at: ".
		"\nhttp://".$GLOBALS['sys_default_domain']."/forum/message.php?msg_id=".$this->getID().
		"\nOr by replying to this e-mail entering your response between the following markers: ".
			"\n".FORUM_MAIL_MARKER.
			"\n(enter your response here)".
			"\n".FORUM_MAIL_MARKER.
			"\n\n".
		"\nBy: " . $this->getPosterRealName() . "\n\n";
		

		$body .= util_line_wrap(util_unconvert_htmlspecialchars($this->getBody())).
		"\n\n______________________________________________________________________".
		"\nYou are receiving this email because you elected to monitor this forum.".
		"\nTo stop monitoring this forum, login to ".$GLOBALS['sys_name']." and visit: ".
		"\nhttp://".$GLOBALS['sys_default_domain']."/forum/monitor.php?forum_id=".$this->Forum->getID() .'&group_id='.$this->Forum->Group->getID().'&stop=1';

		//$extra_headers = 'Reply-to: '.$this->Forum->getUnixName().'@'.$GLOBALS['sys_default_domain'];
		$extra_headers = "Return-Path: <noreply@gforge.org>\n";
		$extra_headers .= "Reply-To: ".$this->Forum->getReturnEmailAddress()."\n";
		$extra_headers .= "Precedence: Bulk\n"
			."List-Id: ".$this->Forum->getName()." <forum".$this->Forum->getId()."@".$GLOBALS['sys_default_domain'].">\n"
			."List-Help: http://".$GLOBALS['sys_default_domain']."/forum/forum.php?id=".$this->Forum->getId()."\n"
			."Message-Id: <forumpost".$this->getId()."@".$GLOBALS['sys_default_domain'].">";
		$parentid = $this->getParentId();
		if (!empty($parentid)) {
 			$extra_headers .= "\nIn-Reply-To: ".$this->Forum->getReturnEmailAddress()."\n"
				."References: <forumpost".$this->getParentId()."@".$GLOBALS['sys_default_domain'].">";
		}

		$subject="[" . $this->Forum->getUnixName() ."][".$this->getID()."] ".util_unconvert_htmlspecialchars($this->getSubject());
		if (count($ids) != 0) {
			$sql="SELECT email FROM users WHERE status='A' AND user_id IN ('".implode($ids,'\',\'')."')";
			$bccres = db_query($sql);
		}
		$BCC =& implode(util_result_column_to_array($bccres),',').','.$this->Forum->getSendAllPostsTo();
//echo $BCC;
		$User = user_get_object($this->getPosterID());
		util_send_message('',$subject,$body,$User->getEmail(),$BCC,$this->getPosterRealName(),$extra_headers);
//		util_handle_message(array_unique($ids),$subject,$body,$this->Forum->getSendAllPostsTo(),'','forumgateway@'.$GLOBALS[sys_default_domain]);
		return true;
	}

}

?>