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