Fixes for image generation
[shipsimu.git] / inc / classes / main / request / class_HttpRequest.php
index ca85b974043bdca7f154e109b1a8005eb05da14b..0241cfc19f92e29a1cb4b20c268369883780b9cb 100644 (file)
@@ -1,12 +1,12 @@
 <?php
 /**
- * A concrete HTTP request class to make HTTP requests more abstract
+ * A concrete and secured HTTP request class to make HTTP requests more abstract
  *
  * @author             Roland Haeder <webmaster@ship-simu.org>
- * @version            0.3.0
- * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, this is free software
  * @license            GNU GPL 3.0 or any newer version
- * @link               http://www.mxchange.org
+ * @link               http://www.ship-simu.org
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -27,6 +27,13 @@ class HttpRequest extends BaseFrameworkSystem implements Requestable {
         */
        private $requestData = array();
 
+       /**
+        * Wether this request is valid and can be further processed. The default is
+        * valid so make sure your intercepting filters sets this attribute to false
+        * when they need to intercept the data flow.
+        */
+       private $requestIsValid = true;
+
        /**
         * Protected constructor
         *
@@ -36,12 +43,6 @@ class HttpRequest extends BaseFrameworkSystem implements Requestable {
                // Call parent constructor
                parent::__construct(__CLASS__);
 
-               // Set part description
-               $this->setObjectDescription("HTTP-Anfrage");
-
-               // Create unique ID number
-               $this->createUniqueID();
-
                // Clean up a little
                $this->removeNumberFormaters();
                $this->removeSystemArray();
@@ -71,22 +72,21 @@ class HttpRequest extends BaseFrameworkSystem implements Requestable {
         * @return      void
         */
        public function prepareRequestData () {
-               // Copy the $_REQUEST array
-               $this->requestData = $_REQUEST;
+               // Copy GET then POST data
+               $this->requestData = array_merge($_GET, $_POST);
        }
 
        /**
         * Checks wether a request element is set
         * @param       $element        Name of the request element we want to check
         * @return      $isSet          Wether the request element is set
-        * @throws      MissingArrayElementsException   Thrown if a request element is not set
         */
        public function isRequestElementSet ($element) {
                // Is this element found?
                if (!isset($this->requestData[$element])) {
-                       // Then throw an exception
-                       throw new MissingArrayElementsException(array($this, 'requestData', $element), self::EXCEPTION_MISSING_ELEMENT);
-               }
+                       // Then return false
+                       return false;
+               } // END - if
 
                // All clear
                return true;
@@ -103,18 +103,31 @@ class HttpRequest extends BaseFrameworkSystem implements Requestable {
                // Initialize value
                $value = null;
 
-               try {
-                       if ($this->isRequestElementSet($element)) {
-                               $value = $this->requestData[$element];
-                       }
-               } catch (MissingArrayElementsException $e) {
-                       // Do nothing here
-               }
+               // Is the element set?
+               if ($this->isRequestElementSet($element)) {
+                       // Get the bare value
+                       $value = $this->requestData[$element];
+
+                       // Secure it against attacks
+                       $value = htmlentities(strip_tags($value), ENT_QUOTES);
+               } // END - if
 
                // Return the element's value
                return $value;
        }
 
+       /**
+        * Setter for request elements
+        *
+        * @param       $element        Request element to se
+        * @param       $value          Value to set
+        * @return      void
+        */
+       public function setRequestElement ($element, $value) {
+               error_log(__METHOD__.":{$element}={$value}");
+               $this->requestData[$element] = $value;
+       }
+
        /**
         * Wrapper method for array_key() function for the request data array
         *
@@ -140,11 +153,59 @@ class HttpRequest extends BaseFrameworkSystem implements Requestable {
                // Does this header exist?
                if (isset($_SERVER[$name])) {
                        $headerValue = $_SERVER[$name];
-               }
+               } // END - if
 
                // Return the value
                return $headerValue;
        }
+
+       /**
+        * Getter for request method. This getter might be useful for security filters
+        *
+        * @return      $requestMethod  Used request method
+        */
+       public final function getRequestMethod () {
+               return $_SERVER['REQUEST_METHOD'];
+       }
+
+       /**
+        * Sets wether the request was valid (default: true)
+        *
+        * @param       $isValid        Wether the request is valid
+        * @return      void
+        */
+       public final function requestIsValid ($isValid = true) {
+               $this->requestIsValid = (bool) $isValid;
+       }
+
+       /**
+        * Returns wether this request is valid
+        *
+        * @return      $requestIsValid         Wether this request is valid
+        */
+       public final function isRequestValid () {
+               return $this->requestIsValid;
+       }
+
+       /**
+        * Reads a cookie and returns it's value or null if not found
+        *
+        * @param       $cookieName             Name of cookie we shall read
+        * @return      $cookieValue    Value of cookie or null if not found
+        */
+       public final function readCookie ($cookieName) {
+               // Default is no cookie with that name found
+               $cookieValue = null;
+
+               // Is the cookie set?
+               if (isset($_COOKIE[$cookieName])) {
+                       // Then get it
+                       $cookieValue = $_COOKIE[$cookieName];
+               } // END - if
+
+               // Return the value
+               return $cookieValue;
+       }
 }
 
 // [EOF]