Sample Code
functions.php
<?php
//------------------------
//-- Functions
//-- functions.php
//-- © Gregory Koberger 2007
//-- gkoberger@gmail.com
//------------------------
//------------------
//-- Functions class
//------------------
class Functions
{
function connect()
{
global $mysqlHost, $mysqlUser, $mysqlPass, $mysqlDb;
//---------------------------------
//-- This isn't used, but I wanted
//-- to demonstrate how the program
//-- would connect
//---------------------------------
mysql_connect("$mysqlHost", $mysqlUser, $mysqlPass);
mysql_select_db($mysqlDb);
}
function outputSkin ($output, $file)
{
//-- Open the html file
if(file_exists($file))
{
$base = substr( $_SERVER["SCRIPT_FILENAME"], 0, strrpos( $_SERVER["SCRIPT_FILENAME"], "/" ) + 1 );
$handle = fopen( $base . $file, "rb" );
$contents = '';
while (!feof($handle) && ! $stop)
{
$contents .= fread($handle, 8192);
$count++;
if($count > 100) $stop = true;
}
fclose($handle);
}
else
{
$contents = "<b>Error</b>: Page not found";
}
//-- Replace the variables in the html file
foreach($output as $key=>$html)
{
$contents = str_replace("(((" . $key . ")))", $html, $contents);
}
//-- Return the output
return $contents;
}
function verifyInput( $input, $type = "text" )
{
//------------------------------
//-- This just checks for random
//-- things to demonstrate how
//-- it validates stuff
//------------------------------
$output = "";
//-- Make sure it's longer than 10 chars
if (strlen( $input ) <= 10)
{
$output .= "<div class=\"error\">Must be at least 10 characters long</div>";
}
//-- Check for HTML
if ( $input != strip_tags( $input ) )
{
$output .= "<div class=\"error\">HTML is not allowed</div>";
}
//-- Return errors
if ($type == "boolean")
{
if ($output == "")
{
return true;
}
else
{
return false;
}
}
else
{
return $output;
}
}
}
$functions = new Functions;
//--------------
//-- Pages class
//--------------
class Pages
{
function main ()
{
global $pages, $functions;
//-- Set up default values
$output["title-val"] = "";
$output["desc-val"] = "";
$output["title-error"] = "";
$output["desc-error"] = "";
$output["message"] = "";
$textOutput = "";
//-- Was the form submitted?
if ($_POST["submit"] && $functions->verifyInput($_POST["title"], "boolean") && $functions->verifyInput($_POST["desc"], "boolean"))
{
//-- Submitted, no errors
$output["message"] = "<div style=\"text-align:center;\"><font color=\"green\">Success! Article added</font></div><br />";
}
elseif ($_POST["submit"])
{
//-- Submitted, with errors
$output["message"] = "<div style=\"text-align:center;\"><font color=\"red\">Errors were found</font></div><br />";
$output["title-val"] = $_POST["title"];
$output["desc-val"] = $_POST["desc"];
//-- Errors with title?
if ( ! $functions->verifyInput($_POST["title"], "boolean") )
{
$output["title-error"] = $functions->verifyInput($_POST["title"]);
}
if ( ! $functions->verifyInput($_POST["desc"], "boolean") )
{
$output["desc-error"] = $functions->verifyInput($_POST["desc"]);
}
}
$textOutput .= $functions->outputSkin ($output, "form.html");
return $textOutput;
}
function showFile ( $file )
{
global $functions;
//-- Open the file
if(file_exists($file))
{
$base = substr( $_SERVER["SCRIPT_FILENAME"], 0, strrpos( $_SERVER["SCRIPT_FILENAME"], "/" ) + 1 );
$handle = fopen($base . $file, "rb");
$contents = '';
while (!feof($handle) && ! $stop)
{
$contents .= fread($handle, 8192);
$count++;
if($count > 100) $stop = true;
}
fclose($handle);
}
else
{
$contents = "<b>Error</b>: Page not found";
}
//-- Display as text, not HTML
$code = str_replace("<", "<", $contents);
$code = str_replace(">", ">", $code);
$code = str_replace(" ", " ", $code);
$code = nl2br($code);
//-- Don't replace variables
$code = str_replace("(((", "(<!-- -->((", $code);
//-- Return the file
$output["code"] = $code;
$output["file"] = $file;
return $functions->outputSkin ($output, "code.html");
}
}
$pages = new Pages;
?>