5 * Copyright 1999-2000, Tim Perdue/Sourceforge
6 * Copyright 2002, Tim Perdue/GForge, LLC
7 * Copyright 2009, Roland Mas
8 * Copyright (C) 2011 Alain Peyrat - Alcatel-Lucent
10 * This file is part of FusionForge. FusionForge is free software;
11 * you can redistribute it and/or modify it under the terms of the
12 * GNU General Public License as published by the Free Software
13 * Foundation; either version 2 of the Licence, or (at your option)
16 * FusionForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 require_once $gfcommon.'include/Error.class.php';
27 require_once $gfcommon.'forum/ForumMessage.class.php';
28 // This string is used when sending the notification mail for identifying the
30 define('FORUM_MAIL_MARKER', '#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+');
33 * Gets a Forum object from its id
35 * @param int the Forum id
36 * @return object the Forum object
38 function &forum_get_object($forum_id) {
39 $res = db_query_params('SELECT group_id FROM forum_group_list WHERE group_forum_id=$1',
41 if (!$res || db_numrows($res) < 1) {
45 $data = db_fetch_array($res);
46 $Group = group_get_object($data["group_id"]);
47 $f = new Forum($Group, $forum_id);
49 $f->fetchData($forum_id);
54 function forum_get_groupid ($forum_id) {
55 $res = db_query_params('SELECT group_id FROM forum_group_list WHERE group_forum_id=$1',
57 if (!$res || db_numrows($res) < 1) {
60 $arr = db_fetch_array($res);
61 return $arr['group_id'];
64 class Forum extends Error {
67 * Associative array of data from db.
69 * @var array $data_array.
78 var $Group; //group object
81 * An array of 'types' for this forum - nested, flat, ultimate, etc.
83 * @var array view_types.
90 * @param object The Group object to which this forum is associated.
91 * @param int The group_forum_id.
92 * @param array The associative array of data.
93 * @return boolean success.
95 function Forum(&$Group, $group_forum_id = false, $arr = false) {
97 if (!$Group || !is_object($Group)) {
98 $this->setError(_('Forums: No Valid Group Object'));
101 if ($Group->isError()) {
102 $this->setError('Forums: '.$Group->getErrorMessage());
105 $this->Group =& $Group;
107 if ($group_forum_id) {
108 if (!$arr || !is_array($arr)) {
109 if (!$this->fetchData($group_forum_id)) {
113 $this->data_array =& $arr;
114 if ($this->data_array['group_id'] != $this->Group->getID()) {
115 $this->setError(_('Group_id in db result does not match Group Object'));
116 $this->data_array = null;
121 // Make sure they can even access this object
123 if (!forge_check_perm ('forum', $this->getID(), 'read')) {
124 $this->setPermissionDeniedError();
125 $this->data_array = null;
129 $this->view_types[] = 'ultimate';
130 $this->view_types[] = 'flat';
131 $this->view_types[] = 'nested';
132 $this->view_types[] = 'threaded';
137 * create - use this function to create a new entry in the database.
139 * @param string The name of the forum.
140 * @param string The description of the forum.
141 * @param string The email address to send all new posts to.
142 * @param int Pass (1) if a welcome message should be created (0) for no welcome message.
143 * @return boolean success.
145 function create($forum_name,$description,$send_all_posts_to='',$create_default_message=1) {
146 if (strlen($forum_name) < 3) {
147 $this->setError(_('Forum Name Must Be At Least 3 Characters'));
150 if (strlen($description) < 10) {
151 $this->setError(_('Forum Description Must Be At Least 10 Characters'));
154 if (!preg_match('/^([_\.0-9a-z-])*$/i',$forum_name)) {
155 $this->setError(_('Illegal Characters in Forum Name'));
158 if ($send_all_posts_to) {
159 $send_all_posts_to = str_replace(';', ',', $send_all_posts_to);
160 $invalid_mails = validate_emails($send_all_posts_to);
161 if (count($invalid_mails) > 0) {
162 $this->setInvalidEmailError($send_all_posts_to);
167 $project_name = $this->Group->getUnixName();
168 $result_list_samename = db_query_params('SELECT 1 FROM mail_group_list WHERE list_name=$1 AND group_id=$2',
170 array($project_name.'-'.strtolower($forum_name),
171 $this->Group->getID()));
173 if (db_numrows($result_list_samename) > 0){
174 $this->setError(_('Mailing List Exists with same name'));
179 // This is a hack to allow non-site-wide-admins to post
180 // news. The news/submit.php checks for proper permissions.
181 // This needs to be revisited.
183 if ($this->Group->getID() == forge_get_config('news_group')) {
184 // Future check will be added.
187 // Current permissions check.
189 if (!forge_check_perm ('forum_admin', $this->Group->getID())) {
190 $this->setPermissionDeniedError();
196 $result = db_query_params('INSERT INTO forum_group_list (group_id,forum_name,description,send_all_posts_to) VALUES ($1,$2,$3,$4)',
197 array($this->Group->getID(),
198 strtolower($forum_name),
199 htmlspecialchars($description),
200 $send_all_posts_to));
202 $this->setError(_('Error Adding Forum:').' '.db_error());
206 $this->group_forum_id=db_insertid($result,'forum_group_list','group_forum_id');
207 $this->fetchData($this->group_forum_id);
209 if ($create_default_message) {
210 $fm=new ForumMessage($this);
211 // Use the system side default language
212 setup_gettext_from_sys_lang();
213 $string = sprintf(_('Welcome to %1$s'), $forum_name);
214 // and switch back to the user preference
215 setup_gettext_from_context();
216 if (!$fm->create($string, $string)) {
217 $this->setError($fm->getErrorMessage());
222 $this->Group->normalizeAllRoles () ;
228 * fetchData - re-fetch the data for this forum from the database.
230 * @param int The forum_id.
231 * @return boolean success.
233 function fetchData($group_forum_id) {
234 $res=db_query_params('SELECT * FROM forum_group_list_vw WHERE group_forum_id=$1 AND group_id=$2',
235 array($group_forum_id, $this->Group->getID()));
236 if (!$res || db_numrows($res) < 1) {
237 $this->setError(_('Invalid forum group identifier'));
240 $this->data_array = db_fetch_array($res);
241 db_free_result($res);
246 * getGroup - get the Group object this ArtifactType is associated with.
248 * @return object The Group object.
250 function &getGroup() {
255 * getID - The id of this forum.
257 * @return int The group_forum_id #.
260 return $this->data_array['group_forum_id'];
264 * getNextThreadID - The next thread_id for a new top in this forum.
266 * @return int The next thread_id #.
268 function getNextThreadID() {
269 $result = db_query_params('SELECT nextval($1)',
270 array('forum_thread_seq'));
271 if (!$result || db_numrows($result) < 1) {
275 return db_result($result, 0, 0);
279 * getUnixName - returns the name used by email gateway
281 * @return string unix name
283 function getUnixName() {
284 return $this->Group->getUnixName().'-'.$this->getName();
288 * getSavedDate - The unix time when the person last hit "save my place".
290 * @return int The unix time.
292 function getSavedDate() {
293 if (isset($this->save_date)) {
294 return $this->save_date;
296 if (session_loggedin()) {
297 $result = db_query_params('SELECT save_date FROM forum_saved_place WHERE user_id=$1 AND forum_id=$2',
300 if ($result && db_numrows($result) > 0) {
301 $this->save_date=db_result($result, 0, 'save_date');
302 return $this->save_date;
304 //highlight new messages from the past week only
305 $this->save_date=(time()-604800);
306 return $this->save_date;
309 //highlight new messages from the past week only
310 $this->save_date=(time()-604800);
311 return $this->save_date;
317 * getName - get the name of this forum.
319 * @return string The name of this forum.
322 return $this->data_array['forum_name'];
326 * getSendAllPostsTo - an optional email address to send all forum posts to.
328 * @return string The email address.
330 function getSendAllPostsTo() {
331 return $this->data_array['send_all_posts_to'];
335 * getDescription - the description of this forum.
337 * @return string The description.
339 function getDescription() {
340 return $this->data_array['description'];
344 * getMessageCount - the total number of messages in this forum.
346 * @return int The count.
348 function getMessageCount() {
349 return $this->data_array['total'];
353 * getThreadCount - the total number of threads in this forum.
355 * @return int The count.
357 function getThreadCount() {
358 return $this->data_array['threads'];
362 * getMostRecentDate - the most recent date of a post to this board.
364 * @return int The most recent date.
366 function getMostRecentDate() {
367 return $this->data_array['recent'];
371 * getMonitoringIDs - return an array of user_id's for those monitoring this forum.
373 * @return array The array of user_id's.
375 function getMonitoringIDs() {
376 $result = db_query_params('SELECT user_id FROM forum_monitored_forums WHERE forum_id=$1',
377 array($this->getID()));
378 return util_result_column_to_array($result);
382 * getReturnEmailAddress() - return the return email address for notification emails
384 * @return string return email address
386 function getReturnEmailAddress() {
388 if(forge_get_config('use_gateways')) {
389 $address = $this->getUnixName();
391 $address = 'noreply';
394 if(forge_get_config('use_gateways') && forge_get_config('forum_return_domain')) {
395 $address .= forge_get_config('forum_return_domain');
397 $address .= forge_get_config('web_host');
403 * setMonitor - Add the current user to the list of people monitoring the forum.
405 * @param int user id of the user which will be set to monitor this forum. Defaults to 0, meaning the current logged in user will be used.
406 * @return boolean success.
408 function setMonitor($u = -1) {
410 if (!session_loggedin()) {
411 $this->setError(_('You can only monitor if you are logged in'));
416 $result = db_query_params('SELECT * FROM forum_monitored_forums WHERE user_id=$1 AND forum_id=$2',
419 if (!$result || db_numrows($result) < 1) {
421 User is not already monitoring thread, so
422 insert a row so monitoring can begin
424 $result = db_query_params('INSERT INTO forum_monitored_forums (forum_id,user_id) VALUES ($1,$2)',
425 array($this->getID(),
429 $this->setError(_('Unable To Add Monitor').' : '.db_error());
438 * stopMonitor - Remove the current user from the list of people monitoring the forum.
440 * @return boolean success.
442 function stopMonitor($u = -1) {
444 if (!session_loggedin()) {
445 $this->setError(_('You can only monitor if you are logged in'));
450 return db_query_params('DELETE FROM forum_monitored_forums WHERE user_id=$1 AND forum_id=$2',
456 * isMonitoring - See if the current user is in the list of people monitoring the forum.
458 * @return boolean is_monitoring.
460 function isMonitoring() {
461 if (!session_loggedin()) {
464 $result = db_query_params('SELECT count(*) AS count FROM forum_monitored_forums WHERE user_id=$1 AND forum_id=$2',
467 $row_count = db_fetch_array($result);
468 return $result && $row_count['count'] > 0;
472 * savePlace - set a unix time into the database for this user, so future messages can be highlighted.
474 * @return boolean success.
476 function savePlace() {
477 if (!session_loggedin()) {
478 $this->setError(_('You Can Only Save Your Place If You Are Logged In'));
481 $result = db_query_params('SELECT * FROM forum_saved_place WHERE user_id=$1 AND forum_id=$2',
485 if (!$result || db_numrows($result) < 1) {
487 User is not already monitoring thread, so
488 insert a row so monitoring can begin
490 $result = db_query_params('INSERT INTO forum_saved_place (forum_id,user_id,save_date) VALUES ($1,$2,$3)',
491 array($this->getID(),
496 $this->setError(_('Forum::savePlace()').': '.db_error());
501 $result = db_query_params('UPDATE forum_saved_place SET save_date=$1 WHERE user_id=$2 AND forum_id=$3',
507 $this->setError('Forum::savePlace() '.db_error());
515 * update - use this function to update an entry in the database.
517 * @param string The name of the forum.
518 * @param string The description of the forum.
519 * @param string The email address to send all new posts to.
520 * @return boolean success.
522 function update($forum_name, $description, $send_all_posts_to = '') {
523 if (strlen($forum_name) < 3) {
524 $this->setError(_('Forum Name Must Be At Least 3 Characters'));
527 if (strlen($description) < 10) {
528 $this->setError(_('Forum Description Must Be At Least 10 Characters'));
531 if (!preg_match('/^([_\.0-9a-z-])*$/i',$forum_name)) {
532 $this->setError(_('Illegal Characters in Forum Name'));
535 if ($send_all_posts_to) {
536 $send_all_posts_to = str_replace(';', ',', $send_all_posts_to);
537 $invalid_mails = validate_emails($send_all_posts_to);
538 if (count($invalid_mails) > 0) {
539 $this->setInvalidEmailError($send_all_posts_to);
544 if (!forge_check_perm('forum_admin', $this->Group->getID())) {
545 $this->setPermissionDeniedError();
549 $res = db_query_params('UPDATE forum_group_list SET
554 AND group_forum_id=$5',
555 array(strtolower($forum_name),
556 htmlspecialchars($description),
558 $this->Group->getID(),
561 if (!$res || db_affected_rows($res) < 1) {
562 $this->setError(_('Error On Update:').': '.db_error());
569 * delete - delete this forum and all its related data.
571 * @param bool I'm Sure.
572 * @param bool I'm REALLY sure.
573 * @return bool true/false;
575 function delete($sure, $really_sure) {
576 if (!$sure || !$really_sure) {
577 $this->setMissingParamsError(_('Please tick all checkboxes.'));
580 if (!forge_check_perm ('forum_admin', $this->Group->getID())) {
581 $this->setPermissionDeniedError();
585 $result = db_query_params('DELETE FROM forum_agg_msg_count WHERE group_forum_id=$1',
586 array($this->getID()));
588 $this->setError(_('Error Deleting Forum:').' '.db_error());
593 $result = db_query_params('DELETE FROM forum_monitored_forums WHERE forum_id=$1',
594 array($this->getID()));
596 $this->setError(_('Error Deleting Forum:').' '.db_error());
601 $result = db_query_params('DELETE FROM forum_saved_place WHERE forum_id=$1',
602 array($this->getID()));
604 $this->setError(_('Error Deleting Forum:').' '.db_error());
609 $result = db_query_params('DELETE FROM forum_attachment WHERE msg_id IN (SELECT msg_id from forum where group_forum_id=$1)',
610 array($this->getID()));
612 $this->setError(_('Error Deleting Forum:').' '.db_error());
617 $result = db_query_params('DELETE FROM forum WHERE group_forum_id=$1',
618 array($this->getID()));
620 $this->setError(_('Error Deleting Forum:').' '.db_error());
625 $result = db_query_params('DELETE FROM forum_group_list WHERE group_forum_id=$1',
626 array($this->getID()));
628 $this->setError(_('Error Deleting Forum:').' '.db_error());
635 $this->Group->normalizeAllRoles();
644 // c-file-style: "bsd"