From 8c14ec379dda1e8bf83d476452119ff6b50a6722 Mon Sep 17 00:00:00 2001 From: Josh Sherman Date: Tue, 12 Aug 2008 01:09:07 +0000 Subject: [PATCH] Bunch of updates to migrate from jLib to Pickles. git-svn-id: http://svn.cleancode.org/svn/pickles@31 4d10bc64-7434-11dc-a737-d2d0f8310089 --- Pickles.php | 19 +--- classes/Controller.php | 116 +++++++++++++-------- classes/Security.php | 18 ++-- classes/Viewer/Debug.php | 8 ++ classes/Viewer/Smarty.php | 13 ++- config/ribbonnutrition.com.xml | 26 +++++ config/verynicenoise.com.xml | 8 +- smarty/functions/function.contact_form.php | 1 - static/js/ajax.js | 14 ++- 9 files changed, 141 insertions(+), 82 deletions(-) create mode 100755 config/ribbonnutrition.com.xml diff --git a/Pickles.php b/Pickles.php index 38f22c1..0b9b75c 100755 --- a/Pickles.php +++ b/Pickles.php @@ -1,14 +1,10 @@ config = Config::getInstance(); - $this->config->load($site); - - // Generate a generic "site down" message - if ($this->config->get('disabled')) { - exit("

{$_SERVER['SERVER_NAME']} is currently down for maintenance

"); - } - + new Controller($controller); } + */ } diff --git a/classes/Controller.php b/classes/Controller.php index 485a2db..16f77b8 100755 --- a/classes/Controller.php +++ b/classes/Controller.php @@ -1,61 +1,98 @@ 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("

{$_SERVER['SERVER_NAME']} is currently down for maintenance

"); } // 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'; - } - } - */ } diff --git a/classes/Security.php b/classes/Security.php index 9d5c8db..8446dc8 100644 --- a/classes/Security.php +++ b/classes/Security.php @@ -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: /'); } diff --git a/classes/Viewer/Debug.php b/classes/Viewer/Debug.php index 1e110e5..6dfc782 100644 --- a/classes/Viewer/Debug.php +++ b/classes/Viewer/Debug.php @@ -8,6 +8,14 @@ class Viewer_Debug extends Viewer_Common { echo '
';
 		var_dump($_REQUEST);
 		echo '
'; + echo '

$_SESSION

' . "\n"; + echo '
';
+		var_dump($_SESSION);
+		echo '
'; + echo '

$_SERVER

' . "\n"; + echo '
';
+		var_dump($_SERVER);
+		echo '
'; } } diff --git a/classes/Viewer/Smarty.php b/classes/Viewer/Smarty.php index 4b3092f..dca0309 100644 --- a/classes/Viewer/Smarty.php +++ b/classes/Viewer/Smarty.php @@ -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')); diff --git a/config/ribbonnutrition.com.xml b/config/ribbonnutrition.com.xml new file mode 100755 index 0000000..16598b8 --- /dev/null +++ b/config/ribbonnutrition.com.xml @@ -0,0 +1,26 @@ + + + localhost + ribbonnutrition + r1bb0nnu7r1710n + ribbonnutrition + + + home + + home + + + + false + + Logout + + + + RN + + joshsherman@gmail.com + + + diff --git a/config/verynicenoise.com.xml b/config/verynicenoise.com.xml index c6fe959..4dea1d3 100755 --- a/config/verynicenoise.com.xml +++ b/config/verynicenoise.com.xml @@ -6,9 +6,9 @@ verynicenoise - home + news - Home + News About Releases Artists @@ -19,8 +19,8 @@ true - Artist Profile - User Account + Artist Profile + User Account Logout diff --git a/smarty/functions/function.contact_form.php b/smarty/functions/function.contact_form.php index 228743b..da57de2 100644 --- a/smarty/functions/function.contact_form.php +++ b/smarty/functions/function.contact_form.php @@ -11,7 +11,6 @@ function smarty_function_contact_form($params, &$smarty) {

-
'; diff --git a/static/js/ajax.js b/static/js/ajax.js index c481f8f..cd2bb1b 100644 --- a/static/js/ajax.js +++ b/static/js/ajax.js @@ -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); } }