From 7f6dcdb3f13179fe42fe1bba5f46ba840f90c79e Mon Sep 17 00:00:00 2001 From: Riddhesh Sanghvi Date: Wed, 26 Sep 2018 12:20:24 +0530 Subject: [PATCH] Move get_curl_info to EE utils Signed-off-by: Riddhesh Sanghvi --- php/utils.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/php/utils.php b/php/utils.php index cc8659420..4be714003 100644 --- a/php/utils.php +++ b/php/utils.php @@ -1594,3 +1594,32 @@ function get_image_versions() { return $img_versions; } + +/** + * Function to get httpcode or port occupancy info. + * + * @param string $url url to get info about. + * @param int $port The port to check. + * @param bool $port_info Return port info or httpcode. + * @param mixed $auth Send http auth with passed value if not false. + * + * @return bool|int port occupied or httpcode. + */ +function get_curl_info( $url, $port = 80, $port_info = false, $auth = false ) { + + $ch = curl_init( $url ); + curl_setopt( $ch, CURLOPT_HEADER, true ); + curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); + curl_setopt( $ch, CURLOPT_NOBODY, true ); + curl_setopt( $ch, CURLOPT_TIMEOUT, 10 ); + curl_setopt( $ch, CURLOPT_PORT, $port ); + if ( $auth ) { + curl_setopt( $ch, CURLOPT_USERPWD, $auth ); + } + curl_exec( $ch ); + if ( $port_info ) { + return empty( curl_getinfo( $ch, CURLINFO_PRIMARY_IP ) ); + } + + return curl_getinfo( $ch, CURLINFO_HTTP_CODE ); +}