diff --git a/README.md b/README.md index d76110a606..a154653f25 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.php b/index.php index 1c9c4fe955..1f2513b61c 100644 --- a/index.php +++ b/index.php @@ -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 = '' . $holder . ''; } + + if (property_exists($user, 'format')) { + if (strtolower($user->format) == 'txt') { + $format = 'txt'; + } + } } else { $holder = "<copyright holders>"; } // 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('', array_pop(explode('
', $license)))); + $license = preg_replace('/<[^>]*>/', '', trim($license)); + header('content-type: plain/text'); +} + +echo $license; ?>