-
-
Notifications
You must be signed in to change notification settings - Fork 793
/
Utils.php
159 lines (124 loc) · 2.82 KB
/
Utils.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
class Utils
{
const TMP_DIR = '/tmp/';
const T_SHELL_COLORS = array(
'nc' => '0',
'black' => '0;30',
'red' => '0;31',
'green' => '0;32',
'orange' => '0;33',
'blue' => '0;34',
'purple' => '0;35',
'cyan' => '0;36',
'light_grey' => '0;37',
'dark_grey' => '1;30',
'light_red' => '1;31',
'light_green' => '1;32',
'yellow' => '1;33',
'light_blue' => '1;34',
'light_purple' => '1;35',
'light_cyan' => '1;36',
'white' => '1;37',
);
public static function help( $error='' )
{
if( is_file(__DIR__.'/README.md') ) {
$help = file_get_contents( __DIR__.'/README.md' )."\n";
preg_match_all( '#```(.*)```#s', $help, $matches );
if( count($matches[1]) ) {
echo trim($matches[1][0])."\n\n";
}
} else {
echo "No help found!\n";
}
if( $error ) {
echo "Error: ".$error."!\n";
}
exit();
}
public static function isIp( $str ) {
return filter_var( $str, FILTER_VALIDATE_IP );
}
public static function isEmail( $str )
{
return filter_var( $str, FILTER_VALIDATE_EMAIL );
}
public static function _print( $str, $color )
{
echo "\033[".self::T_SHELL_COLORS[$color]."m".$str."\033[0m";
}
public static function _println( $str, $color )
{
self::_print( $str, $color );
echo "\n";
}
public static function _array_search( $array, $search, $ignore_case=true )
{
if( $ignore_case ) {
$f = 'stristr';
} else {
$f = 'strstr';
}
if( !is_array($search) ) {
$search = array( $search );
}
foreach( $array as $k=>$v ) {
foreach( $search as $str ) {
if( $f($v, $str) ) {
return $k;
}
}
}
return false;
}
public static function isDomain( $str )
{
$str = strtolower( $str );
if( preg_match('/[^0-9a-z_\-\.]/',$str) || preg_match('/[^0-9a-z]/',$str[0]) || preg_match('/[^a-z]/',$str[strlen($str)-1]) || substr_count($str,'.')>2 || substr_count($str,'.')<=0 ) {
return false;
} else {
return true;
}
}
public static function isSubdomain( $str )
{
$str = strtolower( $str );
if( preg_match('/[^0-9a-z_\-\.]/',$str) || preg_match('/[^0-9a-z]/',$str[0]) || preg_match('/[^a-z]/',$str[strlen($str)-1]) || substr_count($str,'.')<2 ) {
return false;
} else {
return true;
}
}
public static function extractDomain( $host )
{
$tmp = explode( '.', $host );
$cnt = count( $tmp );
$domain = $tmp[$cnt-1];
for( $i=$cnt-2 ; $i>=0 ; $i-- ) {
$domain = $tmp[$i].'.'.$domain;
if( strlen($tmp[$i]) > 3 ) {
break;
}
}
return $domain;
}
public static function cleanOutput( $str )
{
$str = preg_replace( '#\[[0-9;]{1,4}m#', '', $str );
return $str;
}
public static function _exec( $cmd )
{
$output = '';
while( @ob_end_flush() );
$proc = popen( $cmd, 'r' );
while( !feof($proc) ) {
$line = fread( $proc, 4096 );
echo $line;
$output .= $line;
@flush();
}
return $output;
}
}