forked from clue/php-viewvc-api-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.php
More file actions
50 lines (39 loc) · 1.65 KB
/
Loader.php
File metadata and controls
50 lines (39 loc) · 1.65 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
<?php
namespace Clue\React\ViewVcApi\Io;
use SimpleXMLElement;
class Loader
{
private $entities;
public function __construct(array $entities = null)
{
if ($entities === null) {
// get all HTML entities (minus those for XML parsing)
$entities = get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES, 'UTF-8');
unset($entities['<'], $entities['>'], $entities['&']);
}
$this->entities = $entities;
}
public function loadXmlFile($path)
{
return $this->loadXmlString(file_get_contents($path));
}
public function loadXmlString($html)
{
// log output often uses garbled ISO-8859-1 and UTF-8 encodings
$html = \ForceUTF8\Encoding::toUTF8($html);
// fix invalid markup of outdated ViewVC versions
// - help link in footer not terminated
// - selected branch/tag in CVS "sticky tag" dropdown has not attribute value
// - self closing elements with no trailing slash
// - excessive form close tags
$html = str_replace('Help</strong></td>', 'Help</a></strong></td>', $html);
$html = str_replace('selected>', 'selected="selected">', $html);
$html = preg_replace('#<((?:input|br|hr|img)[^\/\>]*)>#', '<$1 />', $html);
$html = preg_replace('#(</table>\s*)</form>\s*(</div>)#', '$1$2', $html);
// replace named HTML entities with their UTF-8 value
$html = str_replace(array_values($this->entities), array_keys($this->entities), $html);
// clean up namespace declaration
$html = str_replace('xmlns="', 'ns="', $html);
return new SimpleXMLElement($html);
}
}