-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
318 lines (278 loc) · 12.6 KB
/
Copy pathindex.php
File metadata and controls
318 lines (278 loc) · 12.6 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
#CMS - CMS Made Simple
#(c)2004-2011 by Ted Kulp (wishy@users.sf.net)
#(c)2011-2017 by The CMSMS Dev Team
#Visit our homepage at: http://www.cmsmadesimple.org
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#$Id$
$starttime = microtime();
$orig_memory = (function_exists('memory_get_usage')?memory_get_usage():0);
/**
* Entry point for all non-admin pages
*
* @package CMS
*/
clearstatcache();
if (!isset($_SERVER['REQUEST_URI']) && isset($_SERVER['QUERY_STRING'])) $_SERVER['REQUEST_URI'] = $_SERVER['PHP_SELF'] . '?' . $_SERVER['QUERY_STRING'];
require_once(__DIR__.'/lib/include.php');
if (!is_writable(TMP_TEMPLATES_C_LOCATION) || !is_writable(TMP_CACHE_LOCATION)) {
echo '<html><title>Error</title></head><body>';
echo '<p>The following directories must be writable by the web server:<br />';
echo 'tmp/cache<br />';
echo 'tmp/templates_c<br /></p>';
echo '<p>Please correct by executing:<br /><em>chmod 777 tmp/cache<br />chmod 777 tmp/templates_c</em><br />or the equivalent for your platform before continuing.</p>';
echo '</body></html>';
exit;
}
@ob_start();
// initial setup
$_app = CmsApp::get_instance(); // internal use only, subject to change.
$config = \cms_config::get_instance();
$params = array_merge($_GET, $_POST);
$smarty = $_app->GetSmarty();
$page = get_pageid_or_alias_from_url();
$contentops = ContentOperations::get_instance();
$contentobj = null;
$trycount = 0;
$showtemplate = true;
\CMSMS\internal\content_cache::get_instance();
$_tpl_cache = new \CMSMS\internal\TemplateCache();
while( $trycount < 2 ) {
$trycount++;
try {
if( $trycount < 2 && is_file(TMP_CACHE_LOCATION.'/SITEDOWN') ) throw new \CmsError503Exception('Site down for maintenance');
if( $trycount < 2 && is_sitedown() ) throw new \CmsError503Exception('Site down for maintenance');
// preview
if( $page == -100) {
if( !isset($_SESSION['__cms_preview__']) ) throw new CmsException('preview selected, but temp data not found');
setup_session(false);
// todo: get the content type, and load it.
$contentops->LoadContentType($_SESSION['__cms_preview_type__']);
$contentobj = unserialize($_SESSION['__cms_preview__']);
$contentobj->SetCachable(FALSE);
$contentobj->SetId(__CMS_PREVIEW_PAGE__);
}
else {
// $page could be an integer ID or a string alias
$contentobj = $contentops->LoadContentFromAlias($page,true);
if( !is_object($contentobj) ) throw new CmsError404Exception('Page '.$page.' not found');
}
// session stuff is needed from here on.
$cachable = $contentobj->Cachable();
$uid = get_userid(FALSE);
if( $page == __CMS_PREVIEW_PAGE__ || $uid || $_SERVER['REQUEST_METHOD'] != 'GET' ) $cachable = false;
setup_session($cachable);
// from here in, we're assured to have a content object of some sort
if( !$contentobj->IsViewable() ) {
$url = $contentobj->GetURL();
if( $url != '' && $url != '#' ) redirect($url);
// not viewable, throw a 404.
throw new CmsError404Exception('Cannot view an unviewable page');
}
if( !$contentobj->IsPermitted() ) throw new CmsError403Exception('Permission denied');
$_app->set_content_object($contentobj);
$smarty->assignGlobal('content_obj',$contentobj);
$smarty->assignGlobal('content_id', $contentobj->Id());
$smarty->assignGlobal('page_id', $page);
$smarty->assignGlobal('page_alias', $contentobj->Alias());
CmsNlsOperations::set_language(); // <- NLS detection for frontend
$smarty->assignGlobal('lang',CmsNlsOperations::get_current_language());
$smarty->assignGlobal('encoding',CmsNlsOperations::get_encoding());
if ( (isset($_REQUEST['showtemplate']) && $_REQUEST['showtemplate'] == 'false') ) {
$_app->disable_template_processing();
}
\CMSMS\HookManager::do_hook('Core::ContentPreRender', [ 'content' => &$contentobj ] );
if( $config['content_processing_mode'] == 2 ) {
debug_buffer('preprocess module action');
\CMSMS\internal\content_plugins::get_default_content_block_content( $contentobj->Id(), $smarty );
}
$html = null;
$showtemplate = $_app->template_processing_allowed();
if( !$showtemplate ) {
$html = \CMSMS\internal\content_plugins::get_default_content_block_content( $contentobj->Id(), $smarty );
$trycount = 99;
}
else {
$tpl_id = $contentobj->TemplateId();
$top = $body = $head = null;
debug_buffer('process template top');
\CMSMS\HookManager::do_hook('Core::PageTopPreRender', [ 'content'=>&$contentobj, 'html'=>&$top ]);
$tpl = $smarty->createTemplate('tpl_top:'.$tpl_id);
$top .= $tpl->fetch();
unset($tpl);
\CMSMS\HookManager::do_hook('Core::PageTopPostRender', [ 'content'=>&$contentobj, 'html'=>&$top ]);
if( $config['content_processing_mode'] == 1 ) {
debug_buffer('preprocess module action');
\CMSMS\internal\content_plugins::get_default_content_block_content( $contentobj->Id() );
}
// if the request has a mact in it, process and cache the output.
debug_buffer('process template body');
\CMSMS\HookManager::do_hook('Core::PageBodyPreRender', [ 'content'=>&$contentobj, 'html'=>&$body ]);
$tpl = $smarty->createTemplate('tpl_body:'.$tpl_id);
$body .= $tpl->fetch();
unset($tpl);
\CMSMS\HookManager::do_hook('Core::PageBodyPostRender', [ 'content'=>&$contentobj, 'html'=>&$body ]);
debug_buffer('process template head');
\CMSMS\HookManager::do_hook('Core::PageHeadPreRender', [ 'content'=>&$contentobj, 'html'=>&$head ]);
$tpl = $smarty->createTemplate('tpl_head:'.$tpl_id);
$head .= $tpl->fetch();
unset($tpl);
\CMSMS\HookManager::do_hook('Core::PageHeadPostRender', [ 'content'=>&$contentobj, 'html'=>&$head ]);
$html = $top.$head.$body;
$trycount = 99; // no more iterations
}
}
catch (CmsError404Exception $e) {
// Catch CMSMS 404 error
// 404 error thrown... gotta do this process all over again
$page = 'error404';
unset($_REQUEST['mact']);
unset($_REQUEST['module']);
unset($_REQUEST['action']);
$handlers = ob_list_handlers();
for ($cnt = 0; $cnt < sizeof($handlers); $cnt++) { ob_end_clean(); }
// specified page not found, load the 404 error page
$contentobj = $contentops->LoadContentFromAlias('error404',true);
if( $showtemplate && is_object($contentobj) ) {
// we have a 404 error page
header("HTTP/1.0 404 Not Found");
header("Status: 404 Not Found");
}
else {
// no 404 error page
header("HTTP/1.0 404 Not Found");
header("Status: 404 Not Found");
echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL was not found on this server.</p>
</body></html>';
exit();
}
}
catch (CmsError403Exception $e) // <- Catch CMSMS 403 error
{
$page = 'error403';
unset($_REQUEST['mact']);
unset($_REQUEST['module']);
unset($_REQUEST['action']);
$handlers = ob_list_handlers();
for ($cnt = 0; $cnt < sizeof($handlers); $cnt++) { ob_end_clean(); }
// specified page not found, load the 404 error page.
$contentobj = $contentops->LoadContentFromAlias('error403',true);
$msg = $e->GetMessage();
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("HTTP/1.0 403 Forbidden");
header("Status: 403 Forbidden");
if( $showtemplate && is_object($contentobj) ) {
// we have a 403 error page.
}
else {
if( !$msg ) $msg = '<p>We are sorry, but you do not have the appropriate permission to view this item.</p>';
echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>'.$msg.'
</body></html>';
exit();
}
}
catch (CmsError503Exception $e) { // <- Catch CMSMS 503 error
$page = 'error503';
unset($_REQUEST['mact']);
unset($_REQUEST['module']);
unset($_REQUEST['action']);
$handlers = ob_list_handlers();
for ($cnt = 0; $cnt < sizeof($handlers); $cnt++) { ob_end_clean(); }
// specified page not found, load the 404 error page.
$contentobj = $contentops->LoadContentFromAlias('error503',true);
$msg = $e->GetMessage();
if( !$msg ) $msg = '<p>We are sorry, but you do not have the appropriate permission to view this item.</p>';
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("HTTP/1.0 503 Temporarily unavailable");
header("Status: 503 Temporarily unavailable");
if( $showtemplate && is_object($contentobj) ) {
// we have a 503 error page.
}
else {
@ob_end_clean();
echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>503 Site down for maintenance./title>
</head><body>
<h1>Sorry, down for maintenance. Check back shortly.</h1>'.$msg.'
</body></html>';
exit();
}
}
catch (Exception $e) {
// Catch rest of exceptions
$handlers = ob_list_handlers();
for ($cnt = 0; $cnt < sizeof($handlers); $cnt++) { ob_end_clean(); }
$code = $e->GetCode();
if( !$showtemplate && $code >= 400 ) {
@ob_end_clean();
header("HTTP/1.0 ".$code.' '.$e->GetMessage());
header("Status ".$code.' '.$e->GetMessage());
}
else {
echo $smarty->errorConsole($e);
}
exit();
}
} // end while trycount
\CMSMS\HookManager::do_hook( 'Core::ContentPostRender', [ 'content' => &$html ] );
if( !headers_sent() ) {
$ct = $_app->get_content_type();
header("Content-Type: $ct; charset=" . CmsNlsOperations::get_encoding());
}
echo $html;
@ob_flush();
if( $page == __CMS_PREVIEW_PAGE__ && isset($_SESSION['__cms_preview__']) ) unset($_SESSION['__cms_preview__']);
$debug = (defined('CMS_DEBUG') && CMS_DEBUG)?TRUE:FALSE;
if( $debug || isset($config['log_performance_info']) || (isset($config['show_performance_info']) && ($showtemplate == true)) ) {
$endtime = microtime();
$memory = (function_exists('memory_get_usage')?memory_get_usage():0);
$memory = $memory - $orig_memory;
$db = $_app->GetDb();
$sql_time = round($db->query_time_total,5);
$sql_queries = $db->query_count;
$memory_peak = (function_exists('memory_get_peak_usage')?memory_get_peak_usage():'n/a');
$time = microtime_diff($starttime,$endtime);
if( isset($config['log_performance_info']) ) {
$out = [ time(), $_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD'], $time, $sql_time, $queries, $memory, $memory_peak ];
$filename = TMP_CACHE_LOCATION.'/performance.log';
error_log(implode('|',$out)."\n", 3, $filename);
} else {
$txt = "Time: $time / SQL: {$sql_time}s for $sql_queries queries / Net Memory: {$memory} / Peak: {$memory_peak}";
echo '<div style="clear: both;"><pre><code>'.$txt.'</code></pre></div>';
}
}
if( $debug || is_sitedown() ) $smarty->clear_compiled_tpl();
if ( $debug && !is_sitedown() ) {
$arr = $_app->get_errors();
foreach ($arr as $error) {
echo $error;
}
}
exit();