1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8require_once('../tiki-setup.php');
9require_once('lib/diff/difflib.php');
10
11if ($prefs['feature_tikitests'] != 'y') {
12	$smarty->assign('msg', tra('This feature is disabled') . ': feature_tikitests');
13	$smarty->display('error.tpl');
14	die;
15}
16
17if ($tiki_p_admin_tikitests != 'y' and $tiki_p_edit_tikitests != 'y') {
18	$smarty->assign('msg', tra('You do not have permission to do that'));
19	$smarty->display('error.tpl');
20	die;
21}
22
23$smarty->assign('tidy', extension_loaded('tidy'));
24$smarty->assign('http', extension_loaded('http'));
25$smarty->assign('curl', extension_loaded('curl'));
26
27/**
28 * @param $element
29 * @return array|null
30 */
31function get_from_dom($element)
32{
33	if ($element === null) {
34		return null;
35	}
36	$es = $element->getElementsByTagName('*');
37	$a = [];
38	foreach ($es as $e) {
39		$a[$e->tagName] = $e->nodeValue;
40	}
41	return $a;
42}
43
44/**
45 * @param $url
46 * @param $xpath
47 * @return mixed|string
48 */
49function enlight_xpath($url, $xpath)
50{
51	global $cookies, $base_url;
52	static $purifier;
53	static $loaded = false;
54	$smarty = TikiLib::lib('smarty');
55
56	$result = [];
57	$data = $url->getElementsByTagName('data')->item(0)->textContent;
58	if (trim($data) == '') {
59		return tra('The page is empty');
60	}
61
62	if (extension_loaded('tidy')) {
63		$data = tidy_parse_string($data, [], 'utf8');
64		tidy_diagnose($data);
65	} else {
66		if (! $loaded) {
67			require_once('lib/htmlpurifier_tiki/HTMLPurifier.tiki.php');
68			$config = getHTMLPurifierTikiConfig();
69			$config->set('Attr.EnableID', true);
70			$purifier = new HTMLPurifier($config);
71			$loaded = true;
72		}
73		if ($purifier) {
74			$data = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>' . $purifier->purify($data) . '</body></html>';
75			//$data = $purifier->purify($data);
76		}
77	}
78
79	$dom_ref = DOMDocument::loadHTML($data);
80	$xp_ref = new DomXPath($dom_ref);
81	$res_ref = $xp_ref->query('//head');
82	$base = $dom_ref->createElement('base');
83	$base->setAttribute('href', $base_url);
84	$res_ref->item(0)->insertBefore($base, $res_ref->item(0)->firstChild);
85	$res_ref = $xp_ref->query($xpath);
86	foreach ($res_ref as $ref) {
87		$ref->setAttribute('style', 'background-color: red;');
88	}
89
90	return $dom_ref->saveHTML();
91}
92
93
94$xml = file_get_contents('tiki_tests/tests/' . basename($_REQUEST['filename']));
95if ($xml == '') {
96	$smarty->assign('msg', tra('The TikiTests Replay File is Empty'));
97	$smarty->display('error.tpl');
98	die();
99} else {
100	$dom = DOMDocument::loadXML($xml);
101	$element_test = $dom->getElementsByTagName('test')->item(0);
102	if ($element_test == null) {
103		$smarty->assign('msg', tra('The TikiTests Replay File is Empty'));
104		$smarty->display('error.tpl');
105		die();
106	}
107}
108
109$result = [];
110$urls = $dom->getElementsByTagName('url');
111
112$count = 0;
113foreach ($urls as $url) {
114	if ($count == $_REQUEST['index']) {
115		echo enlight_xpath($url, $_REQUEST['xpath']);
116	}
117	$count++;
118}
119