Subversion Repositories taios

Rev

Rev 443 | Rev 451 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php

require '_config.php';

class Taios_Page
{
    function __construct($title, $url = "")
    {
        $this->title = $title;
        $this->url = $url;
       
        $this->drawnHeader = false;
        $this->drawnMiddle = false;
        $this->drawnFooter = false;
       
        $this->db = mysql_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PASSWORD);
        if (!$this->db)
        {
            $this->drawError('Failed to connect to database: ' . mysql_error());
        }
       
        if (!mysql_select_db('Tim32'))
        {
            $this->drawError('Failed to select database: ' . mysql_error());
        }
    }
   
    function drawHeader()
    {
        if (!$this->drawnHeader)
        {
            write('<!DOCTYPE html>');
            write('<html>');
            write('<head>');
            write('<meta http-equiv="Content-Type" content="text/html;charset=utf-8">');
            write('<title>Tim32 &middot; ' . $this->title . '</title>');
            write('<link href="' . $this->url . 'styles.css" rel="stylesheet" type="text/css" media="screen">');
            write('</head>');
            write('<body>');
            write('<div class="sidebar">');
            write('<div class="sidebar-header">');
            write('<h1>Tim32</h1>');
            write('</div>');
            write('<div class="sidebar-menu">');
            $this->drawMenuItem('Home', 'index.php');
            $this->drawMenuItem('Blog', 'blog/');
            $this->drawMenuItem('Projects', 'projects/');
            $this->drawMenuItem('Forums', 'forums/');
            $this->drawMenuItem('Wiki', 'wiki/');
            $this->drawMenuItem('Photos', 'photos/');
            write('<br />');
            if ($this->isLoggedIn())
            {
                $this->drawMenuItem('Administration', 'admin/');
                $this->drawMenuItem('Logout', 'logout-do.php');
            }
            else
            {
                $this->drawMenuItem('Login', 'login.php');
                $this->drawMenuItem('Register', 'register.php');
            }
            write('<br />');
           
            $this->drawnHeader = true;
        }
    }
   
    function drawMenuItem($t, $u)
    {
        write('<p><a href="' . $this->url . $u . '">' . $t . '</a></p>');
    }
   
    function drawMiddle()
    {
        if (!$this->drawnMiddle)
        {
            write('<br />');
            write('</div>');
            write('</div>');
            write('<div class="content">');
            write('<h2>' . $this->title . '</h2>');

            $this->drawnMiddle = true;
        }
    }
   
    function drawFooter()
    {
        if (!$this->drawnFooter)
        {
            write('<br /><p class="copyright">&copy; 2011 Tim32 &middot;</p>');
            write('</div>');
            write('</body>');
            write('</html>');
           
            $this->drawnFooter = true;
        }
       
        die();
    }
   
    function drawError($text, $die = true)
    {
        $this->drawHeader();
        $this->drawMiddle();
       
        write('<h4 style="color: red;">Error: ' . $text . '</h4>');
       
        if ($die)
        {
            $this->drawFooter();
            die();
        }
    }
   
    function drawBlogPostTree($id, $first = false)
    {
        $post = $this->getBlogPost($id);
        if ($first)
        {
            write('<h3><a href="post.php?id=' . $id . '">' . $post->title. '</a> <a href="post.php?id=' . $post->parent->ID . '">^</a></h3>');
        }
        else
        {
            write('<a href="post.php?id=' . $id . '"><h3>' . $post->title. '</h3></a>');
        }
        write('<h5 style="color: #666666;">Posted On ' . date('l j F Y', $post->datePosted) . ' by ' . $post->user->name . ' (' . $post->user->username . ')</h5>');
        write('<p>' . $this->replaceBBCode($post->content) . '</p>');

        if ($this->isUserNormal($this->getLoggedInUser()))
        {
            echo '<p class="bold"><a href="add-post.php?id=' . $id . '">Add Comment</a>';
            if ($this->isUserAdmin($this->getLoggedInUser()) || $this->getLoggedInUser()->ID == $post->author->ID)
            {
                echo ' &nbsp; &middot &nbsp; <a href="edit-post.php?id=' . $id . '">Edit Post</a>';
                echo ' &nbsp; &middot &nbsp; <a href="del-post.php?id=' . $id . '">Delete Post</a>';
            }
            write('</p><br />');
        }

        $ids = $this->findIDs('BlogPosts', 'WHERE ParentID=' . $id);
        for ($i = 0; $i < count($ids); $i++)
        {
            write('<div class="indent">');
            $this->drawBlogPostTree($ids[$i]);
            write('</div>');
        }
    }
   
    function drawBlogCategoriesMenu()
    {
        $cats = array();
       
        $ids = $this->findIDs('BlogPosts', 'WHERE ParentID = -1');
        for ($i = 0; $i < count($ids); $i++)
        {
            $cat = $this->getBlogPost($ids[$i])->category;
            if (!in_array($cat, $cats))
            {
                array_push($cats, $cat);
            }
        }
       
        write('<h3>Categories</h3>');
        for ($i = 0; $i < count($cats); $i++)
        {
            $this->drawMenuItem($cats[$i], 'blog/index.php?cat=' . $cats[$i]);
        }
    }
   
    function replaceBBCode($str)
    {
        /*$newstrarray = explode("\n", $str);
        $newstr = "";
        foreach ($newstrarray as $line)
        {
            if ($line == "\n" || $line == " \n" || $line == "\n " || $line == "\n\r")
            {
                $line = "</p><p>";
            }
           
            $newstr .= ($line . "\n");
        }*/

       
        $newstr = $str;    
        $newstr = str_replace("<", "[", $newstr);
        $newstr = str_replace(">", "]", $newstr);
        $newstr = str_replace("\n", "</p><p>", $newstr);
        $newstr = str_replace("\\'", "'", $newstr);
        $newstr = str_replace("\\\"",'"', $newstr);
        $newstr = str_replace('  ', '&nbsp;&nbsp;', $newstr);
        $newstr = str_replace(' :)', ' <img src="' . $this->url . 'data/smilies/face-smile.png" class="smiley" />', $newstr);
        $newstr = str_replace(' :p', ' <img src="' . $this->url . 'data/smilies/face-raspberry.png" class="smiley" />', $newstr);
        $newstr = str_replace(' :P', ' <img src="' . $this->url . 'data/smilies/face-raspberry.png" class="smiley" />',$newstr);
        $newstr = str_replace(' :|', ' <img src="' . $this->url . 'data/smilies/face-plain.png" class="smiley" />',$newstr);
        $newstr = str_replace(' :D', ' <img src="' . $this->url . 'data/smilies/face-laugh.png" class="smiley" />',$newstr);
        $newstr = str_replace(' =D', ' <img src="' . $this->url . 'data/smilies/face-laugh.png" class="smiley" />',$newstr);
        $newstr = str_replace(' :(', ' <img src="' . $this->url . 'data/smilies/face-sad.png" class="smiley" />',$newstr);
        $newstr = str_replace(' :0', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" class="smiley" />',$newstr);
        $newstr = str_replace(' :o', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" class="smiley" />',$newstr);
        $newstr = str_replace(' :O', ' <img src="' . $this->url . 'data/smilies/face-surprise.png" class="smiley" />',$newstr);
        $newstr = str_replace(' :/', ' <img src="' . $this->url . 'data/smilies/face-uncertain.png" class="smiley" />',$newstr);
        $newstr = str_replace(' ;)', ' <img src="' . $this->url . 'data/smilies/face-wink.png" class="smiley" />',$newstr);
       
        $bbcode = array(
        '/\[b\](.+?)\[\/b\]/is',
        '/\[i\](.+?)\[\/i\]/is',
        '/\[u\](.+?)\[\/u\]/is',
        '/\[url\](.+?)\[\/url\]/is',
        '/\[url=(.+?)\](.+?)\[\/url\]/is',
        '/\[code\](.+?)\[\/code\]/is',
        '/\[img\](.+?)\[\/img\]/is',
        '/\[ul\](.+?)\[\/ul\]/is',
        '/\[ol\](.+?)\[\/ol\]/is',
        '/\[li\](.+?)\[\/li\]/is',
        '/\[pre\](.+?)\[\/pre\]/is'
        );

        $html = array(
        '<b>$1</b>',
        '<i>$1</i>',
        '<u>$1</u>',
        '<a href="$1">$1</a>',
        '<a href="$1">$2</a>',
        '<div class="code">$1</div>',
        '<img src="$1" />',
        '<ul>$1</ul>',
        '<ol>$1</ol>',
        '<li>$1</li>',
        '<pre>$1</pre>',
        );

        $newstr = preg_replace($bbcode, $html, $newstr);
       
        return $newstr;
    }

    function redirect($u)
    {
        header('Location: ' . $u);
        die();
    }
   
    function isLoggedIn()
    {
        $cookie = $_COOKIE['Tim32_Login'];
        if (!empty($cookie))
        {
            $clist = explode('|~|', $cookie);
            $user = $this->getUserByUsername($clist[0]);
            if ($user)
            {
                if ($user->password == $clist[1])
                {
                    return true;
                }
            }
        }
       
        return false;
    }
   
    function isUserAdmin()
    {
        if ($this->isLoggedIn())
        {
            if ($this->getLoggedInUser()->accessID <= 0)
            {
                return true;
            }
        }
       
        return false;
    }
   
    function isUserGM()
    {
        if ($this->isLoggedIn())
        {
            if ($this->getLoggedInUser()->accessID <= 1)
            {
                return true;
            }
        }
       
        return false;
    }
   
    function isUserNormal()
    {
        if ($this->isLoggedIn())
        {
            if ($this->getLoggedInUser()->accessID <= 2)
            {
                return true;
            }
        }
       
        return false;
    }
   
    function checkChallengeStatus($challengeID, $previous, $next)
    {
        $currentChallengeID = $this->getLoggedInUser()->challengeID;
       
        if (!$this->isLoggedIn())
        {
            $this->redirect('index.php');
        }
        else if ($currentChallengeID > $challengeID)
        {
            $this->redirect($next . '.php');
        }
        else if ($currentChallengeID < $challengeID)
        {
            $this->redirect($previous . '.php');
        }
    }
   
    function checkLoggedIn()
    {
        if (!$this->isLoggedIn())
        {
            $this->drawError('You need to be logged in.');
        }
    }
   
    function query($query)
    {
        $result = mysql_query($query);
        if (!$result)
        {
            $this->drawError('Query Failed: ' . $query . "\n" . 'MySQL Error: ' . mysql_error());
        }
       
        return $result;
    }
   
    function findIDs($table, $query = '')
    {
        $array = array();
       
        $result = $this->query('SELECT ID FROM ' . $table . ' ' . $query);
        while ($row = mysql_fetch_array($result))
        {
            array_push($array, $row['ID']);
        }
       
        return $array;
    }
   
    function getUserByID($id)
    {
        $result = $this->query('SELECT * FROM Users WHERE ID = ' . $id);
        while ($row = mysql_fetch_array($result))
        {
            $user = new User;
            $user->ID = $row['ID'];
            $user->accessID = $row['AccessID'];
            $user->username = $row['Username'];
            $user->password = $row['Password'];
            $user->emailAddress = $row['EmailAddress'];
            $user->name = $row['Name'];
            $user->challengeID = $row['ChallengeID'];
           
            return $user;
        }
       
        return false;
    }
   
    function getUserByUsername($username)
    {
        $result = $this->query('SELECT * FROM Users WHERE Username = "' . $username . '"');
        while ($row = mysql_fetch_array($result))
        {
            return $this->getUserByID($row['ID']);
        }
       
        return false;
    }
   
    function getLoggedInUser()
    {
        if ($this->isLoggedIn())
        {
            $clist = explode('|~|', $_COOKIE['Tim32_Login']);
            return $this->getUserByUsername($clist[0]);
        }
       
        return false;
    }
   
    function getBlogPost($id)
    {
        $result = $this->query('SELECT * FROM BlogPosts WHERE ID = ' . $id);
        while ($row = mysql_fetch_array($result))
        {
            $post = new BlogPost;
            $post->ID = $row['ID'];
            if ($row['ParentID'] == -1)
            {
                $post->parent = -1;
            }
            else
            {
                $post->parent = $this->getBlogPost($row['ParentID']);
            }
            $post->author = $this->getUserByID($row['AuthorID']);
            $post->user = $this->getUserByID($row['AuthorID']); // For some older pages
            $post->title = $row['Title'];
            $post->content = $row['Content'];
            $post->datePosted = strtotime($row['DatePosted']);
            $post->category = $row['Category'];
            $post->spam = $row['Spam'];
           
            return $post;
        }
       
        $this->drawError('Cannot find blog post, #' . $id);
    }
   
    function getProject($id)
    {
        $result = $this->query('SELECT * FROM Projects WHERE ID = ' . $id);
        while ($row = mysql_fetch_array($result))
        {
            $project = new Project;
           
            $project->ID = $row['ID'];  
            $project->author = $this->getUserByID($row['AuthorID']);
            $project->title = $row['Title'];
            $project->description = $row['Description'];
            $project->logoURL = $row['LogoURL'];
            $project->downloadURL = $row['DownloadURL'];
            $project->websiteURL = $row['WebsiteURL'];
            $project->latestVersion = $row['LatestVersion'];
            $project->lastUpdate = strtotime($row['LastUpdate']);          
           
            return $project;
        }
       
        return false;
    }
   
    function getForumCategory($id)
    {
        $result = $this->query('SELECT * FROM ForumCategories WHERE ID = ' . $id);
        while ($row = mysql_fetch_array($result))
        {
            $f = new ForumCategory;
           
            $f->ID = $row['ID'];
            $f->parent = $this->getForumCategory($row['ParentID']);
            $f->title = $row['Title'];
            $f->description = $row['Description'];
           
            return $f;
        }
       
        return false;
    }
   
    function getForumPost($id)
    {
        $result = $this->query('SELECT * FROM ForumPosts WHERE ID = ' . $id);
        while ($row = mysql_fetch_array($result))
        {
            $f = new ForumPost;
           
            $f->ID = $row['ID'];
            $f->author = $this->getUserByID($row['AuthorID']);
            $f->category = $this->getForumCategory($row['CategoryID']);
            $f->parent = $this->getForumPost($row['ParentID']);
            $f->title = $row['Title'];
            $f->content = $row['Content'];
            $f->datePosted = strtotime($row['DatePosted']);
            $f->spam = $row['Spam'];
           
            return $f;
        }
       
        return false;
    }
   
    function delBlogPost($id)
    {
        $ids = $this->findIDs('BlogPosts', 'WHERE ParentID=' . $id);
        for ($i = 0; $i < count($ids); $i++)
        {
            $this->delBlogPost($ids[$i]);
        }

        $this->query('DELETE FROM BlogPosts WHERE ID=' . $id);
    }

    function getGetID()
    {
        $id = $_GET['id'];
        if (empty($id))
        {
            $id = 1;
        }
       
        return $id;
    }
   
    function getPostID()
    {
        $id = $_POST['id'];
        if (empty($id))
        {
            $id = 1;
        }
       
        return $id;
    }

}

class User
{
    public $ID;
    public $accessID;
    public $username;
    public $password;
    public $emailAddress;
    public $name;

    public $challengeID;
}

class BlogPost
{
    public $ID;
    public $parent;
    public $author;
    public $title;
    public $content;
    public $datePosted;
    public $category;
    public $spam;
}

class Project
{
    public $ID;
    public $author;
    public $title;
    public $description;


    public $logoURL;
    public $downloadURL;
    public $websiteURL;
    public $latestVersion;
    public $lastUpdate;
}

class ForumCategory
{
    public $ID;
    public $parent;
    public $title;
    public $description;

}

class ForumPost
{
    public $id;
    public $author;
    public $category;
    public $parent;
    public $title;
    public $content;
    public $datePosted;
    public $spam;
}

function write($str)
{
    echo $str;
    echo "\n";
}

?>