Subversion Repositories taios

Rev

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

<?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('localhost', 'root', 'puppylinux');
        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('Manage Account', 'admim/?id=' . $this->getLoggedInUser()->ID);
                $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 . '"</a>' . $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('</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 redirect($url)
    {
        header('Location: ' . $url);
        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 query($query)
    {
        $result = mysql_query($query);
        if (!$result)
        {
            $this->drawError('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'];
            $post->user = $this->getUserByID($row['AuthorID']);
            $post->title = $row['Title'];
            $post->content = $row['Content'];
            $post->datePosted = strtotime($row['DatePosted']);
            $post->category = $row['Category'];
           
            return $post;
        }
       
        $this->drawError('Cannot find blog post, #' . $id);
    }
}

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

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

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

?>