Subversion Repositories vgwhois

Rev

Rev 3 | Rev 11 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. #
  4. #  VWhois (ViaThinkSoft WHOIS, a fork of generic Whois / gwhois)
  5. #  Common functions in PHP
  6. #
  7. #  (c) 2013-2015 by Daniel Marschall, ViaThinkSoft <info@daniel-marschall.de>
  8. #
  9. #  License: https://www.gnu.org/licenses/gpl-2.0.html (GPL version 2)
  10. #
  11.  
  12. function getpatternfiles() {
  13.         $out = array();
  14.  
  15.         $files = glob(__DIR__ . '/../../main/pattern/'.'*');
  16.         foreach ($files as &$file) {
  17.                 # see /usr/bin/gwhois
  18.                 if (preg_match('@\.dpkg-@', $file)) continue;
  19.                 if (preg_match('@\.orig$@', $file)) continue;
  20.                 if (preg_match('@\.bak$@',  $file)) continue;
  21.                 if (preg_match('@\.save$@', $file)) continue;
  22.                 if (preg_match('@^\.@',     $file)) continue;
  23.  
  24.                 $out[] = $file;
  25.         }
  26.  
  27.         return $out;
  28. }
  29.  
  30. function get_united_pattern() {
  31.         $cont = '';
  32.  
  33.         $files = getpatternfiles();
  34.         foreach ($files as &$file) {
  35.                 $cont .= file_get_contents($file)."\n\n";
  36.         }
  37.  
  38.         return $cont;
  39. }
  40.  
  41. function cached_file($url, $cache_dir, $max_age = /* 24*60*60 */ 86400) {
  42.         $opts = [
  43.             "http" => [
  44.                 "method" => "GET",
  45.                 "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"
  46.             ]
  47.         ];
  48.         $context = stream_context_create($opts);
  49.  
  50.         $cachefile = $cache_dir . '/' . sha1($url) . '.cache';
  51.         if (!is_dir($cache_dir)) mkdir($cache_dir, 0755, true);
  52.         if (file_age($cachefile) > $max_age) {
  53.                 $cont = file_get_contents($url, 0, $context);
  54.                 file_put_contents($cachefile, $cont);
  55.         } else {
  56.                 $cont = file_get_contents($cachefile);
  57.         }
  58.  
  59.         if ($cont === false) {
  60.                 throw new Exception("Failed to get contents from $url");
  61.         }
  62.  
  63.         return $cont;
  64. }
  65.