10
10
11
11
# Configuration
12
12
$ 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 "
14
14
15
15
define_env ('SITE_NAME ' , 'Spmdwe Editor ' ); # Website name
16
16
define_env ('SAVE_ENABLED ' , true ); # set to false to disable saving ("demo mode")
@@ -73,48 +73,42 @@ function define_env($name, $default) {
73
73
session_start ();
74
74
75
75
## 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 ;
89
84
}
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 ;
109
101
}
102
+
103
+ // Cleanup
104
+ $ user = $ _SERVER ['PHP_AUTH_USER ' ];
105
+ unset($ pass );
110
106
}
107
+
111
108
## 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
+ }
118
112
119
113
## Login
120
114
if ((isset ($ _GET ['login ' ]) and !$ authenticated )) {
@@ -128,6 +122,7 @@ function define_env($name, $default) {
128
122
if (isset ($ _GET ['logout ' ]) and $ authenticated ) {
129
123
unset($ _SESSION ['session_started ' ]);
130
124
$ authenticated = false ;
125
+ http_response_code (401 );
131
126
}
132
127
// $redirect_url = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].substr($_SERVER['REQUEST_URI'], 0, strrpos($_SERVER['REQUEST_URI'],'?'));
133
128
// header("Location: $redirect_url");
@@ -146,7 +141,6 @@ function define_env($name, $default) {
146
141
if (isset ($ _REQUEST ['mode ' ]))
147
142
$ file_mode = $ _REQUEST ['mode ' ];
148
143
149
-
150
144
# Discover the base URL of the application
151
145
$ baseurlapp = dirname ($ _SERVER ['PHP_SELF ' ]);
152
146
if ($ baseurlapp == '/ ' ) $ baseurlapp = '' ;
@@ -181,6 +175,11 @@ function define_env($name, $default) {
181
175
$ message .= "Demo mode - files are just read only \\n " ;
182
176
}
183
177
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
+ }
184
183
185
184
# Set file as read-only
186
185
if ($ file_mode == "readonly " ) {
@@ -228,7 +227,7 @@ function define_env($name, $default) {
228
227
}
229
228
230
229
# 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 ) {
232
231
$ uploadfile = $ file_path . basename ($ _FILES ['file ' ]['name ' ]);
233
232
234
233
if (move_uploaded_file ($ _FILES ['file ' ]['tmp_name ' ], $ uploadfile )) {
@@ -244,7 +243,7 @@ function define_env($name, $default) {
244
243
}
245
244
246
245
# Save template
247
- else if ($ file_mode == "template_save " ) {
246
+ else if ($ file_mode == "template_save " and REQUIRE_AUTH ) {
248
247
$ template = $ _REQUEST ['template ' ];
249
248
$ result = file_put_contents (TEMPLATE_PUBLISH , $ template );
250
249
@@ -341,15 +340,15 @@ function define_env($name, $default) {
341
340
}
342
341
}
343
342
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 ' )));
350
348
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 ));
353
352
354
353
// for unicode output: (http://stackoverflow.com/questions/713293)
355
354
header ('Content-Type: text/html; charset=utf-8 ' );
@@ -360,19 +359,14 @@ function max_upload() {
360
359
//header('Cache-Control: post-check=0, pre-check=0', FALSE);
361
360
//header('Pragma: no-cache');
362
361
363
- # Get template file
364
- $ template_file = htmlspecialchars (file_get_contents (
365
- file_exists (TEMPLATE_PUBLISH ) ? TEMPLATE_PUBLISH : TEMPLATE_EDIT ));
366
-
367
362
# 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 ' );
370
365
371
366
# 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 ))
373
368
include (TEMPLATE_PUBLISH );
374
369
375
- # Use the edit template
370
+ # Use the edit template
376
371
else
377
372
include (TEMPLATE_EDIT );
378
-
0 commit comments