aboutsummaryrefslogtreecommitdiffstats
path: root/tools/init.php
blob: c23d5c96a34d6afe0d5e9686527d370987a96a3e (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php

function url_exists( $url = NULL ) {
    if( empty( $url ) ){
        return false;
    }

    $ch = curl_init( $url );

    // Set a timeout
    curl_setopt( $ch, CURLOPT_TIMEOUT, 5 );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 5 );

    // Set NOBODY to true to make a HEAD-type request
    curl_setopt( $ch, CURLOPT_NOBODY, true );
    // Allow follow redirects
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    // Receive the response as string, not output
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

    $data = curl_exec( $ch );

    // Get the response code
    $httpcode = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
    // close connection
    curl_close( $ch );

    // Accept only answer 200 (Ok), 301 (permanent redirect) or 302 (temporary redirect)
    $accepted_response = array( 200, 301, 302 );
    if( in_array( $httpcode, $accepted_response ) ) {
        return true;
    } else {
        return false;
    }
}

function secToDuration($seconds){
    $minutes  = intval($seconds/60);
    $seconds = ($seconds - ($minutes * 60));
    return $minutes . ' minutes ' . $seconds . ' seconds';
}

function bytes($a) {
    $unim = array("","K","M","G","T","P");
    $c = 0;
    while ($a>=1000) {
        $c++;
        $a = $a/1000;
    }
    return number_format($a,($c ? 2 : 0),",",".")." ".$unim[$c];
}

// Check, if there is not content into URL return 'false'
function breakdrm($url) {
    @$headers = get_headers($url);
    if (preg_match('/^HTTP\/\d\.\d\s+(403)/', $headers[0])){
        return false;
    } else {
        return true;
    }
}