| 1 |
<?php |
| 2 |
defined('ABSPATH') || die('Restricted Access'); |
| 3 |
|
| 4 |
/** |
| 5 |
* @param string $url |
| 6 |
* @param array $options |
| 7 |
* - verifySsl: boolean, default true |
| 8 |
* - headers: array for example ['Content-Type' => 'application/json'] |
| 9 |
* - data: array, if Content-Type is application/json, the data will be json_encoded otherwise it will be urlencoded |
| 10 |
* - dns: string |
| 11 |
* - proxy: array ['host' => '127.0.0.1:8888', 'auth' => 'user:password'] the auth is optional |
| 12 |
* - method: string GET, POST, etc |
| 13 |
* |
| 14 |
* @return array |
| 15 |
*/ |
| 16 |
function acym_makeCurlCall(string $url, array $options = []): array |
| 17 |
{ |
| 18 |
$verifySsl = $options['verifySsl'] ?? true; |
| 19 |
$headers = (!empty($options['headers']) && is_array($options['headers'])) ? $options['headers'] : []; |
| 20 |
$data = (!empty($options['data']) && is_array($options['data'])) ? $options['data'] : []; |
| 21 |
$files = (!empty($options['files']) && is_array($options['files'])) ? $options['files'] : []; |
| 22 |
|
| 23 |
$method = (!empty($options['method']) && in_array($options['method'], ['GET', 'POST'], true)) ? $options['method'] : 'GET'; |
| 24 |
|
| 25 |
$multipart = !empty($options['multipart']) || !empty($files); |
| 26 |
|
| 27 |
$contentTypeKey = ''; |
| 28 |
foreach (array_keys($headers) as $headerName) { |
| 29 |
if (strtolower($headerName) === 'content-type') { |
| 30 |
$contentTypeKey = $headerName; |
| 31 |
break; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
$args = [ |
| 36 |
'method' => $method, |
| 37 |
'sslverify' => (bool)$verifySsl, |
| 38 |
'timeout' => 30, |
| 39 |
]; |
| 40 |
|
| 41 |
if ($method === 'POST' && $multipart) { |
| 42 |
if (!empty($files)) { |
| 43 |
global $wp_filesystem; |
| 44 |
if (empty($wp_filesystem)) { |
| 45 |
require_once ABSPATH.'wp-admin/includes/file.php'; |
| 46 |
WP_Filesystem(); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
$boundary = wp_generate_password(24, false); |
| 51 |
$eol = "\r\n"; |
| 52 |
$body = ''; |
| 53 |
foreach ($data as $name => $value) { |
| 54 |
$body .= '--'.$boundary.$eol; |
| 55 |
$body .= 'Content-Disposition: form-data; name="'.$name.'"'.$eol.$eol; |
| 56 |
$body .= $value.$eol; |
| 57 |
} |
| 58 |
foreach ($files as $file) { |
| 59 |
if (empty($file['path']) || empty($wp_filesystem) || !$wp_filesystem->exists($file['path'])) { |
| 60 |
continue; |
| 61 |
} |
| 62 |
|
| 63 |
$content = $wp_filesystem->get_contents($file['path']); |
| 64 |
if ($content === false) { |
| 65 |
continue; |
| 66 |
} |
| 67 |
|
| 68 |
$fileName = $file['filename'] ?? basename($file['path']); |
| 69 |
$body .= '--'.$boundary.$eol; |
| 70 |
$body .= 'Content-Disposition: form-data; name="'.$file['name'].'"; filename="'.$fileName.'"'.$eol; |
| 71 |
$body .= 'Content-Type: application/octet-stream'.$eol.$eol; |
| 72 |
$body .= $content.$eol; |
| 73 |
} |
| 74 |
$body .= '--'.$boundary.'--'.$eol; |
| 75 |
|
| 76 |
if ($contentTypeKey !== '') { |
| 77 |
unset($headers[$contentTypeKey]); |
| 78 |
} |
| 79 |
$headers['Content-Type'] = 'multipart/form-data; boundary='.$boundary; |
| 80 |
$args['body'] = $body; |
| 81 |
} else { |
| 82 |
$dataFormatted = ''; |
| 83 |
if (!empty($data)) { |
| 84 |
$isJson = $contentTypeKey !== '' && strtolower(trim($headers[$contentTypeKey])) === 'application/json'; |
| 85 |
$dataFormatted = ($method === 'POST' && $isJson) ? wp_json_encode($data) : http_build_query($data); |
| 86 |
} |
| 87 |
if ($method === 'GET' && !empty($dataFormatted)) { |
| 88 |
$url .= (strpos($url, '?') === false ? '?' : '&').$dataFormatted; |
| 89 |
} |
| 90 |
if ($method === 'POST' && !empty($dataFormatted)) { |
| 91 |
$args['body'] = $dataFormatted; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
$args['headers'] = $headers; |
| 96 |
|
| 97 |
$response = wp_remote_request($url, $args); |
| 98 |
|
| 99 |
if (is_wp_error($response)) { |
| 100 |
return ['error' => $response->get_error_message(), 'status_code' => 0]; |
| 101 |
} |
| 102 |
|
| 103 |
$httpCode = (int)wp_remote_retrieve_response_code($response); |
| 104 |
$decoded = json_decode(wp_remote_retrieve_body($response), true); |
| 105 |
|
| 106 |
if (!is_array($decoded)) { |
| 107 |
$decoded = []; |
| 108 |
} |
| 109 |
$decoded['status_code'] = $httpCode; |
| 110 |
|
| 111 |
return $decoded; |
| 112 |
} |
| 113 |
|
| 114 |
function acym_asyncUrlCalls(array $urls): void |
| 115 |
{ |
| 116 |
$args = [ |
| 117 |
'timeout' => 5, |
| 118 |
'blocking' => false, |
| 119 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Applying SSL WP filter. |
| 120 |
'sslverify' => apply_filters('https_local_ssl_verify', false), |
| 121 |
]; |
| 122 |
|
| 123 |
$errors = []; |
| 124 |
foreach ($urls as $url) { |
| 125 |
$response = wp_remote_get($url, $args); |
| 126 |
if (is_wp_error($response)) { |
| 127 |
$errors[] = $response->get_error_message(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if (empty($errors)) { |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
$config = acym_config(); |
| 136 |
$reportPath = $config->get('cron_savepath'); |
| 137 |
if (empty($reportPath)) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
global $wp_filesystem; |
| 142 |
if (empty($wp_filesystem)) { |
| 143 |
require_once ABSPATH.'wp-admin/includes/file.php'; |
| 144 |
WP_Filesystem(); |
| 145 |
} |
| 146 |
if (empty($wp_filesystem)) { |
| 147 |
return; |
| 148 |
} |
| 149 |
|
| 150 |
$reportPath = str_replace(['{year}', '{month}'], [gmdate('Y'), gmdate('m')], $reportPath); |
| 151 |
$reportPath = acym_cleanPath(ACYM_ROOT.trim(html_entity_decode($reportPath))); |
| 152 |
acym_createDir(dirname($reportPath), true, true); |
| 153 |
|
| 154 |
$lr = "\r\n"; |
| 155 |
$message = $lr.$lr.'******************** '.acym_getDate(time()).' ********************'.$lr |
| 156 |
.'An error occurred while calling the queue sending script: '.implode(' | ', $errors); |
| 157 |
|
| 158 |
$existing = $wp_filesystem->exists($reportPath) ? $wp_filesystem->get_contents($reportPath) : ''; |
| 159 |
$wp_filesystem->put_contents($reportPath, $existing.$message, FS_CHMOD_FILE); |
| 160 |
} |
| 161 |
|