summaryrefslogtreecommitdiff
path: root/down/http.php
blob: cf2a302753498cc41538e7e47ff6dfeaf4edbc49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
function isUp($url, $ipv6 = false){
        $agent = "Just checking if you are up or not.";
        $ch=curl_init();

	if ( $ipv6 ){
		$url = "[".$url."]";
		curl_setopt($ch,CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6);
	}

        curl_setopt ($ch, CURLOPT_URL,$url );
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt ($ch,CURLOPT_VERBOSE,false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE);

        $page=curl_exec($ch);
        $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if($httpcode>=200 && $httpcode<400 )
                return 2;
        else if ( $httpcode >= 400)
                return $httpcode;
	else
		return 0;
}

function sanitizeUrl($url){
	if ( ! preg_match("|^[a-zA-Z]+://|", $url) )
		$url = "http://" . $url;
	if ( preg_match("|^(?<host>[a-zA-Z]+://(.+\.[a-zA-Z]+){1,})/.*|", $url, $match) )
		$url = $match["host"];

	return $url;
}