From f644ffabf6d1f733816fa0c9218f35e4a7133f6e Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Wed, 23 Sep 2009 17:47:37 -0400 Subject: [PATCH] - Move some random environment modification functionality out of the request base class - Remove random unused locale code from the controller base class - Various tweaks to the base request/response objects - Add some tweaks to the Cli request/response objects for easier CLI handling --- .../lib/Horde/Controller/Request/Base.php | 193 +-------------------- .../lib/Horde/Controller/Request/Cli.php | 10 ++ .../lib/Horde/Controller/Request/Http.php | 3 +- .../lib/Horde/Controller/Request/Mock.php | 3 + .../lib/Horde/Controller/Response/Base.php | 1 - .../lib/Horde/Controller/Response/Cli.php | 52 ++++++ .../lib/Horde/Controller/Response/Http.php | 1 - 7 files changed, 75 insertions(+), 188 deletions(-) diff --git a/framework/Controller/lib/Horde/Controller/Request/Base.php b/framework/Controller/lib/Horde/Controller/Request/Base.php index dc219d666..219de6f44 100644 --- a/framework/Controller/lib/Horde/Controller/Request/Base.php +++ b/framework/Controller/lib/Horde/Controller/Request/Base.php @@ -26,7 +26,7 @@ * @package Horde_Controller * @subpackage Request */ -class Horde_Controller_Request_Base +abstract class Horde_Controller_Request_Base { /** * Request timestamp @@ -167,12 +167,16 @@ class Horde_Controller_Request_Base /** * The request body * + * @TODO Allow overriding php://input, and make the default interface to + * return an SplFileObject, or a (doesn't currently exist) Horde_File + * object, instead of the raw data. + * * @return string */ public function getBody() { if (!isset($this->_body)) { - $this->_body = file_get_contents("php://input"); + $this->_body = file_get_contents('php://input'); } return $this->_body; } @@ -188,70 +192,6 @@ class Horde_Controller_Request_Base } /** - * Get rid of register_globals variables. - * - * @author Richard Heyes - * @author Stefan Esser - * @url http://www.phpguru.org/article.php?ne_id=60 - */ - public function reverseRegisterGlobals() - { - if (ini_get('register_globals')) { - // Variables that shouldn't be unset - $noUnset = array( - 'GLOBALS', - '_GET', - '_POST', - '_COOKIE', - '_REQUEST', - '_SERVER', - '_ENV', - '_FILES', - ); - - $input = array_merge( - $_GET, - $_POST, - $_COOKIE, - $_SERVER, - $_ENV, - $_FILES, - isset($_SESSION) ? $_SESSION : array() - ); - - foreach ($input as $k => $v) { - if (!in_array($k, $noUnset) && isset($GLOBALS[$k])) { - unset($GLOBALS[$k]); - } - } - } - } - - /** - * @author Ilia Alshanetsky - */ - public function reverseMagicQuotes() - { - set_magic_quotes_runtime(0); - if (get_magic_quotes_gpc()) { - $input = array(&$_GET, &$_POST, &$_REQUEST, &$_COOKIE, &$_ENV, &$_SERVER); - - while (list($k, $v) = each($input)) { - foreach ($v as $key => $val) { - if (!is_array($val)) { - $key = stripslashes($key); - $input[$k][$key] = stripslashes($val); - continue; - } - $input[] =& $input[$k][$key]; - } - } - - unset($input); - } - } - - /** * Turn this request into a URL-encoded query string. */ public function __toString() @@ -259,131 +199,14 @@ class Horde_Controller_Request_Base return http_build_query($this); } - public function getPath() - { - } + abstract public function getPath(); /** * Uniquely identify each request from others. This aids in threading - * related log requests during troubleshooting on a busy server + * related log requests during troubleshooting on a busy server */ private function _initRequestId() { $this->_requestId = (string)new Horde_Support_Uuid; } - - /** - * The default locale (eg. en-us) the application uses. - * - * @var string - * @access private - */ - var $_defaultLocale = 'en-us'; - - /** - * The locales (eg. en-us, fi_fi, se_se etc) the application - * supports. - * - * @var array - * @access private - */ - var $_supportedLocales = NULL; - - /** - * Gets the used character encoding. - * - * Returns the name of the character encoding used in the body of - * this request. - * - * @todo implement this method - * @return string the used character encoding - * @access public - */ - function getCharacterEncoding() - { - // XXX: what to do with this? - } - - /** - * Gets the default locale for the application. - * - * @return string the default locale - * @access public - */ - function getDefaultLocale() - { - return $this->_defaultLocale; - } - - /** - * Gets the supported locales for the application. - * - * @return array the supported locales - * @access public - */ - function getSupportedLocales() - { - return $this->_supportedLocales; - } - - /** - * Deduces the clients preferred locale. - * - * You might want to override this method if you want to do more - * sophisticated decisions. It gets the supported locales and the - * default locale from the class attributes file and tries to find a - * match. If no match is found it uses the default locale. The - * locale is always changed into lowercase. - * - * @return string the locale - * @access public - */ - function getLocale() - { - require_once('HTTP.php'); - - if ($this->_supportedLocales == NULL) { - return $this->_defaultLocale; - } else { - return strtolower(HTTP::negotiateLanguage( $this->_supportedLocales, - $this->_defaultLocale )); - } - } - - /** - * Sets the default locale for the application. - * - * Create an instance of Ismo_Core_Request manually and - * set the default locale with this method. Then add it as the - * application's request class with - * Ismo_Core_Application::setRequest(). - * - * @param string $locale the default locale - * @access public - */ - function setDefaultLocale($locale) - { - $this->_defaultLocale = str_replace('_', '-', $locale); - } - - /** - * Sets the locales supported by the application. - * - * Create an instance of Ismo_Core_Request manually and - * set the supported locales with this method. Then add it as the - * application's request class with - * Ismo_Core_Application::setRequest(). - * - * @param array $locales the locales - * @access public - */ - function setSupportedLocales($locales) - { - if (is_array($locales)) { - foreach ($locales as $n => $locale) { - $this->_supportedLocales[ str_replace('_', '-', $locale) ] = true; - } - } - } - } diff --git a/framework/Controller/lib/Horde/Controller/Request/Cli.php b/framework/Controller/lib/Horde/Controller/Request/Cli.php index ec61c4758..49ad504d1 100644 --- a/framework/Controller/lib/Horde/Controller/Request/Cli.php +++ b/framework/Controller/lib/Horde/Controller/Request/Cli.php @@ -56,6 +56,16 @@ class Horde_Controller_Request_Cli extends Horde_Controller_Request_Base return $this->getPath(); } + public function getMethod() + { + return 'CLI'; + } + + public function setArgv($args) + { + $this->_argv = $args; + } + public function getPath() { return $this->_path; diff --git a/framework/Controller/lib/Horde/Controller/Request/Http.php b/framework/Controller/lib/Horde/Controller/Request/Http.php index c076161e2..1b15e7bba 100644 --- a/framework/Controller/lib/Horde/Controller/Request/Http.php +++ b/framework/Controller/lib/Horde/Controller/Request/Http.php @@ -403,6 +403,7 @@ class Horde_Controller_Request_Http extends Horde_Controller_Request_Base return $this->_sessionId; } + /*########################################################################## # Modifiers ##########################################################################*/ @@ -480,7 +481,7 @@ class Horde_Controller_Request_Http extends Horde_Controller_Request_Base } $_FILES = array_map(array($this, '_fixNestedFiles'), $_FILES); - // create FileUpload object of of the file options + // create FileUpload object of the file options foreach ((array)$_FILES as $name => $options) { if (isset($options['tmp_name'])) { $this->_files[$name] = new Horde_Controller_FileUpload($options); diff --git a/framework/Controller/lib/Horde/Controller/Request/Mock.php b/framework/Controller/lib/Horde/Controller/Request/Mock.php index a595f264e..575cefd3c 100644 --- a/framework/Controller/lib/Horde/Controller/Request/Mock.php +++ b/framework/Controller/lib/Horde/Controller/Request/Mock.php @@ -23,4 +23,7 @@ */ class Horde_Controller_Request_Mock extends Horde_Controller_Request_Base { + public function getPath() + { + } } diff --git a/framework/Controller/lib/Horde/Controller/Response/Base.php b/framework/Controller/lib/Horde/Controller/Response/Base.php index 473f0e1c0..a6963f8c5 100644 --- a/framework/Controller/lib/Horde/Controller/Response/Base.php +++ b/framework/Controller/lib/Horde/Controller/Response/Base.php @@ -36,5 +36,4 @@ class Horde_Controller_Response_Base { $this->_body = $body; } - } diff --git a/framework/Controller/lib/Horde/Controller/Response/Cli.php b/framework/Controller/lib/Horde/Controller/Response/Cli.php index e638b5916..36eee0187 100644 --- a/framework/Controller/lib/Horde/Controller/Response/Cli.php +++ b/framework/Controller/lib/Horde/Controller/Response/Cli.php @@ -13,6 +13,9 @@ */ /** + * @TODO Allow specifying the stream where output is going instead of assuming + * STDOUT. + * * @author Mike Naberezny * @author Derek DeVries * @author Chuck Hagenbuch @@ -23,4 +26,53 @@ */ class Horde_Controller_Response_Cli extends Horde_Controller_Response_Base { + /** + * @var stream + */ + protected $_stream; + + public function construct() + { + $this->_stream = fopen('php://stdout'); + } + + /** + * Writes a string to the Response stream + * + * Can be called with an array of parameters or with a variable number of + * parameters like printf. + * + * @param string $string The string to write to the reponse stream + * @param array|params $params The parameters to replace in the string (think printf) + */ + public function write($string, $params = null) + { + if (!is_array($params)) { + $params = func_get_args(); + array_shift($params); + } + fwrite($this->_stream, vsprintf($string, $params)); + } + + /** + * Writes a newline-terminated string to the Response stream + * + * Can be called with an array of parameters or with a variable number of + * parameters like printf. + * + * @param string $string The string to write to the reponse stream + * @param array|params $params The parameters to replace in the string (think printf) + */ + public function writeLn($string, $params = array()) + { + if (!is_array($params)) { + $params = func_get_args(); + array_shift($params); + } + $line = vsprintf($string, $params); + if (substr($line, -1) != "\n") { + $line .= "\n"; + } + fwrite($this->_stream, $line); + } } diff --git a/framework/Controller/lib/Horde/Controller/Response/Http.php b/framework/Controller/lib/Horde/Controller/Response/Http.php index e26729978..abd4f11fb 100644 --- a/framework/Controller/lib/Horde/Controller/Response/Http.php +++ b/framework/Controller/lib/Horde/Controller/Response/Http.php @@ -220,5 +220,4 @@ class Horde_Controller_Response_Http extends Horde_Controller_Response_Base { header('HTTP/1.0 ' . $code); } - } -- 2.11.0