Compare commits

...

2 Commits

View File

@ -237,7 +237,7 @@ final class MD_STD {
* @param string $url URL to query. * @param string $url URL to query.
* @param integer $timeout Timeout in milliseconds. * @param integer $timeout Timeout in milliseconds.
* *
* @return resource * @return CurlHandle
*/ */
public static function curl_init(string $url, int $timeout) { public static function curl_init(string $url, int $timeout) {
@ -300,9 +300,7 @@ final class MD_STD {
*/ */
public static function runCurlMulti(array $urls, int $timeout = 1200):array { public static function runCurlMulti(array $urls, int $timeout = 1200):array {
if (!($mh = curl_multi_init())) { $mh = curl_multi_init();
throw new Exception("Failed to set up multi handle");
}
$curl_array = []; $curl_array = [];
foreach($urls as $i => $url) { foreach($urls as $i => $url) {
@ -322,7 +320,7 @@ final class MD_STD {
$res = []; $res = [];
foreach($urls as $i => $url) { foreach($urls as $i => $url) {
$res[$i] = curl_multi_getcontent($curl_array[$i]); $res[$i] = curl_multi_getcontent($curl_array[$i]) ?: '';
} }
foreach($urls as $i => $url){ foreach($urls as $i => $url){
@ -508,9 +506,6 @@ final class MD_STD {
public static function openssl_random_pseudo_bytes(int $length):string { public static function openssl_random_pseudo_bytes(int $length):string {
$output = \openssl_random_pseudo_bytes($length); $output = \openssl_random_pseudo_bytes($length);
if ($output === false) {
throw new Exception("Failed generating random pseudo bytes using openssl_random_pseudo_bytes");
}
return $output; return $output;
} }
@ -576,6 +571,36 @@ final class MD_STD {
} }
/**
* Wrapper around the finfo functions to get the mime content type of a file.
*
* @param string $url Expected file path.
*
* @return string
*/
public static function remote_mime_content_type(string $url):string {
if (($tmp_file = \tempnam(\sys_get_temp_dir(), "remote_mime_type_check")) === false) {
throw new Exception("Failed to get temporary file location");
}
$fp = \fopen($tmp_file, 'w');
if (!($ch = \curl_init($url))) {
throw new Exception("Failed to initialize curl for $url");
};
\curl_setopt($ch, CURLOPT_FILE, $fp);
\curl_exec($ch);
\curl_close($ch);
$mime_type = MD_STD::mime_content_type($tmp_file);
\unlink($tmp_file);
return $mime_type;
}
/** /**
* Checks if a file exists, with one of the expected mime types. * Checks if a file exists, with one of the expected mime types.
* *