Skip to content

Commit 95181a3

Browse files
committed
Improved error messages, 404 template, other fixes
1 parent 3dcaf88 commit 95181a3

File tree

1 file changed

+53
-39
lines changed

1 file changed

+53
-39
lines changed

index.php

+53-39
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# Source: http://people.w3.org/~dom/archives/2004/07/testing-php-pages-with-query_string/
1010

1111
# Configuration
12-
$file_name = get_env("HOMEPAGE","home"); # file by default
12+
$file_name = define_env("HOMEPAGE","home"); # file by default
1313
$file_mode = "view"; # "view" (default), "edit", "save", "save_edit", "upload", "template_save", "publish", "published"
1414

1515
define_env('SITE_NAME', 'Spmdwe Editor'); # Website name
@@ -28,12 +28,10 @@
2828

2929
# Application
3030

31-
function get_env($name, $default) {
32-
return isset($_ENV[$name]) ? $_ENV[$name] : $default;
33-
}
3431
function define_env($name, $default) {
3532
$value = isset($_ENV[$name]) ? $_ENV[$name] : $default;
3633
define($name, $value);
34+
return $value;
3735
}
3836

3937
# Ensure if the enviroment is correct
@@ -168,7 +166,10 @@ function define_env($name, $default) {
168166

169167
# Application in Demo mode (read-only)
170168
if(!SAVE_ENABLED or !$authenticated) {
171-
if($file_mode == "template_save") die("Could not save template because authentication is invalid.");
169+
if($file_mode == "template_save")
170+
die("Could not save template because authentication is invalid.");
171+
if($file_mode == "upload")
172+
die("Could not upload files because authentication is invalid.");
172173

173174
$file_mode = "view";
174175
$file_readonly = true;
@@ -267,8 +268,8 @@ function define_env($name, $default) {
267268

268269
# Download ZIP file with all files of the page
269270
else if($file_mode == "downloadzip") {
270-
header( "Content-Type: application/x-zip" );
271-
header( "Content-Disposition: attachment; filename=\"publish.zip\"" );
271+
header("Content-Type: application/x-zip");
272+
header("Content-Disposition: attachment; filename=\"$file_name.zip\"");
272273

273274
$stream = popen("zip -qj - files/$file_name/*", "r");
274275
if($stream) {
@@ -279,42 +280,55 @@ function define_env($name, $default) {
279280
}
280281

281282

283+
if($authenticated) {
284+
# Gets the file contents
285+
$message .= "Opening $file_name in $file_mode mode\\n";
282286

283-
# Gets the file contents
284-
$message .= "Opening $file_name in $file_mode mode\\n";
285-
286-
// check if the file exists
287-
$file_contents = false;
288-
if(file_exists($file_path_md))
289-
$file_contents = file_get_contents($file_path_md);
287+
// check if the file exists
288+
$file_contents = false;
289+
if(file_exists($file_path_md))
290+
$file_contents = file_get_contents($file_path_md);
290291

291292

292-
if($file_contents == false) {
293-
$file_contents = DEFAULT_TEXT;
294-
$file_mode = 'edit';
295-
$message .= "Cannot open $file_path_md. Proceeding in edit mode...\\n";
296-
}
297-
else {
298-
$pos = 0;
299-
do {
300-
$pos_start = strpos($file_contents, CSS_START, $pos); # find the next occurrence of CSS_START
301-
if($pos_start === false) break; # if there is no more CSS
302-
$pos_end = strpos($file_contents, CSS_END, $pos_start); # find the end of CSS block
303-
if($pos_end !== false) { # if the end if found, add it to the list
304-
$file_css[] = substr($file_contents, $pos_start + strlen(CSS_START), $pos_end - $pos_start - strlen(CSS_START));
305-
$pos = $pos_end + strlen(CSS_END);
306-
}
307-
else
308-
$pos = strlen($file_contents) - 1;
309-
} while(true);
293+
if($file_contents == false) {
294+
$file_contents = DEFAULT_TEXT;
295+
$file_mode = 'edit';
296+
$message .= "Cannot open $file_path_md. Proceeding in edit mode...\\n";
297+
}
298+
else {
299+
$pos = 0;
300+
do {
301+
$pos_start = strpos($file_contents, CSS_START, $pos); # find the next occurrence of CSS_START
302+
if($pos_start === false) break; # if there is no more CSS
303+
$pos_end = strpos($file_contents, CSS_END, $pos_start); # find the end of CSS block
304+
if($pos_end !== false) { # if the end if found, add it to the list
305+
$file_css[] = substr($file_contents, $pos_start + strlen(CSS_START), $pos_end - $pos_start - strlen(CSS_START));
306+
$pos = $pos_end + strlen(CSS_END);
307+
}
308+
else
309+
$pos = strlen($file_contents) - 1;
310+
} while(true);
311+
}
310312
}
311313

312-
313314
# if the file was published
314-
$html = "<h1>File not published!</h1>";
315-
if(file_exists($publish_file))
316-
$html = file_get_contents($publish_file);
317-
315+
if($file_mode == "published") {
316+
$html = false;
317+
if(file_exists($publish_file))
318+
$html = file_get_contents($publish_file);
319+
320+
if($html == false) {
321+
http_response_code(404);
322+
$html = <<<TEMPLATE404
323+
<div class="text-center">
324+
<h1 class="error">404</h1>
325+
<p class="lead text-gray-800 mb-5">Page Not Found</p>
326+
<p class="text-gray-500 mb-0">It looks like you found a glitch in the matrix...</p>
327+
<a href="/">&larr; Back to home</a>
328+
</div>
329+
TEMPLATE404;
330+
}
331+
}
318332

319333
# Base path for history files
320334
$file_revisions_path = $file_path.$base_file_name.REVISION_MARKER.'????????_??????.md';
@@ -323,7 +337,7 @@ function define_env($name, $default) {
323337
# List of revision files
324338
$count = 0;
325339
$file_revisions = array();
326-
foreach(glob($file_revisions_path) as $file_revision) {
340+
foreach(glob($file_revisions_path) as $file_revision) {
327341
$file_revisions[$count] = substr($file_revision, strlen($file_path), -strlen('.md'));
328342
$count++;
329343
}
@@ -360,7 +374,7 @@ function define_env($name, $default) {
360374
//header('Pragma: no-cache');
361375

362376
# Preview with the template provided
363-
if($file_mode == "published" and isset($_REQUEST['template']) and REQUIRED_AUTH)
377+
if($file_mode == "published" and isset($_REQUEST['template']) and REQUIRE_AUTH)
364378
eval('?>'.$_REQUEST['template'].'<?php ');
365379

366380
# Preview with the saved template

0 commit comments

Comments
 (0)