Add plain text support. Closes #43

This commit is contained in:
Remy Sharp 2011-10-19 14:19:37 +01:00
parent 558af5d4f3
commit 1dc22ebd9b
2 changed files with 43 additions and 3 deletions

View file

@ -36,6 +36,15 @@ the copyright text, you can include a `url` property:
"url": "http://remysharp.com"
}
And if you want your license to appear as plain text, just add the
`format` property (currently only `txt` and `html` are supported):
{
"copyright": "Remy Sharp, http://remysharp.com",
"url": "http://remysharp.com",
"format": "txt"
}
Finally you can also include a license version target in the JSON file
as explained in the next section.

View file

@ -2,6 +2,7 @@
date_default_timezone_set('Europe/London'); // stop php from whining
$format = 'html';
$user_file = preg_replace('/\.mit-license\..*$/', '', $_SERVER["HTTP_HOST"]);
// sanitise user (not for DNS, but for file reading, I don't know
@ -15,15 +16,35 @@
if (property_exists($user, 'url')) {
$holder = '<a href="'.$user->url.'">' . $holder . '</a>';
}
if (property_exists($user, 'format')) {
if (strtolower($user->format) == 'txt') {
$format = 'txt';
}
}
} else {
$holder = "&lt;copyright holders&gt;";
}
// grab sha from request uri
$request = $_SERVER["REQUEST_URI"];
$request_uri = explode('/', $_SERVER["REQUEST_URI"]);
$request = array_pop($request_uri);
// in case there's a trailing slash (unlikely)
if ($request == '') $request = array_pop($request_uri);
// url file format overrides user preference
if (stripos($request, 'license') === 0) {
$format = array_pop(explode('.', strtolower($request))) == 'txt' ? 'txt' : 'html';
// move down to the next part of the request
$request = array_pop($request_uri);
}
// check if there's a SHA on the url and read this to switch license versions
$sha = '';
if ($request != "" && $request != "/" && $request != "/index.php") {
$sha = preg_replace('/[^a-f0-9]/', '', $_SERVER["REQUEST_URI"]);
$sha = preg_replace('/[^a-f0-9]/', '', $request);
} else if (isset($user) && property_exists($user, 'version')) {
$sha = preg_replace('/[^a-f0-9]/', '', $user->version);
}
@ -46,6 +67,16 @@
// replace info tag and display
$info = date('Y') . ' ' . $holder;
echo str_replace('{{info}}', $info, $license);
$license = str_replace('{{info}}', $info, $license);
// if we want text format, strip out the license from the article tag
// and then strip any other tags in the license.
if ($format == 'txt') {
$license = array_shift(explode('</article>', array_pop(explode('<article>', $license))));
$license = preg_replace('/<[^>]*>/', '', trim($license));
header('content-type: plain/text');
}
echo $license;
?>