Initial import.

git-svn-id: http://svn.cleancode.org/svn/pickles@1 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
Josh Sherman 2007-10-06 17:48:52 +00:00
commit 4bedbb02be
6 changed files with 309 additions and 0 deletions

16
classes/Buffer.php Normal file
View file

@ -0,0 +1,16 @@
<?php
class Buffer {
public static function get() {
$buffer = str_replace(
array(' ', "\r\n", "\n", "\t"),
null,
ob_get_contents()
);
ob_end_clean();
return $buffer;
}
}
?>

39
classes/Config.php Executable file
View file

@ -0,0 +1,39 @@
<?php
class Config {
private static $data;
public static function load($site) {
// @todo no hardcoded paths!
$file = "/home/websites/111110/common/config/{$site}.ini";
if (file_exists($file)) {
self::$data =& parse_ini_file($file, true);
}
else {
Error::addError('Unable to load the configuration file');
return false;
}
return true;
}
public static function get($variable, $section = null) {
return isset($section) ? self::$data[$section][$variable] : self::$data[$variable];
}
public static function enableDebug() {
self::$data['debug'] = true;
}
public static function disableDebug() {
self::$data['debug'] = false;
}
public static function getDebug() {
return self::get('debug');
}
}
?>

175
classes/DB.php Executable file
View file

@ -0,0 +1,175 @@
<?php
class DB {
private static $hostname;
private static $username;
private static $password;
private static $database;
private static $connection;
private static $results;
public static function open() {
self::$hostname =& Config::get('hostname', 'database');
self::$username =& Config::get('username', 'database');
self::$password =& Config::get('password', 'database');
self::$database =& Config::get('database', 'database');
if (isset(self::$hostname) && isset(self::$username) && isset(self::$password) && isset(self::$database)) {
self::$connection = @mysql_connect(self::$hostname, self::$username, self::$password);
if (is_resource(self::$connection)) {
if (!mysql_select_db(self::$database, self::$connection)) {
Error::addWarning("There was an error selecting the '" . self::$database , "' database");
return false;
}
else {
return true;
}
}
else {
Error::addError('There was an error connecting to the database server');
}
return false;
}
else {
Error::addError('There was an error loading the configuration');
}
return false;
}
public static function close() {
if (is_resource(self::$connection)) {
return mysql_close(self::$connection);
}
return false;
}
public static function execute($sql) {
if (!is_resource(self::$connection)) {
self::open();
}
if (trim($sql) != '') {
self::$results = @mysql_query($sql, self::$connection);
if (empty(self::$results)) {
Error::addError('There was an error executing the SQL');
}
else {
return true;
}
}
else {
Error::addWarning('There was no SQL to execute');
}
return false;
}
public static function getField($sql = null) {
if (isset($sql)) {
self::execute($sql);
}
if (is_resource(self::$results)) {
$results = @mysql_fetch_row(self::$results);
if (is_array($results)) {
return $results[0];
}
else {
Error::addWarning('There is nothing to return');
}
}
else {
Error::addError('There is no valid MySQL result resource');
}
return false;
}
public static function getRow($sql = null) {
if (isset($sql)) {
self::execute($sql);
}
if (is_resource(self::$results)) {
$results = @mysql_fetch_assoc(self::$results);
if (is_array($results)) {
return $results;
}
else {
Error::addWarning('There is nothing to return');
}
}
else {
Error::addError('There is no valid MySQL result resource');
}
return false;
}
public static function getArray($sql = null) {
if (isset($sql)) {
self::execute($sql);
}
if (is_resource(self::$results)) {
$return = array();
while ($row =& mysql_fetch_assoc(self::$results)) {
array_push($return, $row);
}
return $return;
}
else {
Error::addError('There is no valid MySQL result resource');
}
return false;
}
public static function insert($table, $columnValues) {
if (trim($table) != '') {
// @todo Check that the table exists, and possibly check that the columns exist as well
if (is_array($columnValues)) {
foreach ($columnValues as $key => $value) {
$columnValues[$key] = "'" . mysql_real_escape_string($value, self::$connection) . "'";
}
self::execute("
INSERT INTO {$table} (
" . implode(array_keys($columnValues), ', ') . "
) VALUES (
" . implode($columnValues, ", ") . "
);
");
return mysql_insert_id(self::$connection);
}
else {
Error::addError('No data was specified');
}
}
else {
Error::addError('No database table was specified');
}
return false;
}
public static function update($table, $columnValues, $conditions) {
}
public static function delete($table, $columnValues, $conditions) {
}
}
?>

56
classes/Error.php Normal file
View file

@ -0,0 +1,56 @@
<?php
class Error {
private static $errors;
private static $warnings;
public static function instance() {
static $object;
if (!is_object($object)) {
$object = new Error();
}
return $object;
}
public static function addError($message) {
self::$errors[] = $message;
return true;
}
public static function addWarning($message) {
self::$warnings[] = $message;
return true;
}
public static function getError() {
return self::$errors;
}
public static function getWarning() {
return self::$warnings;
}
public static function isError() {
if (is_array(self::getError()) || is_array(self::getWarning())) {
return true;
}
return false;
}
public function display() {
if (self::isError()) {
var_dump(self::getError(), self::getWarning());
self::$errors = self::$warnings = null;
return true;
}
return false;
}
}
?>

8
config/meatronic.ini Executable file
View file

@ -0,0 +1,8 @@
debug = true
[database]
hostname = "localhost"
username = "meatronic"
password = "m347r0n1c"
database = "meatronic"

15
jLib.php Executable file
View file

@ -0,0 +1,15 @@
<?php
function __autoload($class) {
require_once "classes/{$class}.php";
}
$server = explode('.', $_SERVER["SERVER_NAME"]);
$site = $server[count($server) - 2];
// @debug
$site = 'meatronic';
Config::load($site);
?>