Browse Source

Use only the hostname to identify users.

Store posts in the database.
Send the micropub request and log the response.
pull/10/head
Aaron Parecki 9 years ago
parent
commit
33a058555b
4 changed files with 77 additions and 85 deletions
  1. +8
    -4
      controllers/auth.php
  2. +57
    -24
      controllers/controllers.php
  3. +11
    -0
      lib/helpers.php
  4. +1
    -57
      views/new-post.php

+ 8
- 4
controllers/auth.php View File

@ -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');
}

+ 57
- 24
controllers/controllers.php View File

@ -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);
}
});

+ 11
- 0
lib/helpers.php View File

@ -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();

+ 1
- 57
views/new-post.php View File

@ -45,13 +45,8 @@
<?php if($this->micropub_endpoint): ?>
<?php if($this->test_response): ?>
<h4>Last response from your Micropub endpoint <span id="last_response_date">(<?= relative_time($this->response_date) ?>)</span></h4>
<?php endif; ?>
<pre id="test_response" style="width: 100%; min-height: 240px;"><?= htmlspecialchars($this->test_response) ?></pre>
<div class="callout">
<p>Clicking "Post" will post this note to your Micropub endpoint. Below is some information about the request that will be made.</p>
<p>Clicking an item will post this note to your Micropub endpoint. Below is some information about the request that will be made.</p>
<table class="table table-condensed">
<tr>
@ -80,57 +75,6 @@
<script>
$(function(){
// ctrl-s to save
$(window).on('keydown', function(e){
if(e.keyCode == 83 && e.ctrlKey){
$("#btn_post").click();
}
});
$("#btn_post").click(function(){
var syndications = [];
$("#syndication-container button.btn-info").each(function(i,btn){
syndications.push($(btn).data('syndication'));
});
$.post("/micropub/post", {
content: $("#note_content").val(),
'in-reply-to': $("#note_in_reply_to").val(),
location: $("#note_location").val(),
category: $("#note_category").val(),
slug: $("#note_slug").val(),
'syndicate-to': syndications.join(',')
}, function(data){
var response = JSON.parse(data);
if(response.location != false) {
$("#note_form").slideUp(200, function(){
$(window).scrollTop($("#test_success").position().top);
});
$("#test_success").removeClass('hidden');
$("#test_error").addClass('hidden');
$("#post_href").attr("href", response.location);
$("#note_content").val("");
$("#note_in_reply_to").val("");
$("#note_category").val("");
$("#note_slug").val("");
} else {
$("#test_success").addClass('hidden');
$("#test_error").removeClass('hidden');
}
$("#last_response_date").html("(just now)");
$("#test_request").html(response.request);
$("#last_request_container").show();
$("#test_response").html(response.response);
});
return false;
});
function location_error(msg) {
$("#note_location_msg").val(msg);
$("#note_location_chk").removeAttr("checked");

Loading…
Cancel
Save