-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcontent.debug.php
More file actions
executable file
·231 lines (174 loc) · 6.88 KB
/
content.debug.php
File metadata and controls
executable file
·231 lines (174 loc) · 6.88 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
if (!defined('BITTER_LANGUAGE_PATH')) {
define('BITTER_LANGUAGE_PATH', EXTENSIONS . '/debugdevkit/lib/bitter/languages');
}
if (!defined('BITTER_FORMAT_PATH')) {
define('BITTER_FORMAT_PATH', EXTENSIONS . '/debugdevkit/lib/bitter/formats');
}
if (!defined('BITTER_CACHE_PATH')) {
define('BITTER_CACHE_PATH', CACHE);
}
require_once(TOOLKIT . '/class.devkit.php');
require_once(EXTENSIONS . '/debugdevkit/lib/bitter/bitter.php');
// Create cache folder:
if (!is_dir(BITTER_CACHE_PATH) && General::realiseDirectory(BITTER_CACHE_PATH) === false){
throw new Exception(__('Failed to create cache folder. Please check "%s" is writable.', array(BITTER_CACHE_PATH)));
}
elseif(!is_writable(BITTER_CACHE_PATH)){
throw new Exception(__('Cache folder is not writable. Please check permissions on "%s".', array(BITTER_CACHE_PATH)));
}
class Content_DebugDevKit_Debug extends DevKit {
protected $_view = '';
protected $_full_utility_list = '';
public function __construct(){
parent::__construct();
$this->_title = __('Debug');
$this->_query_string = parent::__buildQueryString(array('symphony-page', 'debug'));
if (!empty($this->_query_string)) {
$this->_query_string = '&' . General::sanitize($this->_query_string);
}
}
public function build(array $context = array()) {
$this->_view = (strlen(trim($_GET['debug'])) == 0 ? 'xml' : $_GET['debug']);
return parent::build();
}
protected function buildJump(XMLElement $wrapper) {
$list = new XMLElement('ul');
$list->appendChild($this->buildJumpItem(
__('Params'),
'?debug=params' . $this->_query_string,
($this->_view == 'params')
));
$list->appendChild($this->buildJumpItem(
__('XML'),
'?debug=xml' . $this->_query_string,
($this->_view == 'xml')
));
$filename = $this->__relativePath($this->_pagedata['filelocation']);
$item = $this->buildJumpItem(
basename($filename),
"?debug={$filename}" . $this->_query_string,
($this->_view == $filename)
);
$utilities = $this->__buildUtilityList($this->__findUtilitiesInFile($this->_pagedata['filelocation']), 1, $this->_view);
if (is_object($utilities)) {
$item->appendChild($utilities);
}
$list->appendChild($item);
$list->appendChild($this->buildJumpItem(
__('Result'),
'?debug=result' . $this->_query_string,
($this->_view == 'result')
));
$wrapper->appendChild($list);
}
public function buildContent(XMLElement $wrapper) {
$this->addStylesheetToHead(URL . '/extensions/debugdevkit/assets/devkit.css', 'screen', 10);
$this->addScriptToHead(APPLICATION_URL . '/assets/js/lib/jquery.js', 20);
$this->addScriptToHead(URL . '/extensions/debugdevkit/assets/jquery.scrollto.js', 30);
$this->addScriptToHead(URL . '/extensions/debugdevkit/assets/devkit.js', 40);
if ($this->_view == 'params') {
$wrapper->appendChild($this->__buildParams($this->_param));
} else if ($this->_view == 'xml') {
$this->appendSource($wrapper, $this->_xml, 'xml');
} else if ($this->_view == 'result') {
$this->appendSource($wrapper, $this->_output, 'xml');
} else if ($this->_view == 'raw') {
header('Content-Type: application/xml');
echo $this->_xml;
die();
} else {
if ($_GET['debug'] == $this->__relativePath($this->_pagedata['filelocation'])) {
$this->appendSource($wrapper, @file_get_contents($this->_pagedata['filelocation']), 'xsl');
}
else if (is_array($this->_full_utility_list) && !empty($this->_full_utility_list)) {
foreach ($this->_full_utility_list as $u) {
if ($_GET['debug'] != $this->__relativePath($u)) continue;
$this->appendSource($wrapper, @file_get_contents($u), 'xsl');
break;
}
}
}
}
protected function appendSource(XMLElement $wrapper, $source, $language = 'xml') {
$bitter = new Bitter();
$bitter->loadFormat('symphony');
$bitter->loadLanguage($language);
$inner = new XMLElement(
'div', $bitter->process($source)
);
$inner->setAttribute('id', 'source');
$wrapper->appendChild($inner);
if($this->_view == 'xml') {
$viewRaw = Widget::Anchor('','?debug=raw');
$viewRaw->setAttribute('id', 'type');
$viewRaw->appendChild(new XMLElement('span',__('Plain XML')));
$wrapper->appendChild($viewRaw);
}
}
protected function __buildParams($params) {
if (!is_array($params) || empty($params)) return;
$params = General::array_map_recursive(array('General', 'sanitize'), $params);
$wrapper = new XMLElement('div');
$wrapper->setAttribute('id', 'params');
$table = new XMLElement('table');
foreach ($params as $key => $value) {
$value = is_array($value) ? implode(', ', $value) : $value;
$row = new XMLElement('tr');
$row->appendChild(new XMLElement('th', "\${$key}"));
$row->appendChild(new XMLElement('td', "'{$value}'"));
$table->appendChild($row);
}
$wrapper->appendChild($table);
return $wrapper;
}
protected function __buildUtilityList($utilities, $level=1, $view = null) {
if (!is_array($utilities) || empty($utilities)) return;
$list = new XMLElement('ul');
foreach ($utilities as $u) {
$filename = $this->__relativePath($u);
$item = $this->buildJumpItem(
basename($filename),
"?debug={$filename}" . $this->_query_string,
($view == "{$filename}"),
"?debug-edit={$u}" . $this->_query_string
);
$child_utilities = $this->__findUtilitiesInFile($u);
if (is_array($child_utilities) && !empty($child_utilities)) {
$item->appendChild($this->__buildUtilityList($child_utilities, $level + 1, $view));
}
$list->appendChild($item);
}
return $list;
}
protected function __findUtilitiesInFile($filename) {
if(file_exists($filename) && is_readable($filename)) {
$xsl = file_get_contents($filename);
}
if ($xsl == '') return;
$utilities = array();
try {
$xsl = @new SimpleXMLElement($xsl);
}
catch(Exception $e) { // simply abort recursion in this branch if XSL file contains invalid XML
return $utilities;
}
$matches = $xsl->xpath("*[local-name()='import' or local-name()='include']");
foreach($matches AS $match) {
$attributes = $match->attributes();
if($attributes["href"] != basename($filename) && file_exists(realpath(dirname($filename) . "/" . $attributes["href"])))
$utilities[] = realpath(dirname($filename) . "/" . $attributes["href"]);
}
if (!is_array($this->_full_utility_list)) {
$this->_full_utility_list = array();
}
if (is_array($utilities) && !empty($utilities)) {
$this->_full_utility_list = array_merge($utilities, $this->_full_utility_list);
}
return $utilities;
}
private function __relativePath($filename) {
// remove path to DOCROOT from absolute path. the realpath mess is necessary to cope with Windows paths (realpath always returns C:\Programs\... instead of /Programs/...)
return str_replace('\\','/',str_replace(realpath(DOCROOT),'',realpath($filename)));
}
}