Completed
Push — master ( c9558f...4ab188 )
by Stefano
02:24
created

Request::server()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 9.2
cc 4
eloc 2
nc 8
nop 2
1
<?php
2
3
/**
4
 * Request
5
 *
6
 * Handles the HTTP request for the current execution.
7
 *
8
 * @package core
9
 * @author [email protected]
10
 * @copyright Caffeina srl - 2015 - http://caffeina.it
11
 */
12
13
class Request {
14
  use Module;
15
16
  protected static $body,
17
                   $accepts;
18
19
  /**
20
   * Handle Content Negotiation requests
21
   *
22
   * @param  string $key The name of the negotiation subject
23
   * @param  string $choices A query string for the negotiation choices (See RFC 7231)
24
   *
25
   * @return Object The preferred content if $choices is empty else return best match
26
   */
27
  public static function accept($key='type',$choices=''){
28
    if (null === static::$accepts) static::$accepts = [
29
      'type'     => new Negotiation(isset($_SERVER['HTTP_ACCEPT'])          ? $_SERVER['HTTP_ACCEPT']          : ''),
30
      'language' => new Negotiation(isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : ''),
31
      'encoding' => new Negotiation(isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : ''),
32
      'charset'  => new Negotiation(isset($_SERVER['HTTP_ACCEPT_CHARSET'])  ? $_SERVER['HTTP_ACCEPT_CHARSET']  : ''),
33
    ];
34
    return empty(static::$accepts[$key])
35
      ? false
36
      : ( empty($choices)
37
           ? static::$accepts[$key]->preferred()
38
           : static::$accepts[$key]->best($choices)
39
      );
40
  }
41
42
  /**
43
   * Retrive a value from generic input (from the $_REQUEST array)
44
   * Returns all elements if you pass `null` as $key
45
   *
46
   * @param  string $key The name of the input value
47
   *
48
   * @return Object The returned value or $default.
49
   */
50
  public static function input($key=null,$default=null){
51
    return $key ? (isset($_REQUEST[$key]) ? new Object($_REQUEST[$key]) : (is_callable($default)?call_user_func($default):$default))  : new Object($_REQUEST[$key]);
52
  }
53
54
  /**
55
   * Retrive a value from environment (from the $_ENV array)
56
   * Returns all elements if you pass `null` as $key
57
   *
58
   * @param  string $key The name of the input value
59
   *
60
   * @return Object The returned value or $default.
61
   */
62
  public static function env($key=null,$default=null){
63
    return $key ? (filter_input(INPUT_ENV,$key) ?: (is_callable($default)?call_user_func($default):$default))  : $_ENV;
64
  }
65
66
  /**
67
   * Retrive a value from server (from the $_SERVER array)
68
   * Returns all elements if you pass `null` as $key
69
   *
70
   * @param  string $key The name of the input value
71
   *
72
   * @return Object The returned value or $default.
73
   */
74
  public static function server($key=null,$default=null){
75
    return $key ? (filter_input(INPUT_SERVER,$key) ?: (is_callable($default)?call_user_func($default):$default))  : $_SERVER;
76
  }
77
78
  /**
79
   * Retrive a value from generic input (from the $_POST array)
80
   * Returns all elements if you pass `null` as $key
81
   *
82
   * @param  string $key The name of the input value
83
   *
84
   * @return Object The returned value or $default.
85
   */
86
  public static function post($key=null,$default=null){
87
    return $key ? (filter_input(INPUT_POST,$key) ?: (is_callable($default)?call_user_func($default):$default))  : $_POST;
88
  }
89
90
  /**
91
   * Retrive a value from generic input (from the $_GET array)
92
   * Returns all elements if you pass `null` as $key
93
   *
94
   * @param  string $key The name of the input value
95
   *
96
   * @return Object The returned value or $default.
97
   */
98
  public static function get($key=null,$default=null){
99
    return $key ? (filter_input(INPUT_GET,$key) ?: (is_callable($default)?call_user_func($default):$default))  : $_GET;
100
  }
101
102
  /**
103
   * Retrive uploaded file (from the $_FILES array)
104
   * Returns all uploaded files if you pass `null` as $key
105
   *
106
   * @param  string $key The name of the input value
107
   *
108
   * @return Object The returned value or $default.
109
   */
110
  public static function files($key=null,$default=null){
111
    return $key ? (isset($_FILES[$key]) ? $_FILES[$key] : (is_callable($default)?call_user_func($default):$default))  : $_FILES;
112
  }
113
114
  /**
115
   * Retrive cookie (from the $_COOKIE array)
116
   * Returns all cookies if you pass `null` as $key
117
   *
118
   * @param  string $key The name of the input value
119
   *
120
   * @return Object The returned value or $default.
121
   */
122
  public static function cookie($key=null,$default=null){
123
    return $key ? (filter_input(INPUT_COOKIE,$key) ?: (is_callable($default)?call_user_func($default):$default))  : $_COOKIE;
124
  }
125
126
  /**
127
   * Returns the current host, complete with protocol (pass `false` to omit).
128
   *
129
   * @return string
130
   */
131
  public static function host($protocol=true){
132
    $host = filter_input(INPUT_SERVER,'HOSTNAME') ?: (
133
          filter_input(INPUT_SERVER,'SERVER_NAME') ?:
134
          filter_input(INPUT_SERVER,'HTTP_HOST')
135
    );
136
    return ($protocol ? 'http' . (filter_input(INPUT_SERVER,'HTTPS')?'s':'') . '://' : '') . Filter::with('core.request.host',$host);
137
  }
138
139
  /**
140
   * Returns the current request URL, complete with host and protocol.
141
   *
142
   * @return string
143
   */
144
  public static function URL(){
145
    return static::host(true) . static::URI(false);
146
  }
147
148
  /**
149
   * Retrive header
150
   * Returns all headers if you pass `null` as $key
151
   *
152
   * @param  string $key The name of the input value
153
   *
154
   * @return Object The returned value or null.
155
   */
156
  public static function header($key=null,$default=null){
157
    $key = 'HTTP_'.strtr(strtoupper($key),'-','_');
158
    return $key ? (filter_input(INPUT_SERVER,$key) ?: (is_callable($default)?call_user_func($default):$default)) : $_SERVER;
159
  }
160
161
  /**
162
   * Returns the current request URI.
163
   *
164
   * @param  boolean $relative If true, trim the URI relative to the application index.php script.
165
   *
166
   * @return string
167
   */
168
  public static function URI($relative=true){
169
    // On some web server configurations PHP_SELF is not populated.
170
    $self = filter_input(INPUT_SERVER,'SCRIPT_NAME') ?: filter_input(INPUT_SERVER,'PHP_SELF');
171
    // Search REQUEST_URI in $_SERVER
172
    $serv_uri = filter_input(INPUT_SERVER,'PATH_INFO') ?: (
173
          filter_input(INPUT_SERVER,'ORIG_PATH_INFO') ?:
174
          filter_input(INPUT_SERVER,'REQUEST_URI')
175
    );
176
    $uri = strtok($serv_uri,'?');
177
    $uri = ($uri == $self) ? '/' : $uri;
178
179
    // Add a filter here, for URL rewriting
180
    $uri = Filter::with('core.request.URI',$uri);
181
182
    $uri = rtrim($uri,'/');
183
184
    if ($relative){
185
      $base = rtrim(dirname($self),'/');
186
      $uri = str_replace($base,'',$uri);
187
    }
188
189
    return $uri ?: '/';
190
  }
191
192
  /**
193
   * Returns the current base URI (The front-controller directory)
194
   *
195
   * @return string
196
   */
197
  public static function baseURI(){
198
    // On some web server configurations PHP_SELF is not populated.
199
    $uri = dirname(filter_input(INPUT_SERVER,'SCRIPT_NAME') ?: filter_input(INPUT_SERVER,'PHP_SELF'));
200
    return $uri ?: '/';
201
  }
202
203
  /**
204
   * Returns the HTTP Method
205
   *
206
   * @return string
207
   */
208
  public static function method(){
209
   return Filter::with('core.request.method',strtolower(filter_input(INPUT_SERVER,'REQUEST_METHOD')?:'get'));
210
  }
211
212
  /**
213
   * Returns the remote IP
214
   *
215
   * @return string
216
   */
217
  public static function IP(){
218
   return Filter::with('core.request.IP',strtolower(filter_input(INPUT_SERVER,'REMOTE_ADDR')?:''));
219
  }
220
221
  /**
222
   * Returns the remote UserAgent
223
   *
224
   * @return string
225
   */
226
  public static function UA(){
227
   return Filter::with('core.request.UA',strtolower(filter_input(INPUT_SERVER,'HTTP_USER_AGENT')?:''));
228
  }
229
230
231
  /**
232
   * Returns request body data, convert to object if content type is JSON
233
   * Gives you all request data if you pass `null` as $key
234
   *
235
   * @param  string $key The name of the key requested
236
   *
237
   * @return mixed The request body data
238
   */
239
  public static function data($key=null,$default=null){
240
    if (null===static::$body){
241
      $json = (false !== stripos(filter_input(INPUT_SERVER,'HTTP_CONTENT_TYPE'),'json'))
242
           || (false !== stripos(filter_input(INPUT_SERVER,'CONTENT_TYPE'),'json'));
243
      if ($json) {
244
        static::$body = json_decode(file_get_contents("php://input"));
245
      } else {
246
       if (empty($_POST)) {
247
          static::$body = file_get_contents("php://input");
248
        } else {
249
          static::$body = (object)$_POST;
250
        }
251
      }
252
    }
253
    return $key ? (isset(static::$body->$key) ? static::$body->$key : (is_callable($default)?call_user_func($default):$default))  : static::$body;
254
  }
255
256
}
257