Skip to content

Instantly share code, notes, and snippets.

@mrdaemon
Created January 23, 2011 09:54
Show Gist options
  • Save mrdaemon/791949 to your computer and use it in GitHub Desktop.
Save mrdaemon/791949 to your computer and use it in GitHub Desktop.
The KnifaCode (tm)
<?php
/**
* Design Module.
*
* @package Module
* @subpackage Design
*/
/**
* Include System stuff.
*/
require_once( "include/System.inc.php" );
/**
* Design Module
*
* Currently provides functions to print out the header and footer of the page, leaving the
* content div to be wrote into.
*
* @package Module
* @subpackage Design
*/
class CDesignModule extends CSystemModule
{
/**
* Current page title.
* @var string
*/
var $pageTitle;
/**
* Page started has started?
* @var bool
*/
var $pageStarted;
/**
* Start Page
*
* Prints out the header and shit down to the content div.
*
* @param string $pageTitle The title of the page.
*/
function StartPage( $pageTitle = "" )
{
// Set the page title.
if ( $pageTitle )
$this->pageTitle = "Glasnost :: " . $pageTitle;
// Page has started
$this->pageStarted = true;
require( "include/Design/pageHeader.inc.php" );
}
/**
* End Page
*
* Closes off the page printing footer etc.
*/
function EndPage()
{
// Page ended.
$this->pageStarted = false;
require( "include/Design/pageFooter.inc.php");
}
/**
* Constructor
*
* Initalize various thingymajigs such as the page title.
*/
function __construct()
{
// Set default page title.
$this->pageTitle = "Loliserv";
$this->pageStarted = false;
}
}
?>
<?php
/**
* Error Module Definitions.
*
* @package Module
* @subpackage Error
*/
/**
* Include System stuff.
*/
require_once( "include/System.inc.php" );
/**
* Error Module.
*
* This does various error reporting functions such as stopping the execution of a script and
* displaying a nice message for the user. The user and system error functions are pretty much
* identical right now but I'm keeping them seperate incase I need to do something else.
*
* @package Module
* @subpackage Error
*/
class CErrorModule extends CSystemModule
{
/**
* Displays a user error message.
*
* This displays an error the user and stops the script. This should be used for things like
* when a user enters a message too long and you want to tell them that and stop the script.
*
* @param string $errorMessage A message to display to the user and log in the database.
* @param bool $haltScript If we should stop the entire script or not.
*/
function UserError( $errorMessage, $haltScript=true )
{
// Gay
global $System;
// Print the message.
$this->DisplayError( $errorMessage, "Oops." );
// Halt the script if we are told to.
if ( $haltScript )
// End the page
$System->Design->EndPage();
die();
}
/**
* Displays a system error.
*
* This prints out and logs and error message, as the title shows.
*
* @param string $errorMessage The error message.
* @param bool $haltScript If we should stop the script or not.
*/
function SystemError( $errorMessage, $haltScript = true )
{
// Gay
global $System;
// Get the error filename. Isn't particulary reliable.
$errorFile = $_SERVER["PHP_SELF"];
// Display the error
$this->DisplayError( $errorMessage, "System Error", $errorFile );
if ( $haltScript )
// End the page
$System->Design->EndPage();
die();
}
/**
* Error Display Function.
*
* So I don't need to keep writing this shit out over and over. Doesn't do anything but print
* out the error.
*
* @param string $errorMessage The actual error message.
* @param string $errorTitle The title for the error message.
* @param string $errorInfo Misc info that will show up underneath.
*/
function DisplayError( $errorMessage, $errorTitle="Error", $errorInfo="" )
{
// >:E
global $System;
// Start the page if it has not been already.
if ( !$System->Design->pageStarted )
$System->Design->StartPage();
// Buh.
echo "<div class='Error'>";
// Print it out.
echo "<h1>$errorTitle</h1>" .
"$errorMessage";
// Also print out misc info if any was sent.
if ( $errorInfo )
echo "<br /><em>($errorInfo)</em>";
echo "</div>";
}
}
?>
<?php
/**
* System class and other things relating to it.
*
* This page contains the System class definition which contains all things neccesarry to make
* applications for the site. Also contains the class module which classes should be based off
* of.
*
* A global System object called $System is created when the page is included so that it can be
* used.
*
* @package System
*
*/
/**
* Config file.
*/
require_once( "include/Config.inc.php" );
require_once( "include/SystemModule.inc.php" );
require_once( "include/SQL/SQLModule.inc.php" );
require_once( "include/Error/ErrorModule.inc.php" );
require_once( "include/Form/FormModule.inc.php" );
require_once( "include/User/UserModule.inc.php" );
require_once( "include/File/FileModule.inc.php" );
require_once( "include/Image/ImageModule.inc.php" );
require_once( "include/Design/DesignModule.inc.php" );
/**
* Main System Class.
*
* This is the main system class. All of the other moduley bits such as SQL functions will be in
* here. It is neater.
*
* Including this page will create a System object called $System to be used.
*
*
* @package System
*/
class CSystem
{
var $SQL; // SQL module object.
var $Error; // Error reporting module.
var $Form; // Forms module
var $User; // User module.
var $File; // FILE MODULE DESU.
var $Image; // image modulleee
var $Design; // page design shite.
/**
* Create new instances of all of the modules to be used.
*/
function Init()
{
// Start sessions.
session_start();
$this->Error = new CErrorModule;
$this->SQL = new CSQLModule;
$this->User = new CUserModule;
$this->Form = new CFormModule;
$this->File = new CFileModule;
$this->Image = new CImageModule;
$this->Design = new CDesignModule;
}
}
/**
* System object to be used in the applications.
* @global CSystem $System
* @name System
*/
$System = new CSystem;
$System->Init();
?>
<?php
/**
* User Module Definitions.
*
* @package Module
* @subpackage User
*/
/**
* Include System stuff.
*/
require_once( "include/System.inc.php" );
/**
* User Module.
*
* Provides functions that allow you to manipulate and get data about usery people things and
* stuff like that.
*
* @package Module
* @subpackage User
*/
class CUserModule extends CSystemModule
{
/**
* The current logged in user
* @var CUserInfo
*/
var $currentUser;
/**
* Checks if a user exists or not.
*
* This function checks if a user exists or not, as the descrption suggests. Takes in the ID
* or name and then returns true/false depending on if they exist or not. Simple. Also only
* pick one or the other.
*
* @todo Find a better way of seeing if it's a username sent or not. Regex?
*
* @param int|string $userSearch The ID or name to search for.
*
* @return bool True if the user exists, false if not.
*/
function CheckExists( $userSearch )
{
// Gay scope is gay.
global $System;
// use two diffrent queries if we have a numeric argument or not
if ( is_numeric( $userSearch ) )
$userQuery = $System->SQL->Query( "SELECT * FROM User WHERE ID=$userSearch" );
else
$userQuery = $System->SQL->Query( "SELECT * FROM User WHERE Username='$userSearch'" );
// if the number of rows found is over 1, return true, else false.
if ( $System->SQL->GetNumRows($userQuery) > 0 )
return true;
// else not.
return false;
}
/**
* Returns a link to the user's profile from an ID or name.
*
* Prints out a link to a user's page so you don't have to do it over and over.
*
* @param int|string $userSearch The ID or name to search for.
*/
function UserPageLink( $userSearch )
{
if ( !$this->CheckExists( $userSearch ) )
return;
$userData = $this->GetUserInfo( $userSearch );
return "<a class='User' href='userView.php?id=" . $userData->userID . "'>" .
stripslashes( $userData->userName ) . "</a>";
}
/**
* Gets the info about a user.
*
* Returns a CUserInfo object filled with the user's basic data, all this does is basically
* gets that object.
*
* @param int|string $userSearch The ID or name to search for.
*
* @return CUserInfo The user info object with the user's stuff in it.
*/
function GetUserInfo( $userSearch )
{
$tempInfo = new CUserInfo( $userSearch );
return $tempInfo;
}
/**
* Constructor.
*/
function __construct()
{
// Lolwut
global $System;
// Check if there is the auto login cookie!
$autoLogin = $_COOKIE["User_AutoLogin"];
if ( $autoLogin )
{
$userName = $_COOKIE["User_AutoLogin_User"];
$pass = $_COOKIE["User_AutoLogin_Pass"];
// Query to see if the username and password is valid.
$validQuery = $System->SQL->Query( "SELECT id FROM User WHERE Username='$userName' AND Password='$pass'" );
$validNum = $System->SQL->GetNumRows( $validQuery ); // will be > 0 if detials are valid
// Valid login?
if ( $validNum > 0 )
{
// Get the user's ID.
$userID = $System->SQL->GetResult( $validQuery );
// Set the session
$_SESSION["User_currentUserID"] = $userID;
}
}
// Grab the data for the current logged in user, if they are logged in.
if ( $_SESSION["User_currentUserID"] )
$this->currentUser = $this->GetUserInfo( $_SESSION["User_currentUserID"] );
}
}
/**
* User Information class.
*
* Holds basic information about the user such as name, id and e-mail. It is easier to work this
* way you see.
*
* @package Module
* @subpackage User
*/
class CUserInfo
{
/**
* Flag that tell us if the user is the one that is currently logged in.
* @var bool
*/
var $loggedIn;
/**
* Flag that tells us if the user is an admin.
* @var bool
*/
var $admin;
/**
* User's ID.
* @var int
*/
var $userID;
/**
* User's name.
* @var string
*/
var $userName;
/**
* User's password (MD5).
* @var string
*/
var $userPass;
/**
* User's e-mail.
* @var string
*/
var $userEMail;
/**
* User's theme
* @var string
*/
var $theme;
/**
* Should show NSFW images?
* @var bool
*/
var $showNSFW;
/**
* Constructor automagically gets the user information and fills it all out.
*
* Takes either an ID or a name.
*
* @param int|string $userSearch The ID or name of the user.
*/
function __construct( $userSearch )
{
// dumb variable scope is dumb
global $System;
// use two diffrent queries if we have a numeric argument or not
if ( is_numeric( $userSearch ) )
$userQuery = $System->SQL->Query( "SELECT ID,Username,Password,EMail,Admin,Theme,ShowNSFW FROM User WHERE ID=$userSearch" );
else
$userQuery = $System->SQL->Query( "SELECT ID,Username,Password,EMail,Admin,Theme,ShowNSFW FROM User WHERE Username='$userSearch'" );
// Check if there was actually any rows sent. We could use the function in the user module
// to do this but since we've already queried there is no point in doing another one.
if ( $System->SQL->GetNumRows( $userQuery ) < 1 )
// $System->Error->SystemError( "No user found.", false ); -- Noisy.
return;
// Get and set the datas.
$this->userID = $System->SQL->GetResult( $userQuery, 0, 0 );
$this->userName = $System->SQL->GetResult( $userQuery, 0, 1 );
$this->userPass = $System->SQL->GetResult( $userQuery, 0, 2 );
$this->userEMail = $System->SQL->GetResult( $userQuery, 0, 3 );
$this->admin = $System->SQL->GetResult( $userQuery, 0, 4 );
$this->theme = $System->SQL->GetResult( $userQuery, 0, 5 );
$this->showNSFW = $System->SQL->GetResult( $userQuery, 0, 6 );
// Also set the logged in flag if they are logged in.
if ( $_SESSION["User_currentUserID"] == $this->userID )
$this->loggedIn = true;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment