Skip to content

Commit

Permalink
Add plain text support. Closes remy#43
Browse files Browse the repository at this point in the history
  • Loading branch information
remy committed Oct 19, 2011
1 parent 558af5d commit 1dc22eb
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -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.

Expand Down
37 changes: 34 additions & 3 deletions index.php
Expand Up @@ -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
Expand All @@ -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);
}
Expand All @@ -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;

?>

0 comments on commit 1dc22eb

Please sign in to comment.