Skip to content

Commit be1a405

Browse files
committed
Properly logout and improve overall security
1 parent 943282f commit be1a405

File tree

4 files changed

+92
-82
lines changed

4 files changed

+92
-82
lines changed

Dockerfile

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@ FROM php:apache
33
# Enable ReWrite module
44
RUN a2enmod rewrite
55

6+
# Install zip
7+
RUN apt update && \
8+
apt install -y zip && \
9+
apt clean && rm -rf /var/lib/apt/lists/*
10+
611
VOLUME /var/www/html/files/
712

813
COPY . /var/www/html/
914

10-
RUN chmod a-w README.md EXAMPLES.md MARKDOWN-STYLES.md && \
15+
RUN ln -svf ../../README.md files/readme/readme.md && \
16+
ln -svf ../../EXAMPLES.md files/examples/examples.md && \
17+
ln -svf ../../MARKDOWN-STYLES.md files/markdown_styles/markdown_styles.md && \
18+
chmod a-w README.md EXAMPLES.md MARKDOWN-STYLES.md && \
19+
chown www-data files/ && \
1120
echo "upload_max_filesize = 50M" > /usr/local/etc/php/conf.d/upload_size.ini && \
1221
echo "post_max_size = 50M" >> /usr/local/etc/php/conf.d/upload_size.ini

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ You can set environment variables to customize your installation:
7070
- `USER`: Username used for authentication
7171
- `PASS`: Password for the user
7272

73+
For a multi-arch build, it can be done with:
74+
75+
docker buildx build --push -t rigon/spmdwe --platform linux/386,linux/amd64,linux/arm/v6,linux/arm/v7,linux/arm64/v8,linux/ppc64le .
7376

7477
Download
7578
---

index.php

+53-59
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

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

1515
define_env('SITE_NAME', 'Spmdwe Editor'); # Website name
1616
define_env('SAVE_ENABLED', true); # set to false to disable saving ("demo mode")
@@ -73,48 +73,42 @@ function define_env($name, $default) {
7373
session_start();
7474

7575
## Check if authentication is provided
76-
if(!isset($_SERVER['PHP_AUTH_USER']) or !isset($_SERVER['PHP_AUTH_PW'])) {
77-
$authenticated = false;
78-
$message .= 'Authentication failed!\nProceeding in published mode...\n';
79-
}
80-
## Check user credentials using environment variables
81-
elseif(isset($_ENV['USER']) and isset($_ENV['PASS'])) {
82-
$user = $_SERVER['PHP_AUTH_USER'];
83-
$pass = $_SERVER['PHP_AUTH_PW'];
84-
if($user == $_ENV['USER'] and $pass == $_ENV['PASS'])
85-
$authenticated = true;
86-
else {
87-
$authenticated = false;
88-
$message .= 'Authentication failed!\nProceeding in published mode...\n';
76+
if(isset($_SERVER['PHP_AUTH_USER']) and isset($_SERVER['PHP_AUTH_PW'])) {
77+
78+
## Check user credentials using environment variables (overrides htpasswd)
79+
if(isset($_ENV['USER']) and isset($_ENV['PASS'])) {
80+
$user = $_SERVER['PHP_AUTH_USER'];
81+
$pass = $_SERVER['PHP_AUTH_PW'];
82+
if($user == $_ENV['USER'] and $pass == $_ENV['PASS'])
83+
$authenticated = true;
8984
}
90-
}
91-
## Check user credentials using htpasswd
92-
elseif(file_exists(AUTH_FILE)) {
93-
$user = escapeshellarg($_SERVER['PHP_AUTH_USER']);
94-
$pass = escapeshellarg($_SERVER['PHP_AUTH_PW']);
95-
96-
exec("htpasswd -vb ".AUTH_FILE." $user $pass 2>&1", $output, $returnval);
97-
$message .= implode('\n', $output).'\n';
98-
99-
// Start session if valid
100-
if($returnval == 0 and isset($_GET['login']))
101-
$_SESSION['session_started'] = true;
102-
103-
// Authenticate user if valid
104-
if($returnval == 0 and isset($_SESSION['session_started']) and $_SESSION['session_started'] == true)
105-
$authenticated = true;
106-
else {
107-
$authenticated = false;
108-
$message .= 'Authentication failed!\nProceeding in published mode...\n';
85+
86+
## Check user credentials using htpasswd
87+
elseif(file_exists(AUTH_FILE)) {
88+
$user = escapeshellarg($_SERVER['PHP_AUTH_USER']);
89+
$pass = escapeshellarg($_SERVER['PHP_AUTH_PW']);
90+
91+
exec("htpasswd -vb ".AUTH_FILE." $user $pass 2>&1", $output, $returnval);
92+
$message .= implode('\n', $output).'\n';
93+
94+
// Start session if valid
95+
if($returnval == 0 and isset($_GET['login']))
96+
$_SESSION['session_started'] = true;
97+
98+
// Authenticate user if valid
99+
if($returnval == 0 and isset($_SESSION['session_started']) and $_SESSION['session_started'] == true)
100+
$authenticated = true;
109101
}
102+
103+
// Cleanup
104+
$user = $_SERVER['PHP_AUTH_USER'];
105+
unset($pass);
110106
}
107+
111108
## Not authenticated
112-
else
113-
$authenticated = false;
114-
115-
// Cleanup
116-
$user = $_SERVER['PHP_AUTH_USER'];
117-
unset($pass);
109+
if(!$authenticated) {
110+
$message .= 'Authentication failed!\n';
111+
}
118112

119113
## Login
120114
if((isset($_GET['login']) and !$authenticated)) {
@@ -128,6 +122,7 @@ function define_env($name, $default) {
128122
if(isset($_GET['logout']) and $authenticated) {
129123
unset($_SESSION['session_started']);
130124
$authenticated = false;
125+
http_response_code(401);
131126
}
132127
// $redirect_url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'],'?'));
133128
// header("Location: $redirect_url");
@@ -146,7 +141,6 @@ function define_env($name, $default) {
146141
if(isset($_REQUEST['mode']))
147142
$file_mode = $_REQUEST['mode'];
148143

149-
150144
# Discover the base URL of the application
151145
$baseurlapp = dirname($_SERVER['PHP_SELF']);
152146
if($baseurlapp == '/') $baseurlapp = '';
@@ -181,6 +175,11 @@ function define_env($name, $default) {
181175
$message .= "Demo mode - files are just read only\\n";
182176
}
183177

178+
# Published mode
179+
if($file_mode == "preview" or ($file_mode == "view" and !$authenticated and REQUIRE_AUTH)) {
180+
$file_mode = "published";
181+
$message .= 'Proceeding in published mode.\n';
182+
}
184183

185184
# Set file as read-only
186185
if($file_mode == "readonly") {
@@ -228,7 +227,7 @@ function define_env($name, $default) {
228227
}
229228

230229
# Upload a new file
231-
else if($file_mode == "upload" and !$file_readonly) {
230+
else if($file_mode == "upload" and !$file_readonly and REQUIRE_AUTH) {
232231
$uploadfile = $file_path . basename($_FILES['file']['name']);
233232

234233
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
@@ -244,7 +243,7 @@ function define_env($name, $default) {
244243
}
245244

246245
# Save template
247-
else if($file_mode == "template_save") {
246+
else if($file_mode == "template_save" and REQUIRE_AUTH) {
248247
$template = $_REQUEST['template'];
249248
$result = file_put_contents(TEMPLATE_PUBLISH, $template);
250249

@@ -341,15 +340,15 @@ function define_env($name, $default) {
341340
}
342341
}
343342

344-
function max_upload() {
345-
// Determines the maximum upload size allowed
346-
$max_upload = (int)(ini_get('upload_max_filesize'));
347-
$max_post = (int)(ini_get('post_max_size'));
348-
$memory_limit = (int)(ini_get('memory_limit'));
349-
$upload_mb = min($max_upload, $max_post, $memory_limit);
343+
// Determines the maximum upload size allowed
344+
$max_upload = min(
345+
(int)(ini_get('upload_max_filesize')),
346+
(int)(ini_get('post_max_size')),
347+
(int)(ini_get('memory_limit')));
350348

351-
return $upload_mb;
352-
}
349+
# Get template file
350+
$template_file = htmlspecialchars(file_get_contents(
351+
file_exists(TEMPLATE_PUBLISH) ? TEMPLATE_PUBLISH : TEMPLATE_EDIT));
353352

354353
// for unicode output: (http://stackoverflow.com/questions/713293)
355354
header('Content-Type: text/html; charset=utf-8');
@@ -360,19 +359,14 @@ function max_upload() {
360359
//header('Cache-Control: post-check=0, pre-check=0', FALSE);
361360
//header('Pragma: no-cache');
362361

363-
# Get template file
364-
$template_file = htmlspecialchars(file_get_contents(
365-
file_exists(TEMPLATE_PUBLISH) ? TEMPLATE_PUBLISH : TEMPLATE_EDIT));
366-
367362
# Preview with the template provided
368-
if(isset($_REQUEST['preview']) and isset($_REQUEST['template']))
369-
eval('?>'.$_REQUEST['template'].'<?php ');
363+
if($file_mode == "published" and isset($_REQUEST['template']) and REQUIRED_AUTH)
364+
eval('?>'.$_REQUEST['template'].'<?php ');
370365

371366
# Preview with the saved template
372-
else if(file_exists(TEMPLATE_PUBLISH) and (!$authenticated or (isset($_REQUEST['preview']) and !isset($_REQUEST['template']))))
367+
elseif($file_mode == "published" and file_exists(TEMPLATE_PUBLISH))
373368
include(TEMPLATE_PUBLISH);
374369

375-
# Use the edit template
370+
# Use the edit template
376371
else
377372
include(TEMPLATE_EDIT);
378-

template.php

+26-22
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<?php
22
/**
3-
GLOBAL VARIABLES
4-
================
5-
SITE_NAME - name of the website
6-
$base_file_name - name of the file, all revision files have the same base filename
7-
$file_name - filename of the current file
8-
$file_revisions - list of revisions to the file
9-
$file_readonly - flag indicating if the file is read only
10-
$file_mode - mode of the file, view or edit
11-
$file_contents - contents of the file
12-
$file_css - list of CSS snippets
13-
$list_files - list of attached files
14-
$url_files - URL to the files
15-
$baseurlapp - base URL for the application, i. e. URL without the filename
16-
$baseurl - URL with the filename
17-
$html - HTML of the published version of the file
18-
19-
*/
3+
* GLOBAL VARIABLES
4+
* ================
5+
* SITE_NAME - name of the website
6+
* $base_file_name - name of the file, all revision files have the same base filename
7+
* $file_name - filename of the current file
8+
* $file_revisions - list of revisions to the file
9+
* $file_readonly - flag indicating if the file is read only
10+
* $file_mode - mode of the file (view, edit or published)
11+
* $file_contents - contents of the file
12+
* $file_css - list of CSS snippets
13+
* $list_files - list of attached files
14+
* $url_files - URL to the files
15+
* $baseurlapp - base URL for the application, i. e. URL without the filename
16+
* $baseurl - URL with the filename
17+
* $html - HTML of the published version of the file
18+
* $max_upload - Maximum size for uploading data
19+
*/
2020
?><!DOCTYPE html>
2121
<html>
2222
<head>
@@ -269,7 +269,7 @@
269269
<ul class="dropdown-menu" role="menu">
270270
<li><a id="template_edit_button" href="#">Edit template</a></li>
271271
<li><a id="publish" href="#">Publish file</a></li>
272-
<li><a href="<?php echo $baseurl; ?>?preview" target="_blank">View published version</a></li>
272+
<li><a href="<?php echo $baseurl; ?>?mode=preview" target="_blank">View published version</a></li>
273273
<li class="divider"></li>
274274
<li><a id="download_html" href="#">Download as HTML</a></li>
275275
<li><a id="download_markdown" href="#">Download as Markdown</a></li>
@@ -327,7 +327,9 @@
327327
<div class="container">
328328
<div class="row">
329329
<!-- Markdown -->
330-
<div id="wmd-preview-editor" class="viewer col-md-<?php echo ($file_mode=="edit" ? "6" : "12"); ?>"></div>
330+
<div id="wmd-preview-editor" class="viewer col-md-<?php echo ($file_mode=="edit" ? "6" : "12"); ?>">
331+
<?php if($file_mode=="published") { echo $html; } ?>
332+
</div>
331333

332334
<!-- Editor -->
333335
<?php if ($file_mode=="edit") { ?>
@@ -398,7 +400,9 @@
398400
});
399401

400402
// Fills the viewer with HTML
401-
$("#wmd-preview-editor").html(converter.makeHtml(markdown));
403+
if(mode != 'published') {
404+
$("#wmd-preview-editor").html(converter.makeHtml(markdown));
405+
}
402406

403407
if(mode == 'edit') {
404408
// Sets textarea with the text
@@ -445,7 +449,7 @@
445449
});
446450

447451
$("#template_preview").click(function() {
448-
$.post("<?php echo $baseurl; ?>?preview", {
452+
$.post("<?php echo $baseurl; ?>?mode=preview", {
449453
template: templateEditor.getValue()
450454
}).done(function(data) {
451455
var previewWindow = window.open("", "spmdwe_preview");
@@ -477,7 +481,7 @@
477481

478482
var dropzone = new Dropzone(document.documentElement, { // Make the whole document a dropzone
479483
url: "<?php echo $baseurl; ?>", // Set the url
480-
maxFilesize: <?php echo max_upload(); ?>,
484+
maxFilesize: <?php echo $max_upload; ?>,
481485
thumbnailWidth: 80,
482486
thumbnailHeight: 80,
483487
parallelUploads: 30,

0 commit comments

Comments
 (0)