9
9
# Source: http://people.w3.org/~dom/archives/2004/07/testing-php-pages-with-query_string/
10
10
11
11
# Configuration
12
- $ file_name = get_env ("HOMEPAGE " ,"home " ); # file by default
12
+ $ file_name = define_env ("HOMEPAGE " ,"home " ); # file by default
13
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
28
28
29
29
# Application
30
30
31
- function get_env ($ name , $ default ) {
32
- return isset ($ _ENV [$ name ]) ? $ _ENV [$ name ] : $ default ;
33
- }
34
31
function define_env ($ name , $ default ) {
35
32
$ value = isset ($ _ENV [$ name ]) ? $ _ENV [$ name ] : $ default ;
36
33
define ($ name , $ value );
34
+ return $ value ;
37
35
}
38
36
39
37
# Ensure if the enviroment is correct
@@ -168,7 +166,10 @@ function define_env($name, $default) {
168
166
169
167
# Application in Demo mode (read-only)
170
168
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. " );
172
173
173
174
$ file_mode = "view " ;
174
175
$ file_readonly = true ;
@@ -267,8 +268,8 @@ function define_env($name, $default) {
267
268
268
269
# Download ZIP file with all files of the page
269
270
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 \"" );
272
273
273
274
$ stream = popen ("zip -qj - files/ $ file_name/* " , "r " );
274
275
if ($ stream ) {
@@ -279,42 +280,55 @@ function define_env($name, $default) {
279
280
}
280
281
281
282
283
+ if ($ authenticated ) {
284
+ # Gets the file contents
285
+ $ message .= "Opening $ file_name in $ file_mode mode \\n " ;
282
286
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 );
290
291
291
292
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
+ }
310
312
}
311
313
312
-
313
314
# 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="/">← Back to home</a>
328
+ </div>
329
+ TEMPLATE404 ;
330
+ }
331
+ }
318
332
319
333
# Base path for history files
320
334
$ file_revisions_path = $ file_path .$ base_file_name .REVISION_MARKER .'????????_??????.md ' ;
@@ -323,7 +337,7 @@ function define_env($name, $default) {
323
337
# List of revision files
324
338
$ count = 0 ;
325
339
$ file_revisions = array ();
326
- foreach (glob ($ file_revisions_path ) as $ file_revision ) {
340
+ foreach (glob ($ file_revisions_path ) as $ file_revision ) {
327
341
$ file_revisions [$ count ] = substr ($ file_revision , strlen ($ file_path ), -strlen ('.md ' ));
328
342
$ count ++;
329
343
}
@@ -360,7 +374,7 @@ function define_env($name, $default) {
360
374
//header('Pragma: no-cache');
361
375
362
376
# 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 )
364
378
eval ('?> ' .$ _REQUEST ['template ' ].'<?php ' );
365
379
366
380
# Preview with the saved template
0 commit comments