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 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
|
<?php
/**
* GForge Forums Facility
*
* Copyright 2002 GForge, LLC
* http://gforge.org/
*
* @version $Id: Forum.class 5037 2005-11-28 20:04:49Z lcorso $
*
* 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');
require_once('common/forum/ForumMessage.class');
// This string is used when sending the notification mail for identifying the
// user response
define('FORUM_MAIL_MARKER', '#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+#+');
class Forum extends Error {
/**
* Associative array of data from db.
*
* @var array $data_array.
*/
var $data_array;
/**
* The Group object.
*
* @var object $Group.
*/
var $Group; //group object
/**
* An array of 'types' for this forum - nested, flat, ultimate, etc.
*
* @var array view_types.
*/
var $view_types;
var $current_user_perm;
/**
* Constructor.
*
* @param object The Group object to which this forum is associated.
* @param int The group_forum_id.
* @param array The associative array of data.
* @return boolean success.
*/
function Forum(&$Group, $group_forum_id=false, $arr=false) {
global $Language;
$this->Error();
if (!$Group || !is_object($Group)) {
$this->setError($Language->getText('general','error_no_valid_group_object','Forum'));
return false;
}
if ($Group->isError()) {
$this->setError('Forum:: '.$Group->getErrorMessage());
return false;
}
$this->Group =& $Group;
if ($group_forum_id) {
if (!$arr || !is_array($arr)) {
if (!$this->fetchData($group_forum_id)) {
return false;
}
} else {
$this->data_array =& $arr;
if ($this->data_array['group_id'] != $this->Group->getID()) {
$this->setError($Language->getText('general','error_group_id'));
$this->data_array = null;
return false;
}
}
//
// Make sure they can even access this object
//
if (!$this->userCanView()) {
$this->setPermissionDeniedError();
$this->data_array = null;
return false;
}
}
$this->view_types[]='ultimate';
$this->view_types[]='flat';
$this->view_types[]='nested';
$this->view_types[]='threaded';
return true;
}
/**
* create - use this function to create a new entry in the database.
*
* @param string The name of the forum.
* @param string The description of the forum.
* @param int Pass (1) if it should be public (0) for private.
* @param string The email address to send all new posts to.
* @param int Pass (1) if a welcome message should be created (0) for no welcome message.
* @param int Pass (1) if we should allow non-logged-in users to post (0) for mandatory login.
* @return boolean success.
*/
function create($forum_name,$description,$is_public=1,$send_all_posts_to='',$create_default_message=1,$allow_anonymous=1) {
global $Language;
if (strlen($forum_name) < 3) {
$this->setError($Language->getText('forum_common','error_min_name_length'));
return false;
}
if (strlen($description) < 10) {
$this->setError($Language->getText('forum_common','error_min_desc_length'));
return false;
}
if (eregi('[^_\.0-9a-z-]',$forum_name)) {
$this->setError($Language->getText('forum_common','error_illegal_characters'));
return false;
}
if ($send_all_posts_to) {
$invalid_mails = validate_emails($send_all_posts_to);
if (count($invalid_mails) > 0) {
$this->setInvalidEmailError();
return false;
}
}
// This is a hack to allow non-site-wide-admins to post
// news. The news/submit.php checks for proper permissions.
// This needs to be revisited.
global $sys_news_group;
if ($this->Group->getID() == $sys_news_group) {
// Future check will be added.
} else {
// Current permissions check.
$perm =& $this->Group->getPermission( session_get_user() );
if (!$perm || !is_object($perm) || !$perm->isForumAdmin()) {
$this->setPermissionDeniedError();
return false;
}
}
$sql="INSERT INTO forum_group_list (group_id,forum_name,is_public,description,send_all_posts_to,allow_anonymous)
VALUES ('".$this->Group->getId()."',
'". strtolower($forum_name) ."',
'$is_public',
'". htmlspecialchars($description) ."',
'$send_all_posts_to',
'$allow_anonymous')";
db_begin();
$result=db_query($sql);
if (!$result) {
db_rollback();
$this->setError($Language->getText('forum_common','error_adding_forum').db_error());
return false;
}
$this->group_forum_id=db_insertid($result,'forum_group_list','group_forum_id');
$this->fetchData($this->group_forum_id);
if (!$this->addAllUsers()) {
db_rollback();
return false;
}
if ($create_default_message) {
$fm=new ForumMessage($this);
if (!$fm->create("Welcome to ".$forum_name,"Welcome to ".$forum_name)) {
$this->setError($fm->getErrorMessage());
return false;
}
}
db_commit();
return true;
}
/**
* fetchData - re-fetch the data for this forum from the database.
*
* @param int The forum_id.
* @return boolean success.
*/
function fetchData($group_forum_id) {
global $Language;
$res=db_query("SELECT * FROM forum_group_list_vw
WHERE group_forum_id='$group_forum_id'
AND group_id='". $this->Group->getID() ."'");
if (!$res || db_numrows($res) < 1) {
$this->setError($Language->getText('forum_common','error_invalid_group_forum_id'));
return false;
}
$this->data_array =& db_fetch_array($res);
db_free_result($res);
return true;
}
/**
* getGroup - get the Group object this ArtifactType is associated with.
*
* @return object The Group object.
*/
function &getGroup() {
return $this->Group;
}
/**
* getID - The id of this forum.
*
* @return int The group_forum_id #.
*/
function getID() {
return $this->data_array['group_forum_id'];
}
/**
* getNextThreadID - The next thread_id for a new top in this forum.
*
* @return int The next thread_id #.
*/
function getNextThreadID() {
$result=db_query("SELECT nextval('forum_thread_seq')");
if (!$result || db_numrows($result) < 1) {
echo db_error();
return false;
} else {
return db_result($result,0,0);
}
}
/**
* getUnixName - returns the name used by email gateway
*
* @return string unix name
*/
function getUnixName() {
return $this->Group->getUnixName().'-'.$this->getName();
}
/**
* getSavedDate - The unix time when the person last hit "save my place".
*
* @return int The unix time.
*/
function getSavedDate() {
if ($this->save_date) {
return $this->save_date;
} else {
if (session_loggedin()) {
$sql="SELECT save_date FROM forum_saved_place
WHERE user_id='".user_getid()."' AND forum_id='". $this->getID() ."';";
$result = db_query($sql);
if ($result && db_numrows($result) > 0) {
$this->save_date=db_result($result,0,'save_date');
return $this->save_date;
} else {
//highlight new messages from the past week only
$this->save_date=(time()-604800);
return $this->save_date;
}
} else {
//highlight new messages from the past week only
$this->save_date=(time()-604800);
return $this->save_date;
}
}
}
/**
* allowAnonymous - does this forum allow non-logged in users to post.
*
* @return boolean allow_anonymous.
*/
function allowAnonymous() {
return $this->data_array['allow_anonymous'];
}
/**
* isPublic - Is this forum open to the general public.
*
* @return boolean is_public.
*/
function isPublic() {
return $this->data_array['is_public'];
}
/**
* getName - get the name of this forum.
*
* @return string The name of this forum.
*/
function getName() {
return $this->data_array['forum_name'];
}
/**
* getSendAllPostsTo - an optional email address to send all forum posts to.
*
* @return string The email address.
*/
function getSendAllPostsTo() {
return $this->data_array['send_all_posts_to'];
}
/**
* getDescription - the description of this forum.
*
* @return string The description.
*/
function getDescription() {
return $this->data_array['description'];
}
/**
* getMessageCount - the total number of messages in this forum.
*
* @return int The count.
*/
function getMessageCount() {
return $this->data_array['total'];
}
/**
* getThreadCount - the total number of threads in this forum.
*
* @return int The count.
*/
function getThreadCount() {
return $this->data_array['threads'];
}
/**
* getMostRecentDate - the most recent date of a post to this board.
*
* @return int The most recent date.
*/
function getMostRecentDate() {
return $this->data_array['recent'];
}
/**
* getMonitoringIDs - return an array of user_id's for those monitoring this forum.
*
* @return array The array of user_id's.
*/
function getMonitoringIDs() {
$sql="SELECT user_id FROM forum_monitored_forums WHERE forum_id='".$this->getID()."'";
$result=db_query($sql);
return util_result_column_to_array($result);
}
/**
* getReturnEmailAddress() - return the return email address for notification emails
*
* @return string return email address
*/
function getReturnEmailAddress() {
global $sys_default_domain, $sys_use_gateways;
$address = '';
if($sys_use_gateways) {
$address .= $this->getUnixName();
} else {
$address .= 'noreply';
}
$address .= '@';
if($sys_use_gateways && isset($GLOBALS['sys_forum_return_domain'])) {
$address .= $GLOBALS['sys_forum_return_domain'];
} else {
$address .= $sys_default_domain;
}
return $address;
}
/**
* setMonitor - Add the current user to the list of people monitoring the forum.
*
* @return boolean success.
*/
function setMonitor() {
global $Language;
if (!session_loggedin()) {
$this->setError($Language->getText('forum_common','error_set_monitor'));
return false;
}
$sql="SELECT * FROM forum_monitored_forums
WHERE user_id='".user_getid()."' AND forum_id='".$this->getID()."';";
$result = db_query($sql);
if (!$result || db_numrows($result) < 1) {
/*
User is not already monitoring thread, so
insert a row so monitoring can begin
*/
$sql="INSERT INTO forum_monitored_forums (forum_id,user_id)
VALUES ('".$this->getID()."','".user_getid()."')";
$result = db_query($sql);
if (!$result) {
$this->setError($Language->getText('forum_common','error_unable_to_add_monitor').' : '.db_error());
return false;
}
}
return true;
}
/**
* stopMonitor - Remove the current user from the list of people monitoring the forum.
*
* @return boolean success.
*/
function stopMonitor() {
global $Language;
if (!session_loggedin()) {
$this->setError($Language->getText('forum_common','error_set_monitor'));
return false;
}
$sql="DELETE FROM forum_monitored_forums
WHERE user_id='".user_getid()."' AND forum_id='".$this->getID()."';";
return db_query($sql);
}
/**
* isMonitoring - See if the current user is in the list of people monitoring the forum.
*
* @return boolean is_monitoring.
*/
function isMonitoring() {
if (!session_loggedin()) {
return false;
}
$sql="SELECT count(*) FROM forum_monitored_forums WHERE user_id='".user_getid()."' AND forum_id='".$this->getID()."';";
$result = db_query($sql);
$row_count = db_fetch_array($result);
return $result && $row_count['count'] > 0;
}
/**
* savePlace - set a unix time into the database for this user, so future messages can be highlighted.
*
* @return boolean success.
*/
function savePlace() {
global $Language;
if (!session_loggedin()) {
$this->setError($Language->getText('forum_common','error_save_when_logged_in'));
return false;
}
$sql="SELECT * FROM forum_saved_place
WHERE user_id='".user_getid()."' AND forum_id='".$this->getID()."'";
$result = db_query($sql);
if (!$result || db_numrows($result) < 1) {
/*
User is not already monitoring thread, so
insert a row so monitoring can begin
*/
$sql="INSERT INTO forum_saved_place (forum_id,user_id,save_date)
VALUES ('".$this->getID()."','".user_getid()."','".time()."')";
$result = db_query($sql);
if (!$result) {
$this->setError($Language->getText('forum_common_forum','error_save_place').': '.db_error());
return false;
}
} else {
$sql="UPDATE forum_saved_place
SET save_date='".time()."'
WHERE user_id='".user_getid()."' AND forum_id='".$this->getID()."'";
$result = db_query($sql);
if (!$result) {
$this->setError('Forum::savePlace() '.db_error());
return false;
}
}
return true;
}
/**
* update - use this function to update an entry in the database.
*
* @param string The name of the forum.
* @param string The description of the forum.
* @param int Pass (1) if it should be public (0) for private.
* @param string The email address to send all new posts to.
* @param int Pass (1) if we should allow non-logged-in users to post (0) for mandatory login.
* @return boolean success.
*/
function update($forum_name,$description,$send_all_posts_to='') {
global $Language;
if (strlen($forum_name) < 3) {
$this->setError($Language->getText('forum_common','error_min_name_length'));
return false;
}
if (strlen($description) < 10) {
$this->setError($Language->getText('forum_common','error_min_desc_length'));
return false;
}
if (eregi('[^_\.0-9a-z-]',$forum_name)) {
$this->setError($Language->getText('forum_common','error_illegal_characters'));
return false;
}
if ($send_all_posts_to) {
$invalid_mails = validate_emails($send_all_posts_to);
if (count($invalid_mails) > 0) {
$this->setInvalidEmailError();
return false;
}
}
if (!$this->userIsAdmin()) {
$this->setPermissionDeniedError();
return false;
}
$res=db_query("UPDATE forum_group_list SET
forum_name='". strtolower($forum_name) ."',
description='". htmlspecialchars($description) ."',
send_all_posts_to='$send_all_posts_to'
WHERE group_id='".$this->Group->getID()."'
AND group_forum_id='".$this->getID()."'");
if (!$res || db_affected_rows($res) < 1) {
$this->setError($Language->getText('general','error_on_update').': '.db_error());
return false;
}
return true;
}
/**
* delete - delete this forum and all its related data.
*
* @param bool I'm Sure.
* @param bool I'm REALLY sure.
* @return bool true/false;
*/
function delete($sure, $really_sure) {
if (!$sure || !$really_sure) {
$this->setMissingParamsError();
return false;
}
if (!$this->userIsAdmin()) {
$this->setPermissionDeniedError();
return false;
}
db_begin();
db_query("DELETE FROM forum_agg_msg_count
WHERE group_forum_id='".$this->getID()."'");
//echo '1'.db_error();
db_query("DELETE FROM forum_monitored_forums
WHERE forum_id='".$this->getID()."'");
//echo '2'.db_error();
db_query("DELETE FROM forum_saved_place
WHERE forum_id='".$this->getID()."'");
//echo '3'.db_error();
db_query("DELETE FROM forum
WHERE group_forum_id='".$this->getID()."'");
//echo '4'.db_error();
db_query("DELETE FROM forum_group_list
WHERE group_forum_id='".$this->getID()."'");
//echo '5'.db_error();
db_commit();
return true;
}
/**
* addAllUsers - add all users to this forum.
*
* @return boolean success.
*/
function addAllUsers() {
global $sys_news_group;
if ($this->Group->getID() == $sys_news_group) {
return true;
}
if (!$this->userIsAdmin()) {
$this->setPermissionDeniedError();
return false;
}
$sql="INSERT INTO forum_perm (group_forum_id,user_id,perm_level)
SELECT '".$this->getID()."',user_id,forum_flags
FROM user_group
WHERE
group_id='".$this->Group->getID()."'
AND NOT EXISTS (SELECT user_id FROM forum_perm
WHERE group_forum_id='".$this->getID()."'
AND user_id=user_group.user_id);";
$res= db_query($sql);
if (!$res) {
$this->setError(db_error());
return false;
} else {
return true;
}
}
/**
* addUser - add a user to this subproject.
*
* @param int user_id of the new user.
* @return boolean success.
*/
function addUser($id) {
if (!$this->userIsAdmin()) {
$this->setPermissionDeniedError();
return false;
}
if (!$id) {
$this->setMissingParamsError();
return false;
}
$sql="SELECT * FROM forum_perm
WHERE group_forum_id='".$this->getID()."'
AND user_id='$id'";
$result=db_query($sql);
if (db_numrows($result) > 0) {
return true;
} else {
$sql="INSERT INTO forum_perm (group_forum_id,user_id,perm_level)
VALUES ('".$this->getID()."','$id',0)";
$result=db_query($sql);
if ($result && db_affected_rows($result) > 0) {
return true;
} else {
$this->setError(db_error());
return false;
}
}
}
/**
* updateUser - update a user's permissions.
*
* @param int user_id of the user to update.
* @param int (0) read only, (1) tech only, (2) admin & tech (3) admin only.
* @return boolean success.
*/
function updateUser($id,$perm_level) {
if (!$this->userIsAdmin()) {
$this->setPermissionDeniedError();
return false;
}
if (!$id) {
$this->setMissingParamsError();
return false;
}
//
// Update and test if it already exists
//
$sql="UPDATE forum_perm SET perm_level='$perm_level'
WHERE user_id='$id' AND group_forum_id='".$this->getID()."'";
$result=db_query($sql);
if (db_affected_rows($result) < 1) {
//
// If not, insert it.
//
$sql="INSERT INTO forum_perm (group_forum_id,user_id,perm_level) VALUES
('".$this->getID()."','$id','$perm_level')";
$result=db_query($sql);
if (!$result) {
$this->setError(db_error());
return false;
} else {
return true;
}
} else {
return true;
}
}
/**
* deleteUser - delete a user's permissions.
*
* @param int user_id of the user who's permissions to delete.
* @return boolean success.
*/
function deleteUser($id) {
if (!$this->userIsAdmin()) {
$this->setPermissionDeniedError();
return false;
}
if (!$id) {
$this->setMissingParamsError();
return false;
}
$sql="DELETE FROM forum_perm
WHERE user_id='$id' AND group_forum_id='".$this->getID()."'";
$result=db_query($sql);
if ($result) {
return true;
} else {
$this->setError(db_error());
return false;
}
}
/*
USER PERMISSION FUNCTIONS
*/
/**
* userCanView - determine if the user can view this subproject.
*
* @return boolean user_can_view.
*/
function userCanView() {
if ($this->isPublic()) {
return true;
} else {
if (!session_loggedin()) {
return false;
} else {
//
// You must have an entry in project_perm if this subproject is not public
//
if ($this->getCurrentUserPerm() >= 0) {
return true;
} else {
return false;
}
}
}
}
/**
* userCanPost - see if the logged-in user's perms are >= 1 or Group ForumAdmin.
*
* @return boolean user_can_post.
*/
function userCanPost() {
if (($this->isPublic() && $this->allowAnonymous()) || $this->userIsAdmin()) {
return true;
} elseif ($this->isPublic() && session_loggedin()) {
return true;
} else {
if (!session_loggedin()) {
return false;
} else {
if ($this->getCurrentUserPerm() >= 1) {
return true;
} else {
return false;
}
}
}
}
/**
* userIsAdmin - see if the logged-in user's perms are >= 2 or Group ForumAdmin.
*
* @return boolean user_is_admin.
*/
function userIsAdmin() {
if (!session_loggedin()) {
return false;
} else {
$perm =& $this->Group->getPermission( session_get_user() );
if (($this->getCurrentUserPerm() >= 2) || ($perm->isForumAdmin())) {
return true;
} else {
return false;
}
}
}
/**
* getCurrentUserPerm - get the logged-in user's perms from forum_perm.
*
* @return int perm level for the logged-in user.
*/
function getCurrentUserPerm() {
if (!session_loggedin()) {
return -1;
} else {
if (!isset($this->current_user_perm)) {
$sql="select perm_level
FROM forum_perm
WHERE group_forum_id='". $this->getID() ."'
AND user_id='".user_getid()."'";
$this->current_user_perm=db_result(db_query($sql),0,0);
}
return $this->current_user_perm;
}
}
}
?>
|