-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphantom.php
55 lines (51 loc) · 1.25 KB
/
phantom.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
<?php
function error(){
header("HTTP/1.0 404 Not Found");
include_once('path-to-error-page');
};
function cache(){
$hash = md5($_GET['url']);
if (file_exists('path-to-phantom-cache' . $hash)) {
$cache = file_get_contents('path-to-phantom-cache' . $hash);
if (strpos($cache, '404 ERROR')){
error();
}else{
echo $cache;
}
} else {
$maxTries = 5;
while (!$content && $maxTries > 0){
if ($content = phantom($_GET['url'], $maxTries)){
file_put_contents('path-to-phantom-cache' . $hash, $content);
if (strpos($content, '404 ERROR')){
error();
}else{
echo $content;
}
}else{
$maxTries--;
}
}
if ($maxTries == 0){
header("HTTP/1.0 408 Request Timeout");
include_once('path-to-error-page');
}
};
};
function phantom($url, $trie){
$seconds = 1;
$maxTries = 5;
$timeout = $maxTries - $trie + $seconds;
$result = shell_exec('timeout ' . $timeout . ' ' . 'path-to-phantom' . ' page.js ' . 'http://' . $_SERVER['SERVER_NAME'] . '/#' . $url);
if (strpos($result, '<html')){
return $result . '<!-- Tries left: ' . $trie. ' -->';
}else{
return false;
}
};
if (preg_match("/^([0-9a-z]+\/?)*$/", $_GET['url'])){
cache();
}else{
error();
};
?>