Subversion Repositories vgwhois

Rev

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

  1. <?php
  2.  
  3. #
  4. #  VGWhoIs (ViaThinkSoft Global WhoIs, a fork of generic Whois / gwhois)
  5. #  Common functions in PHP
  6. #
  7. #  (c) 2013-2019 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.                 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) {
  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.  
  45.         $cachefile = $cache_dir . '/' . sha1($url) . '.cache';
  46.         if (!is_dir($cache_dir)) mkdir($cache_dir, 0755, true);
  47.         if (file_age($cachefile) > $max_age) {
  48.                 $cont = file_get_contents($url, 0, $context);
  49.                 file_put_contents($cachefile, $cont);
  50.         } else {
  51.                 $cont = file_get_contents($cachefile);
  52.         }
  53.  
  54.         if ($cont === false) {
  55.                 throw new Exception("Failed to get contents from $url");
  56.         }
  57.  
  58.         return $cont;
  59. }
  60.