diff --git a/controllers/auth.php b/controllers/auth.php index d9a6d0e..6fd78e4 100644 --- a/controllers/auth.php +++ b/controllers/auth.php @@ -57,6 +57,10 @@ function normalizeMeURL($url) { return build_url($me); } +function hostname($url) { + return parse_url($url, PHP_URL_HOST); +} + $app->get('/', function($format='html') use($app) { $res = $app->response(); @@ -106,7 +110,7 @@ $app->get('/auth/start', function() use($app) { // If the user has already signed in before and has a micropub access token, skip // the debugging screens and redirect immediately to the auth endpoint. // This will still generate a new access token when they finish logging in. - $user = ORM::for_table('users')->where('url', $me)->find_one(); + $user = ORM::for_table('users')->where('url', hostname($me))->find_one(); if($user && $user->access_token && !array_key_exists('restart', $params)) { $user->micropub_endpoint = $micropubEndpoint; @@ -121,7 +125,7 @@ $app->get('/auth/start', function() use($app) { if(!$user) $user = ORM::for_table('users')->create(); - $user->url = $me; + $user->url = hostname($me); $user->date_created = date('Y-m-d H:i:s'); $user->micropub_endpoint = $micropubEndpoint; $user->authorization_endpoint = $authorizationEndpoint; @@ -260,7 +264,7 @@ $app->get('/auth/callback', function() use($app) { } - $user = ORM::for_table('users')->where('url', $me)->find_one(); + $user = ORM::for_table('users')->where('url', hostname($me))->find_one(); if($user) { // Already logged in, update the last login date $user->last_login = date('Y-m-d H:i:s'); @@ -270,7 +274,7 @@ $app->get('/auth/callback', function() use($app) { } else { // New user! Store the user in the database $user = ORM::for_table('users')->create(); - $user->url = $me; + $user->url = hostname($me); $user->date_created = date('Y-m-d H:i:s'); $user->last_login = date('Y-m-d H:i:s'); } diff --git a/controllers/controllers.php b/controllers/controllers.php index 409d15e..57a4e51 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -128,30 +128,63 @@ $app->post('/post', function() use($app) { }); print_r($params); - - - // Now send to the micropub endpoint - // $r = micropub_post($user->micropub_endpoint, $params, $user->micropub_access_token); - // $request = $r['request']; - // $response = $r['response']; - - // Check the response and look for a "Location" header containing the URL - // if($response && preg_match('/Location: (.+)/', $response, $match)) { - // $location = $match[1]; - // $user->micropub_success = 1; - // } else { - // $location = false; - // } - - // $user->save(); - - $app->response()->body(json_encode(array( - // 'request' => htmlspecialchars($request), - // 'response' => htmlspecialchars($response), - // 'location' => $location, - // 'error' => $r['error'], - // 'curlinfo' => $r['curlinfo'] - ))); + + // Store the post in the database + $entry = ORM::for_table('entries')->create(); + $entry->user_id = $user->id; + $entry->published = date('Y-m-d H:i:s'); + + if(k($params, 'location') && $location=parse_geo_uri($params['location'])) { + $entry->latitude = $location['latitude']; + $entry->longitude = $location['longitude']; + if($timezone=get_timezone($location['latitude'], $location['longitude'])) { + $entry->timezone = $timezone->getName(); + $entry->tz_offset = $timezone->getOffset(new DateTime()); + } + } else { + $entry->timezone = 'UTC'; + $entry->tz_offset = 0; + } + + if(k($params, 'drank')) { + $entry->content = $params['drank']; + } elseif(k($params, 'custom_caffeine')) { + $entry->content = $params['custom_caffeine']; + } elseif(k($params, 'custom_alcohol')) { + $entry->content = $params['custom_alcohol']; + } + + $entry->save(); + + // Send to the micropub endpoint if one is defined, and store the result + + if($user->micropub_endpoint) { + $mp_request = array( + 'h' => 'entry', + 'content' => $entry->content, + 'location' => k($params, 'location') + ); + + $r = micropub_post($user->micropub_endpoint, $mp_request, $user->access_token); + $request = $r['request']; + $response = $r['response']; + + $entry->micropub_response = $response; + + // Check the response and look for a "Location" header containing the URL + if($response && preg_match('/Location: (.+)/', $response, $match)) { + $url = $match[1]; + $user->micropub_success = 1; + $entry->micropub_success = 1; + $entry->canonical_url = $url; + } + + $entry->save(); + } else { + $url = Config::$base_url . $user->url . '/' . $entry->id; + } + + $app->redirect($url); } }); diff --git a/lib/helpers.php b/lib/helpers.php index 329eba8..dcc6b33 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -55,6 +55,17 @@ function k($a, $k, $default=null) { } } +function parse_geo_uri($uri) { + if(preg_match('/geo:([\-\+]?[0-9\.]+),([\-\+]?[0-9\.]+)/', $uri, $match)) { + return array( + 'latitude' => (double)$match[1], + 'longitude' => (double)$match[2], + ); + } else { + return false; + } +} + function get_timezone($lat, $lng) { try { $ch = curl_init(); diff --git a/views/new-post.php b/views/new-post.php index 7ed6bf8..1484986 100644 --- a/views/new-post.php +++ b/views/new-post.php @@ -45,13 +45,8 @@ micropub_endpoint): ?> - test_response): ?> -

Last response from your Micropub endpoint (response_date) ?>)

- -
test_response) ?>
-
-

Clicking "Post" will post this note to your Micropub endpoint. Below is some information about the request that will be made.

+

Clicking an item will post this note to your Micropub endpoint. Below is some information about the request that will be made.

@@ -80,57 +75,6 @@