-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser_emulator.lib.php
More file actions
433 lines (338 loc) · 11.6 KB
/
Copy pathbrowser_emulator.lib.php
File metadata and controls
433 lines (338 loc) · 11.6 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
<?php
// $emulator = new LIB_Browser_Emulator();
// $emulator->base_url('');
// $emulator->replace_js(TRUE);
// $emulator->replace_link(TRUE);
// $emulator->replace_form(TRUE);
// $emulator->get('https://www.google.com/search?q=test&hl=en&gbv=1&tbm=isch&ei=OJ7IT5OSBLPT4QSN_fjsDw&start=20&sa=N');
// // OR:
// // $emulator->process();
class LIB_Browser_Emulator
{
//--------------------------------------------------------------------------
private $base_url = '';
private $cookie_path = 'temp/';
private $cmd_key = array(
'url' => 'u',
'referer' => 'r',
'method' => 'm'
);
private $replace_js = FALSE;
private $replace_link = FALSE;
private $replace_form = FALSE;
// private $replace_img = FALSE;
private $method = 'post';
private $request = NULL;
private $session_id = NULL;
private $content = NULL;
private $referer = NULL;
private $headers = array();
private $log = array();
private $request_data = array();
//--------------------------------------------------------------------------
public function __construct()
{
$this->request = new stdClass;
}
//--------------------------------------------------------------------------
public function session_id($val = NULL)
{
if ($val !== NULL) $this->session_id = $val;
return $this->session_id;
}
//--------------------------------------------------------------------------
public function content($val = NULL)
{
if ($val !== NULL) $this->content = $val;
return $this->content;
}
//--------------------------------------------------------------------------
public function method($val = NULL)
{
if ($val !== NULL) $this->method = $val;
return $this->method;
}
//--------------------------------------------------------------------------
public function cookie_path($val = NULL)
{
if ($val !== NULL) $this->cookie_path = $val;
return $this->cookie_path;
}
//--------------------------------------------------------------------------
public function headers($key = NULL, $val = NULL)
{
if ($key)
{
if ($val) return ($this->headers[$key] = $val);
return isset($this->headers[$key]) ? $this->headers[$key] : NULL;
}
return $this->headers;
}
//--------------------------------------------------------------------------
public function cookie_file()
{
return $this->cookie_path() . $this->request->domain . '.txt';
}
//--------------------------------------------------------------------------
public function parsed_content()
{
$content = $this->content();
// foreach ($this->headers as $key => $val)
// {
// if ($key) header($key . ': ' . $val);
// }
if ( ! empty($this->headers['content-type']))
{
header('Content-type: ' . $this->headers['content-type']);
if (strpos($this->headers['content-type'], 'text/html') !== FALSE)
{
$site_url = $this->request->scheme . '://' . $this->request->host;
$content = preg_replace('/((href|src)=["\']?)\/([^\/])/ui', '$1' . $site_url . '/$3' , $content);
if ($this->replace_form())
{
$content = preg_replace_callback('/(<form [^>]*>)/ui', array($this, '_replace_form_callback'), $content);
}
if ($this->replace_js())
{
$content = preg_replace('@<script[^>]*?.*?</script>@siu', '', $content);
}
if ($this->replace_link())
{
$content = preg_replace_callback('/(<a [^>]*href=(["\']))([^\2]+?)(\2[^<]*>)/ui', array($this, '_replace_link_callback'), $content);
}
}
}
return $content;
}
//--------------------------------------------------------------------------
public function _replace_form_callback($form)
{
$attr = array();
preg_match_all('/([\w]+)[ ]*=(["\' ]?)([^"\']*)[\2]?/i', $form[0], $matches);
if (count($matches))
{
foreach ($matches[1] as $i => $key) $attr[$key] = $matches[3][$i];
}
array_walk($attr, 'trim');
if ( ! empty($attr['action']))
{
if ($attr['action']{0} == '/') $attr['action'] = $this->request->url . (substr($this->request->url, -1) == '/'?'':'/') . substr($attr['action'], 1);
// elseif (substr($attr['action'], 0, 4) != 'http') $attr['action'] = $this->request->url . $attr['action'];
}
$attr['action'] = $this->base_url()
. '?' . $this->cmd_key['url'] . '=' . urlencode(str_replace('&', '&', !empty($attr['action']) ? $attr['action'] : $this->request->url))
. '&' . $this->cmd_key['referer'] . '=' . urlencode($this->request->url);
if (empty($attr['method']) || strtolower($attr['method'])=='get')
{
$attr['action'] .= '&' . $this->cmd_key['method'] . '=get';
}
$attr['method'] = 'post';
$result = '';
foreach ($attr as $key => $val) $result .= " {$key}=\"{$val}\"";
return "<form{$result}>";
}
// //--------------------------------------------------------------------------
public function _replace_link_callback($matches)
{
if (substr($matches[3], 0, 4) != 'http') return $matches[0];
return $matches[1] . $this->base_url() . '?' . $this->cmd_key['url'] . '=' . urlencode(str_replace('&', '&', $matches[3])) . $matches[4];
// echo htmlspecialchars(print_r($matches, 1));
}
//--------------------------------------------------------------------------
public function base_url($val = NULL)
{
if ($val !== NULL) $this->base_url = $val;
return $this->base_url;
}
//--------------------------------------------------------------------------
public function replace_link($val = NULL)
{
if ($val !== NULL) $this->replace_link = (bool)$val;
return $this->replace_link;
}
//--------------------------------------------------------------------------
public function replace_js($val = NULL)
{
if ($val !== NULL) $this->replace_js = (bool)$val;
return $this->replace_js;
}
//--------------------------------------------------------------------------
public function replace_form($val = NULL)
{
if ($val !== NULL) $this->replace_form = (bool)$val;
return $this->replace_form;
}
//--------------------------------------------------------------------------
// public function replace_img($val = NULL)
// {
// if ($val !== NULL) $this->replace_img = (bool)$val;
// return $this->replace_img;
// }
//--------------------------------------------------------------------------
public function log($key = NULL, $msg = NULL)
{
if ($key && $msg) $this->log[] = $key . ': ' . $msg;
return $this->log;
}
//--------------------------------------------------------------------------
public function referer($val = NULL)
{
if ($val !== NULL) $this->referer = $val;
return $this->referer;
}
//--------------------------------------------------------------------------
public function user_agent()
{
return $_SERVER['HTTP_USER_AGENT'];
}
//--------------------------------------------------------------------------
public function request_data($key = NULL, $val = NULL)
{
if ($key === FALSE)
{
$this->request_data = array();
}
if ($key)
{
if ($val)
{
$this->request_data[$key] = $val;
}
else
{
if (is_array($key))
{
$this->request_data = array_merge($this->request_data, $key);
}
else
{
$list = explode('&', $key);
foreach ($list as $row)
{
$row = explode('=', $row);
if (count($row) == 2) $this->request_data[$row[0]] = $row[1];
}
}
}
}
return $this->request_data;
}
//--------------------------------------------------------------------------
public function request_data_str()
{
$resutl = '';
foreach ($this->request_data as $key => $val)
{
$resutl .= ($resutl ? '&' : '') . $key . '=' . $val;
}
return $resutl;
}
//--------------------------------------------------------------------------
public function get($request)
{
$this->log('GET', $request);
if (substr($request, 0, 4) != 'http') $request = 'http://' . $request;
$param = parse_url($request);
foreach ($param as $key => $val) $this->request->$key = $val;
$this->request->src = $request;
$this->request->domain = preg_replace('/^.*?\.?([^\.]+\.[^\.]{2,5})$/i', '\1', $this->request->host);
if ($this->request_data() && $this->method() == 'get')
{
$this->request->query = $this->request_data_str();
}
$this->request->url = $this->request->scheme . '://'
. $this->request->host
. (empty($this->request->path) ? '' : $this->request->path)
. (empty($this->request->query) ? '' : '?' . $this->request->query);
$this->referer($this->request->url);
// инициализация cURL
$ch = curl_init($this->request->url);
// получать заголовки
curl_setopt($ch, CURLOPT_HEADER, 1);
// Устанавливаем USER_AGENT
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent());
// елси проверятся откуда пришел пользователь, то указываем допустимый заголовок HTTP Referer:
if ($this->referer())
{
curl_setopt ($ch, CURLOPT_REFERER, $this->referer());
}
if ($this->request_data() && $this->method() == 'post')
{
// использовать метод POST
curl_setopt ($ch, CURLOPT_POST, 1);
// передаем поля формы
curl_setopt ($ch, CURLOPT_POSTFIELDS, $this->request_data_str());
}
// сохранять информацию Cookie в файл, чтобы потом можно было ее использовать
curl_setopt ($ch, CURLOPT_COOKIEJAR, $this->cookie_file());
curl_setopt ($ch, CURLOPT_COOKIEFILE, $this->cookie_file());
// возвращать результат работы
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE);
// не проверять SSL сертификат
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
// не проверять Host SSL сертификата
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
// это необходимо, чтобы cURL не высылал заголовок на ожидание
curl_setopt ($ch, CURLOPT_HTTPHEADER, array('Expect:'));
// выполнить запрос
curl_exec ($ch);
// получить результат работы
$this->_curl_get_content($ch);
// закрыть сессию работы с cURL
curl_close($ch);
}
//--------------------------------------------------------------------------
private function _curl_get_content($ch)
{
$result = curl_multi_getcontent($ch);
$nl = strpos(substr($result, 0, 300), "\r\n") ? "\r\n" : "\n";
$headers_length = strpos($result, $nl . $nl);
$headers_text = explode("\n", trim(substr($result, 0, $headers_length)));
$content = substr($result, $headers_length + strlen($nl) * 2);
$this->headers = array();
foreach ($headers_text as $i => $row)
{
if ( ($divider_pos = strpos($row, ':')) )
{
$key = trim(substr($row, 0, $divider_pos));
$val = trim(substr($row, $divider_pos+1));
$this->headers[strtolower($key)] = $val;
}
else
{
$this->headers[] = $row;
}
}
// $this->headers($headers);
if (isset($this->headers['location']))
{
$this->log('REDIRECT', $this->headers['location']);
return $this->get($this->headers['location']);
}
$this->content($content);
}
//--------------------------------------------------------------------------
public function process()
{
if (empty($_GET[$this->cmd_key['url']])) return;
if ( ! empty($_GET[$this->cmd_key['referer']]))
{
$this->referer($_GET[$this->cmd_key['referer']]);
}
if (count($_POST))
{
if (isset($_GET[$this->cmd_key['method']]) && $_GET[$this->cmd_key['method']] == 'get')
{
$this->method('get');
$this->request_data($_POST);
}
else
{
$this->method('post');
$this->request_data($_POST);
}
}
$this->get($_GET[$this->cmd_key['url']]);
}
//--------------------------------------------------------------------------
}