PluginProbe
Sessions / 2.3.1
Sessions v2.3.1
2.1.0 2.10.0 2.11.0 2.12.0 2.13.0 2.13.1 2.13.2 2.13.3 2.14.0 2.2.0 2.3.0 2.3.1 2.4.0 2.4.1 2.5.0 2.6.0 2.6.1 2.6.2 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.1.0 3.1.1 All 39 releases
sessions / includes / system / class-http.php

class-http.php in Sessions 2.3.1, at includes/system/class-http.php

222 lines 6.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * HTTP handling
4 *
5 * Handles all HTTP operations and detection.
6 *
7 * @package System
8 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
9 * @since 1.0.0
10 */
11
12 namespace POSessions\System;
13
14 /**
15 * Define the HTTP functionality.
16 *
17 * Handles all HTTP operations and detection.
18 *
19 * @package System
20 * @author Pierre Lannoy <https://pierre.lannoy.fr/>.
21 * @since 1.0.0
22 */
23 class Http {
24
25 /**
26 * The list of available verbs.
27 *
28 * @since 1.0.0
29 * @var array $verbs Maintains the verbs list.
30 */
31 public static $verbs = [ 'get', 'head', 'post', 'put', 'delete', 'connect', 'options', 'trace', 'patch', 'unknown' ];
32
33 /**
34 * The list of available contexts.
35 *
36 * @since 1.0.0
37 * @var array $contexts Maintains the contexts list.
38 */
39 public static $contexts = [ 'inbound', 'outbound', 'unknown' ];
40
41 /**
42 * The list of available schemes.
43 *
44 * @since 1.0.0
45 * @var array $schemes Maintains the schemes list.
46 */
47 public static $schemes = [ 'http', 'https', 'unknown' ];
48
49 /**
50 * The list of HTTP codes meaning success.
51 *
52 * @since 1.0.0
53 * @var array $http_success_codes Maintains the success codes list.
54 */
55 public static $http_success_codes = [ 100, 101, 102, 200, 201, 202, 203, 204, 205, 206, 207, 300, 301, 302, 303, 304, 305, 306, 307, 308 ];
56
57 /**
58 * The list of HTTP codes meaning effective pass.
59 *
60 * @since 1.0.0
61 * @var array $http_success_codes Maintains the effective pass codes list.
62 */
63 public static $http_effective_pass_codes = [ 200, 201, 202 ];
64
65 /**
66 * The list of HTTP codes meaning error.
67 *
68 * @since 1.0.0
69 * @var array $http_error_codes Maintains the error codes list.
70 */
71 public static $http_error_codes = [ 400, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 422, 423, 424, 425, 426, 428, 429, 431, 444, 449, 450, 451, 494, 495, 496, 497, 499, 500, 501, 502, 503, 504, 505, 506, 507, 508, 509, 510, 511, 598, 599 ];
72
73 /**
74 * The list of HTTP codes meaning global failure.
75 *
76 * @since 1.0.0
77 * @var array $http_failure_codes Maintains the failure codes list.
78 */
79 public static $http_failure_codes = [ 0 ];
80
81 /**
82 * The list of HTTP codes meaning quota problems.
83 *
84 * @since 1.0.0
85 * @var array $http_quota_codes Maintains the quota problems codes list.
86 */
87 public static $http_quota_codes = [ 429, 509, 999 ];
88
89 /**
90 * The list of possible "not" sub TLDs.
91 *
92 * @since 1.0.0
93 * @var array $not_sub_tlds Maintains the possible "not" sub TLDs list.
94 */
95 public static $not_sub_tlds = [ 'wp' ];
96
97 /**
98 * The list of HTTP status.
99 *
100 * @since 1.0.0
101 * @var array $http_status_codes Maintains the status list.
102 */
103 public static $http_status_codes = [
104 000 => 'Global timeout',
105 100 => 'Continue',
106 101 => 'Switching Protocols',
107 102 => 'Processing',
108 200 => 'OK',
109 201 => 'Created',
110 202 => 'Accepted',
111 203 => 'Non-Authoritative Information',
112 204 => 'No Content',
113 205 => 'Reset Content',
114 206 => 'Partial Content',
115 207 => 'Multi-Status',
116 300 => 'Multiple Choices',
117 301 => 'Moved Permanently',
118 302 => 'Found',
119 303 => 'See Other',
120 304 => 'Not Modified',
121 305 => 'Use Proxy',
122 306 => '(Unused)',
123 307 => 'Temporary Redirect',
124 308 => 'Permanent Redirect',
125 400 => 'Bad Request',
126 401 => 'Unauthorized',
127 402 => 'Payment Required',
128 403 => 'Forbidden',
129 404 => 'Not Found',
130 405 => 'Method Not Allowed',
131 406 => 'Not Acceptable',
132 407 => 'Proxy Authentication Required',
133 408 => 'Request Timeout',
134 409 => 'Conflict',
135 410 => 'Gone',
136 411 => 'Length Required',
137 412 => 'Precondition Failed',
138 413 => 'Request Entity Too Large',
139 414 => 'Request-URI Too Long',
140 415 => 'Unsupported Media Type',
141 416 => 'Requested Range Not Satisfiable',
142 417 => 'Expectation Failed',
143 418 => 'I\'m a teapot',
144 419 => 'Authentication Timeout',
145 420 => 'Enhance Your Calm',
146 422 => 'Unprocessable Entity',
147 423 => 'Locked',
148 424 => 'Method Failure',
149 425 => 'Unordered Collection',
150 426 => 'Upgrade Required',
151 428 => 'Precondition Required',
152 429 => 'Too Many Requests',
153 431 => 'Request Header Fields Too Large',
154 444 => 'No Response',
155 449 => 'Retry With',
156 450 => 'Blocked by Windows Parental Controls',
157 451 => 'Unavailable For Legal Reasons',
158 494 => 'Request Header Too Large',
159 495 => 'Cert Error',
160 496 => 'No Cert',
161 497 => 'HTTP to HTTPS',
162 499 => 'Client Closed Request',
163 500 => 'Internal Server Error',
164 501 => 'Not Implemented',
165 502 => 'Bad Gateway',
166 503 => 'Service Unavailable',
167 504 => 'Gateway Timeout',
168 505 => 'HTTP Version Not Supported',
169 506 => 'Variant Also Negotiates',
170 507 => 'Insufficient Storage',
171 508 => 'Loop Detected',
172 509 => 'Bandwidth Limit Exceeded',
173 510 => 'Not Extended',
174 511 => 'Network Authentication Required',
175 598 => 'Network read timeout error',
176 599 => 'Network connect timeout error',
177 999 => 'Forbidden by quota manager',
178 ];
179
180 /**
181 * Set the plugin user agent.
182 *
183 * @return string The user agent to use.
184 * @since 1.0.0
185 */
186 public static function user_agent() {
187 return POSE_PRODUCT_NAME . ' (' . Environment::wordpress_version_id() . '; ' . Environment::plugin_version_id() . '; +' . POSE_PRODUCT_URL . ')';
188 }
189
190 /**
191 * Get the top domain of a full fqdn host.
192 *
193 * @param string $host The host.
194 * @param boolean $resolve Optional. Try to resolve the host name.
195 * @return string The top domain.
196 * @since 1.0.0
197 */
198 public static function top_domain( $host, $resolve = true ) {
199 if ( filter_var( $host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 | FILTER_FLAG_IPV4 ) ) {
200 if ( $resolve ) {
201 if ( filter_var( $host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_RES_RANGE | FILTER_FLAG_NO_PRIV_RANGE ) ) {
202 return self::top_domain( gethostbyaddr( $host ), false );
203 } else {
204 return $host;
205 }
206 } else {
207 return $host;
208 }
209 }
210 $parts = explode( '.', $host );
211 $middle_part = array_slice( $parts, -2, 1 );
212 $mid = $middle_part[0];
213 $subtld = ( strlen( $mid ) <= 3 );
214 if ( in_array( $mid, self::$not_sub_tlds, true ) ) {
215 $subtld = false;
216 }
217 $slice = ( $subtld && ( count( $parts ) > 2 ) ) ? 3 : 2;
218 $result = implode( '.', array_slice( $parts, ( 0 - $slice ), $slice ) );
219 return $result;
220 }
221 }
222