Fixes for image generation
[shipsimu.git] / inc / classes / main / request / class_HttpRequest.php
index 820a27ac1a048edfe5db36bfa50a3270bcfc39b6..0241cfc19f92e29a1cb4b20c268369883780b9cb 100644 (file)
@@ -1,10 +1,10 @@
 <?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.0.0
- * @copyright  Copyright(c) 2007, 2008 Roland Haeder, this is free software
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, this is free software
  * @license            GNU GPL 3.0 or any newer version
  * @link               http://www.ship-simu.org
  *
@@ -21,7 +21,7 @@
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
-class HttpRequest extends BaseFrameworkSystem implements Requestable, Iterateable {
+class HttpRequest extends BaseFrameworkSystem implements Requestable {
        /**
         * Array for the request data
         */
@@ -43,12 +43,6 @@ class HttpRequest extends BaseFrameworkSystem implements Requestable, Iterateabl
                // Call parent constructor
                parent::__construct(__CLASS__);
 
-               // Set part description
-               $this->setObjectDescription("HTTP request");
-
-               // Create unique ID number
-               $this->createUniqueID();
-
                // Clean up a little
                $this->removeNumberFormaters();
                $this->removeSystemArray();
@@ -78,22 +72,21 @@ class HttpRequest extends BaseFrameworkSystem implements Requestable, Iterateabl
         * @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;
@@ -110,13 +103,14 @@ class HttpRequest extends BaseFrameworkSystem implements Requestable, Iterateabl
                // 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;
@@ -130,6 +124,7 @@ class HttpRequest extends BaseFrameworkSystem implements Requestable, Iterateabl
         * @return      void
         */
        public function setRequestElement ($element, $value) {
+               error_log(__METHOD__.":{$element}={$value}");
                $this->requestData[$element] = $value;
        }
 
@@ -158,7 +153,7 @@ class HttpRequest extends BaseFrameworkSystem implements Requestable, Iterateabl
                // Does this header exist?
                if (isset($_SERVER[$name])) {
                        $headerValue = $_SERVER[$name];
-               }
+               } // END - if
 
                // Return the value
                return $headerValue;
@@ -176,7 +171,7 @@ class HttpRequest extends BaseFrameworkSystem implements Requestable, Iterateabl
        /**
         * Sets wether the request was valid (default: true)
         *
-        * @param       $isValid                Wether the request is valid
+        * @param       $isValid        Wether the request is valid
         * @return      void
         */
        public final function requestIsValid ($isValid = true) {
@@ -191,6 +186,26 @@ class HttpRequest extends BaseFrameworkSystem implements Requestable, Iterateabl
        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]