Added the new classes and stuff.

git-svn-id: http://svn.cleancode.org/svn/pickles@30 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
Josh Sherman 2008-07-12 23:28:44 +00:00
parent d23619c120
commit 046d265347
19 changed files with 1012 additions and 0 deletions

61
Pickles.php Executable file
View file

@ -0,0 +1,61 @@
<?php
date_default_timezone_set('America/New_York');
// @todo no hard coded paths
//define('PICKLES_PATH', '/var/www/josh/common/');
define('PATH', getcwd() . '/');
function __autoload($class) {
// @todo fix the path when we move to prod
//$file = PICKLES_PATH . 'pickles_classes/' . str_replace('_', '/', $class) . '.php';
$file = PATH . '../../common/pickles_classes/' . str_replace('_', '/', $class) . '.php';
if (file_exists($file)) {
require_once $file;
}
}
class Pickles extends Object {
protected $config = null;
private $controller = null;
public function __construct($site, $controller = 'Web') {
parent::__construct();
// Load the config for the site passed in
$this->config = Config::getInstance();
$this->config->load($site);
// Generate a generic "site down" message
if ($this->config->get('disabled')) {
exit("<h2><em>{$_SERVER['SERVER_NAME']} is currently down for maintenance</em></h2>");
}
new Controller($controller);
}
}
/*
if (Config::getSession() && !isset($_SESSION)) {
session_start();
}
// Use the FCKeditor instead of textareas
// @todo add a wrapper for these two
if (Config::getFCKEditor()) {
require_once JLIB_PATH . 'common/static/fckeditor/fckeditor.php';
}
// Load up MagpieRSS is so desired
if (Config::getMagpieRSS()) {
require_once JLIB_PATH . '/var/www/josh/common/contrib/magpierss/rss_fetch.inc';
}
//Request::load();
*/
?>

21
classes/ArrayUtils.php Executable file
View file

@ -0,0 +1,21 @@
<?php
class ArrayUtils {
public static function object2array($object) {
if (is_object($object)) {
$object = (array)$object;
}
foreach ($object as $key => $value) {
if (is_object($value)) {
$object[$key] = self::object2array($value);
}
}
return $object;
}
}
?>

44
classes/Config.php Executable file
View file

@ -0,0 +1,44 @@
<?php
class Config extends Singleton {
private static $instance;
private function __construct() { }
public static function getInstance() {
if (!self::$instance instanceof Config) {
self::$instance = new Config();
}
return self::$instance;
}
public function load($site) {
// @todo no hardcoded paths!
$file = '/var/www/josh/common/config/' . $site . '.xml';
if (file_exists($file)) {
$config_array = ArrayUtils::object2array(simplexml_load_file($file));
if (is_array($config_array)) {
foreach ($config_array as $variable => $value) {
if ($value == 'true' || $value == array()) {
$value = (bool) $value;
}
$this->$variable = $value == array() ? (bool) $value : $value;
}
}
return true;
}
else {
Error::addError('Unable to load the configuration file');
return false;
}
}
}
?>

83
classes/Controller.php Executable file
View file

@ -0,0 +1,83 @@
<?php
class Controller extends Object {
private $model = null;
private $viewer = null;
private $session = null;
public function __construct($controller) {
parent::__construct();
// Establish the session
if ($controller != 'CLI') {
$this->session = Session::getInstance();
}
// Grab the passed in model or use the default
$name = isset($_REQUEST['model']) ? $_REQUEST['model'] : $this->config->get('navigation', 'default');
// Load the model
$file = '../models/' . $name . '.php';
if (file_exists($file)) {
require_once $file;
if (strpos($name, '/') === false) {
$class = $name;
$section = $name;
$event = null;
}
else {
$class = str_replace('/', '_', $name);
list($section, $event) = split('/', $name);
}
if (class_exists($class)) {
$this->model = new $class;
if ($this->model->get('auth') === true) {
Security::authenticate();
}
$this->model->set('name', $name);
$this->model->set('section', $section);
$this->model->set('event', $event);
$this->model->__default();
}
else {
// @todo
exit();
}
// Load the viewer
$this->viewer = Viewer::factory($this->model);
$this->viewer->display();
}
}
/*
if ((isset($_REQUEST['section']) && $_REQUEST['section'] == 'admin')) {
}
// Check if we're accessing an admin sub section and load the logic script
if (isset($_REQUEST['section']) && $_REQUEST['section'] != 'admin' && $is_admin) {
if ($_REQUEST['section'] == 'admin.logout') {
Session::logout();
}
// Add the admin section if we're authenticated
if (isset($_SESSION['user_id']) || isset($_SESSION['artist_id'])) {
if (Config::get('menu', 'admin') == 'true') {
$navigation['admin'] = 'Admin';
}
}
*/
}
?>

238
classes/DB.php Executable file
View file

@ -0,0 +1,238 @@
<?php
class DB extends Singleton {
private static $instance;
private $hostname;
private $username;
private $password;
private $database;
private $connection;
private $results;
private function __construct() { }
public static function getInstance() {
if (!self::$instance instanceof DB) {
self::$instance = new DB();
}
return self::$instance;
}
public function open() {
if (!is_resource($this->connection)) {
$config = Config::getInstance();
$this->hostname = $config->get('database', 'hostname');
$this->username = $config->get('database', 'username');
$this->password = $config->get('database', 'password');
$this->database = $config->get('database', 'database');
if (isset($this->hostname) && isset($this->username) && isset($this->password) && isset($this->database)) {
$this->connection = @mysql_connect($this->hostname, $this->username, $this->password);
if (is_resource($this->connection)) {
if (!mysql_select_db($this->database, $this->connection)) {
Error::addWarning("There was an error selecting the '" . $this->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;
}
return true;
}
public function close() {
if (is_resource($this->connection)) {
return mysql_close($this->connection);
}
return false;
}
public function execute($sql) {
$this->open();
if (trim($sql) != '') {
$this->results = @mysql_query($sql, $this->connection);
if (empty($this->results)) {
Error::addError('There was an error executing the SQL');
Error::addError(mysql_error());
}
else {
return true;
}
}
else {
Error::addWarning('There was no SQL to execute');
}
return false;
}
public function getField($sql = null) {
if (isset($sql)) {
$this->execute($sql);
}
if (is_resource($this->results)) {
$results = @mysql_fetch_row($this->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 null;
}
public function getRow($sql = null) {
if (isset($sql)) {
$this->execute($sql);
}
if (is_resource($this->results)) {
$results = @mysql_fetch_assoc($this->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 null;
}
public function getArray($sql = null) {
if (isset($sql)) {
$this->execute($sql);
}
if (is_resource($this->results)) {
$return = null;
while ($row = mysql_fetch_assoc($this->results)) {
if (!is_array($return)) {
$return = array();
}
array_push($return, $row);
}
return $return;
}
else {
Error::addError('There is no valid MySQL result resource');
}
return null;
}
public function insert($table, $columnValues) {
$this->open();
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] = $value == null ? 'NULL' : "'" . mysql_real_escape_string(stripslashes($value), $this->connection) . "'";
}
$this->execute("
INSERT INTO {$table} (
" . implode(array_keys($columnValues), ', ') . "
) VALUES (
" . implode($columnValues, ", ") . "
);
");
return mysql_insert_id($this->connection);
}
else {
Error::addError('No data was specified');
}
}
else {
Error::addError('No database table was specified');
}
return false;
}
public function update($table, $columnValues, $conditions) {
$this->open();
if (trim($table) != '') {
// @todo Check that the table exists, and possibly check that the columns exist as well
$fields = $where = null;
if (is_array($columnValues)) {
foreach ($columnValues as $key => $value) {
$fields .= ($fields ? ', ' : null) . $key . " = '" . mysql_real_escape_string(stripslashes($value), $this->connection) . "'";
}
if (is_array($conditions)) {
foreach ($conditions as $key => $value) {
$where = ($where == null) ? 'WHERE ' : ' AND ';
if ($value == null) {
$where .= $key . ' IS NULL';
}
else {
$where .= $key . " = '" . mysql_real_escape_string(stripslashes($value), $this->connection) . "'";
}
}
$sql = 'UPDATE ' . $table . ' SET ' . $fields . $where;
if ($this->execute($sql)) {
return true;
}
}
else {
Error::addError('No conditions were specified');
}
}
else {
Error::addError('No data was specified');
}
}
else {
Error::addError('No database table was specified');
}
return false;
}
public function delete($table, $columnValues, $conditions) {
}
}
?>

67
classes/Error.php Executable file
View file

@ -0,0 +1,67 @@
<?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()) {
if (self::getError()) {
foreach (self::getError() as $error) {
echo "{$error}<br />";
}
}
if (self::getWarning()) {
foreach (self::getWarning() as $error) {
echo "{$warning}<br />";
}
}
self::$errors = self::$warnings = null;
return true;
}
return false;
}
}
?>

51
classes/ImageUtils.php Executable file
View file

@ -0,0 +1,51 @@
<?php
class ImageUtils {
static function check($type, $types = array('image/jpeg', 'image/gif', 'image/png')) {
if (!is_array($types)) {
$types[0] = $types;
}
return in_array($type, $types);
}
static function move($origin, $destination) {
move_uploaded_file($origin, $destination);
imagedestroy($origin);
}
static function resize() {
}
static function convert($original, $destination, $keep_original = true) {
var_dump('convert ' . $original . ' ' . $destination);
var_dump( exec('convert ' . $original . ' ' . $destination) );
}
/*
if ($_FILES['image']['type'] == 'image/jpeg') {
$original = $directory . 'original.jpg';
$source = imagecreatefromjpeg($original);
list($width, $height) = getimagesize($original);
$sizes = array('small' => 75, 'medium' => 150, 'large' => 500);
foreach ($sizes as $name => $size) {
$temp = imagecreatetruecolor($size, $size);
imagecopyresampled($temp, $source, 0, 0, 0, 0, $size, $size, $width, $height);
imagejpeg($temp, "{$directory}{$name}.jpg", 85);
imagedestroy($temp);
}
imagedestroy($source);
*/
}
?>

47
classes/Mail.php Executable file
View file

@ -0,0 +1,47 @@
<?php
class Mail {
static function send($recipients = null, $prefix = null) {
$config = Config::getInstance();
$defaults = $config->get('contact');
if (!isset($recipients)) {
$recipients = $defaults['recipients']['recipient'];
}
if (is_array($recipients)) {
$to = null;
foreach ($recipients as $recipient) {
$to .= (isset($to) ? ',' : '') . $recipient;
}
}
else {
$to = $recipients;
}
if (!isset($prefix)) {
$prefix = isset($defaults['prefix']) ? $defaults['prefix'] : null;
}
$subject = str_replace("\n", '', (isset($prefix) ? "[{$prefix}] " : '') . $_REQUEST['subject']);
if (mail($to, $subject, stripslashes($_REQUEST['message']), "From: {$_REQUEST['email']}\r\n")) {
$type = 'success';
$message = 'Message sent successfully';
}
else {
$type = 'error';
$message = 'An unexpected error has occurred';
}
$return = array(
'type' => $type,
'message' => $message
);
return $return;
}
}
?>

37
classes/Model.php Normal file
View file

@ -0,0 +1,37 @@
<?php
class Model extends Object {
protected $db = null;
protected $data = array();
protected $name = null;
public function __construct() {
parent::__construct();
$this->db = DB::getInstance();
}
public function getAuth() {
return $this->get('auth');
}
public function getData() {
return $this->get('data');
}
public function getView() {
return $this->get('view');
}
public function __destruct() {
parent::__destruct();
}
public function __default() {
}
}
?>

49
classes/Object.php Normal file
View file

@ -0,0 +1,49 @@
<?php
class Object {
protected $config = null;
public function __construct() {
$this->config = Config::getInstance();
}
public function __destruct() {
}
/*
// @todo maybe later
public function __get($variable) {
if (!isset($this->data[$variable])) {
$this->data[$variable] = null;
}
return $this->data[$variable];
}
*/
public function get($variable, $array_element = null) {
if (isset($this->$variable)) {
if (isset($array_element)) {
$array = $this->$variable;
if (isset($array[$array_element])) {
return $array[$array_element];
}
}
else {
return $this->$variable;
}
}
return false;
}
public function set($variable, $value) {
$this->$variable = $value;
}
}
?>

28
classes/Request.php Executable file
View file

@ -0,0 +1,28 @@
<?php
class Request {
private static $request;
public static function load() {
if (is_array($_REQUEST)) {
foreach ($_REQUEST as $key => $value) {
self::$request[$key] = $value;
unset($_REQUEST[$key]);
}
}
return true;
}
public static function get($variable) {
if (isset(self::$request[$variable])) {
return self::$request[$variable];
}
return false;
}
}
?>

51
classes/Security.php Normal file
View file

@ -0,0 +1,51 @@
<?php
class Security extends Object {
static function authenticate() {
$db = DB::getInstance();
if (isset($_SERVER['PHP_AUTH_USER'])) {
$from = '
FROM users
WHERE email = "' . $_SERVER['PHP_AUTH_USER'] . '"
AND password = "' . md5($_SERVER['PHP_AUTH_PW']) . '"
AND admin = 1;
';
$db->execute('SELECT COUNT(id) ' . $from);
if ($db->getField() != 0) {
$db->execute('SELECT id ' . $from);
$_SESSION['user_id'] = $db->getField();
}
else {
$_SESSION['user_id'] = null;
}
}
if (!isset($_SESSION['user_id'])) {
header('WWW-Authenticate: Basic realm="Site Admin"');
header('HTTP/1.0 401 Unauthorized');
exit('No shirt, no shoes, no salvation. Access denied.');
}
else {
// Commented out to allow navigation to the page intended
//header('Location: /');
//exit();
}
}
static function logout() {
$_SERVER['PHP_AUTH_USER'] = null;
$_SESSION['user_id'] = null;
$_SESSION['artist_id'] = null;
$_SESSION['admin'] = false;
session_destroy();
header('Location: /');
}
}
?>

59
classes/Session.php Executable file
View file

@ -0,0 +1,59 @@
<?php
class Session extends Singleton {
private static $instance;
public $id = null;
private function __construct() {
if (ini_get('session.auto_start') == 0) {
session_start();
}
$this->id = session_id();
}
public static function getInstance() {
if (!self::$instance instanceof Session) {
self::$instance = new Session();
}
return self::$instance;
}
public function destroy() {
// foreach ($_SESSION as $variable => $value)
foreach (array_keys($_SESSION) as $variable) {
session_unregister($variable);
}
session_destroy();
}
public function __clone() {
trigger_error('Clone is not allowed for ' . __CLASS__, E_USER_ERROR);
}
public function __get($var) {
if (!isset($_SESSION[$var])) {
$_SESSION[$var] = null;
}
return $_SESSION[$var];
}
function __set($var,$val) {
return ($_SESSION[$var] = $val);
}
public function __isset($var) {
return isset($_SESSION[$var]) || isset($this->$var);
}
public function __destruct() {
session_write_close();
}
}
?>

30
classes/Singleton.php Normal file
View file

@ -0,0 +1,30 @@
<?php
class Singleton {
private function __construct() { }
public function get($variable, $array_element = null) {
if (isset($this->$variable)) {
if (isset($array_element)) {
$array = $this->$variable;
if (isset($array[$array_element])) {
return $array[$array_element];
}
}
else {
return $this->$variable;
}
}
return false;
}
public function set($variable, $value) {
$this->$$variable = $value;
}
}
?>

14
classes/Viewer.php Normal file
View file

@ -0,0 +1,14 @@
<?php
class Viewer {
private function __construct() { }
public static function factory(Model $model) {
$class = 'Viewer_' . $model->getView();
return new $class($model);
}
}
?>

16
classes/Viewer/Common.php Normal file
View file

@ -0,0 +1,16 @@
<?php
abstract class Viewer_Common extends Object {
protected $model = null;
public function __construct(Model $model) {
parent::__construct();
$this->model = $model;
}
abstract public function display();
}
?>

15
classes/Viewer/Debug.php Normal file
View file

@ -0,0 +1,15 @@
<?php
class Viewer_Debug extends Viewer_Common {
public function display() {
echo '<h1>Debug</h1>' . "\n";
echo '<h2>$_REQUEST</h2>' . "\n";
echo '<pre>';
var_dump($_REQUEST);
echo '</pre>';
}
}
?>

18
classes/Viewer/JSON.php Normal file
View file

@ -0,0 +1,18 @@
<?php
class Viewer_JSON extends Viewer_Common {
public function display() {
header('Content-type: application/json; charset=utf-8');
if (!function_exists('json_encode')) {
echo '{ "type" : "error", "message" : "json_encode() not found" }';
} else {
echo json_encode($this->model->getData());
}
}
}
?>

83
classes/Viewer/Smarty.php Normal file
View file

@ -0,0 +1,83 @@
<?php
class Viewer_Smarty extends Viewer_Common {
public function display() {
// Obliterates any passed in PHPSESSID (thanks Google)
if (stripos($_SERVER['REQUEST_URI'], '?PHPSESSID=') !== false) {
list($request_uri, $phpsessid) = split('\?PHPSESSID=', $_SERVER['REQUEST_URI'], 2);
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $request_uri);
exit();
}
// XHTML compliancy stuff
ini_set('arg_separator.output', '&amp;');
ini_set('url_rewriter.tags', 'a=href,area=href,frame=src,input=src,fieldset=');
// @todo Create a wrapper so that we can auto load this
//var_dump(getcwd(), );
require_once 'contrib/smarty/libs/Smarty.class.php';
$smarty = new Smarty();
// @todo Perhaps the templates directory would be better suited as a config variable?
$smarty->template_dir = '../templates/';
// @todo instead of having this in /tmp (which is Linux-scentric) perhaps move it to a folder in the common dir
$temp_path = "/tmp/smarty/{$_SERVER['SERVER_NAME']}/";
$cache_dir = $temp_path . 'cache';
$compile_dir = $temp_path . 'compile';
if (!file_exists($cache_dir)) { mkdir($cache_dir, 0777, true); }
if (!file_exists($compile_dir)) { mkdir($compile_dir, 0777, true); }
$smarty->cache_dir = $cache_dir ;
$smarty->compile_dir = $compile_dir;
$smarty->load_filter('output','trimwhitespace');
// Include custom Smarty functions
$directory = PATH . '../../common/smarty/functions/';
if (is_dir($directory)) {
if ($handle = opendir($directory)) {
while (($file = readdir($handle)) !== false) {
if (!preg_match('/^\./', $file)) {
list($type, $name, $ext) = split('\.', $file);
require_once $directory . $file;
$smarty->register_function($name, "smarty_{$type}_{$name}");
}
}
closedir($handle);
}
}
// Pass all of our controller values to Smarty
$smarty->assign('navigation', $this->config->get('navigation', 'sections'));
$smarty->assign('section', $this->model->get('section'));
$smarty->assign('action', $this->model->get('action')); // @todo rename me to event
$smarty->assign('admin', $this->config->get('admin', 'sections'));
$smarty->assign('template', '../templates/' . $this->model->get('name') . '.tpl'); //$template);
// Only load the session if it's available
if (isset($_SESSION)) {
$smarty->assign('session', $_SESSION);
}
$data = $this->model->getData();
if (isset($data) && is_array($data)) {
foreach ($data as $variable => $value) {
$smarty->assign($variable, $value);
}
}
// Load it up!
header('Content-type: text/html; charset=UTF-8');
$smarty->display('index.tpl');
}
}
?>