@@ -334,27 +334,29 @@ public function getGitHubRepos(string $username): array
334334 protected function isValidUrl (string $ url ): bool
335335 {
336336 try {
337- // Create context to follow redirects
338- $ context = stream_context_create ([
339- 'http ' => [
340- 'method ' => 'HEAD ' ,
341- 'follow_location ' => true ,
342- 'max_redirects ' => 5 ,
343- 'timeout ' => 10 ,
344- 'user_agent ' => 'REDAXOZipInstall/2.2.1 '
345- ],
346- 'https ' => [
347- 'method ' => 'HEAD ' ,
348- 'follow_location ' => true ,
349- 'max_redirects ' => 5 ,
350- 'timeout ' => 10 ,
351- 'user_agent ' => 'REDAXOZipInstall/2.2.1 '
352- ]
353- ]);
354-
355- /** @var array<int, string>|false $headers */
356- $ headers = @get_headers ($ url , 1 , $ context );
357- return $ headers && (str_contains ($ headers [0 ], '200 ' ) || str_contains ($ headers [0 ], '302 ' ));
337+ // Check if cURL is available
338+ if (!function_exists ('curl_init ' )) {
339+ return false ;
340+ }
341+
342+ $ ch = curl_init ();
343+ curl_setopt ($ ch , CURLOPT_URL , $ url );
344+ curl_setopt ($ ch , CURLOPT_NOBODY , true ); // HEAD request
345+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
346+ curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
347+ curl_setopt ($ ch , CURLOPT_MAXREDIRS , 5 );
348+ curl_setopt ($ ch , CURLOPT_TIMEOUT , 10 );
349+ curl_setopt ($ ch , CURLOPT_CONNECTTIMEOUT , 5 );
350+ curl_setopt ($ ch , CURLOPT_USERAGENT , 'REDAXOZipInstall/2.2.1 ' );
351+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , true );
352+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , 2 );
353+
354+ curl_exec ($ ch );
355+ $ httpCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
356+ $ error = curl_error ($ ch );
357+ curl_close ($ ch );
358+
359+ return empty ($ error ) && ($ httpCode === 200 || $ httpCode === 302 );
358360 } catch (Exception $ e ) {
359361 trigger_error ('Error checking URL validity: ' . $ e ->getMessage (), E_USER_WARNING );
360362 return false ; // In case of an exception consider the URL invalid
@@ -371,37 +373,39 @@ protected function isValidUrl(string $url): bool
371373 protected function downloadFile (string $ url , string $ destination ): bool
372374 {
373375 try {
374- // Create context with options to follow redirects
375- $ context = stream_context_create ([
376- 'http ' => [
377- 'method ' => 'GET ' ,
378- 'follow_location ' => true ,
379- 'max_redirects ' => 5 ,
380- 'timeout ' => 30 ,
381- 'user_agent ' => 'REDAXOZipInstall/2.2.1 ' ,
382- 'header ' => [
383- 'Accept: application/zip, application/octet-stream, */* ' ,
384- 'Cache-Control: no-cache '
385- ]
386- ],
387- 'https ' => [
388- 'method ' => 'GET ' ,
389- 'follow_location ' => true ,
390- 'max_redirects ' => 5 ,
391- 'timeout ' => 30 ,
392- 'user_agent ' => 'REDAXOZipInstall/2.2.1 ' ,
393- 'header ' => [
394- 'Accept: application/zip, application/octet-stream, */* ' ,
395- 'Cache-Control: no-cache '
396- ]
397- ]
376+ // Check if cURL is available
377+ if (!function_exists ('curl_init ' )) {
378+ trigger_error ('cURL extension is required for downloading files ' , E_USER_WARNING );
379+ return false ;
380+ }
381+
382+ $ ch = curl_init ();
383+ curl_setopt ($ ch , CURLOPT_URL , $ url );
384+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , true );
385+ curl_setopt ($ ch , CURLOPT_FOLLOWLOCATION , true );
386+ curl_setopt ($ ch , CURLOPT_MAXREDIRS , 5 );
387+ curl_setopt ($ ch , CURLOPT_TIMEOUT , 60 );
388+ curl_setopt ($ ch , CURLOPT_CONNECTTIMEOUT , 10 );
389+ curl_setopt ($ ch , CURLOPT_USERAGENT , 'REDAXOZipInstall/2.2.1 ' );
390+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , true );
391+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , 2 );
392+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , [
393+ 'Accept: application/zip, application/octet-stream, */* ' ,
394+ 'Cache-Control: no-cache '
398395 ]);
399396
400- /** @var string|false $content */
401- $ content = @file_get_contents ($ url , false , $ context );
402- if ($ content === false ) {
397+ $ content = curl_exec ($ ch );
398+ $ httpCode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
399+ $ error = curl_error ($ ch );
400+ curl_close ($ ch );
401+
402+ if ($ content === false || $ httpCode !== 200 || !empty ($ error )) {
403+ if (!empty ($ error )) {
404+ trigger_error ('cURL error: ' . $ error , E_USER_WARNING );
405+ }
403406 return false ;
404407 }
408+
405409 return rex_file::put ($ destination , $ content );
406410 } catch (Exception $ e ) {
407411 trigger_error ('Error downloading file: ' . $ e ->getMessage (), E_USER_WARNING );
0 commit comments