diff options
author | Aaron Parecki <aaron@parecki.com> | 2014-09-07 11:48:52 -0700 |
---|---|---|
committer | Aaron Parecki <aaron@parecki.com> | 2014-09-07 11:48:52 -0700 |
commit | cf8ecf1fc9942dc00f0a5a11b313f8babbc9c3cd (patch) | |
tree | af6a3e99b19383ea5642f530451ff450072a9345 | |
parent | 9cfa0ff2c18e25a9c00d80624e6abea487bd9136 (diff) |
adds bookmark posting interface with bookmarklet. now any URL can auto-login given a login token.
-rw-r--r-- | controllers/controllers.php | 57 | ||||
-rw-r--r-- | lib/helpers.php | 4 | ||||
-rw-r--r-- | public/css/style.css | 81 | ||||
-rw-r--r-- | views/layout.php | 1 | ||||
-rw-r--r-- | views/new-bookmark.php | 106 | ||||
-rw-r--r-- | views/new-post.php (renamed from views/dashboard.php) | 100 | ||||
-rw-r--r-- | views/partials/bookmark-bookmarklet.php | 5 | ||||
-rw-r--r-- | views/partials/syndication-js.php | 24 |
8 files changed, 278 insertions, 100 deletions
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 @@ <?php function require_login(&$app) { + $params = $app->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(); diff --git a/lib/helpers.php b/lib/helpers.php index 60eda75..cf751c6 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -27,6 +27,10 @@ function partial($template, $data=array(), $debug=false) { return ob_get_clean(); } +function js_bookmarklet($partial, $context) { + return str_replace('+','%20',urlencode(str_replace(array("\n"),array(''),partial($partial, $context)))); +} + function session($key) { if(array_key_exists($key, $_SESSION)) return $_SESSION[$key]; diff --git a/public/css/style.css b/public/css/style.css index 7872e7e..7cb152c 100644 --- a/public/css/style.css +++ b/public/css/style.css @@ -109,3 +109,84 @@ body { word-break: break-all; word-wrap: break-word; } + + + + + /* new posts */ + + #syndication-container ul { + list-style-type: none; + margin: 0; + padding: 10px; + } + #syndication-container li { + padding: 0; + margin-bottom: 6px; + } + #syndication-container button { + max-width: 240px; + text-shadow: none; + } + #syndication-container button img { + float: left; + margin-left: 10px; + } + + #last_response_date { + font-size: 80%; + font-weight: normal; + } + + #btn_post { + margin-bottom: 10px; + } + + @media all and (max-width: 480px) { + #note_location_img_wide { + display: none; + } + #note_location_img_small { + display: block; + } + } + @media all and (min-width: 480px) { + #note_location_img_wide { + display: block; + } + #note_location_img_small { + display: none; + } + } + + .img-visible { + -webkit-border-bottom-right-radius: 0; + -webkit-border-bottom-left-radius: 0; + -moz-border-radius-bottomright: 0; + -moz-border-radius-bottomleft: 0; + border-bottom-right-radius: 0; + border-bottom-left-radius: 0; + } + + #note_location_img img { + margin-top: -1px; + border: 1px solid #ccc; + -webkit-border-bottom-right-radius: 4px; + -webkit-border-bottom-left-radius: 4px; + -moz-border-radius-bottomright: 4px; + -moz-border-radius-bottomleft: 4px; + border-bottom-right-radius: 4px; + border-bottom-left-radius: 4px; + } + + .callout { + border-left: 4px #5bc0de solid; + background-color: #f4f8fa; + padding: 20px; + margin-top: 10px; + } + .callout table { + margin-bottom: 0; + } + + diff --git a/views/layout.php b/views/layout.php index 3d181e4..531dec9 100644 --- a/views/layout.php +++ b/views/layout.php @@ -57,6 +57,7 @@ <? if(session('me')) { ?> <li><a href="/new">New Post</a></li> + <li><a href="/bookmark">Bookmark</a></li> <? } ?> <li><a href="/docs">Docs</a></li> diff --git a/views/new-bookmark.php b/views/new-bookmark.php new file mode 100644 index 0000000..5738036 --- /dev/null +++ b/views/new-bookmark.php @@ -0,0 +1,106 @@ + <div class="narrow"> + <?= partial('partials/header') ?> + + <div style="float: right; margin-top: 6px;"> + <button class="btn btn-success" id="btn_post">Save Bookmark</button> + </div> + + <div style="clear: both;"> + <div class="alert alert-success hidden" id="test_success"><strong>Success! We found a Location header in the response!</strong><br>Your post should be on your website now!<br><a href="" id="post_href">View your post</a></div> + <div class="alert alert-danger hidden" id="test_error"><strong>Your endpoint did not return a Location header.</strong><br>See <a href="/creating-a-micropub-endpoint">Creating a Micropub Endpoint</a> for more information.</div> + </div> + + <form role="form" style="margin-top: 20px;" id="note_form"> + + <div class="form-group"> + <label for="note_bookmark"><code>bookmark</code></label> + <input type="text" id="note_bookmark" value="<?= $this->bookmark_url ?>" class="form-control"> + </div> + + <div class="form-group"> + <label for="note_name"><code>name</code></label> + <input type="text" id="note_name" value="<?= $this->bookmark_name ?>" class="form-control"> + </div> + + <div class="form-group"> + <label for="note_content"><code>content</code> (optional)</label> + <textarea id="note_content" value="" class="form-control" style="height: 5em;"><?= $this->bookmark_content ?></textarea> + </div> + + <div class="form-group"> + <label for="note_category"><code>category</code> (optional, comma-separated list of tags)</label> + <input type="text" id="note_category" value="<?= $this->bookmark_tags ?>" class="form-control" placeholder="e.g. web, personal"> + </div> + + <div class="form-group"> + <label for="note_syndicate-to"><code>syndicate-to</code> <a href="javascript:reload_syndications()">(refresh)</a></label> + <div id="syndication-container"> + <?php + if($this->syndication_targets) { + echo '<ul>'; + foreach($this->syndication_targets as $syn) { + echo '<li><button data-syndication="'.$syn['target'].'" class="btn btn-default btn-block"><img src="'.$syn['favicon'].'" width="16" height="16"> '.$syn['target'].'</button></li>'; + } + echo '</ul>'; + } else { + ?><div class="bs-callout bs-callout-warning">No syndication targets were found on your site. + You can provide a <a href="/docs#syndication">list of supported syndication targets</a> that will appear as checkboxes here.</div><?php + } + ?> + </div> + </div> + </form> + + + <hr> + <div style="text-align: right;"> + Bookmarklet: <a href="javascript:<?= js_bookmarklet('partials/bookmark-bookmarklet', $this) ?>" class="btn btn-default btn-xs">bookmark</a> + </div> + + </div> + +<script> +$(function(){ + + $("#btn_post").click(function(){ + + var syndications = []; + $("#syndication-container button.btn-info").each(function(i,btn){ + syndications.push($(btn).data('syndication')); + }); + + $.post("/micropub/post", { + bookmark: $("#note_bookmark").val(), + name: $("#note_name").val(), + content: $("#note_content").val(), + category: $("#note_category").val(), + 'syndicate-to': syndications.join(',') + }, function(data){ + var response = JSON.parse(data); + + if(response.location != false) { + + // $("#test_success").removeClass('hidden'); + $("#test_error").addClass('hidden'); + // $("#post_href").attr("href", response.location); + + // $("#note_bookmark").val(""); + // $("#note_content").val(""); + // $("#note_category").val(""); + + window.location = response.location; + } else { + $("#test_success").addClass('hidden'); + $("#test_error").removeClass('hidden'); + } + + }); + return false; + }); + + bind_syndication_buttons(); +}); + +<?= partial('partials/syndication-js') ?> + +</script> diff --git a/views/dashboard.php b/views/new-post.php index d88a59c..5221dc8 100644 --- a/views/dashboard.php +++ b/views/new-post.php @@ -212,106 +212,8 @@ $(function(){ bind_syndication_buttons(); }); -function reload_syndications() { - $.getJSON("/micropub/syndications", function(data){ - if(data.targets) { - $("#syndication-container").html('<ul></ul>'); - for(var i in data.targets) { - var target = data.targets[i].target; - var favicon = data.targets[i].favicon; - $("#syndication-container ul").append('<li><button data-syndication="'+target+'" class="btn btn-default btn-block"><img src="'+favicon+'" width="16" height="16"> '+target+'</button></li>'); - } - bind_syndication_buttons(); - } else { - - } - console.log(data); - }); -} - -function bind_syndication_buttons() { - $("#syndication-container button").unbind("click").click(function(){ - $(this).toggleClass('btn-info'); - return false; - }); -} +<?= partial('partials/syndication-js') ?> </script> -<style type="text/css"> - - #syndication-container ul { - list-style-type: none; - margin: 0; - padding: 10px; - } - #syndication-container li { - padding: 0; - margin-bottom: 6px; - } - #syndication-container button { - max-width: 240px; - text-shadow: none; - } - #syndication-container button img { - float: left; - margin-left: 10px; - } - - #last_response_date { - font-size: 80%; - font-weight: normal; - } - - #btn_post { - margin-bottom: 10px; - } - - @media all and (max-width: 480px) { - #note_location_img_wide { - display: none; - } - #note_location_img_small { - display: block; - } - } - @media all and (min-width: 480px) { - #note_location_img_wide { - display: block; - } - #note_location_img_small { - display: none; - } - } - - .img-visible { - -webkit-border-bottom-right-radius: 0; - -webkit-border-bottom-left-radius: 0; - -moz-border-radius-bottomright: 0; - -moz-border-radius-bottomleft: 0; - border-bottom-right-radius: 0; - border-bottom-left-radius: 0; - } - - #note_location_img img { - margin-top: -1px; - border: 1px solid #ccc; - -webkit-border-bottom-right-radius: 4px; - -webkit-border-bottom-left-radius: 4px; - -moz-border-radius-bottomright: 4px; - -moz-border-radius-bottomleft: 4px; - border-bottom-right-radius: 4px; - border-bottom-left-radius: 4px; - } - - .callout { - border-left: 4px #5bc0de solid; - background-color: #f4f8fa; - padding: 20px; - margin-top: 10px; - } - .callout table { - margin-bottom: 0; - } -</style> diff --git a/views/partials/bookmark-bookmarklet.php b/views/partials/bookmark-bookmarklet.php new file mode 100644 index 0000000..a4b095d --- /dev/null +++ b/views/partials/bookmark-bookmarklet.php @@ -0,0 +1,5 @@ +javascript:(function(){ + var t;try{t=((window.getSelection&&window.getSelection())||(document.getSelection&&document.getSelection())||(document.selection&&document.selection.createRange&&document.selection.createRange().text));}catch(e){t="";}; + window.location="http://quill.dev/bookmark?url="+encodeURIComponent(window.location.href)+"&content="+encodeURIComponent('"'+t+'"')+"&name="+encodeURIComponent(document.title)+"&token=<?= $this->token ?>"; +})(); + diff --git a/views/partials/syndication-js.php b/views/partials/syndication-js.php new file mode 100644 index 0000000..bd657f3 --- /dev/null +++ b/views/partials/syndication-js.php @@ -0,0 +1,24 @@ + +function reload_syndications() { + $.getJSON("/micropub/syndications", function(data){ + if(data.targets) { + $("#syndication-container").html('<ul></ul>'); + for(var i in data.targets) { + var target = data.targets[i].target; + var favicon = data.targets[i].favicon; + $("#syndication-container ul").append('<li><button data-syndication="'+target+'" class="btn btn-default btn-block"><img src="'+favicon+'" width="16" height="16"> '+target+'</button></li>'); + } + bind_syndication_buttons(); + } else { + + } + console.log(data); + }); +} + +function bind_syndication_buttons() { + $("#syndication-container button").unbind("click").click(function(){ + $(this).toggleClass('btn-info'); + return false; + }); +} |