Subversion Repositories vgwhois

Rev

Rev 5 | Rev 76 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 daniel-mar 1
<?php
2
 
3
#
11 daniel-mar 4
#  VGWhoIs (ViaThinkSoft Global WhoIs, a fork of generic Whois / gwhois)
5 daniel-mar 5
#  Common functions in PHP
2 daniel-mar 6
#
11 daniel-mar 7
#  (c) 2013-2019 by Daniel Marschall, ViaThinkSoft <info@daniel-marschall.de>
2 daniel-mar 8
#
5 daniel-mar 9
#  License: https://www.gnu.org/licenses/gpl-2.0.html (GPL version 2)
2 daniel-mar 10
#
11
 
12
function getpatternfiles() {
13
        $out = array();
14
 
15
        $files = glob(__DIR__ . '/../../main/pattern/'.'*');
16
        foreach ($files as &$file) {
17
                if (preg_match('@^\.@',     $file)) continue;
18
 
19
                $out[] = $file;
20
        }
21
 
22
        return $out;
23
}
24
 
25
function get_united_pattern() {
26
        $cont = '';
27
 
28
        $files = getpatternfiles();
29
        foreach ($files as &$file) {
30
                $cont .= file_get_contents($file)."\n\n";
31
        }
32
 
33
        return $cont;
34
}
35
 
36
function cached_file($url, $cache_dir, $max_age = /* 24*60*60 */ 86400) {
3 daniel-mar 37
        $opts = [
38
            "http" => [
39
                "method" => "GET",
40
                "header" => "User-Agent: Google Chrome Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36.\r\n"
41
            ]
42
        ];
43
        $context = stream_context_create($opts);
44
 
2 daniel-mar 45
        $cachefile = $cache_dir . '/' . sha1($url) . '.cache';
3 daniel-mar 46
        if (!is_dir($cache_dir)) mkdir($cache_dir, 0755, true);
2 daniel-mar 47
        if (file_age($cachefile) > $max_age) {
3 daniel-mar 48
                $cont = file_get_contents($url, 0, $context);
2 daniel-mar 49
                file_put_contents($cachefile, $cont);
50
        } else {
51
                $cont = file_get_contents($cachefile);
52
        }
3 daniel-mar 53
 
54
        if ($cont === false) {
55
                throw new Exception("Failed to get contents from $url");
56
        }
57
 
2 daniel-mar 58
        return $cont;
59
}