-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.php
109 lines (93 loc) · 3.54 KB
/
proxy.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
<?php
include_once 'simplehtmldom/simple_html_dom.php';
include_once 'simplehtmldom/HtmlWeb.php';
include_once 'functions.php';
session_set_cookie_params(array("path" => dirname($_SERVER['PHP_SELF']) . '/'));
session_start();
if (!isset($_REQUEST['url'])) die("need url");
$url = $_REQUEST['url'];
// load the document
if (!isset($_REQUEST['forcerefresh']) && isset($_SESSION[$url])) {
$doc = new simplehtmldom\HtmlDocument($_SESSION[$url]);
} else {
// either want to force refresh or new URL
$n = new simplehtmldom\HtmlWeb();
$doc = $n->load($url);
$_SESSION[$url] = "$doc"; // turn into a string
}
// figure out what to show
if (isset($_REQUEST['containersel']) && !empty($_REQUEST['containersel'])) {
$root = $doc->find($_REQUEST['containersel'], 0);
if (!$root) die("can't find that selector");
} else {
$root = $doc->root;
}
// annotate with the selectors
foreach ($root->find("*") as $el) {
$el->setAttribute("data-selector", compute_selector($root, $el));
}
// remove any javascript and potentially styles
foreach ($doc->find("script") as $el) $el->remove();
if (isset($_REQUEST['removestyles'])) {
foreach ($doc->find("style") as $el) $el->remove();
foreach ($doc->find("*[style]") as $el) $el->remove();
foreach ($doc->find('link[rel="stylesheet"]') as $el) $el->remove();
}
// make links and image sources absolute
foreach ($doc->find("a, img") as $el) {
foreach (["href", "src"] as $attr) {
if ($el->hasAttribute($attr)) {
$el->setAttribute($attr, getAbsoluteUrl($_GET['url'], $el->getAttribute($attr)));
}
}
}
// insert the scripts
/*
foreach (["https://code.jquery.com/jquery.min.js", absurl("proxystuff.js")] as $sc) {
$scriptel = $doc->createElement("script");
$scriptel->setAttribute("src", $sc);
$scriptel->setAttribute("type", "text/javascript");
$doc->root->find("head", 0)->appendChild($scriptel);
}
*/
// add a simple stylesheet
/*
$styleel = $doc->createElement("link");
$styleel->setAttribute("rel", "stylesheet");
$styleel->setAttribute("href", absurl("proxy_helper.css"));
$doc->root->find("head", 0)->appendChild($styleel);
*/
// add a base reference so relative links work
foreach ($doc->find("base") as $el) $el->remove(); // remove existing if any
$baseel = $doc->createElement("base");
$baseel->setAttribute("href", $url);
$headel = $doc->root->find("head", 0);
$headel->appendChild($baseel);
// need to make this element the first in the <head>
array_unshift($headel->children, array_pop($headel->children));
array_unshift($headel->nodes, array_pop($headel->nodes));
// output document
echo $root->innertext . PHP_EOL;
function compute_selector($root, $el) {
$path = array();
$continue = true;
while ($continue && $el != null && $root != $el && $el->nodetype === \simplehtmldom\HtmlNode::HDOM_TYPE_ELEMENT) {
$selector = $el->tag;
if ($el->hasAttribute("id")) {
$selector = '#' . $el->getAttribute("id");
$continue = false;
} else {
$sib = $el;
$nth = 0;
while ($sib->nodetype === \simplehtmldom\HtmlNode::HDOM_TYPE_ELEMENT
&& ($sib = $sib->previousSibling())) {
if ($sib->tag === $el->tag) $nth++;
}
// even if there is more than one, not appending `[0]` is fine since later on I just select the first matching element
if ($nth > 0) $selector .= "[$nth]";
}
array_unshift($path, $selector);
$el = $el->parent;
}
return implode(" > ", $path);
}