git-svn-id: http://svn.cleancode.org/svn/pickles@79 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
Josh Sherman 2008-10-18 13:42:10 +00:00
parent d6d774e04b
commit b53f52ac1e
12 changed files with 614 additions and 241 deletions

View file

@ -93,10 +93,10 @@ class Config extends Object {
/**
* Gets the authentication value
*
* @return boolean The model's authentication setting or false
* @return boolean The module's authentication setting or false
*/
public function getAuthentication() {
if (isset($this->models->authentication) && $this->models->authentication == 'true') {
if (isset($this->modules->authentication) && $this->modules->authentication == 'true') {
return true;
}
@ -106,10 +106,10 @@ class Config extends Object {
/**
* Gets the authentication value
*
* @return boolean The model's authentication setting or false
* @return boolean The module's authentication setting or false
*/
public function getDebug() {
if (isset($this->models->debug) && $this->models->debug == 'true') {
if (isset($this->modules->debug) && $this->modules->debug == 'true') {
return true;
}
@ -117,13 +117,13 @@ class Config extends Object {
}
/**
* Alias for $config->models->default with string cast
* Alias for $config->modules->default with string cast
*
* @return Returns the default model set or null
* @return Returns the default module set or null
*/
public function getDefaultModel() {
if (isset($this->models->default)) {
return (string)$this->models->default;
public function getDefaultModule() {
if (isset($this->modules->default)) {
return (string)$this->modules->default;
}
return 'home';
@ -135,7 +135,7 @@ class Config extends Object {
* @return boolean The site's disabled setting or false
*/
public function getDisabled() {
if (isset($this->models->disabled) && $this->models->disabled == 'true') {
if (isset($this->modules->disabled) && $this->modules->disabled == 'true') {
return true;
}
@ -158,10 +158,10 @@ class Config extends Object {
/**
* Gets the session value
*
* @return boolean The model's session setting or false
* @return boolean The module's session setting or false
*/
public function getSession() {
if (isset($this->models->session) && $this->models->session == 'true') {
if (isset($this->modules->session) && $this->modules->session == 'true') {
return true;
}
@ -169,30 +169,30 @@ class Config extends Object {
}
/**
* Gets the shared model
* Gets the shared module
*
* @param string $requested_model The model being requested
* @return string The name of the shared model or null
* @param string $requested_module The module being requested
* @return string The name of the shared module or null
*/
public function getSharedModel($requested_model) {
public function getSharedModule($requested_module) {
$additional = null;
if (strpos($requested_model, '/') !== false) {
list($requested_model, $additional) = split('/', $requested_model, 2);
if (strpos($requested_module, '/') !== false) {
list($requested_module, $additional) = split('/', $requested_module, 2);
$additional = '/' . $additional;
}
if (isset($this->models->shared->model)) {
foreach ($this->models->shared->model as $shared_model) {
if (isset($shared_model->alias)) {
if ($requested_model == $shared_model->alias) {
return (string)$shared_model->name . $additional;
if (isset($this->modules->shared->module)) {
foreach ($this->modules->shared->module as $shared_module) {
if (isset($shared_module->alias)) {
if ($requested_module == $shared_module->alias) {
return (string)$shared_module->name . $additional;
}
}
else {
if ($requested_model == $shared_model->name) {
return (string)$shared_model->name . $additional;
if ($requested_module == $shared_module->name) {
return (string)$shared_module->name . $additional;
}
}
}
@ -233,10 +233,10 @@ class Config extends Object {
/**
* Gets the viewer value
*
* @return boolean The model's viewer setting or false
* @return boolean The module's viewer setting or false
*/
public function getViewer() {
if (isset($this->models->viewer) && $this->models->viewer == 'true') {
if (isset($this->modules->viewer) && $this->modules->viewer == 'true') {
return true;
}

View file

@ -28,8 +28,8 @@
* Controller Class
*
* The heavy lifter of PICKLES, makes the calls to get the session and
* configuration loaded. Loads models, serves up user authentication when
* the model asks for it, and loads the viewer that the model has requested.
* configuration loaded. Loads modules, serves up user authentication when
* the module asks for it, and loads the viewer that the module has requested.
* Default values are present to make things easier on the user.
*
* @usage <code>new Controller($config);</code>
@ -48,8 +48,7 @@ class Controller extends Object {
parent::__construct();
// Creates the core objects that don't need a Config object
$logger = new Logger();
$error = new Error($logger);
$error = new Error();
// Check the passed config variables object type
if (is_object($config)) {
@ -86,7 +85,7 @@ class Controller extends Object {
unset($filename);
// Creates all the other core objects we need to pass around
$db = new DB($config, $logger, $error);
$db = new DB($config, $error);
$mailer = new Mailer($config, $error);
// Generate a generic "site down" message
@ -94,121 +93,121 @@ class Controller extends Object {
exit("<h2><em>{$_SERVER['SERVER_NAME']} is currently down for maintenance</em></h2>");
}
// Grab the passed in model or use the default
$model_name = isset($_REQUEST['model']) ? strtr($_REQUEST['model'], '-', '_') : $config->getDefaultModel();
// Grab the passed in module or use the default
$module_name = isset($_REQUEST['module']) ? strtr($_REQUEST['module'], '-', '_') : $config->getDefaultModule();
/**
* @todo Maybe the logout shouldn't be an internal thing, what if the
* user wanted to call the logout page something else? or better
* yet, they want to next it, like /users/logout?
*/
if ($model_name == 'logout') {
if ($module_name == 'logout') {
$security = new Security($config, $db);
$security->logout();
}
else {
// Loads the requested model's information
$model_file = '../models/' . $model_name . '.php';
// Loads the requested module's information
$module_file = '../modules/' . $module_name . '.php';
/**
* @todo Rename "section" to something like "current" or "selected"
* @todo Figure out WTF "event" is being used for
*/
if (strpos($model_name, '/') === false) {
$class = $model_name;
$section = $model_name;
if (strpos($module_name, '/') === false) {
$class = $module_name;
$section = $module_name;
$event = null;
}
else {
$class = strtr($model_name, '/', '_');
list($section, $event) = split('/', $model_name);
$class = strtr($module_name, '/', '_');
list($section, $event) = split('/', $module_name);
}
// Establishes the shared model information
$shared_model_name = $config->getSharedModel($model_name);
$shared_model_file = PICKLES_PATH . 'models/' . $shared_model_name . '.php';
// Establishes the shared module information
$shared_module_name = $config->getSharedModule($module_name);
$shared_module_file = PICKLES_PATH . 'modules/' . $shared_module_name . '.php';
// Tries to load the site level model
if (file_exists($model_file)) {
require_once $model_file;
// Tries to load the site level module
if (file_exists($module_file)) {
require_once $module_file;
if (class_exists($class)) {
$model = new $class($config, $db, $mailer);
$module = new $class($config, $db, $mailer);
}
}
// Tries to load the shared model
else if (file_exists($shared_model_file) && $shared_model_name != false) {
if (strpos($shared_model_name, '/') === false) {
$class = $shared_model_name;
// Tries to load the shared module
else if (file_exists($shared_module_file) && $shared_module_name != false) {
if (strpos($shared_module_name, '/') === false) {
$class = $shared_module_name;
}
else {
$class = strtr($shared_model_name, '/', '_');
$class = strtr($shared_module_name, '/', '_');
}
if (class_exists($class)) {
$model = new $class($config, $db, $mailer);
$module = new $class($config, $db, $mailer);
}
}
// Loads the stock model
// Loads the stock module
else {
$model = new Model($config, $db, $mailer);
$module = new Module($config, $db, $mailer);
}
// Checks if we loaded a model file and no class was present
if ($model != null) {
// Checks if we loaded a module file and no class was present
if ($module != null) {
// Potentially starts the session if it's not started already
if ($model->getSession() === true) {
if ($module->getSession() === true) {
if (ini_get('session.auto_start') == 0) {
session_start();
}
}
// Potentially requests use authentication
if ($model->getAuthentication() === true) {
if ($module->getAuthentication() === true) {
if (!isset($security)) {
$security = new Security($config, $db);
}
$security->authenticate();
}
// Potentially executes the model's logic
if (method_exists($model, '__default')) {
$model->__default();
// Potentially executes the module's logic
if (method_exists($module, '__default')) {
$module->__default();
if (isset($mailer->message)) {
$status = $mailer->send();
$model->type = $status['type'];
$model->message = $status['message'];
$module->type = $status['type'];
$module->message = $status['message'];
}
}
// Creates a new viewer object
$viewer_name = $model->getViewer();
if (in_array($viewer_name, array('JSON', 'PHP', 'RSS', 'Smarty'))) {
$viewer_class = 'Viewer_' . $viewer_name;
$viewer = new $viewer_class($config, $error);
$display_name = $module->getDisplay();
if (in_array($display_name, array('JSON', 'PHP', 'RSS', 'Smarty'))) {
$display_class = 'Display_' . $display_name;
$display = new $display_class($config, $error);
}
else {
$error->addError('Invalid viewer specified (' . $viewer_name . ')');
$error->addError('Invalid display specified (' . $viewer_name . ')');
}
// Sets the viewers properties
$viewer->model_name = $model_name;
$viewer->shared_name = $shared_model_name;
$viewer->section = $section;
$viewer->data = $model->getData();
// Sets the displays properties
$display->module_name = $module_name;
$display->shared_name = $shared_module_name;
$display->section = $section;
$display->data = $module->getData();
// Runs the requested viewer's display function
$viewer->display();
// Runs the requested rendering function
$display->render();
// Do some cleanup
if (isset($security)) {
unset($security);
}
unset($model, $viewer);
unset($db, $mailer, $config, $error, $logger);
unset($module, $viewer);
unset($db, $mailer, $config, $error);
}
}
}

View file

@ -50,7 +50,6 @@ class DB extends Object {
private $database;
private $error;
protected $logger;
/**
* Private MySQL resources
@ -58,11 +57,10 @@ class DB extends Object {
private $connection;
private $results;
public function __construct(Config $config, Logger $logger, Error $error) {
public function __construct(Config $config, Error $error) {
parent::__construct();
$this->logger = $logger;
$this->error = $error;
$this->error = $error;
$this->hostname = $config->database->hostname;
$this->username = $config->database->username;
@ -142,7 +140,8 @@ class DB extends Object {
$this->open();
if (trim($sql) != '') {
$this->logger->write('sql', trim(str_replace(array("\t", "\n"), array('', ' '), $sql)));
Logger::write('sql', trim(str_replace(array("\t", "\n"), array('', ' '), $sql)));
$this->results = mysql_query($sql, $this->connection);
if (empty($this->results)) {
$this->error->addError('There was an error executing the SQL');

106
classes/Display/Common.php Normal file
View file

@ -0,0 +1,106 @@
<?php
/**
* Common Display Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007, 2008 Joshua John Sherman
* @link http://phpwithpickles.org
* @license http://www.gnu.org/copyleft/lesser.html
* @package PICKLES
*/
/**
* Common Display Class
*
* This is the class that each viewer class should be extending from.
*/
abstract class Display_Common extends Object {
/**
* Constructor
*
* Runs the parent's constructor and adds the module to the object.
*/
public function __construct(Config $config, Error $error) {
parent::__construct();
$this->config = $config;
$this->error = $error;
/**
* @todo This may need to be flipped on only for Smarty and PHP templates
*/
// 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=');
header('Content-type: text/html; charset=UTF-8');
if ($this->config->getDebug() === true) {
$superglobals = array($GLOBALS, $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST, $_ENV);
?>
<div class="debug" style="border: 2px solid black; padding: 5px; margin: 10px;">
<h1>PICKLES Debug Console</h1>
<?php
/*
foreach ($superglobals as $superglobal => $array) {
?>
<h2><h2>
<?php
}
*/
?>
<h2>$_REQUEST</h2>
<pre><?php var_dump($_REQUEST); ?></pre>
<h2>$_SESSION</h2>
<pre><?php var_dump($_SESSION); ?></pre>
<h2>$_SERVER</h2>
<pre><?php var_dump($_SERVER); ?></pre>
</div>
<?php
/*
function display_buffer() {
$buffer = str_replace(
array(' ', "\r\n", "\n", "\t"),
null,
ob_get_contents()
);
ob_end_clean();
exit($buffer);
}
*/
}
}
/**
* Abstract rendering function that is overloaded within the loaded viewer
*/
abstract public function render();
}
?>

51
classes/Display/JSON.php Normal file
View file

@ -0,0 +1,51 @@
<?php
/**
* JSON Display Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007, 2008 Joshua John Sherman
* @link http://phpwithpickles.org
* @license http://www.gnu.org/copyleft/lesser.html
* @package PICKLES
*/
/**
* JSON Display
*
* Displays data in JavaScript Object Notation. Requires PHP 5 >= 5.2.0 or
* PECL json 1.2.0 or 1.2.1
*
* @link http://json.org/
* @link http://us.php.net/json_encode
* @link http://pecl.php.net/package/json
*/
class Display_JSON extends Display_Common {
/**
* Renders the data in JSON format
*/
public function render() {
if (!function_exists('json_encode')) {
echo '{ "type" : "error", "message" : "json_encode() not found" }';
} else {
echo json_encode($this->data);
}
}
}
?>

100
classes/Display/PHP.php Normal file
View file

@ -0,0 +1,100 @@
<?php
/**
* PHP Display Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007, 2008 Joshua John Sherman
* @link http://phpwithpickles.org
* @license http://www.gnu.org/copyleft/lesser.html
* @package PICKLES
*/
/**
* PHP Display
*
* Displays the associated PHP templates for the Model. This is
* very similar to the Smarty viewer, but less overhead since it's
* straight PHP. The PHP viewer also utilizes a different caching
* system than Smarty. The general rules around the caching will
* be the same though.
*/
class Display_PHP extends Display_Common {
private $template = null;
private $shared_template = null;
/**
* Renders the PHP templated pages
*/
public function render() {
// Establishes the template names
$this->template = SITE_PATH . '../templates/' . $this->module_name . '.php';
$this->shared_template = PICKLES_PATH . 'templates/' . $this->shared_name . '.php';
//if (filemtime($this->template)) {
// readfile('/var/www/josh/pickles/var/joshtronic.localhost/smarty/cache/home.html');
//}
/**
* @todo There's a bug with the store home page since it's a redirect, maybe
*/
if (!file_exists($this->template)) {
if (file_exists($this->shared_template)) {
$this->template = $this->shared_template;
}
}
// Brings these variables to scope
/**
* @todo Section or module needs to go, having both seems dumb.
*/
$section = $this->section;
$module = $this->module_name;
$template = $this->template;
// Loads the data from the config
$config = $this->config->getPublicData();
// Loads the data from the module
$data = $this->data;
// If there's data set, this brings it into scope
if (isset($this->data) && is_array($this->data)) {
extract($this->data);
}
// If the index.php file is present, load it, else load the template directly
/**
* @todo Should there be additional logic to allow the module or the
* template to determine whether or not the index should be loaded?
*/
if (file_exists(SITE_PATH . '../templates/index.php')) {
require_once SITE_PATH . '../templates/index.php';
}
else if (file_exists($this->template)) {
require_once $this->template;
}
/**
* @todo Resurrect my buffer clean up code
*/
}
}
?>

100
classes/Display/RSS.php Normal file
View file

@ -0,0 +1,100 @@
<?php
/**
* RSS Display Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007, 2008 Joshua John Sherman
* @link http://phpwithpickles.org
* @license http://www.gnu.org/copyleft/lesser.html
* @package PICKLES
*/
/**
* RSS Display
*
* Displays data in RSS version 2.0 format.
*
* @link http://cyber.law.harvard.edu/rss/rss.html
* @todo Need to add support for RSS v1.0 as well as ATOM feeds. This may
* result in my abstracting out these classes a bit more (Probably a
* Feed viewer that would take a parameter to determine which type of
* of feed to use).
*/
class Display_RSS extends Display_Common {
/**
* Render the RSS feed data
*
* Uses a combination of configuration options and a properly formatted data
* array to create an RSS v2.0 feed.
*
* @todo Error handling is non-existant.
*/
public function render() {
if (isset($this->data->channel)) {
$channel = $this->data['channel'];
$channel = $this->config->rss->$channel;
if (isset($this->data->items)) {
$items = $this->data['items'];
}
else {
$this->error->addError('No items were provided');
}
}
else {
$this->error->addError('No channel was specified');
}
header('Content-type: application/rss+xml; charset=UTF-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<rss version="2.0">
<channel>
<title><?=$channel['title'];?></title>
<link>http://<?=$_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];?></link>
<description><?=$channel['description'];?></description>
<category><?=$channel['category'];?></category>
<language><?=$channel['language'] ? $channel['language'] : 'en-us';?></language>
<?php
if (is_array($items)) {
foreach ($items as $key => $item) {
$date = date('r', strtotime($item['date']));
if ($key == 0) {
echo "<lastBuildDate>{$date}</lastBuildDate>";
}
?>
<item>
<title><?=$item['title'];?></title>
<link><?=$item['link'];?></link>
<description><![CDATA[<?=$item['description'];?>]]></description>
<author><?=$item['author'];?></author>
<pubDate><?=$date;?></pubDate>
<guid><?=$item['guid'];?></guid>
</item>
<?php
}
}
?>
</channel>
</rss>
<?php
}
}
?>

146
classes/Display/Smarty.php Normal file
View file

@ -0,0 +1,146 @@
<?php
/**
* Smarty Display Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007, 2008 Joshua John Sherman
* @link http://phpwithpickles.org
* @license http://www.gnu.org/copyleft/lesser.html
* @package PICKLES
*/
/**
* Smarty Display
*
* Displays the associated Smarty templates for the Model.
*
* @link http://smarty.net/
*/
class Display_Smarty extends Display_Common {
/**
* Render the Smarty generated pages
*/
public function render() {
$smarty = new Smarty();
// Establishes our paths
$smarty->template_dir = SITE_PATH . '../templates/';
$cache_dir = SMARTY_PATH . 'cache';
$compile_dir = SMARTY_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;
/**
* @todo move this to the config
*/
// Enables caching
$smarty->caching = 1;
$smarty->compile_check = true;
$smarty->cache_lifetime = 3600;
var_dump($smarty->is_cached('index.tpl', $this->model_name));
// Loads the trim whitespace filter
$smarty->load_filter('output','trimwhitespace');
// Includes the PICKLES custom Smarty functions
$directory = PICKLES_PATH . 'functions/smarty/';
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);
}
}
// Establishes the template names
$template = SITE_PATH . '../templates/' . $this->model_name . '.tpl';
$shared_template = PICKLES_PATH . 'templates/' . $this->shared_name . '.tpl';
/**
* @todo There's a bug with the store home page since it's a redirect
*/
if (!file_exists($template)) {
if (file_exists($shared_template)) {
$template = $shared_template;
}
}
// Pass all of our controller values to Smarty
$smarty->assign('section', $this->section);
$smarty->assign('model', $this->model_name);
$smarty->assign('template', $template);
// Loads the data from the config
$data = $this->config->getPublicData();
if (isset($data) && is_array($data)) {
$smarty->assign('config', $data);
}
// Loads the model's data
if (isset($this->data) && is_array($this->data)) {
foreach ($this->data as $variable => $value) {
$smarty->assign($variable, $value);
}
}
/**
* @todo There's no error checking for the index... should it be shared,
* and should the error checking occur anyway since any shit could
* happen?
*/
/*
$template = '../templates/index.tpl';
$shared_template = str_replace('../', '../../pickles/', $template);
if (!file_exists($template)) {
if (file_exists($shared_template)) {
$template = $shared_template;
}
}
*/
// If the index.tpl file is present, load it, else load the template directly
/**
* @todo Should there be additional logic to allow the model or the
* template to determine whether or not the index should be loaded?
*/
if ($smarty->template_exists('index.tpl')) {
$smarty->display('index.tpl', $this->model_name);
}
else {
$smarty->display($template);
}
}
}
?>

View file

@ -41,15 +41,6 @@ class Error extends Object {
private $errors = null;
private $warnings = null;
protected $logger;
/**
* Constructor
*/
public function __construct(Logger $logger) {
$this->logger = $logger;
}
/**
* Adds an error message
*
@ -58,7 +49,7 @@ class Error extends Object {
*/
public function addError($message) {
$this->errors[] = $message;
$this->logger->write('error', '[error] ' . $message);
Logger::write('error', '[error] ' . $message);
return true;
}
@ -70,7 +61,7 @@ class Error extends Object {
*/
public function addWarning($message) {
$this->warnings[] = $message;
$this->logger->write('error', '[warning] ' . $message);
Logger::write('error', '[warning] ' . $message);
return true;
}

View file

@ -1,115 +0,0 @@
<?php
/**
* Image Utilities Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007, 2008 Joshua John Sherman
* @link http://phpwithpickles.org
* @license http://www.gnu.org/copyleft/lesser.html
* @package PICKLES
*/
/**
* Incomplete collection of image manipulation utilities
*
* I'm not entirely sure if this class is being utilized anywhere, especially
* since a lot of the functions are not finished, or var_dump() stuff. The
* functions are (or eventually will be) wrappers for ImageMagick commands. The
* other route would be to code it all in pure PHP as to not have to add another
* dependency to PICKLES. After checking the PHP documentation on image
* processing, it seems there's an ImageMagick module that can utilized, that may
* end up being the route that is taken. Until then, this class is useless shit.
*
* @todo Make the class usable.
*/
class Image extends Object {
public function __construct() {
parent::__construct();
}
/**
* Checks an image format
*
* This is basically just a wrapper for in_array(). The passed image type is
* checked against the passed array of types (else it will use the default
* list).
*
* @param string $type The type of being being checked
* @param array $types An array of valid image types, defaults to the common
* web formats of jpg, gif and png
* @return boolean If the passed type is in the list of valid types
*/
public function check($type, $types = array('image/jpeg', 'image/gif', 'image/png')) {
if (!is_array($types)) {
$types[0] = $types;
}
return in_array($type, $types);
}
/**
* Moves an uploaded file
*
* Handles not only moving an uploaded file to another location, but removes
* the original file once it's been moved. It's ashame that the function
* move_uploaded_file() just copies a file, and doesn't actually move it.
*
* @param string $origin The uploaded file that is to be moved
* @param string $destination The destination for the origin file
* @todo This function needs to return the status of the move
* @todo Currently if the move fails, the origin file will still be removed.
*/
public function move($origin, $destination) {
move_uploaded_file($origin, $destination);
imagedestroy($origin);
}
public function resize() {
}
public 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);
*/
}
?>

View file

@ -29,11 +29,7 @@
*/
class Logger extends Object {
public function __construct() {
parent::__construct();
}
public function write($type, $message, $class = null) {
public static function write($type, $message, $class = null) {
if (!file_exists(LOG_PATH)) { mkdir(LOG_PATH, 0777, true); }
$message = '[' . date('r') . '] [client ' . $_SERVER['REMOTE_ADDR'] . '] [uri ' . $_SERVER['REQUEST_URI'] . '] [script ' . $_SERVER['SCRIPT_NAME'] . '] ' . $message;

View file

@ -1,7 +1,7 @@
<?php
/**
* Model Class File for PICKLES
* Module Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
@ -25,14 +25,14 @@
*/
/**
* Model Class
* Module Class
*
* Every model in PICKLES at both the core and site levels need to extend
* this class. It handles the getting of common model variables (auth, data
* and view) as well as making sure that every model has a database object
* available.
* Every module (page) in PICKLES at both the core and site levels should
* extend this class. It handles the getting of common module variables
* (auth, data and view) as well as making sure that every module has a
* database object available.
*/
class Model extends Object {
class Module extends Object {
/**
* Data array used by the viewer
@ -50,7 +50,7 @@ class Model extends Object {
protected $db = null;
/**
* Name of the model
* Name of the module
*/
protected $name = null;
@ -59,14 +59,14 @@ class Model extends Object {
*/
protected $mailer = null;
protected $authentication = null;
protected $viewer = null;
protected $session = null;
protected $authentication = false;
protected $viewer = DISPLAY_PHP;
protected $session = false;
/**
* Constructor
*
* Handles calling the parent constructor and sets up the model's
* Handles calling the parent constructor and sets up the module's
* internal config and database object
*/
public function __construct(Config $config, DB $db, Mailer $mailer) {
@ -80,9 +80,9 @@ class Model extends Object {
/**
* Gets the authenticate value
*
* Order of precedence: Model, Config, Guess (guess is always false)
* Order of precedence: Module, Config, Guess (guess is always false)
*
* @return boolean Whether or not the model requires user authentication
* @return boolean Whether or not the module requires user authentication
*/
public function getAuthentication() {
if ($this->authentication != null) {
@ -98,7 +98,7 @@ class Model extends Object {
/**
* Gets the session value
*
* Order of precedence: Auth On, Model, Config, Guess (guess is always false)
* Order of precedence: Auth On, Module, Config, Guess (guess is always false)
*
* @return boolean Whether or not the session needs to be started
*/
@ -117,27 +117,27 @@ class Model extends Object {
}
/**
* Gets the requested Viewer
* Gets the requested Display
*
* Order of precedence: Model, Config, Guess (guess is always Smarty)
* Order of precedence: Module, Config, Guess (guess is always Smarty)
*
* @return string The viewer that the model has requested to be used
* @return string The viewer that the module has requested to be used
* @todo Guess shouldn't be Smarty, it should be the dummy PHP template.
* @todo Use the config override value to help determine.
*/
public function getViewer() {
if ($this->viewer == null) {
public function getDisplay() {
if ($this->display == null) {
return isset($argv) ? 'CLI' : 'Smarty';
}
else {
return $this->viewer;
return $this->display;
}
}
/**
* Alias for $model->data
* Alias for $module->data
*
* @return array Associative array of data that was set by the model
* @return array Associative array of data that was set by the module
*/
public function getData() {
if (isset($this->data)) {
@ -163,9 +163,9 @@ class Model extends Object {
/**
* Default function
*
* This function is overloaded by the model. The __default() function
* This function is overloaded by the module. The __default() function
* is where any code that needs to be executed at run time needs to be
* placed. It's not in the constructor as the model needs to be
* placed. It's not in the constructor as the module needs to be
* instantiated first so that the authorization requirements can be
* checked without running code it's potentially not supposed to have
* been executed.