Subversion Repositories vgwhois

Rev

Rev 143 | 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) 2011-2024 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. include_once __DIR__ . '/ipv4_functions.inc.php';
  13. include_once __DIR__ . '/ipv6_functions.inc.php';
  14. include_once __DIR__ . '/grep_functions.inc.php';
  15. include_once __DIR__ . '/gwi_functions.inc.php';
  16.  
  17. function file_age($filename) {
  18.         $m = file_exists($filename) ? filemtime($filename) : 0;
  19.         return time()-$m;
  20. }
  21.  
  22. function human_timediff($t) {
  23.         if ($t < 60) {
  24.                 $e = 'seconds';
  25.         } else if ($t < 60*60) {
  26.                 $t /= 60;
  27.                 $e = 'minutes';
  28.         } else if ($t < 24*60*60) {
  29.                 $t /= 60*60;
  30.                 $e = 'hours';
  31.         } else if ($t < 30*24*60*60) {
  32.                 $t /= 24*60*60;
  33.                 $e = 'days';
  34.         } else if ($t < 365*24*60*60) {
  35.                 $t /= 30*24*60*60;
  36.                 $e = 'months';
  37.         } else {
  38.                 $t /= 365*24*60*60;
  39.                 $e = 'years';
  40.         }
  41.         $t = floor($t);
  42.         return "$t $e";
  43. }
  44.  
  45. # http://www.phpeasycode.com/whois/
  46. # TODO: code duplicate in maintenance/pattern-generator/generate_newgtld
  47. function QueryWhoisServer($whoisserver, $domain, $port=43, $timeout=10) {
  48.         $fp = @fsockopen($whoisserver, $port, $errno, $errstr, $timeout);
  49.         if (!$fp) die("Socket Error " . $errno . " - " . $errstr);
  50.         // if ($whoisserver == "whois.verisign-grs.com") $domain = "=$domain"; // whois.verisign-grs.com requires the equals sign ("=") or it returns any result containing the searched string.
  51.         fputs($fp, $domain . "\r\n");
  52.         $out = "";
  53.         while(!feof($fp)){
  54.                 $out .= fgets($fp);
  55.         }
  56.         fclose($fp);
  57.  
  58.         $res = "";
  59.         if ((strpos(strtolower($out), "error") === FALSE) && (strpos(strtolower($out), "not allocated") === FALSE)) {
  60.                 $rows = explode("\n", $out);
  61.                 foreach($rows as $row) {
  62.                         $row = trim($row);
  63.                         if (($row != '') && ($row[0] != '#') && ($row[0] != '%')) {
  64.                                 $res .= "$row\n";
  65.                         }
  66.                 }
  67.         }
  68.         return $res;
  69. }
  70.  
  71. # TODO: rename (without "gwitc")
  72. function gwitc_is_port_open($server, $default_port, $timeout=3) {
  73.         // TODO: "whois.namecoin.us" will always answer to a port request, because the domain parking service is shit
  74.  
  75.         $x = explode(':', $server, 2);
  76.         $host = $x[0];
  77.         $port = isset($x[1]) ? $x[1] : $default_port;
  78.  
  79.         // First try with TOR
  80. #       $cmd = "vtor -- nc -zw$timeout $host $port 2>/dev/null";
  81. #       exec($cmd, $out, $code);
  82. #       if ($code == 0) return true;
  83.  
  84.         // Try without TOR
  85.         $cmd = "nc -zw$timeout $host $port 2>/dev/null";
  86.         exec($cmd, $out, $code);
  87.         return ($code == 0);
  88. }
  89.  
  90. function getAllFiles($directory, $recursive = true, $include_dirs = false, $include_files = true) {
  91.         $result = array();
  92.         $handle = opendir($directory);
  93.         if (substr($directory, -1) == '/') $directory = substr($directory, 0, strlen($directory)-1);
  94.         if ($include_dirs) {
  95.                 $result[] = $directory;
  96.         }
  97.         while ($datei = readdir($handle)) {
  98.                 if (($datei != '.') && ($datei != '..')) {
  99.                         $file = $directory.'/'.$datei;
  100.                         if (is_dir($file)) {
  101.                                 if ($include_dirs && !$recursive) {
  102.                                         $result[] = $file;
  103.                                 }
  104.                                 if ($recursive) {
  105.                                         $result = array_merge($result, getAllFiles($file, $recursive, $include_dirs, $include_files));
  106.                                 }
  107.                         } else {
  108.                                 if ($include_files) $result[] = $file;
  109.                         }
  110.                 }
  111.         }
  112.         closedir($handle);
  113.         return $result;
  114. }
  115.  
  116. // TOR capable
  117. function file_get_contents2($url, $postvalues='', $headers=array()) {
  118.         # exec ("wget -N -O - -- ".escapeshellarg($url), $out);
  119.  
  120.         $add_cmd = '';
  121.         foreach ($headers as $name => $h) {
  122.                 if (is_numeric($name)) {
  123.                         $add_cmd .= "-H ".escapeshellarg($h)." ";
  124.                 } else {
  125.                         $add_cmd .= "-H ".escapeshellarg($name.': '.$h)." ";
  126.                 }
  127.         }
  128.  
  129.         if ($postvalues != '') {
  130.                 $add_cmd .= "-d ".escapeshellarg($postvalues)." ";
  131.         }
  132.  
  133.         exec("curl --cookie ~/.cookiejar --cookie-jar ~/.cookiejar -s -k -L -A 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0' $add_cmd".escapeshellarg($url), $out);
  134.  
  135.         return implode("\n", $out);
  136. }
  137.  
  138. function make_tabs($text, $abstand = 4) {
  139.         $encoding = @ini_get('default_charset');
  140.         if ($encoding === false) $encoding = null;
  141.         $ary = explode("\n", $text);
  142.         $longest = 0;
  143.         foreach ($ary as $a) {
  144.                 $bry = explode(':', $a, 2);
  145.                 if (count($bry) < 2) continue;
  146.                 $c = !is_null($encoding) ? mb_strlen($bry[0], $encoding) : strlen($bry[0]);
  147.                 if ($c > $longest) $longest = $c;
  148.         }
  149.         foreach ($ary as $n => $a) {
  150.                 $bry = explode(':', $a, 2);
  151.                 if (count($bry) < 2) continue;
  152.                 $c_ = !is_null($encoding) ? mb_strlen($bry[0], $encoding) : strlen($bry[0]);
  153.                 $rep = $longest - $c_ + $abstand;
  154.                 if ($rep < 1) {
  155.                         $wh = '';
  156.                 } else {
  157.                         $wh = str_repeat(' ', $rep);
  158.                 }
  159.                 $ary[$n] = $bry[0].':'.$wh.trim($bry[1]);
  160.         }
  161.         $x = implode("\n", $ary);
  162.         return $x;
  163. }
  164.  
  165. /**
  166.  * Converts tabs to the appropriate amount of spaces while preserving formatting
  167.  *
  168.  * @author      Aidan Lister <aidan@php.net>
  169.  * @version     1.2.0
  170.  * @link        http://aidanlister.com/repos/v/function.tab2space.php
  171.  * @param       string    $text     The text to convert
  172.  * @param       int       $spaces   Number of spaces per tab column
  173.  * @param       boolean   $html     Output as HTML or not
  174.  * @return      string    The text with tabs replaced
  175.  */
  176. function tab2space($text, $spaces = 4, $html = false) {
  177.         // Snippet from PHP Share: http://www.phpshare.org/scripts/Convert-Tabs-to-Spaces
  178.         // Modified by Daniel Marschall: Added $html param
  179.  
  180.         // Explode the text into an array of single lines
  181.         $lines = explode("\n", $text);
  182.  
  183.         // Loop through each line
  184.         foreach ($lines as $line) {
  185.                 // Break out of the loop when there are no more tabs to replace
  186.                 while (false !== $tab_pos = strpos($line, "\t")) {
  187.                         // Break the string apart, insert spaces then concatenate
  188.                         $start = substr($line, 0, $tab_pos);
  189.                         $tab = str_repeat($html ? '&nbsp;' : '', $spaces - $tab_pos % $spaces);
  190.                         $end = substr($line, $tab_pos + 1);
  191.                         $line = $start . $tab . $end;
  192.                 }
  193.  
  194.                 $result[] = $line;
  195.         }
  196.  
  197.         return implode("\n", $result);
  198. }
  199.  
  200. function trim_each_line($x) {
  201.         $res = '';
  202.         foreach (explode("\n", $x) as $y) {
  203.                 $res .= trim($y)."\n";
  204.         }
  205.         return $res;
  206. }
  207.  
  208. function generateRandomString($length = 10) {
  209.         $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  210.         $randomString = '';
  211.         for ($i = 0; $i < $length; $i++) {
  212.                 $randomString .= $characters[rand(0, strlen($characters) - 1)];
  213.         }
  214.         return $randomString;
  215. }
  216.  
  217. # http://stackoverflow.com/a/9361531
  218. function _uniord($c) {
  219.         if (ord($c[0]) >=0 && ord($c[0]) <= 127)
  220.                 return ord($c[0]);
  221.         if (ord($c[0]) >= 192 && ord($c[0]) <= 223)
  222.                 return (ord($c[0])-192)*64 + (ord($c[1])-128);
  223.         if (ord($c[0]) >= 224 && ord($c[0]) <= 239)
  224.                 return (ord($c[0])-224)*4096 + (ord($c[1])-128)*64 + (ord($c[2])-128);
  225.         if (ord($c[0]) >= 240 && ord($c[0]) <= 247)
  226.                 return (ord($c[0])-240)*262144 + (ord($c[1])-128)*4096 + (ord($c[2])-128)*64 + (ord($c[3])-128);
  227.         if (ord($c[0]) >= 248 && ord($c[0]) <= 251)
  228.                 return (ord($c[0])-248)*16777216 + (ord($c[1])-128)*262144 + (ord($c[2])-128)*4096 + (ord($c[3])-128)*64 + (ord($c[4])-128);
  229.         if (ord($c[0]) >= 252 && ord($c[0]) <= 253)
  230.                 return (ord($c[0])-252)*1073741824 + (ord($c[1])-128)*16777216 + (ord($c[2])-128)*262144 + (ord($c[3])-128)*4096 + (ord($c[4])-128)*64 + (ord($c[5])-128);
  231.         if (ord($c[0]) >= 254 && ord($c[0]) <= 255)    //  error
  232.                 return FALSE;
  233.         return 0;
  234. }
  235.  
  236. # urn:OID:2.0999 -> .2.999
  237. function normalize_oid($oid, $leading_dot=true) {
  238.         # remove urn:oid: and oid:
  239.         $oid = preg_replace('@^(urn:oid:|oid:|)@i', '', $oid);
  240.  
  241.         # add leading dot if it does not already exist
  242.         $oid = preg_replace('@^\.@', '', $oid);
  243.         $oid = '.' . $oid;
  244.  
  245.         # remove leading zeros (requires leading dot)
  246.         $oid = preg_replace('@\.0*([1-9])@', '.$1', $oid);
  247.  
  248.         if (!$leading_dot) {
  249.                 $oid = preg_replace('@^\\.@s', '', $oid);
  250.         }
  251.  
  252.         return $oid;
  253. }
  254.  
  255. function generateRandomToken($haystack, $length = 20) {
  256.         do {
  257.                 $t = generateRandomString($length);
  258.         } while (strpos($haystack, $t) !== false);
  259.         return $t;
  260. }
  261.  
  262. function github_latest_commit($author, $repo) {
  263.         $cont = file_get_contents2("https://api.github.com/repos/$author/$repo/commits");
  264.         if ($cont === false) return false;
  265.         $json = json_decode($cont, true);
  266.         if ($json === false) return false;
  267.         return $json[0]['sha'] ?? false;
  268. }
  269.