Added isGetRequestMethod(), isHeadRequestMethod() and isPostRequestMethod().
[core.git] / inc / classes / main / request / html / class_HtmlRequest.php
1 <?php
2 /**
3  * A concrete and secured HTML request class to make HTML 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 HtmlRequest 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      $requestInstance        An instance of this class
40          */
41         public static final function createHtmlRequest () {
42                 // Create an instance
43                 $requestInstance = new HtmlRequest();
44
45                 // Prepare the HTML request data for usage
46                 $requestInstance->prepareRequestData();
47
48                 // Return the prepared instance
49                 return $requestInstance;
50         }
51
52         /**
53          * Checks if the request method is GET.
54          *
55          * @return      $isGet  Whether the request method is GET
56          */
57         public function isGetRequestMethod () {
58                 // Check it
59                 return ($this->getRequestMethod() == 'GET');
60         }
61
62         /**
63          * Checks if the request method is HEAD.
64          *
65          * @return      $isHead         Whether the request method is HEAD
66          */
67         public function isHeadRequestMethod () {
68                 // Check it
69                 return ($this->getRequestMethod() == 'HEAD');
70         }
71
72         /**
73          * Checks if the request method is POST.
74          *
75          * @return      $isPost         Whether the request method is POST
76          */
77         public function isPostRequestMethod () {
78                 // Check it
79                 return ($this->getRequestMethod() == 'POST');
80         }
81
82         /**
83          * Prepares the HTML request data for usage by currently copying
84          * $_REQUEST into a private attribute. Later on we can add more
85          * things for initialization here.
86          *
87          * @return      void
88          */
89         public function prepareRequestData () {
90                 // Copy GET then POST data
91                 $this->setRequestData(array_merge($_GET, $_POST));
92         }
93
94         /**
95          * Getter for a header element or 'null' if header was not found
96          *
97          * @param       $headerName             Name of the header
98          * @return      $headerValue    Value of the header or 'null' if not found
99          */
100         public function getHeaderElement ($headerName) {
101                 // Default return value on error
102                 $headerValue = NULL;
103
104                 // Construct the name
105                 $name = 'HTTP_' . strtoupper($this->convertDashesToUnderscores($headerName));
106
107                 // Does this header exist?
108                 if (isset($_SERVER[$name])) {
109                         $headerValue = $_SERVER[$name];
110                 } // END - if
111
112                 // Return the value
113                 return $headerValue;
114         }
115
116         /**
117          * Getter for request method. This getter might be useful for security filters
118          *
119          * @return      $requestMethod  Used request method
120          */
121         public final function getRequestMethod () {
122                 return $_SERVER['REQUEST_METHOD'];
123         }
124
125         /**
126          * Reads a cookie and returns it's value or null if not found
127          *
128          * @param       $cookieName             Name of cookie we shall read
129          * @return      $cookieValue    Value of cookie or null if not found
130          */
131         public final function readCookie ($cookieName) {
132                 // Default is no cookie with that name found
133                 $cookieValue = NULL;
134
135                 // Is the cookie set?
136                 if (isset($_COOKIE[$cookieName])) {
137                         // Then get it
138                         $cookieValue = $_COOKIE[$cookieName];
139                 } // END - if
140
141                 // Return the value
142                 return $cookieValue;
143         }
144 }
145
146 // [EOF]
147 ?>