PluginProbe
Dr. Flex / trunk
Dr. Flex vtrunk
trunk 1.0.2 1.0.3 1.0.4 2.0.0 2.0.1 2.0.2 2.0.3
dr-flex / components / drflex_utils.php

drflex_utils.php in Dr. Flex trunk, at components/drflex_utils.php

202 lines 5.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 *
5 *
6 * @category The Dr. Flex plugin utility functions.
7 * @author Johannes Dato
8 * @copyright Copyright (c) 2020
9 * @link https://dr-flex.de
10 * @version 2.0.3
11 */
12 include_once('drflex_constants.php');
13
14 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
15 //
16 // Plugin specific utilities.
17
18 /**
19 * Fetches a resource from a URL.
20 *
21 * Function to fetch a resource from a URL and return it in an array in the following format:
22 * array(
23 * 'content' => 'This is an example result.',
24 * 'content-type' => 'text/plain'
25 * );
26 *
27 * @since 1.0.0
28 *
29 * @param string $url The url to fetch a resource from.
30 * @return array|false Array with content and content-type of the requested resource or false if request results in error.
31 */
32 function drflex_utils_fetch_from_url($url, $is_dr_flex_host = false)
33 {
34 $content = null;
35 $content_type = null;
36 $status = null;
37
38 $request_headers = array(
39 );
40
41 $api_key = get_option('drflex_api_key', null);
42 if ($api_key !== null) {
43 $request_headers["Authorization"] = "Bearer " . $api_key;
44 }
45
46 $accept_encoding = isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : '';
47 $encodings = explode(', ', $accept_encoding);
48 if (in_array('br', $encodings)) {
49 $request_headers["X-Accept-Encoding"] = "br";
50 }
51
52 if ($is_dr_flex_host) {
53 $url = $GLOBALS['drflex_host'] . $url;
54 }
55
56 $args = array(
57 "headers" => $request_headers,
58 );
59
60 $response = null;
61 $attenpts = 1;
62
63 $was_request_successful = false;
64 while ( ( !$was_request_successful ) && $attenpts < 4 ) {
65 $attenpts++;
66
67 $response = wp_remote_get( $url, $args );
68 $was_request_successful = ! is_array( $response ) || is_wp_error( $response );
69 }
70
71 if ( is_array( $response ) && ! is_wp_error( $response ) ) {
72 $headers = $response['headers'];
73 $content_type = $headers["Content-Type"];
74 $x_content_encoding = $headers["X-Content-Encoding"];
75 $status = $response["response"]["code"];
76 $content = $response['body'];
77 } else {
78 drflex_error("Could not receive a response for url: $url.");
79 return false;
80 }
81
82 return array(
83 'content' => $content,
84 'content-type' => $content_type,
85 'x-content-encoding' => $x_content_encoding,
86 'status' => $status
87 );
88 }
89
90 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
91 //
92 // General utilities.
93
94 /**
95 * Checks if a target string ends with an end string.
96 *
97 * @since 1.0.0
98 *
99 * @param string $target String that will be checked.
100 * @param string $end sequence that the target string will be checked for.
101 * @return boolean Boolean indicating whether the target string ends with end.
102 *
103 */
104 function drflex_utils_ends_with($target, $end)
105 {
106 $length = strlen($end);
107 if (!$length) {
108 return true;
109 }
110 return substr($target, -$length) === $end;
111 }
112
113 /**
114 * Unescapes an escaped JSON document.
115 *
116 * The following rules are applied with the unescaping process:
117 *
118 * Newline is replaced with \n
119 * Carriage return is replaced with \r
120 * Double quote is replaced with \"
121 * Backslash is replaced with \\
122 *
123 * @since 1.0.0
124 *
125 * @param string $escaped_json The json string to be unescaped.
126 * @return string The unescaped json string.
127 *
128 */
129 function drflex_utils_json_unescape($escaped_json)
130 {
131 $result = str_replace("\\\\", "\\", $escaped_json);
132 $result = str_replace("\\\"", "\"", $result);
133 $result = str_replace("\\r", "\r", $result);
134 $result = str_replace("\\n", "\n", $result);
135 return $result;
136 }
137
138 /**
139 * Function to separate a url and its arguments
140 *
141 * When supplied with a given URL e.g. https://example.com?arg1=foo&arg2=bar
142 * it returns the following array:
143 *
144 * array(
145 * 'path' => 'https://example.com',
146 * 'arguments' =>
147 * array(
148 * 'arg1' = 'foo',
149 * 'arg2' = 'bar',
150 * )
151 * );
152 *
153 * @since 1.0.0
154 *
155 * @param string $url_with_args URL with arguments
156 * @return array path and arguments separated
157 *
158 */
159 function drflex_utils_parse_url_with_arguments($url_with_args)
160 {
161 // Parse arguments
162 $path_and_arguments = explode('?', $url_with_args);
163 $path = $path_and_arguments[0];
164 $parsed_arguments = array();
165
166 if (count($path_and_arguments) > 1) {
167 $arguments = $path_and_arguments[1];
168
169 if (!empty($arguments)) {
170 $args_array = explode('&', $arguments);
171 if (!empty($args_array)) {
172 foreach ($args_array as $arg) {
173 if ($arg != '') {
174 $arg_map = explode('=', $arg);
175 if (!empty($arg_map)) {
176 if (count($arg_map) == 2) {
177 $parsed_arguments[$arg_map[0]] = $arg_map[1];
178 } else if (count($arg_map) == 1) {
179 $parsed_arguments[$arg_map[0]] = 'no_value';
180 }
181 }
182 }
183 }
184 }
185 }
186 }
187
188 return array(
189 'path' => $path,
190 'arguments' => $parsed_arguments
191 );
192 }
193
194 /**
195 * Function that returns the rest route that is the base rest route for all the cache requests.
196 *
197 * @return string rest route base url
198 */
199 function drflex_utils_get_rest_route()
200 {
201 return '?rest_route=/' . $GLOBALS['drflex_wordpress_rest_prefix'] . '/';
202 }