From cf8ecf1fc9942dc00f0a5a11b313f8babbc9c3cd Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sun, 7 Sep 2014 11:48:52 -0700 Subject: adds bookmark posting interface with bookmarklet. now any URL can auto-login given a login token. --- controllers/controllers.php | 57 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) (limited to 'controllers') diff --git a/controllers/controllers.php b/controllers/controllers.php index dc18584..0de366b 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -1,6 +1,21 @@ request()->params(); + if(array_key_exists('token', $params)) { + try { + $data = JWT::decode($params['token'], Config::$jwtSecret); + $_SESSION['user_id'] = $data->user_id; + $_SESSION['me'] = $data->me; + } catch(DomainException $e) { + header('X-Error: DomainException'); + $app->redirect('/', 301); + } catch(UnexpectedValueException $e) { + header('X-Error: UnexpectedValueException'); + $app->redirect('/', 301); + } + } + if(!array_key_exists('user_id', $_SESSION)) { $app->redirect('/'); return false; @@ -9,6 +24,14 @@ function require_login(&$app) { } } +function generate_login_token() { + return JWT::encode(array( + 'user_id' => $_SESSION['user_id'], + 'me' => $_SESSION['me'], + 'created_at' => time() + ), Config::$jwtSecret); +} + $app->get('/new', function() use($app) { if($user=require_login($app)) { @@ -26,7 +49,7 @@ $app->get('/new', function() use($app) { } } - $html = render('dashboard', array( + $html = render('new-post', array( 'title' => 'New Post', 'micropub_endpoint' => $user->micropub_endpoint, 'micropub_scope' => $user->micropub_scope, @@ -40,6 +63,38 @@ $app->get('/new', function() use($app) { } }); + +$app->get('/bookmark', function() use($app) { + if($user=require_login($app)) { + $params = $app->request()->params(); + + $url = ''; + $name = ''; + $content = ''; + $tags = ''; + + if(array_key_exists('url', $params)) + $url = $params['url']; + + if(array_key_exists('name', $params)) + $name = $params['name']; + + if(array_key_exists('content', $params)) + $content = $params['content']; + + $html = render('new-bookmark', array( + 'title' => 'New Bookmark', + 'bookmark_url' => $url, + 'bookmark_name' => $name, + 'bookmark_content' => $content, + 'bookmark_tags' => $tags, + 'token' => generate_login_token(), + 'syndication_targets' => json_decode($user->syndication_targets, true) + )); + $app->response()->body($html); + } +}); + $app->post('/prefs', function() use($app) { if($user=require_login($app)) { $params = $app->request()->params(); -- cgit v1.2.3