Location: PHPKode > scripts > Forum Class > forum.php
<?php
/**
 * Forum class with admin defined main categories, user defined topics, posts and notifications.

CREATE TABLE IF NOT EXISTS `forum_categories` (
  `cid` int(5) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  `desc` varchar(300) NOT NULL,
  PRIMARY KEY  (`cid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `forum_notify` (
  `nid` int(5) NOT NULL auto_increment,
  `tid` int(5) NOT NULL,
  `cid` int(5) NOT NULL,
  `uid` int(5) NOT NULL,
  `email` varchar(50) NOT NULL,
  PRIMARY KEY  (`nid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `forum_posts` (
  `pid` int(5) NOT NULL auto_increment,
  `text` text NOT NULL,
  `tid` int(5) NOT NULL,
  `uid` int(5) NOT NULL,
  PRIMARY KEY  (`pid`),
  KEY `tid` (`tid`),
  KEY `uid` (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `forum_topics` (
  `tid` int(5) NOT NULL auto_increment,
  `name` varchar(100) NOT NULL,
  `desc` varchar(300) NOT NULL,
  `cid` int(5) NOT NULL,
  `uid` int(5) NOT NULL,
  PRIMARY KEY  (`tid`),
  KEY `cid` (`cid`),
  KEY `uid` (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

 *
 * @author Kristián Feldsam, please send donation to hide@address.com
 */
class forum {
    private $TABLE_PREFIX,$ADMIN,$UID,$USERNAME,$EMAIL,$notifyFromEmail,$notifySubject,$notifyCategoryText,$notifyTopicText;
    public $result_type;

    function __construct($uid,$username,$email,$admin=false,$configPath=null){
        require_once $configPath.'forum.config.php';
        $this->UID = $uid;
        $this->USERNAME = $username;
        $this->EMAIL = $email;
        $this->ADMIN = $admin;
        $this->TABLE_PREFIX = $config['table_prefix'];
        $this->setNotifyOptions($config['notifyFromEmail'],$config['notifySubject'],$config['notifyCategoryText'],$config['notifyTopicText']);
        $this->result_type = $config['result_type'];
    }
    
    function addCategory($name,$desc){
        if(!$this->checkPermissions('categories'))return '403';
        return mysql_query('INSERT INTO `'.$this->TABLE_PREFIX.'forum_categories` (`name`,`desc`) VALUES("'.$name.'","'.$desc.'")');
    }

    function editCategory($cid,$name,$desc){
        if(!$this->checkPermissions('categories'))return '403';
        return mysql_query('UPDATE `'.$this->TABLE_PREFIX.'forum_categories` SET `name`="'.$name.'", `desc`="'.$desc.'" WHERE `cid`="'.(int)$cid.'"');
    }

    function delCategory($cid){
        if(!$this->checkPermissions('categories'))return '403';
        $this->delFromNotifyList(Array('cid' => $cid));
		$result = mysql_query('SELECT `tid` FROM '.$this->TABLE_PREFIX.'forum_topics WHERE `cid`="'.(int)$cid.'"');
		while($data = mysql_fetch_array($result)){
			$this->delTopic($data['tid'],false);
		}
        return mysql_query('DELETE FROM `'.$this->TABLE_PREFIX.'forum_categories` WHERE `cid`="'.(int)$cid.'"');
    }

    function listCategories($id=null,$orderBy=null,$limit=null){
        if((int)$id)$whereClauses = ' WHERE `cid`="'.$id.'"';
        if($orderBy)$orderByClauses = ' ORDER BY '.$orderBy;
        if($limit)$limitClauses = ' LIMIT '.$limit;
        $result = mysql_query('SELECT `cid`,`name`,`desc` FROM `'.$this->TABLE_PREFIX.'forum_categories`'.$whereClauses.$orderByClauses.$limitClauses);
        while($data = mysql_fetch_array($result, $this->result_type)){
            $out[] = $data;
        }
        return $out;
    }

    function countCategories(){
        return mysql_fetch_row(mysql_query('SELECT count(`cid`) FROM `'.$this->TABLE_PREFIX.'forum_categories`'));
    }

    function addTopic($name,$desc,$cid,$notify=null){
        $result = mysql_query('INSERT INTO `'.$this->TABLE_PREFIX.'forum_topics` (`name`,`desc`,`cid`,`uid`) VALUES("'.$name.'","'.$desc.'","'.(int)$cid.'","'.$this->UID.'")');
        $insId = mysql_insert_id();
		if($notify && $result)$this->addToNotifyList($insId);
        if($result)$this->sendNotify('cid', $cid, $name.' - '.$desc);
        return $insId;
    }

    function editTopic($tid,$name,$desc,$cid=null){
        if(!$this->checkPermissions('topics', $tid))return '403';
        if((int)$cid)$changeCid = ', `cid`="'.$cid.'"';
        return mysql_query('UPDATE `'.$this->TABLE_PREFIX.'forum_topics` SET `name`="'.$name.'", `desc`="'.$desc.'"'.$changeCid.' WHERE `tid`="'.(int)$tid.'"');
    }

    function delTopic($tid,$checkPerm=true){
        if(!$this->checkPermissions('topics', $tid) && $checkPerm)return '403';
		$this->delFromNotifyList(Array('tid' => $tid));
		$result = mysql_query('SELECT `pid` FROM '.$this->TABLE_PREFIX.'forum_posts WHERE `tid`="'.(int)$tid.'"');
		while($data = mysql_fetch_array($result)){
			$this->delPost($data['pid'],false);
		}
        return mysql_query('DELETE FROM `'.$this->TABLE_PREFIX.'forum_topics` WHERE `tid`="'.(int)$tid.'"');
    }

    function listTopics($cid,$id=null,$orderBy=null,$limit=null){
        if((int)$id)$whereClauses = ' WHERE `tid`="'.$id.'"';
        if($orderBy)$orderByClauses = ' ORDER BY '.$orderBy;
        if($limit)$limitClauses = ' LIMIT '.$limit;
        $result = mysql_query('SELECT `tid`,`name`,`desc`,`uid`,`cid` FROM `'.$this->TABLE_PREFIX.'forum_topics`'.$whereClauses.$orderByClauses.$limitClauses);
        while($data = mysql_fetch_array($result, $this->result_type)){
            $out[] = $data;
        }
        return $out;
    }

    function countTopics($cid){
        return mysql_fetch_row(mysql_query('SELECT count(`tid`) FROM `'.$this->TABLE_PREFIX.'forum_topics` WHERE `cid`="'.(int)$cid.'"'));
    }

    function addPost($text,$tid,$notify=false){
		if(!(int)$tid || empty($text))return false;
        $result = mysql_query('INSERT INTO `'.$this->TABLE_PREFIX.'forum_posts` (`text`,`tid`,`uid`) VALUES("'.$text.'","'.(int)$tid.'","'.$this->UID.'")');
        if($notify && $result)$this->addToNotifyList($tid);
        if($result)$this->sendNotify('tid',$tid,$text);
        return $result;
    }

    function editPost($pid,$text,$tid=null){
        if(!$this->checkPermissions('posts', $pid))return '403';
        if((int)$tid)$changeTid = ', tid="'.$tid.'"';
        return mysql_query('UPDATE '.$this->TABLE_PREFIX.'forum_posts SET text="'.$text.'"'.$changeTid.' WHERE pid="'.(int)$pid.'"');
    }

    function delPost($pid,$checkPerm=true){
        if(!$this->checkPermissions('posts', $pid) && $checkPerm)return '403';
        return mysql_query('DELETE FROM '.$this->TABLE_PREFIX.'forum_posts WHERE pid="'.(int)$pid.'"');
    }

    function listPosts($tid,$orderBy=null,$limit=null){
        if($orderBy)$orderByClauses = ' ORDER BY '.$orderBy;
        if($limit)$limitClauses = ' LIMIT '.$limit;
        $result = mysql_query('SELECT `pid`,`text`,`tid`,`uid` FROM `'.$this->TABLE_PREFIX.'forum_posts` WHERE `tid`="'.(int)$tid.'"'.$orderByClauses.$limitClauses);
        while($data = mysql_fetch_array($result, $this->result_type)){
            $out[] = $data;
        }
        return $out;
    }

    function countPosts($tid){
        return mysql_fetch_row(mysql_query('SELECT count(`pid`) FROM `'.$this->TABLE_PREFIX.'forum_posts` WHERE `tid`="'.(int)$tid.'"'));
    }

    function addToNotifyList($tid=null,$cid=null){
        if(!(int)$tid && !(int)$cid)return false;
        return mysql_query('INSERT INTO `'.$this->TABLE_PREFIX.'forum_notify` (`uid`,`email`,`tid`,`cid`) VALUES("'.$this->UID.'","'.$this->EMAIL.'","'.(int)$tid.'","'.(int)$cid.'")');
    }

    function delFromNotifyList($id=Array()){
        if(!is_array($id))$id = Array('nid' => $id);
        if(!count($id))return false;
        foreach($id as $key => $val){
            $where[] = '`'.$key.'`="'.$val.'"';
        }
        $where = join(' AND ', $where);
        return mysql_query('DELETE FROM `'.$this->TABLE_PREFIX.'forum_notify` WHERE '.$where);
    }

    function sendNotify($mode,$id,$desc){
        if(!(int)$id)return false;
        switch ($mode){
            case 'tid':
                $where = '`tid`="'.$id.'"';
                $notifyText = $this->notifyTopicText;
                $result = mysql_query('SELECT `name` FROM `'.$this->TABLE_PREFIX.'forum_topics` WHERE `tid`="'.$id.'"');
                break;
            case 'cid':
                $where = '`cid`="'.$id.'"';
                $notifyText = $this->notifyCategoryText;
                $result = mysql_query('SELECT `name` FROM `'.$this->TABLE_PREFIX.'forum_categories` WHERE `cid`="'.$id.'"');
                break;
            default:
                return false;
        }
        $data = mysql_fetch_array($result);
        $notifyText = strtr($notifyText, Array('%user' => utf8_decode($this->USERNAME), '%desc' => utf8_decode($desc), '%name' => utf8_decode($data['name'])));
        $result = mysql_query('SELECT `email` FROM `'.$this->TABLE_PREFIX.'forum_notify` WHERE '.$where.' AND `uid`!="'.$this->UID.'"');
        while($data = mysql_fetch_array($result)){
            $mailer=new Mailer($this->notifyFromEmail,$this->notifyFromEmail,$data['email'],$this->notifySubject,$notifyText);
            $mailer->send();
        }
    }

    function setNotifyOptions($notifyFromEmail,$notifySubject,$notifyCategoryText,$notifyTopicText){
        $this->notifyFromEmail = $notifyFromEmail;
        $this->notifySubject = $notifySubject;
        $this->notifyCategoryText = $notifyCategoryText;
        $this->notifyTopicText = $notifyTopicText;
    }

    function checkPermissions($method,$id=null){
        switch ($method){
            case 'categories':
                if(!$this->ADMIN)return false;
                break;
            case 'topics':
                $checkUid = mysql_query('SELECT `tid` FROM `'.$this->TABLE_PREFIX.'forum_topics` WHERE `tid`="'.(int)$id.'" AND `uid`="'.$this->UID.'"');
                if(!$this->ADMIN && !mysql_num_rows($checkUid))return false;
                break;
            case 'posts':
                $checkUid = mysql_query('SELECT `pid` FROM `'.$this->TABLE_PREFIX.'forum_posts` WHERE `pid`="'.(int)$id.'" AND `uid`="'.$this->UID.'"');
                if(!$this->ADMIN && !mysql_num_rows($checkUid))return false;
                break;
            default:
                return false;
        }
        return true;
    }
}
?>
Return current item: Forum Class