forked from sitemason/sm6-devlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSitemason.php
executable file
·198 lines (150 loc) · 6.11 KB
/
Sitemason.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
<?php
/*------------------------------------------------------------------------------------------
Sitemason, Inc.
www.sitemason.com
Sitemason® PHP Template Development Library
v6.0
This script sets up the currently-viewed materials: Site, Folder, and Tool, then presents
these objects as "smCurrent" variables to the template.
History:
20120208 tgraham initial content
20121127 tgraham overhauled for SM6+SM4/JSON approach
20130703 tgraham re-worked for devlib 6.0b16
Copyright (C) 2013 Sitemason, Inc. All Rights Reserved.
------------------------------------------------------------------------------------------*/
function __autoload($className) {
$parts = explode('_', $className);
$path = implode(DIRECTORY_SEPARATOR, $parts);
require_once $path . '.php';
}
//
// fetch navigation data
//
if (smShouldDebugApiRequests > 1) { $smConsoleDebug .= "console.info('Making nav data request');"; }
$navRequest = new SMRequest(array('url' => siteURL, 'feedType' => 'navjson'));
$smSiteNavigationData = $navRequest->getResponseData();
if (!$smSiteNavigationData || $navRequest->getResponseCode() != '200') {
$url = $navRequest->getDataURL();
die('<h3>Oh no. This doesn\'t look good...</h3><p>The library could not fetch navigation data for this site. The request returned a '. $navRequest->getResponseCode() .'.</p><p>'. $url .'</p>');
}
//
// $smCurrentFolder
//
$debugStart = microtime();
if (smShouldDebugApiRequests > 1) { $smConsoleDebug .= "console.info('instantiating \$smCurrentFolder in Sitemason.php');"; }
$smCurrentFolder = new SMFolder();
if (smShouldDebugApiRequests > 1) { $smConsoleDebug .= "console.info('Finished with \$smCurrentFolder in Sitemason.php');"; }
if (smDebugToolStructure) { echo '<h1>Done with smCurrentFolder!</h1>'; }
$debugStop = microtime();
$debugDuration = $debugStop - $debugStart;
if (smShouldDebugTiming) {
$smConsoleDebug .= 'console.info("TIMING: create smCurrentFolder: '. $debugDuration .'s");'."\n";
}
//
// Handle special cases: 404, JSON, XML, etc.
//
// look for 404 errors on the currently-viewed page
$currentFolderHttpResponseCode = $smCurrentFolder->getRequest()->getResponseCode();
$currentFolderHttpContentType = $smCurrentFolder->getRequest()->getResponseContentType();
if ($currentFolderHttpResponseCode == 200) {
//
// Process Sitemason JSON
//
if ($currentFolderHttpContentType == 'application/json') {
header('Content-type: text/html', true, $currentFolderHttpResponseCode);
//
// $smCurrentTool
//
$smCurrentTool = $smCurrentFolder->getCurrentTool();
#$smCurrentTool->setFolder($smCurrentFolder); // set in SMFolder
// Look for special query params
parse_str($_SERVER['QUERY_STRING'],$queryString);
// redirect
if ($smCurrentTool->getRedirectURL()) {
header('Location: '. $smCurrentTool->getRedirectURL());
exit();
}
// special output parameters
else if (isset($queryString['json'])) {
$smCurrentTool->printJson();
exit();
}
//
else if (isset($queryString['describe'])) {
echo '<p>';
echo ' <a href="#smCurrentSite">smCurrentSite</a><br />';
echo ' <a href="#smCurrentTool">smCurrentTool</a><br />';
echo '</p>';
echo '<a name="smCurrentSite"></a><h2>smCurrentSite:</h2>';
echo '<pre>'. $smCurrentSite->describe() .'</pre>';
echo '<a name="smCurrentTool"></a><h2>smCurrentTool:</h2>';
echo '<pre>'. $smCurrentTool->describe() .'</pre>';
exit();
}
//
// $smCurrentSite
//
$smCurrentFolderPath = $smCurrentFolder->getPath();
if (smDebugToolStructure) { echo '<b>smCurrentFolder\'s Path is: '. $smCurrentFolderPath .'</b><br/>'; }
//if $smCurrentFolder's path is "/", then it is the site.
if ($smCurrentFolderPath == '/') {
if (smDebugToolStructure) { echo '<b>smCurrentFolder is the root/site! Setting smCurrentSite = smCurrentFolder!</b><br>'; }
$smCurrentSite = $smCurrentFolder;
}
// otherwise, smCurrentTool is in a subfolder. Instantiate a folder for the root/site.
else {
if (smDebugToolStructure) { echo '<b>smCurrentFolder is a subfolder. Instantiating an SMFolder for the root/site</b><br>'; }
$requestData = array('url' => siteURL, 'feedType' => 'sitejson');
$debugStart = microtime();
$smCurrentSite = new SMFolder($requestData);
$debugStop = microtime();
$debugDuration = $debugStop - $debugStart;
if (smShouldDebugTiming) {
$smConsoleDebug .= 'console.info("TIMING: create smCurrentFolder: '. $debugDuration .'s");'."\n";
}
}
if (smDebugToolStructure) { echo '<h1>Done with smCurrentSite!</h1>'; }
}
/*
if we didn't get JSON but we got a 200, then we're not going to build-out the Sitemason data structure.
Send out the responseData as-is, but set the content-type appropriately
*/
else {
header('Content-type: '. $currentFolderHttpContentType, true, $currentFolderHttpResponseCode);
echo $smCurrentFolder->getRequest()->getResponseData();
exit();
}
}
//
// Non-200 status codes
//
// Redirect
if ($currentFolderHttpResponseCode == 301 || $currentFolderHttpResponseCode == 302) {
header('Location: '. $smCurrentFolder->getRequest()->getResponseData());
exit();
}
// bad URL
else if ($currentFolderHttpResponseCode >= 400) {
// allow $smCurrentSite to be present for any 404 page
$requestData = array('url' => siteURL, 'feedType' => 'sitejson');
$smCurrentFolder = new SMFolder($requestData);
$smCurrentSite = $smCurrentFolder;
header('Content-type: text/html', true, $currentFolderHttpResponseCode);
// look for a custom 404 in the template directory
if (file_exists(smFullPathForTemplateDirectory .'/404.php')) {
require_once(smFullPathForTemplateDirectory .'/404.php');
}
// look for the tool template's 404
else if (file_exists(smFullPathForToolTemplateSetDirectory .'/404.php')) {
require_once(smFullPathForToolTemplateSetDirectory .'/404.php');
}
// if we're not already on the /404 page, redirect there
else if ($_SERVER['PHP_SELF'] != '/404') {
header ('Location: /404');
}
else {
echo '404 - file not found.';
}
exit();
}
?>