-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurlUtil.php
More file actions
139 lines (124 loc) · 3.82 KB
/
Copy pathCurlUtil.php
File metadata and controls
139 lines (124 loc) · 3.82 KB
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
<?php
class CurlUtil {
public static function profile(&$request, $options) {
if(isset($options['headers']) && is_array($options['headers'])) {
$headers = $options['headers'];
} else {
$headers = array();
}
if(isset($options['ip'])) $request->ip($options['ip']);
if(isset($options['proxy'])) $request->proxy($options['proxy']);
if(isset($options['ua'])) $request->ua($options['ua']);
if(isset($options['cookieFile'])) $request->cookieFile = $options['cookieFile'];
if(isset($options['lang'])) {
}
if(isset($options['accept'])) {
}
if(isset($options['ajax'])) {
$headers[] = 'X-Requested-With: XMLHttpRequest';
}
$request->options[CURLOPT_HTTPHEADERS] = $options['headers'];
return $request;
}
public static function varsToArray($str){
$arr = array();
parse_str($str, $arr);
return $arr;
}
public static $ifconfigRegex = '@addr:(?=([0-9.]+))(?!(?:10\.|127\.|172\.[1-3][6-9_0]\.|192\.|169\.254\.[0-9.]+))@';
public static function discoverInterfaces() {
$a = shell_exec( 'ifconfig -a' );
preg_match_all( self::$ifconfigRegex, $a, $m);
return $m[1];
}
public static $linkRegex = '@<\s*a\s*[^>]*?href\s*=[\"\'\s]*(.*?)[\"\'\s]*[^>]*?>(.*?)<\s*/\s*a\s*>@is';
public static $urlRegex = '@(https?://[a-zA-z0-9-_]+)@i';
public static $baseRegex = '@<\s*base\s*href\s*=[\"\'\s]*(.+?)[\"\'\s]*[^>]*/?>@i';
public static function links($str,$mode='html',$base=null) {
$links = array();
switch($html) {
case 'html':
preg_match_all(self::$linkRegex,$str,$m);
if(($p = stripos($str,'<base')) !== false) {
preg_match(self::$baseRegex,$str,$b);
$base = $b[1];
//expand links
}
if($base)
array_walk($m[1], array(self, 'resolveUrl'),$base);
$links = array($m[1],$m[2]);
break;
case 'txt':
preg_match_all(self::$urlRegex,$str,$m);
break;
}
return $links;
}
public static function resolveUrl($url,$i,$base) {
if (!strlen($base)) return $url;
// Step 2
if (!strlen($url)) return $base;
// Step 3
if (preg_match('!^[a-z]+:!i', $url)) return $url;
$base = parse_url($base);
if ($url{0} == "#") {
// Step 2 (fragment)
$base['fragment'] = substr($url, 1);
return self::glueUrl($base);
}
unset($base['fragment']);
unset($base['query']);
if (substr($url, 0, 2) == "//") {
// Step 4
return self::glueUrl(array(
'scheme'=>$base['scheme'],
'path'=>substr($url,2),
));
} else if ($url{0} == "/") {
// Step 5
$base['path'] = $url;
} else {
// Step 6
$path = explode('/', $base['path']);
$url_path = explode('/', $url);
// Step 6a: drop file from base
array_pop($path);
// Step 6b, 6c, 6e: append url while removing "." and ".." from
// the directory portion
$end = array_pop($url_path);
foreach ($url_path as $segment) {
if ($segment == '.') {
// skip
} else if ($segment == '..' && $path && $path[sizeof($path)-1] != '..') {
array_pop($path);
} else {
$path[] = $segment;
}
}
// Step 6d, 6f: remove "." and ".." from file portion
if ($end == '.') {
$path[] = '';
} else if ($end == '..' && $path && $path[sizeof($path)-1] != '..') {
$path[sizeof($path)-1] = '';
} else {
$path[] = $end;
}
// Step 6h
$base['path'] = join('/', $path);
}
// Step 7
return self::glueUrl($base);
}
public static function glueUrl($parsed) {
$uri = isset($parsed['scheme']) ? $parsed['scheme'].':'.((strtolower($parsed['scheme']) == 'mailto') ? '' : '//') : '';
$uri .= isset($parsed['user']) ? $parsed['user'].(isset($parsed['pass']) ? ':'.$parsed['pass'] : '').'@' : '';
$uri .= isset($parsed['host']) ? $parsed['host'] : '';
$uri .= isset($parsed['port']) ? ':'.$parsed['port'] : '';
if(isset($parsed['path']))
$uri .= (substr($parsed['path'], 0, 1) == '/') ? $parsed['path'] : ('/'.$parsed['path']);
$uri .= isset($parsed['query']) ? '?'.$parsed['query'] : '';
$uri .= isset($parsed['fragment']) ? '#'.$parsed['fragment'] : '';
return $uri;
}
}
?>