/
library.php
executable file
·295 lines (250 loc) · 11.6 KB
/
library.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
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
function populate(&$html, &$dict, &$links, &$path, &$base, $norw) {
if(!$html || $html->nodeType == XML_TEXT_NODE) return;
// If mod_rewrite is not enabled, modify links
if($html->nodeName == 'a' && $html->nodeType == XML_ELEMENT_NODE) {
$original_target = substr($html->getAttribute("href"),0);
$target = $original_target;
if($target[0] != '/') {
$target = '/'.$base.$path.'/'.$target;
}
if(in_array($original_target, $links)) {
if($norw) {
// Chop off base path, for terseness
if(strstr($target, $base))
$target = substr($target, strlen($base));
$html->getAttributeNode("href")->nodeValue = rtrim($base,"/")."/?p=".$target;
} else
$html->getAttributeNode("href")->nodeValue = $target;
}
}
for($i = 0; $i < $html->childNodes->length; $i++) {
$element = $html->childNodes->item($i);
if($element->nodeType == XML_ELEMENT_NODE) {
// ID and Class innerHTML replacement
$replacement = $dict[$element->getAttribute('id')];
if(!$replacement) $replacement = $dict[$element->getAttribute('class')];
if($replacement) {
// Clear out old nodes
foreach($element->childNodes as $child) {
$element->removeChild($child);
}
// Add in new ones
foreach($replacement->childNodes as $child) {
$element->appendChild($html->ownerDocument->importNode($child, TRUE));
}
}
// Taking care of entire tag replacement
if(!$replacement) {
$replacement = $dict[$element->tagName];
if($replacement) {
populate($imported_element, $dict, $links, $path, $base, $norw);
foreach($replacement->childNodes as $child) {
$imported_element = $html->ownerDocument->importNode($child, TRUE);
$element->parentNode->insertBefore($imported_element, $element);
}
$element->parentNode->removeChild($element);
}
}
populate($element, $dict, $links, $path, $base, $norw);
}
}
}
function renderPage($template, $page, $rw, $basepath="/") {
// TODO: refactor this so we don't overrite it
$pagename = $page;
// Get Simple Website Schema
$xml = new DOMDocument();
$xml->load($template);
$xml_xpath = new DOMXPath($xml);
// Get the template
$template = $xml->documentElement->getAttribute("template");
// Load the template
$html = new DOMDocument();
$html->load($template);
$html_xpath = new DOMXPath($html);
// Load the default page (the first one)
$requested_page_name = str_replace(" ","_",preg_replace("/[^_.\/a-zA-Z0-9 ']/","",strtolower(rtrim($page,"/"))));
if($requested_page_name[0] == '/') $requested_page_name = substr($requested_page_name,1);
$requested_page = $xml_xpath->query("/site/page")->item(0);
// Query for the actual page
$sections = explode("/", $requested_page_name);
// This is largely to make the search case insensitive
$query = "/site/page[translate(@link,'ABCDEFGHIJKLMNOPQRSTUVWXYZ ','abcdefghijklmnopqrstuvwxyz_') =\"".implode("\"]/subpages/page[translate(@link, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ ', 'abcdefghijklmnopqrstuvwxyz_') =\"",$sections)."\"]";
$query = $xml_xpath->query($query);
if($query->item(0))
$requested_page = $query->item(0);
// Start scoping variables
$variables = array();
$links = array();
// Add breadcrumb variables
$path_elements = array();
$count = count($sections) - 1;
$ptr = $requested_page;
while($ptr->nodeName != "site") {
if($ptr->getAttribute("link")) {
$path = $ptr->getAttribute("link");
$path_elements[] = $path;
$node = $xml->createElement(str_repeat("sub",$count)."path");
$node->appendChild($xml->createTextNode($path));
$variables[str_repeat("sub",$count)."path"] = $node;
$count--;
}
$ptr = $ptr->parentNode;
}
// Add variables from page
foreach($requested_page->childNodes as $element) {
if($element->nodeName != "#text")
$variables[$element->nodeName] = $element;
}
// If the page has subpages, set the scope to be of the subpages, otherwise, the current page
$scope;
$offset = 0;
if($xml_xpath->query("subpages",$requested_page)->item(0)) {
$scope = $xml_xpath->query("subpages", $requested_page)->item(0);
$offset += 1;
} else {
$scope = $requested_page;
}
// How deep we're tracing up/how, ie. many "../"'s we need to add
$depth = 2*(count($sections)) + $offset;
for($i = 0; $i < 2*(count($sections)) + $offset; $i++) {
// Add variables from owner (if not already defined)
foreach($scope->childNodes as $element) {
if(!in_array((string)$element->nodeName,array("#text","variables","page","subpages"))) {
if(!$variables[$element->nodeName]) {
$variables[$element->nodeName] = $element;
}
}
}
// Pages must be in a subpages node, thus we should bail here
if ($scope->nodeName == "page") {
$scope = $scope->parentNode;
continue;
}
// Trace current path
$path = "";
$ptr = $scope;
$subdepth = 0;
while($ptr->nodeName != "site") {
if($ptr->getAttribute("link")) {
$path = ((string) $ptr->getAttribute("link"))."/".$path;
}
$ptr = $ptr->parentNode;
}
$path = $basepath.$path;//str_repeat("../",($depth-1)/2).$path;
// Calculated how many "sub"s to add to this set of links (TODO: fix up this grossness)
$prefix = str_repeat("sub", (2*count($sections) - 1 - $i)/2 + $offset);
// Add all pages to sublinks
$menu_links = $xml->createElement($prefix."links");
// Extract the links
$query = $xml_xpath->query("page", $scope);
foreach($query as $page) {
$link = (string) $page->getAttribute("link");
$name = (string) $page->getAttribute("name");
$links[] = $path.$link;
// If they're not hidden, link them
if($page->getAttribute("hidden") != "true") {
$ref = $page->getAttribute("ref");
$link_node = $xml->createElement("a");
$link_href = $xml->createAttribute("href");
$link_href->appendChild($xml->createTextNode($ref ? $ref : $path.$link));
$link_node->appendChild($link_href);
$link_node->appendChild($xml->createTextNode($name ? $name : $link));
if(in_array($link, $path_elements)) {
$link_style = $xml->createAttribute("class");
$link_style->appendChild($xml->createTextNode("selected"));
$link_node->appendChild($link_style);
}
$menu_links->appendChild($link_node);
$menu_links->appendChild($xml->createTextNode(" "));
}
}
$variables[$prefix."links"] = $menu_links;
// Pop to parent
$scope = $scope->parentNode;
}
// Build the all encompassing breadcrum
$path = "path";
$breadcrumb = $xml->createElement("breadcrumb");
while($variables[$path]) {
$container = $xml->createElement("div");
$source = $variables[$path];
foreach($source->childNodes as $child) {
$container->appendChild($child->cloneNode(TRUE));
}
$breadcrumb->appendChild($container);
$path = "sub".$path;
}
$variables["breadcrumb"] = $breadcrumb;
// Build the all encompassing links_list
$path = "links";
$alllinks = $xml->createElement("alllinks");
while($variables[$path]) {
$container = $xml->createElement("div");
$source = $variables[$path];
foreach($source->childNodes as $child) {
$container->appendChild($child->cloneNode(TRUE));
}
$alllinks->appendChild($container);
$path = "sub".$path;
}
$variables["alllinks"] = $alllinks;
// Set the title
$site_title = $xml->documentElement->getAttribute('title');
$page_title = $requested_page->getAttribute('title');
$html_xpath->query("/html/head/title")->item(0)->nodeValue = $site_title.' '.$page_title;
// Set the css
$css = $html->createElement("link");
$css_href = $html->createAttribute("href");
$subpath = $xml->documentElement->getAttribute('css');
$css_href->appendChild($html->createTextNode(($subpath[0] == '/' ? "" : $basepath).$subpath));
$css_rel = $html->createAttribute("rel");
$css_rel->appendChild($html->createTextNode("stylesheet"));
$css_type = $html->createAttribute("type");
$css_type->appendChild($html->createTextNode("text/css"));
$css->appendChild($css_href);
$css->appendChild($css_rel);
$css->appendChild($css_type);
$html_xpath->query("/html/head")->item(0)->appendChild($css);
//print_r($variables);
//print_r($links);
// Substitute all the IDs in the html with the corresponding page elements
$path = rtrim($pagename,"/")."/";
populate($html, $variables, $links, $path, $basepath, !$rw);
return $html->saveHTML();
}
function compilePages($template, $filepath, $urlpath="/") {
// Get Simple Website Schema
$xml = new DOMDocument();
$xml->load($template);
$xml_xpath = new DOMXPath($xml);
$query = $xml_xpath->query("//page");
$home = TRUE;
foreach($query as $page) {
//We don't need to generate links to external sources
if ($page->getAttribute("ref")) continue;
//Get the full path
$path = "";
$ptr = $page;
while($ptr->nodeName != "site") {
if($ptr->getAttribute("link"))
$path = ((string) $ptr->getAttribute("link"))."/".$path;
$ptr = $ptr->parentNode;
}
// TODO: Sanitize these
//echo $path."...";
@mkdir(rtrim($filepath,"/")."/".trim($path,"/"), 0777, TRUE);
$fh = fopen(rtrim($filepath,"/")."/".trim($path,"/")."/index.html", "w");
fwrite($fh, renderPage($template, $path, TRUE, rtrim($urlpath,"/")."/"));
fclose($fh);
//We need to generate the default index under to title too
if ($home === TRUE) {
$fh = fopen(rtrim($filepath,"/")."/index.html", "w");
fwrite($fh, renderPage($template, $path, TRUE, rtrim($urlpath,"/")."/"));
fclose($fh);
$home = FALSE;
}
//echo " done<br />";
}
}