This repository was archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlpurified.php
More file actions
100 lines (81 loc) · 3.96 KB
/
Copy pathhtmlpurified.php
File metadata and controls
100 lines (81 loc) · 3.96 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
#!/usr/bin/php
<?php
require_once 'HTMLPurifier.auto.php';
class MaiaDisplayLinkURI extends HTMLPurifier_Injector
{
public $name = 'DisplayLinkURI';
public $needed = array('a');
private $idcount = 1;
public function handleElement(&$token) {
}
public function handleEnd(&$token) {
if (isset($token->start->attr['href'])){
$url = MaiaDisplayLinkURI::pretty_url($token->start->attr['href']);
unset($token->start->attr['href']);
$token->start->attr['class'] = 'DisplayLink';
$token->start->attr['id'] = 'DisplayLink_' . $this->idcount;
$token = array_merge(array($token,
new HTMLPurifier_Token_Start('span', array('class' => 'DisplayLinkURL', 'id'=>'tip_DisplayLink_' . $this->idcount))),
$url,
array(
new HTMLPurifier_Token_End('span')
));
$this->idcount += 1;
} else {
// nothing to display
}
}
private static function color_tokens($part, $class) {
$token = array(
new HTMLPurifier_Token_Start('font', array('class' => "DisplayLink_" . $class)),
new HTMLPurifier_Token_Text($part),
new HTMLPurifier_Token_End('font')
);
return $token;
}
private static function pretty_url($url) {
// Make sure we have a string to work with
if(!empty($url)) {
// Explode into URL keys
$urllist=parse_url($url);
// Make sure we have a valid result set and a query field
if(is_array($urllist) ) {
// Build the the final output URL
$newurl=array();
if (isset($urllist["scheme"])) {$newurl = array_merge($newurl, MaiaDisplayLinkURI::color_tokens(($urllist['scheme'] . "://"),"scheme")); }
if (isset($urllist["user"])) {$newurl = array_merge($newurl, MaiaDisplayLinkURI::color_tokens($urllist["user"] . ":", "user")); }
if (isset($urllist["pass"])) {$newurl = array_merge($newurl, MaiaDisplayLinkURI::color_tokens($urllist["pass"] . "@", "pass")); }
if (isset($urllist["host"])) {$newurl = array_merge($newurl, MaiaDisplayLinkURI::color_tokens($urllist["host"], "host")); }
if (isset($urllist["port"])) {$newurl = array_merge($newurl, MaiaDisplayLinkURI::color_tokens(":" . $urllist["port"], "port")); }
if (isset($urllist["path"])) {$newurl = array_merge($newurl, MaiaDisplayLinkURI::color_tokens($urllist["path"], "path")); }
if (isset($urllist["query"])) {$newurl = array_merge($newurl, MaiaDisplayLinkURI::color_tokens(("?" . $urllist["query"]), "query")); }
if (isset($urllist["fragment"])) {$newurl = array_merge($newurl, MaiaDisplayLinkURI::color_tokens("#" . $urllist["fragment"], "fragment")); }
return $newurl;
}
}
return array();
}
}
function sanitize_html($body)
{
global $purifier_cache;
if (!isset($purifier_cache)) {
$purifier_cache = null;
}
$config = HTMLPurifier_Config::createDefault();
if ($purifier_cache) {
$config->set('Cache.SerializerPath', $purifier_cache);
} else {
$config->set('Cache.DefinitionImpl', null);
}
$config->set('URI.Disable', true);
$config->set('Attr.EnableID', true);
$config->set('AutoFormat.Custom', array(new MaiaDisplayLinkURI));
$purifier = new HTMLPurifier($config);
$html = $purifier->purify($body);
return ($html);
}
$document=file_get_contents("php://stdin");
$sanitized = sanitize_html($document);
print $sanitized;
?>