Updated copyright:
[core.git] / inc / classes / main / request / web / class_HttpRequest.php
1 <?php
2 /**
3  * A concrete and secured HTTP request class to make HTTP requests more abstract
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.org
10  * @todo                Move out the cookie part to a seperate class, e.g. Cookie
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 class HttpRequest extends BaseRequest implements Requestable {
26         /**
27          * Protected constructor
28          *
29          * @return      void
30          */
31         protected function __construct () {
32                 // Call parent constructor
33                 parent::__construct(__CLASS__);
34         }
35
36         /**
37          * Creates an instance of this class and prepares it a little
38          *
39          * @return      $httpInstance   An instance of this class
40          */
41         public static final function createHttpRequest () {
42                 // Create an instance
43                 $httpInstance = new HttpRequest();
44
45                 // Prepare the HTTP request data for usage
46                 $httpInstance->prepareRequestData();
47
48                 // Return the prepared instance
49                 return $httpInstance;
50         }
51
52         /**
53          * Prepares the HTTP request data for usage by currently copying
54          * $_REQUEST into a private attribute. Later on we can add more
55          * things for initialization here.
56          *
57          * @return      void
58          */
59         public function prepareRequestData () {
60                 // Copy GET then POST data
61                 $this->setRequestData(array_merge($_GET, $_POST));
62         }
63
64         /**
65          * Getter for a header element or 'null' if header was not found
66          *
67          * @param       $headerName             Name of the header
68          * @return      $headerValue    Value of the header or 'null' if not found
69          */
70         public function getHeaderElement ($headerName) {
71                 // Default return value on error
72                 $headerValue = NULL;
73
74                 // Construct the name
75                 $name = 'HTTP_' . strtoupper($this->convertDashesToUnderscores($headerName));
76
77                 // Does this header exist?
78                 if (isset($_SERVER[$name])) {
79                         $headerValue = $_SERVER[$name];
80                 } // END - if
81
82                 // Return the value
83                 return $headerValue;
84         }
85
86         /**
87          * Getter for request method. This getter might be useful for security filters
88          *
89          * @return      $requestMethod  Used request method
90          */
91         public final function getRequestMethod () {
92                 return $_SERVER['REQUEST_METHOD'];
93         }
94
95         /**
96          * Reads a cookie and returns it's value or null if not found
97          *
98          * @param       $cookieName             Name of cookie we shall read
99          * @return      $cookieValue    Value of cookie or null if not found
100          */
101         public final function readCookie ($cookieName) {
102                 // Default is no cookie with that name found
103                 $cookieValue = NULL;
104
105                 // Is the cookie set?
106                 if (isset($_COOKIE[$cookieName])) {
107                         // Then get it
108                         $cookieValue = $_COOKIE[$cookieName];
109                 } // END - if
110
111                 // Return the value
112                 return $cookieValue;
113         }
114 }
115
116 // [EOF]
117 ?>