Subversion Repositories vgwhois

Rev

Rev 7 | Rev 48 | 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
#
5 daniel-mar 7
#  (c) 2011-2013 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
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) or die("Socket Error " . $errno . " - " . $errstr);
49
        // 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.
50
        fputs($fp, $domain . "\r\n");
51
        $out = "";
52
        while(!feof($fp)){
53
                $out .= fgets($fp);
54
        }
55
        fclose($fp);
56
 
57
        $res = "";
58
        if ((strpos(strtolower($out), "error") === FALSE) && (strpos(strtolower($out), "not allocated") === FALSE)) {
59
                $rows = explode("\n", $out);
60
                foreach($rows as $row) {
61
                        $row = trim($row);
62
                        if (($row != '') && ($row[0] != '#') && ($row[0] != '%')) {
63
                                $res .= "$row\n";
64
                        }
65
                }
66
        }
67
        return $res;
68
}
69
 
70
# TODO: rename (without "gwitc")
71
function gwitc_is_port_open($server, $default_port, $timeout=3) {
72
        // TODO: "whois.namecoin.us" will always answer to a port request, because the domain parking service is shit
73
 
74
        $x = explode(':', $server, 2);
75
        $host = $x[0];
76
        $port = isset($x[1]) ? $x[1] : $default_port;
77
 
78
        // First try with TOR
79
#       $cmd = "vtor -- nc -zw$timeout $host $port 2>/dev/null";
80
#       exec($cmd, $out, $code);
81
#       if ($code == 0) return true;
82
 
83
        // Try without TOR
84
        $cmd = "nc -zw$timeout $host $port 2>/dev/null";
85
        exec($cmd, $out, $code);
86
        return ($code == 0);
87
}
88
 
89
function getAllFiles($directory, $recursive = true, $include_dirs = false, $include_files = true) {
90
        $result = array();
91
        $handle = opendir($directory);
92
        if (substr($directory, -1) == '/') $directory = substr($directory, 0, strlen($directory)-1);
93
        if ($include_dirs) {
94
                $result[] = $directory;
95
        }
96
        while ($datei = readdir($handle)) {
97
                if (($datei != '.') && ($datei != '..')) {
98
                        $file = $directory.'/'.$datei;
99
                        if (is_dir($file)) {
100
                                if ($include_dirs && !$recursive) {
101
                                        $result[] = $file;
102
                                }
103
                                if ($recursive) {
104
                                        $result = array_merge($result, getAllFiles($file, $recursive, $include_dirs, $include_files));
105
                                }
106
                        } else {
107
                                if ($include_files) $result[] = $file;
108
                        }
109
                }
110
        }
111
        closedir($handle);
112
        return $result;
113
}
114
 
115
// TOR capable
116
function file_get_contents2($url, $postvalues='', $headers=array()) {
117
        # exec ("wget -N -O - -- ".escapeshellarg($url), $out);
118
 
119
        $add_cmd = '';
120
        foreach ($headers as $name => $h) {
121
                if (is_numeric($name)) {
122
                        $add_cmd .= "-H ".escapeshellarg($h)." ";
123
                } else {
124
                        $add_cmd .= "-H ".escapeshellarg($name.': '.$h)." ";
125
                }
126
        }
127
 
128
        if ($postvalues != '') {
129
                $add_cmd .= "-d ".escapeshellarg($postvalues)." ";
130
        }
131
 
132
        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);
133
 
134
        return implode("\n", $out);
135
}
136
 
137
function make_tabs($text, $abstand = 4) {
138
        $ary = explode("\n", $text);
139
        $longest = 0;
140
        foreach ($ary as $a) {
141
                $bry = explode(':', $a, 2);
142
                if (count($bry) < 2) continue;
143
                $c = strlen($bry[0]);
144
                if ($c > $longest) $longest = $c;
145
        }
146
        foreach ($ary as $n => $a) {
147
                $bry = explode(':', $a, 2);
148
                if (count($bry) < 2) continue;
149
                $rep = $longest-strlen($bry[0]) + $abstand;
150
                if ($rep < 1) {
151
                        $wh = '';
152
                } else {
153
                        $wh = str_repeat(' ', $rep);
154
                }
155
                $ary[$n] = $bry[0].':'.$wh.trim($bry[1]);
156
        }
157
        $x = implode("\n", $ary);
158
        return $x;
159
}
160
 
161
function uc_latin1($str) {
162
        # Source: http://de3.php.net/manual/en/function.strtoupper.php#82592
163
        $str = strtoupper(strtr($str, "àáâãäåæçèéêëìíîïðñòóôõöøùúûüý",
164
                                      "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝ"));
165
        return strtr($str, array("ß" => "SS"));
166
}
167
 
168
/**
169
 * Converts tabs to the appropriate amount of spaces while preserving formatting
170
 *
171
 * @author      Aidan Lister <aidan@php.net>
172
 * @version     1.2.0
173
 * @link        http://aidanlister.com/repos/v/function.tab2space.php
174
 * @param       string    $text     The text to convert
175
 * @param       int       $spaces   Number of spaces per tab column
176
 * @param       boolean   $html     Output as HTML or not
177
 * @return      string    The text with tabs replaced
178
 */
179
function tab2space($text, $spaces = 4, $html = false) {
180
        // Snippet from PHP Share: http://www.phpshare.org/scripts/Convert-Tabs-to-Spaces
181
        // Modified by Daniel Marschall: Added $html param
182
 
183
        // Explode the text into an array of single lines
184
        $lines = explode("\n", $text);
185
 
186
        // Loop through each line
187
        foreach ($lines as $line) {
188
                // Break out of the loop when there are no more tabs to replace
189
                while (false !== $tab_pos = strpos($line, "\t")) {
190
                        // Break the string apart, insert spaces then concatenate
191
                        $start = substr($line, 0, $tab_pos);
192
                        $tab = str_repeat($html ? '&nbsp;' : '', $spaces - $tab_pos % $spaces);
193
                        $end = substr($line, $tab_pos + 1);
194
                        $line = $start . $tab . $end;
195
                }
196
 
197
                $result[] = $line;
198
        }
199
 
200
        return implode("\n", $result);
201
}
202
 
203
function trim_each_line($x) {
204
        $res = '';
205
        foreach (explode("\n", $x) as $y) {
206
                $res .= trim($y)."\n";
207
        }
208
        return $res;
209
}
210
 
211
function generateRandomString($length = 10) {
212
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
213
        $randomString = '';
214
        for ($i = 0; $i < $length; $i++) {
215
                $randomString .= $characters[rand(0, strlen($characters) - 1)];
216
        }
217
        return $randomString;
218
}
219
 
220
# http://stackoverflow.com/a/9361531
221
function _uniord($c) {
222
        if (ord($c{0}) >=0 && ord($c{0}) <= 127)
223
                return ord($c{0});
224
        if (ord($c{0}) >= 192 && ord($c{0}) <= 223)
225
                return (ord($c{0})-192)*64 + (ord($c{1})-128);
226
        if (ord($c{0}) >= 224 && ord($c{0}) <= 239)
227
                return (ord($c{0})-224)*4096 + (ord($c{1})-128)*64 + (ord($c{2})-128);
228
        if (ord($c{0}) >= 240 && ord($c{0}) <= 247)
229
                return (ord($c{0})-240)*262144 + (ord($c{1})-128)*4096 + (ord($c{2})-128)*64 + (ord($c{3})-128);
230
        if (ord($c{0}) >= 248 && ord($c{0}) <= 251)
231
                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);
232
        if (ord($c{0}) >= 252 && ord($c{0}) <= 253)
233
                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);
234
        if (ord($c{0}) >= 254 && ord($c{0}) <= 255)    //  error
235
                return FALSE;
236
        return 0;
237
}
238
 
239
# urn:OID:2.0999 -> .2.999
240
function normalize_oid($oid, $leading_dot=true) {
241
        # remove urn:oid: and oid:
242
        $oid = preg_replace('@^(urn:oid:|oid:|)@i', '', $oid);
243
 
244
        # add leading dot if it does not already exist
245
        $oid = preg_replace('@^\.@', '', $oid);
246
        $oid = '.' . $oid;
247
 
248
        # remove leading zeros (requires leading dot)
249
        $oid = preg_replace('@\.0*([1-9])@', '.$1', $oid);
250
 
251
        if (!$leading_dot) {
252
                $oid = preg_replace('@^\\.@s', '', $oid);
253
        }
254
 
255
        return $oid;
256
}
257
 
258
function generateRandomToken($haystack, $length = 20) {
259
        do {
260
                $t = generateRandomString($length);
261
        } while (strpos($haystack, $t) !== false);
262
        return $t;
263
}
7 daniel-mar 264
 
265
function github_commit_count($user, $repo) {
266
        $cont = file_get_contents("https://github.com/$user/$repo");
267
        if (preg_match('@>\s*(\d+)\s*</span>\s*commits@smU', $cont, $m)) {
268
            return $m[1];
269
        } else {
270
                return "?";
271
        }
272
}