From c5ecbbee05a9f63388c799d9d0ffaba6cb7260fc Mon Sep 17 00:00:00 2001 From: Remy Sharp Date: Thu, 20 Oct 2011 12:13:08 +0100 Subject: [PATCH] support curl based user creation - let users do it entirely themselves --- index.php | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/index.php b/index.php index 57d27f4f..c0440413 100644 --- a/index.php +++ b/index.php @@ -5,12 +5,34 @@ $format = 'html'; $theme = 'default'; $user_file = preg_replace('/\.mit-license\..*$/', '', $_SERVER["HTTP_HOST"]); - // sanitise user (not for DNS, but for file reading, I don't know // just in case it's hacked about with or something bananas. $user_file = preg_replace('/[^a-z0-9\-]/', '', $user_file); $user_file = 'users/'.$user_file.'.json'; +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + try { + $data = json_decode(file_get_contents('php://input')); + if (!property_exists($data, 'copyright')) { + Throw new Exception('>>> JSON requires "copyright" property and value'); + } + + if (file_exists($user_file)) { + Throw new Exception(wordwrap('>>> User already exists - to update values, please send a pull request on https://github.com/remy/mit-license')); + } + + if (!file_put_contents($user_file, json_encode($data))) { + Throw new Exception(wordwrap('>>> Unable to create new user - please send a pull request on https://github.com/remy/mit-license')); + } + + echo '>>> MIT license page created: http://' . $_SERVER['HTTP_HOST'] . "\n\n"; + + } catch (Exception $e) { + echo $e->getMessage() . "\n\n"; + } + exit; +} + if (file_exists($user_file)) { $user = json_decode(file_get_contents($user_file)); $holder = htmlentities($user->copyright, ENT_COMPAT | ENT_HTML5, 'UTF-8');