- Move some random environment modification functionality out of the request
authorChuck Hagenbuch <chuck@horde.org>
Wed, 23 Sep 2009 21:47:37 +0000 (17:47 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Wed, 23 Sep 2009 21:47:37 +0000 (17:47 -0400)
  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

framework/Controller/lib/Horde/Controller/Request/Base.php
framework/Controller/lib/Horde/Controller/Request/Cli.php
framework/Controller/lib/Horde/Controller/Request/Http.php
framework/Controller/lib/Horde/Controller/Request/Mock.php
framework/Controller/lib/Horde/Controller/Response/Base.php
framework/Controller/lib/Horde/Controller/Response/Cli.php
framework/Controller/lib/Horde/Controller/Response/Http.php

index dc219d6..219de6f 100644 (file)
@@ -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 <ilia@php.net>
-     */
-    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 <code>Ismo_Core_Request</code> manually and
-     * set the default locale with this method. Then add it as the
-     * application's request class with
-     * <code>Ismo_Core_Application::setRequest()</code>.
-     *
-     * @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 <code>Ismo_Core_Request</code> manually and
-     * set the supported locales with this method. Then add it as the
-     * application's request class with
-     * <code>Ismo_Core_Application::setRequest()</code>.
-     *
-     * @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;
-            }
-        }
-    }
-
 }
index ec61c47..49ad504 100644 (file)
@@ -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;
index c076161..1b15e7b 100644 (file)
@@ -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);
index a595f26..575cefd 100644 (file)
@@ -23,4 +23,7 @@
  */
 class Horde_Controller_Request_Mock extends Horde_Controller_Request_Base
 {
+    public function getPath()
+    {
+    }
 }
index e638b59..36eee01 100644 (file)
@@ -13,6 +13,9 @@
  */
 
 /**
+ * @TODO Allow specifying the stream where output is going instead of assuming
+ * STDOUT.
+ *
  * @author     Mike Naberezny <mike@maintainable.com>
  * @author     Derek DeVries <derek@maintainable.com>
  * @author     Chuck Hagenbuch <chuck@horde.org>
  */
 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);
+    }
 }
index e267299..abd4f11 100644 (file)
@@ -220,5 +220,4 @@ class Horde_Controller_Response_Http extends Horde_Controller_Response_Base
     {
         header('HTTP/1.0 ' . $code);
     }
-
 }