forked from davidcorbin/browserproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.php
184 lines (110 loc) · 4.04 KB
/
parse.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
// otherwise preg_replace_callback couldn't handle very large strings -- increase the default by 100
ini_set('pcre.backtrack_limit', 1000000 * 100);
function css_url($matches){
$url = trim($matches[1]);
if(stripos($url, 'data:') === 0){
return $matches[0];
}
return 'url(\''.proxify_url($url).'\')';
}
function proxify_css($str){
$str = preg_replace_callback('@url\s*\((?:\'|"|)(.*?)(?:\'|"|)\)@im', 'css_url', $str);
return $str;
}
function html_href($matches){
return 'href="'.proxify_url($matches[1]).'"';
}
function html_src($matches){
if(stripos(trim($matches[1]), 'data:') === 0){
return $matches[0];
}
return 'src="'.proxify_url($matches[1]).'"';
}
function html_action($matches){
$new_action = proxify_url($matches[1]);
$result = str_replace($matches[1], $new_action, $matches[0]);
// change form method to POST!!!
$result = str_replace("<form", '<form method="POST"', $result);
return $result;
}
function proxify_html($str){
$str = proxify_css($str);
// html
$str = preg_replace_callback('@href=["|\'](.+?)["|\']@im', 'html_href', $str);
$str = preg_replace_callback('@src=["|\'](.+?)["|\']@i', 'html_src', $str);
$str = preg_replace_callback('@<form[^>]*action=["|\'](.+?)["|\'][^>]*>@i', 'html_action', $str);
$str = preg_replace_callback('@<meta\s*http-equiv="refresh"\s*content="[^;]*;\s*url=(.*?)"@i', function($matches){
return str_replace($matches[1], proxify_url($matches[1]), $matches[0]);
}, $str);
return $str;
}
// video player to be used
define('PLAYER_URL', '//www.php-proxy.com/assets/flowplayer-latest.swf');
function vid_player($url, $width, $height){
$video_url = proxify_url($url); // proxify!
$video_url = rawurlencode($video_url); // encode before embedding it into player's parameters
$html = '<object id="flowplayer" width="'.$width.'" height="'.$height.'" data="'.PLAYER_URL.'" type="application/x-shockwave-flash">
<param name="allowfullscreen" value="true" />
<param name="wmode" value="transparent" />
<param name="flashvars" value=\'config={"clip":"'.$video_url.'", "plugins": {"controls": {"autoHide" : false} }}\' />
</object>';
return $html;
}
function replace_title($input, $replace = ''){
return preg_replace('@<title>.*?<\/title>@s', '<title>'.$replace.'</title>', $input);
}
function remove_script($input){
$result = preg_replace("@<\s*script[^>]*[^/]>(.*?)<\s*/\s*script\s*>@is", '', $input);
$result = preg_replace("@<\s*script\s*>(.*?)<\s*/\s*script\s*>@is", '', $result);
return $result;
}
function chop_sub($host){
$parts = explode(".", $host);
if(count($parts) > 2){
unset($parts[0]);
return implode(".", $parts);
}
return false;
}
// will only be called on html, js, css, and other text-like pages
function parse($url, $output, $type){
// let's measure the time it takes to proxify and regex replace some file
$start = time_ms();
// check something.www.youtube.com then www.youtube.com then youtube.com...
$h = URL_HOST;
do {
$class_name = str_replace(".", "_", $h);
$file = $class_name.'.php';
if(file_exists('parser/'.$file)){
// load that parser!
include('parser/'.$file);
if(class_exists($class_name, true)){
$parser = new $class_name;
$output = $parser->parse($output, $url, $type);
}
break;
} else {
$h = chop_sub($h);
}
} while ($h);
// remove all iframe
$output = preg_replace('@<iframe.*?>.*?<\/iframe>@s', '', $output);
global $config;
if($type == 'html'){
if($config['remove_script']){
$output = remove_script($output);
}
$output = proxify_html($output);
}
if($config['replace_title']){
$output = replace_title($output, $config['replace_title']);
}
if($type == 'css'){
$output = proxify_css($output);
}
$time = time_ms() - $start;
$output .= '<!-- parsed in '.$time.' milliseconds using proxy! -->';
return $output;
}
?>