-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServer.php
More file actions
155 lines (132 loc) · 3.33 KB
/
Server.php
File metadata and controls
155 lines (132 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
namespace Tribe\Libs\Request;
class Server {
protected $headers;
/**
* Get all request headers, manually if not on Apache.
*
* @return array
*/
public function get_headers(): array {
if ( function_exists( 'getallheaders' ) ) {
return getallheaders();
}
if ( ! is_array( $_SERVER ) ) {
return [];
}
$headers = [];
foreach ( $_SERVER as $name => $value ) {
if ( substr( $name, 0, 5 ) == 'HTTP_' ) {
$headers[ str_replace( ' ', '-', ucwords( strtolower( str_replace( '_', ' ', substr( $name, 5 ) ) ) ) ) ] = $value;
}
}
$this->headers = $headers;
return $this->headers;
}
/**
* Get all of the input values depending on method.
*
* @return array
*/
public function get_input(): array {
if ( $this->has_body() ) {
return $this->get_json_input();
}
switch ( $this->get_method() ) {
case 'POST':
return $_POST;
default:
return $_GET;
}
}
/**
* Check if the request contains body content.
*
* @return bool
*/
public function has_body(): bool {
return ! empty( file_get_contents( 'php://input' ) );
}
/**
* Return request Method type.
*
* @return null|string
*/
public function get_method() {
$method = $_SERVER['REQUEST_METHOD'];
if ( 'POST' === $method ) {
$override = $this->get_header( 'X-HTTP-METHOD-OVERRIDE' );
if ( $override ) {
$method = strtoupper( $override );
}
}
return $method;
}
/**
* Get the JSON values from the request body.
*
* @return array
*/
public function get_json_input(): array {
$json_content = file_get_contents( 'php://input' );
return json_decode( $json_content, true );
}
/**
* Assemble the current URL from the Request.
*
* @param bool $use_forwarded_host
*
* @return string
*/
public function get_url( $use_forwarded_host = false ) {
$s = $_SERVER;
$ssl = ( ! empty( $s['HTTPS'] ) && $s['HTTPS'] == 'on' );
$sp = strtolower( $s['SERVER_PROTOCOL'] );
$protocol = substr( $sp, 0, strpos( $sp, '/' ) ) . ( ( $ssl ) ? 's' : '' );
$port = $s['SERVER_PORT'];
$port = ( ( ! $ssl && $port == '80' ) || ( $ssl && $port == '443' ) ) ? '' : ':' . $port;
$host = ( $use_forwarded_host && isset( $s['HTTP_X_FORWARDED_HOST'] ) ) ? $s['HTTP_X_FORWARDED_HOST'] : ( isset( $s['HTTP_HOST'] ) ? $s['HTTP_HOST'] : null );
$host = isset( $host ) ? $host : $s['SERVER_NAME'] . $port;
return $protocol . '://' . $host;
}
/**
* Get any query parameters from the request.
*
* @return array
*/
public function get_query_params() {
// @phpstan-ignore-next-line
return $_GET ?? [];
}
/**
* Return the current instance of $wp_query. Gets the most-up-to-date global each time as it may change depending
* on when it is referenced.
*
* @return \WP_Query
*/
public function get_query(): \WP_Query {
global $wp_query;
return $wp_query;
}
/**
* Get the current path from the request. Optionally include query parameters.
*
* @param bool $include_params
*
* @return string
*/
public function get_path( $include_params = false ) {
$path = trim( $_SERVER['REQUEST_URI'] ?? '', '/' );
return $include_params ? $path : strtok( $path, '?' );
}
/**
* Get the header value by the specified key.
*
* @param string $key
*
* @return string|null
*/
public function get_header( $key ) {
return $this->headers[ $key ] ?? null;
}
}