Skip to content

Instantly share code, notes, and snippets.

@holmesconan
Created August 19, 2018 12:29
Show Gist options
  • Select an option

  • Save holmesconan/614b93a8143abdcad46216c821a7290e to your computer and use it in GitHub Desktop.

Select an option

Save holmesconan/614b93a8143abdcad46216c821a7290e to your computer and use it in GitHub Desktop.
TiddlyWiki 5 PHP Saver
<?php
$uri = $_SERVER['REQUEST_URI'];
$ext = pathinfo($uri, PATHINFO_EXTENSION);
$full_path = $_SERVER['DOCUMENT_ROOT'] . $uri . (empty($ext) ? '.html' : '');
/**
* if (@strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time ||
* trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) {
* header("HTTP/1.1 304 Not Modified");
* exit;
*/
switch (strtoupper($_SERVER['REQUEST_METHOD'])) {
case 'OPTIONS':
header('DAV: 1, 2');
break;
case 'HEAD':
if (file_exists($full_path)) {
$last_modified_time = filemtime($full_path);
$etag = md5_file($full_path);
header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_time)." GMT");
header("ETag: $etag");
} else {
header('404 Not Found');
}
break;
case 'PUT':
$inputStream = fopen('php://input', 'r');
$outputStream = fopen($full_path, 'w');
stream_copy_to_stream($inputStream, $outputStream);
fclose($inputStream);
fclose($outputStream);
break;
case 'GET':
default:
if (preg_match('/\.(?:png|jpg|jpeg|gif|html)$/', $uri)) {
return false; // serve the requested resource as-is.
} else {
if (file_exists($full_path)) {
readfile($full_path);
} else {
header('404 Not Found');
echo $full_path . ' is not found.';
}
}
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment