From c6b3d04034915d24de0ab4ebf96ae83ce5eb5ee1 Mon Sep 17 00:00:00 2001 From: Ben Klang Date: Sat, 2 Jan 2010 22:39:39 -0500 Subject: [PATCH] Shout: Convert base.php to Application.php --- shout/devices.php | 11 +++-- shout/extensions.php | 12 ++--- shout/lib/Application.php | 78 ++++++++++++++++++++++++++++---- shout/lib/Forms/ExtensionForm.php | 14 +++--- shout/lib/base.php | 95 --------------------------------------- 5 files changed, 90 insertions(+), 120 deletions(-) delete mode 100644 shout/lib/base.php diff --git a/shout/devices.php b/shout/devices.php index 3a2ec5561..5133c4f98 100644 --- a/shout/devices.php +++ b/shout/devices.php @@ -8,8 +8,11 @@ * * @author Ben Klang */ -@define('SHOUT_BASE', dirname(__FILE__)); -require_once SHOUT_BASE . '/lib/base.php'; +require_once dirname(__FILE__) . '/lib/Application.php'; + +$shout = new Shout_Application(array('init' => true)); +$context = $_SESSION['shout']['context']; + require_once SHOUT_BASE . '/lib/Forms/DeviceForm.php'; $action = Horde_Util::getFormData('action'); @@ -50,7 +53,7 @@ case 'edit': // Create a new add/edit form $devid = Horde_Util::getFormData('devid'); - $devices = $shout_devices->getDevices($context); + $devices = $shout->devices->getDevices($context); $vars = new Horde_Variables($devices[$devid]); $vars->set('action', $action); @@ -96,7 +99,7 @@ default: } // Fetch the (possibly updated) list of extensions -$devices = $shout_devices->getDevices($context); +$devices = $shout->devices->getDevices($context); Horde::addScriptFile('stripe.js', 'horde'); require SHOUT_TEMPLATES . '/common-header.inc'; diff --git a/shout/extensions.php b/shout/extensions.php index 48a7f73d3..821e06aa2 100644 --- a/shout/extensions.php +++ b/shout/extensions.php @@ -8,10 +8,12 @@ * * @author Ben Klang */ -@define('SHOUT_BASE', dirname(__FILE__)); -require_once SHOUT_BASE . '/lib/base.php'; +require_once dirname(__FILE__) . '/lib/Application.php'; + +$shout = new Shout_Application(array('init' => true)); +$context = $_SESSION['shout']['context']; + require_once SHOUT_BASE . '/lib/Forms/ExtensionForm.php'; -//require_once SHOUT_BASE . '/lib/Shout.php'; $action = Horde_Util::getFormData('action'); @@ -48,7 +50,7 @@ case 'edit': // Create a new add/edit form $extension = Horde_Util::getFormData('extension'); - $extensions = $shout_extensions->getExtensions($context); + $extensions = $shout->extensions->getExtensions($context); $vars = new Horde_Variables($extensions[$extension]); if ($action == 'edit') { $vars->set('oldextension', $extension); @@ -98,7 +100,7 @@ default: } // Fetch the (possibly updated) list of extensions -$extensions = $shout_extensions->getExtensions($context); +$extensions = $shout->extensions->getExtensions($context); Horde::addScriptFile('stripe.js', 'horde'); require SHOUT_TEMPLATES . '/common-header.inc'; diff --git a/shout/lib/Application.php b/shout/lib/Application.php index 069c8ba05..a7353e90c 100644 --- a/shout/lib/Application.php +++ b/shout/lib/Application.php @@ -13,18 +13,82 @@ * @author Ben Klang * @package Shout */ -define('SHOUT_BASE', dirname(__FILE__) . '/..'); +if (!defined('SHOUT_BASE')) { + define('SHOUT_BASE', dirname(__FILE__). '/..'); +} + +if (!defined('HORDE_BASE')) { + /* If horde does not live directly under the app directory, the HORDE_BASE + * constant should be defined in config/horde.local.php. */ + if (file_exists(SHOUT_BASE. '/config/horde.local.php')) { + include SHOUT_BASE . '/config/horde.local.php'; + } else { + define('HORDE_BASE', SHOUT_BASE . '/..'); + } +} + +/* Load the Horde Framework core (needed to autoload + * Horde_Registry_Application::). */ +require_once HORDE_BASE . '/lib/core.php'; class Shout_Application extends Horde_Registry_Application { public $version = 'H4 (1.0-git)'; - protected $_contexts = null; - protected $_extensions = null; - protected $_devices = null; + public $contexts = null; + public $extensions = null; + public $devices = null; public function __construct($args = array()) { + if (!empty($args['init'])) { + $GLOBALS['registry'] = &Horde_Registry::singleton(); + $registry = &$GLOBALS['registry']; + try { + $registry->pushApp('shout', array('check_perms' => true, + 'logintasks' => true)); + } catch (Horde_Exception $e) { + Horde::authenticationFailure('shout', $e); + } + + // Ensure Shout is properly configured before use + $shout_configured = (@is_readable(SHOUT_BASE . '/config/conf.php')); + if (!$shout_configured) { + Horde_Test::configFilesMissing('Shout', SHOUT_BASE, array('conf.php')); + } + + define('SHOUT_TEMPLATES', $registry->get('templates')); + $this->contexts = Shout_Driver::factory('storage'); + $this->extensions = Shout_Driver::factory('extensions'); + $this->devices = Shout_Driver::factory('devices'); + + try { + $contexts = $this->contexts->getContexts(); + } catch (Shout_Exception $e) { + $notification->push($e); + $contexts = false; + } + + $notification = Horde_Notification::singleton(); + $GLOBALS['notification'] = $notification; + $notification->attach('status'); + + if (count($contexts) == 1) { + // Default to the user's only context + if (!empty($context) && $context != $contexts[0]) { + $notification->push(_("You do not have permission to access that context."), 'horde.error'); + } + $context = $contexts[0]; + } elseif (!empty($context) && !in_array($context, $contexts)) { + $notification->push(_("You do not have permission to access that context."), 'horde.error'); + $context = false; + } elseif (!empty($context)) { + $notification->push("Please select a context to continue.", 'horde.info'); + $context = false; + } + + $_SESSION['shout']['context'] = $context; + } } public function perms() @@ -35,12 +99,11 @@ class Shout_Application extends Horde_Registry_Application } require_once dirname(__FILE__) . '/base.php'; - $shout_contexts = Shout_Driver::factory('storage'); $perms['tree']['shout']['superadmin'] = false; $perms['title']['shout:superadmin'] = _("Super Administrator"); - $contexts = $shout_contexts->getContexts(); + $contexts = $this->contexts->getContexts(); $perms['tree']['shout']['contexts'] = false; $perms['title']['shout:contexts'] = _("Contexts"); @@ -62,9 +125,6 @@ class Shout_Application extends Horde_Registry_Application } } - // function _shout_getContexts($searchfilters = SHOUT_CONTEXT_ALL, - // $filterperms = null) - return $perms; } } diff --git a/shout/lib/Forms/ExtensionForm.php b/shout/lib/Forms/ExtensionForm.php index 20da7a8d4..b633ad2de 100644 --- a/shout/lib/Forms/ExtensionForm.php +++ b/shout/lib/Forms/ExtensionForm.php @@ -17,13 +17,13 @@ class ExtensionDetailsForm extends Horde_Form { /** * ExtensionDetailsForm constructor. * - * @global $shout_extensions + * @global $shout->extensions * @param $vars * @return */ function __construct(&$vars) { - global $shout_extensions; + global $shout; $context = $_SESSION['shout']['context']; $action = $vars->get('action'); @@ -52,11 +52,11 @@ class ExtensionDetailsForm extends Horde_Form { * Process this form, saving its information to the backend. * * @param string $context Context in which to execute this save - * FIXME: is there a better way to get the $context and $shout_extensions? + * FIXME: is there a better way to get the $context and $shout->extensions? */ function execute() { - global $shout_extensions; + global $shout; $extension = $this->_vars->get('extension'); $context = $this->_vars->get('context'); @@ -70,7 +70,7 @@ class ExtensionDetailsForm extends Horde_Form { 'mailboxpin' => $this->_vars->get('mailboxpin'), ); - $shout_extensions->saveExtension($context, $extension, $details); + $shout->extensions->saveExtension($context, $extension, $details); } } @@ -94,9 +94,9 @@ class ExtensionDeleteForm extends Horde_Form function execute() { - global $shout_extensions; + global $shout; $context = $this->_vars->get('context'); $extension = $this->_vars->get('extension'); - $shout_extensions->deleteExtension($context, $extension); + $shout->extensions->deleteExtension($context, $extension); } } diff --git a/shout/lib/base.php b/shout/lib/base.php deleted file mode 100644 index d3e2737ce..000000000 --- a/shout/lib/base.php +++ /dev/null @@ -1,95 +0,0 @@ - - * @since Shout 0.1 - * @package Shout - */ - -if (!defined('SHOUT_BASE')) { - define('SHOUT_BASE', dirname(__FILE__). '/..'); -} - -if (!defined('HORDE_BASE')) { - /* If horde does not live directly under the app directory, the HORDE_BASE - * constant should be defined in config/horde.local.php. */ - if (file_exists(SHOUT_BASE. '/config/horde.local.php')) { - include SHOUT_BASE . '/config/horde.local.php'; - } else { - define('HORDE_BASE', SHOUT_BASE . '/..'); - } -} - -// Load the Horde Framework core, and set up inclusion paths. -require_once HORDE_BASE . '/lib/core.php'; - -$registry = &Horde_Registry::singleton(); -try { - $registry->pushApp('shout', array('check_perms' => true, 'logintasks' => true)); -} catch (Horde_Exception $e) { - Horde::authenticationFailureRedirect('shout', $e); -} - -$conf = &$GLOBALS['conf']; -@define('SHOUT_TEMPLATES', $registry->get('templates')); - -// Ensure Shout is properly configured before use -$shout_configured = (@is_readable(SHOUT_BASE . '/config/conf.php')); -if (!$shout_configured) { - Horde_Test::configFilesMissing('Shout', SHOUT_BASE, array('conf.php')); -} - -$notification = Horde_Notification::singleton(); -$notification->attach('status'); - -//// Shout base libraries. -//require_once SHOUT_BASE . '/lib/Shout.php'; -//require_once SHOUT_BASE . '/lib/Driver.php'; -// -//// Form libraries. -//require_once 'Horde/Form.php'; -//require_once 'Horde/Form/Renderer.php'; -// -//// Variable handling libraries -//require_once 'Horde/Variables.php'; -//require_once 'Horde/Text/Filter.php'; -// -//// UI classes. -//require_once 'Horde/UI/Tabs.php'; - -$shout_contexts = Shout_Driver::factory('storage'); -$shout_extensions = Shout_Driver::factory('extensions'); -$shout_devices = Shout_Driver::factory('devices'); - -$context = Horde_Util::getFormData('context'); -$section = Horde_Util::getFormData('section'); - -try { - $contexts = $shout_contexts->getContexts(); -} catch (Shout_Exception $e) { - $notification->push($e); - $contexts = false; -} - -if (count($contexts) == 1) { - // Default to the user's only context - if (!empty($context) && $context != $contexts[0]) { - $notification->push(_("You do not have permission to access that context."), 'horde.error'); - } - $context = $contexts[0]; -} elseif (!empty($context) && !in_array($context, $contexts)) { - $notification->push(_("You do not have permission to access that context."), 'horde.error'); - $context = false; -} elseif (!empty($context)) { - $notification->push("Please select a context to continue.", 'horde.info'); - $context = false; -} - -$_SESSION['shout']['context'] = $context; -- 2.11.0