Bunch of updates to migrate from jLib to Pickles.

git-svn-id: http://svn.cleancode.org/svn/pickles@31 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
Josh Sherman 2008-08-12 01:09:07 +00:00
parent 046d265347
commit 8c14ec379d
9 changed files with 141 additions and 82 deletions

View file

@ -1,14 +1,10 @@
<?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';
$file = PATH . '../../common/classes/' . str_replace('_', '/', $class) . '.php';
if (file_exists($file)) {
require_once $file;
@ -17,24 +13,17 @@ function __autoload($class) {
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);
}
*/
}

View file

@ -1,61 +1,98 @@
<?php
// @todo Possibly remove the conditionals for the CLI view
class Controller extends Object {
private $model = null;
private $viewer = null;
private $session = null;
public function __construct($controller) {
parent::__construct();
/*
protected $config = null;
private $controller = null;
*/
public function __construct($site, $controller = 'Web') {
parent::__construct();
// Establish the session
if ($controller != 'CLI') {
$this->session = Session::getInstance();
$this->session = Session::getInstance();
// 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>");
}
// 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 ($name == 'logout') {
Security::logout();
}
else {
// 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();
if (strpos($name, '/') === false) {
$class = $name;
$section = $name;
$event = null;
}
else {
$class = str_replace('/', '_', $name);
list($section, $event) = split('/', $name);
}
$this->model->set('name', $name);
$this->model->set('section', $section);
$this->model->set('event', $event);
if (class_exists($class)) {
$this->model = new $class;
$this->model->__default();
}
else {
// @todo
exit();
}
if ($this->model->get('auth') == false) {
$this->model->set('auth', $this->config->get('behavior', 'auth'));
}
if ($this->model->get('view') == false) {
if ($this->config->get('behavior', 'view') != false) {
$view = $this->config->get('behavior', 'view');
}
else {
// Perhaps Smarty shouldn't be assumed at this point...
$view = isset($argv) ? 'CLI' : 'Smarty';
}
// Load the viewer
$this->viewer = Viewer::factory($this->model);
$this->viewer->display();
$this->model->set('view', $view);
}
if ($this->model->get('auth') === true && $controller != 'CLI') {
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();
}
}
//var_dump($name, $this->session, $_SESSION, $_SERVER);
}
/*
@ -69,13 +106,6 @@ class Controller extends Object {
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';
}
}
*/
}

View file

@ -3,7 +3,8 @@
class Security extends Object {
static function authenticate() {
$db = DB::getInstance();
$db = DB::getInstance();
$session = Session::getInstance();
if (isset($_SERVER['PHP_AUTH_USER'])) {
$from = '
@ -16,14 +17,14 @@ class Security extends Object {
$db->execute('SELECT COUNT(id) ' . $from);
if ($db->getField() != 0) {
$db->execute('SELECT id ' . $from);
$_SESSION['user_id'] = $db->getField();
$session->user_id = $db->getField();
}
else {
$_SESSION['user_id'] = null;
$session->user_id = null;
}
}
if (!isset($_SESSION['user_id'])) {
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.');
@ -36,12 +37,11 @@ class Security extends Object {
}
static function logout() {
$_SERVER['PHP_AUTH_USER'] = null;
$_SESSION['user_id'] = null;
$_SESSION['artist_id'] = null;
$_SESSION['admin'] = false;
$session = Session::getInstance();
$session->destroy();
session_destroy();
unset($_SERVER['PHP_AUTH_USER']);
unset($_SERVER['PHP_AUTH_PW']);
header('Location: /');
}

View file

@ -8,6 +8,14 @@ class Viewer_Debug extends Viewer_Common {
echo '<pre>';
var_dump($_REQUEST);
echo '</pre>';
echo '<h2>$_SESSION</h2>' . "\n";
echo '<pre>';
var_dump($_SESSION);
echo '</pre>';
echo '<h2>$_SERVER</h2>' . "\n";
echo '<pre>';
var_dump($_SERVER);
echo '</pre>';
}
}

View file

@ -16,7 +16,6 @@ class Viewer_Smarty extends Viewer_Common {
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();
@ -53,8 +52,18 @@ class Viewer_Smarty extends Viewer_Common {
}
}
$navigation = $this->config->get('navigation', 'sections');
// Add the admin section if we're authenticated
// @todo add code to check if the user is logged in
if (false) {
if ($this->config->get('admin', 'menu') == true) {
$navigation['admin'] = 'Admin';
}
}
// Pass all of our controller values to Smarty
$smarty->assign('navigation', $this->config->get('navigation', 'sections'));
$smarty->assign('navigation', $navigation);
$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'));

26
config/ribbonnutrition.com.xml Executable file
View file

@ -0,0 +1,26 @@
<config>
<database>
<hostname>localhost</hostname>
<username>ribbonnutrition</username>
<password>r1bb0nnu7r1710n</password>
<database>ribbonnutrition</database>
</database>
<navigation>
<default>home</default>
<sections>
<home>home</home>
</sections>
</navigation>
<admin>
<menu>false</menu>
<sections>
<logout>Logout</logout>
</sections>
</admin>
<contact>
<prefix>RN</prefix>
<recipients>
<recipient>joshsherman@gmail.com</recipient>
</recipients>
</contact>
</config>

View file

@ -6,9 +6,9 @@
<database>verynicenoise</database>
</database>
<navigation>
<default>home</default>
<default>news</default>
<sections>
<home>Home</home>
<news>News</news>
<about>About</about>
<releases>Releases</releases>
<artists>Artists</artists>
@ -19,8 +19,8 @@
<admin>
<menu>true</menu>
<sections>
<artist>Artist Profile</artist>
<user>User Account</user>
<artist_edit>Artist Profile</artist_edit>
<user_edit>User Account</user_edit>
<logout>Logout</logout>
</sections>
</admin>

View file

@ -11,7 +11,6 @@ function smarty_function_contact_form($params, &$smarty) {
<textarea name="message" title="required" class="contact_textarea"></textarea><br /><br />
<div class="contact_button">
<input type="button" value="Send Message" onclick="ajaxSubmit(this.parentNode.parentNode); return false;" />
<input type="submit" />
</div>
</form>
';

View file

@ -71,17 +71,15 @@ function ajaxSubmit(form) {
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
// We need to split the response because the response comes
// back in this format: type | message
// Where type could be error or success (and eventually warning)
var responseText = request.responseText;
var splitResponse = responseText.split('|');
var responseObject = eval( "(" + request.responseText + ")" );
var responseElement = document.createElement('div');
responseElement.className = splitResponse[0];
var responseMessage = document.createTextNode(splitResponse[1]);
responseElement.className = responseObject.type;
var responseMessage = document.createTextNode(responseObject.message);
responseElement.appendChild(responseMessage);
form.insertBefore(responseElement, form.firstChild);
}
}