Subversion Repositories vgwhois

Rev

Rev 5 | Go to most recent revision | Details | Last modification | View Log | RSS feed

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