phpBB3 class

By sunslayer on Jun 12, 2010

phpBB class that retrieves common tasks
edit the private vars to your settings, this was tested using PHPBB3

<?php
    class PHPBB
    {
        private $phpbb_host = "localhost";
        private $phpbb_user = "root";
        private $phpbb_pass = "password";
        private $phpbb_dbname = "PHPBB";
        private $phpbb_prefix = "phpbb_";
        protected $phpbb_mysql;
        protected $phpbb_table;
        function __construct()
        {
            $this->phpbb_mysql = mysql_connect($this->phpbb_host,$this->phpbb_user,$this->phpbb_pass) 
                or die(printf('Error connecting to Database: %s',mysql_error()));
            mysql_select_db($this->phpbb_dbname,$this->phpbb_mysql);
        }
        public function allforums()
        {
            $this->phpbb_change_table("forums");
            return $this->phpbb_fetch("select `forum_name` from `$this->phpbb_table`");
        }
        public function allusers()
        {
            $this->phpbb_change_table("users");
            return $this->phpbb_fetch("select `username` from `$this->phpbb_table");
        }
        public function close(){ return mysql_close($this->phpbb_mysql); }
        public function GetForumID($fname)
        {
            $this->phpbb_change_table("forums");
            $sql=$this->phpbb_fetch("select `forum_id` from `$this->phpbb_table` where `forum_name` = '$fname'");
            return $sql[0]['forum_id'];
        }
        public function GetForumByID($fid)
        {
            $this->phpbb_change_table("forums");
            $sql=$this->phpbb_fetch("select `forum_name` from `$this->phpbb_table` where `forum_id` = '$fid'");
            return $sql[0]['forum_name'];
        }
        public function GetLastPost()
        {
            $this->phpbb_change_table("posts");
            $sql=$this->phpbb_fetch("select `forum_last_poster_name`,`forum_name`,`forum_last_post_time`,`forum_last_post_id` FROM `phpbb_forums` order by `forum_last_post_time` + 0 desc limit 1");
            $tname=$this->phpbb_fetch("select `topic_id` from `$this->phpbb_table` where `post_id` = ".$sql[0]['forum_last_post_id']);
            $sql[0]['topic_name']=$this->GetTopicByID($tname[0]['topic_id']);
            return $sql[0];
        }
        public function GetTopicByID($tid)
        {
            $this->phpbb_change_table("topics");
            $sql=$this->phpbb_fetch("select `topic_title` from `$this->phpbb_table` where `topic_id` = '$tid'");
            return $sql[0]['topic_title'];
        }
        public function GetUserByID($uid)
        {
            $this->phpbb_change_table("users");
            $sql=$this->phpbb_fetch("select `username` from `$this->phpbb_table` where `user_id` = '$uid'");
            return $sql[0]['username'];
        }
        public function GetUserID($username)
        {
            $this->phpbb_change_table("users");
            $sql=$this->phpbb_fetch("select `user_id` from `$this->phpbb_table` where `username` = '$username'");
            return $sql[0]['user_id'];
        }
        public function phpbb_array_rem($array)
        {
            foreach($array as $a => $b)
                if(!is_numeric($a)) $result[$a]=($b)?$b:"N/A";
            return $result;
        }
        public function phpbb_change_table($newtable){$this->phpbb_table=$this->phpbb_prefix.$newtable;}
        public function phpbb_fetch($sql)
        {
            if(!is_resource($sql))
                $sql=mysql_query($sql,$this->phpbb_mysql);
            while($row=mysql_fetch_array($sql))
                $results[]=$this->phpbb_array_rem($row);
            return $results;
        }
        public function phpbb_query($query){return $this->phpbb_fetch($query);}
    }
?>

Comments

Sign in to comment.
sunslayer   -  Jun 13, 2010

that still will only work if your in the phpbb dir
if this file is in ./stuff/classes then you can't include ./stuff/phpbb, i haven't found a solution yet so if you know how to that'd be great

 Respond  
sean   -  Jun 13, 2010

you can add this to be able to include the config.php regardless of your file's path

$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(FILE, '.'), 1);
include($phpbb_root_path . 'config.' . $phpEx);

 Respond  
sunslayer   -  Jun 13, 2010

i thought about including that file but depending on where this file is being called you will have to edit the include anyways which seemed like more of a hassle then just editing a few vars

 Respond  
sean   -  Jun 13, 2010

outside of slightly better and more consistent syntax, this is pretty handy - especially when creating phpBB mods. one thing i would change, phpBB3 has a config.php file that contains all database related configuration; including that file would prevent users from having to declare that information again here.

rated 5/10 for average

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.