From d62b497b401a767c9a8db153726e0c7e1f2c474e Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Wed, 10 Feb 2016 18:36:55 -0800 Subject: [PATCH] add interface for posting travel itinerary --- controllers/controllers.php | 45 ++++++-- lib/helpers.php | 51 +++++---- public/js/script.js | 32 ++++++ views/new-itinerary.php | 209 ++++++++++++++++++++++++++++++++++++ 4 files changed, 306 insertions(+), 31 deletions(-) create mode 100644 public/js/script.js create mode 100644 views/new-itinerary.php diff --git a/controllers/controllers.php b/controllers/controllers.php index 70e84cf..0b63699 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -130,6 +130,18 @@ $app->get('/favorite', function() use($app) { } }); +$app->get('/itinerary', function() use($app) { + if($user=require_login($app)) { + $params = $app->request()->params(); + + $html = render('new-itinerary', array( + 'title' => 'Itinerary', + 'authorizing' => false + )); + $app->response()->body($html); + } +}); + $app->get('/photo', function() use($app) { if($user=require_login($app)) { $params = $app->request()->params(); @@ -217,7 +229,7 @@ $app->get('/add-to-home', function() use($app) { if($user=require_login($app)) { if(array_key_exists('start', $params)) { $_SESSION['add-to-home-started'] = true; - + $token = JWT::encode(array( 'user_id' => $_SESSION['user_id'], 'me' => $_SESSION['me'], @@ -260,7 +272,7 @@ $app->get('/email', function() use($app) { 'test_response' => $test_response, 'user' => $user )); - $app->response()->body($html); + $app->response()->body($html); } }); @@ -294,7 +306,7 @@ $app->get('/favorite-popup', function() use($app) { $params = $app->request()->params(); $html = $app->render('favorite-popup.php', array( - 'url' => $params['url'], + 'url' => $params['url'], 'token' => $params['token'] )); $app->response()->body($html); @@ -339,7 +351,7 @@ function create_favorite(&$user, $url) { if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) { $tweet_id = $match[1]; - $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret, + $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret, $user->twitter_access_token, $user->twitter_token_secret); $result = $twitter->post('favorites/create', array( 'id' => $tweet_id @@ -373,7 +385,7 @@ function create_repost(&$user, $url) { if($user->twitter_access_token && preg_match('/https?:\/\/(?:www\.)?twitter\.com\/[^\/]+\/status(?:es)?\/(\d+)/', $url, $match)) { $tweet_id = $match[1]; - $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret, + $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret, $user->twitter_access_token, $user->twitter_token_secret); $result = $twitter->post('statuses/retweet/'.$tweet_id); } @@ -390,8 +402,8 @@ $app->get('/favorite.js', function() use($app) { $r = create_favorite($user, $params['url']); $app->response()->body($app->render('favorite-js.php', array( - 'url' => $params['url'], - 'like_url' => $r['location'], + 'url' => $params['url'], + 'like_url' => $r['location'], 'error' => $r['error'], // 'facebook_id' => $facebook_id ))); @@ -464,6 +476,20 @@ $app->post('/repost', function() use($app) { } }); +$app->post('/itinerary', function() use($app) { + if($user=require_login($app)) { + $params = $app->request()->params(); + + $r = micropub_post_for_user($user, json_decode($params['data'], true), null, true); + + $app->response()->body(json_encode(array( + 'location' => $r['location'], + 'error' => $r['error'], + 'response' => $r['response'] + ))); + } +}); + $app->get('/micropub/syndications', function() use($app) { if($user=require_login($app)) { $data = get_syndication_targets($user); @@ -545,7 +571,7 @@ $app->get('/auth/twitter', function() use($app) { // If there is an existing Twitter token, check if it is valid // Otherwise, generate a Twitter login link $twitter_login_url = false; - $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret, + $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret, $user->twitter_access_token, $user->twitter_token_secret); if(array_key_exists('login', $params)) { @@ -582,7 +608,7 @@ $app->get('/auth/twitter/callback', function() use($app) { if($user=require_login($app)) { $params = $app->request()->params(); - $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret, + $twitter = new \TwitterOAuth\Api(Config::$twitterClientID, Config::$twitterClientSecret, $_SESSION['twitter_auth']['oauth_token'], $_SESSION['twitter_auth']['oauth_token_secret']); $credentials = $twitter->getAccessToken($params['oauth_verifier']); @@ -634,4 +660,3 @@ $app->get('/auth/instagram/callback', function() use($app) { $app->redirect('/settings'); } }); - diff --git a/lib/helpers.php b/lib/helpers.php index 8f435f0..a2796e1 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -75,23 +75,23 @@ function get_timezone($lat, $lng) { } if(!function_exists('http_build_url')) { - function http_build_url($parsed_url) { - $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; - $host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; - $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; - $user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; - $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : ''; - $pass = ($user || $pass) ? "$pass@" : ''; - $path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; - $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; - $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; - return "$scheme$user$pass$host$port$path$query$fragment"; - } + function http_build_url($parsed_url) { + $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; + $host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; + $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; + $user = isset($parsed_url['user']) ? $parsed_url['user'] : ''; + $pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : ''; + $pass = ($user || $pass) ? "$pass@" : ''; + $path = isset($parsed_url['path']) ? $parsed_url['path'] : ''; + $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; + $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; + return "$scheme$user$pass$host$port$path$query$fragment"; + } } -function micropub_post_for_user(&$user, $params, $file_path = NULL) { +function micropub_post_for_user(&$user, $params, $file_path = NULL, $json = false) { // Now send to the micropub endpoint - $r = micropub_post($user->micropub_endpoint, $params, $user->micropub_access_token, $file_path); + $r = micropub_post($user->micropub_endpoint, $params, $user->micropub_access_token, $file_path, $json); $user->last_micropub_response = substr(json_encode($r), 0, 1024); $user->last_micropub_response_date = date('Y-m-d H:i:s'); @@ -109,7 +109,7 @@ function micropub_post_for_user(&$user, $params, $file_path = NULL) { return $r; } -function micropub_post($endpoint, $params, $access_token, $file_path = NULL) { +function micropub_post($endpoint, $params, $access_token, $file_path = NULL, $json = false) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $endpoint); curl_setopt($ch, CURLOPT_POST, true); @@ -118,14 +118,23 @@ function micropub_post($endpoint, $params, $access_token, $file_path = NULL) { // https://github.com/aaronpk/Quill/issues/4 // http://indiewebcamp.com/irc/2015-02-14#t1423955287064 $httpheaders = array('Authorization: Bearer ' . $access_token); - $params = array_merge(array( - 'h' => 'entry', - 'access_token' => $access_token - ), $params); + + if(!$json) { + $params = array_merge(array( + 'h' => 'entry', + 'access_token' => $access_token + ), $params); + } if(!$file_path) { - $post = http_build_query($params); - $post = preg_replace('/%5B[0-9]+%5D/', '%5B%5D', $post); // change [0] to [] + if($json) { + $params['access_token'] = $access_token; + $httpheaders[] = 'Content-type: application/json'; + $post = json_encode($params); + } else { + $post = http_build_query($params); + $post = preg_replace('/%5B[0-9]+%5D/', '%5B%5D', $post); // change [0] to [] + } } else { $finfo = finfo_open(FILEINFO_MIME_TYPE); $mimetype = finfo_file($finfo, $file_path); diff --git a/public/js/script.js b/public/js/script.js new file mode 100644 index 0000000..399af05 --- /dev/null +++ b/public/js/script.js @@ -0,0 +1,32 @@ + + function tz_seconds_to_offset(seconds) { + var tz_offset = ''; + var hours = zero_pad(Math.abs(seconds / 60 / 60)); + var minutes = zero_pad(Math.floor(seconds / 60) % 60); + return (seconds < 0 ? '-' : '+') + hours + ":" + minutes; + } + function zero_pad(num) { + num = "" + num; + if(num.length == 1) { + num = "0" + num; + } + return num; + } + + +$(function(){ + + // Set the date from JS + var d = new Date(); + $("#note_date").val(d.getFullYear()+"-"+zero_pad(d.getMonth()+1)+"-"+zero_pad(d.getDate())); + $("#note_time").val(zero_pad(d.getHours())+":"+zero_pad(d.getMinutes())+":"+zero_pad(d.getSeconds())); + $("#note_tzoffset").val(tz_seconds_to_offset(d.getTimezoneOffset() * 60 * -1)); + + // ctrl-s to save + $(window).on('keydown', function(e){ + if(e.keyCode == 83 && e.ctrlKey){ + $("#btn_post").click(); + } + }); + +}) diff --git a/views/new-itinerary.php b/views/new-itinerary.php new file mode 100644 index 0000000..8d8811c --- /dev/null +++ b/views/new-itinerary.php @@ -0,0 +1,209 @@ +
+ + +
+ + +
+ +
+ +

Legs

+
+ +
+ + +
+ + +
+ +
+ +
+ +
+ +
+ + + + + + +