| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\PhpOffice\PhpSpreadsheet\Calculation\Web; |
| 4 |
|
| 5 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Functions; |
| 6 |
use TablePress\PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError; |
| 7 |
use TablePress\PhpOffice\PhpSpreadsheet\Cell\Cell; |
| 8 |
use TablePress\PhpOffice\PhpSpreadsheet\Shared\StringHelper; |
| 9 |
|
| 10 |
class Service |
| 11 |
{ |
| 12 |
/** |
| 13 |
* WEBSERVICE. |
| 14 |
* |
| 15 |
* Returns data from a web service on the Internet or Intranet. |
| 16 |
* |
| 17 |
* Excel Function: |
| 18 |
* Webservice(url) |
| 19 |
* |
| 20 |
* @return string the output resulting from a call to the webservice |
| 21 |
* @param mixed $url |
| 22 |
*/ |
| 23 |
public static function webService($url, ?Cell $cell = null): ?string |
| 24 |
{ |
| 25 |
if (is_array($url)) { |
| 26 |
$url = Functions::flattenSingleValue($url); |
| 27 |
} |
| 28 |
$url = trim(StringHelper::convertToString($url, false)); |
| 29 |
if (mb_strlen($url) > 2048) { |
| 30 |
return ExcelError::VALUE(); // Invalid URL length |
| 31 |
} |
| 32 |
$parsed = parse_url($url); |
| 33 |
$scheme = $parsed['scheme'] ?? ''; |
| 34 |
if ($scheme !== 'http' && $scheme !== 'https') { |
| 35 |
return ExcelError::VALUE(); // Invalid protocol |
| 36 |
} |
| 37 |
$domainWhiteList = (($nullsafeVariable1 = ($nullsafeVariable2 = $cell) ? $nullsafeVariable2->getWorksheet()->getParent() : null) ? $nullsafeVariable1->getDomainWhiteList() : null) ?? []; |
| 38 |
$host = $parsed['host'] ?? ''; |
| 39 |
if (!in_array($host, $domainWhiteList, true)) { |
| 40 |
return ($cell === null) ? null : Functions::NOT_YET_IMPLEMENTED; // will be converted to oldCalculatedValue or null |
| 41 |
} |
| 42 |
|
| 43 |
// Get results from the webservice |
| 44 |
$ctxArray = [ |
| 45 |
'http' => [ |
| 46 |
'follow_location' => 0, |
| 47 |
'user_agent' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36', |
| 48 |
], |
| 49 |
]; |
| 50 |
if ($scheme === 'https') { |
| 51 |
$ctxArray['ssl'] = ['crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT]; |
| 52 |
} |
| 53 |
$ctx = stream_context_create($ctxArray); |
| 54 |
$output = @file_get_contents($url, false, $ctx); |
| 55 |
if ($output === false || mb_strlen($output) > 32767) { |
| 56 |
return ExcelError::VALUE(); // Output not a string or too long |
| 57 |
} |
| 58 |
|
| 59 |
return ($output === '') ? Functions::NOT_YET_IMPLEMENTED : $output; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* URLENCODE. |
| 64 |
* |
| 65 |
* Returns data from a web service on the Internet or Intranet. |
| 66 |
* |
| 67 |
* Excel Function: |
| 68 |
* urlEncode(text) |
| 69 |
* |
| 70 |
* @return string the url encoded output |
| 71 |
* @param mixed $text |
| 72 |
*/ |
| 73 |
public static function urlEncode($text): string |
| 74 |
{ |
| 75 |
if (!is_string($text)) { |
| 76 |
return ExcelError::VALUE(); |
| 77 |
} |
| 78 |
|
| 79 |
return str_replace('+', '%20', urlencode($text)); |
| 80 |
} |
| 81 |
} |
| 82 |
|