From bce43daf2816705239c63cdc1f22d4d80d9d168a Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sun, 5 Oct 2014 23:39:19 -0700 Subject: [PATCH] parse home page h-card for the user's name and profile photo --- composer.lock | 51 +++++++++++++++++++++++++++++++++++++---- controllers/auth.php | 16 +++++++++++++ lib/config.template.php | 2 ++ views/layout.php | 8 ++++--- 4 files changed, 69 insertions(+), 8 deletions(-) diff --git a/composer.lock b/composer.lock index 56fd90b..fa030ad 100644 --- a/composer.lock +++ b/composer.lock @@ -5,6 +5,46 @@ ], "hash": "da493f8238b017e74269bc3cdeba5e92", "packages": [ + { + "name": "barnabywalters/mf-cleaner", + "version": "v0.1.3", + "source": { + "type": "git", + "url": "https://github.com/barnabywalters/php-mf-cleaner.git", + "reference": "4ecbfc5a2893a5140b07afafb0bb00619e853809" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/barnabywalters/php-mf-cleaner/zipball/4ecbfc5a2893a5140b07afafb0bb00619e853809", + "reference": "4ecbfc5a2893a5140b07afafb0bb00619e853809", + "shasum": "" + }, + "require-dev": { + "php": ">=5.3", + "phpunit/phpunit": "*" + }, + "suggest": { + "mf2/mf2": "To parse microformats2 structures from (X)HTML" + }, + "type": "library", + "autoload": { + "files": [ + "src/BarnabyWalters/Mf2/Functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Barnaby Walters", + "email": "barnaby@waterpigs.co.uk" + } + ], + "description": "Cleans up microformats2 array structures", + "time": "2014-05-16 15:25:47" + }, { "name": "firebase/php-jwt", "version": "dev-master", @@ -51,19 +91,20 @@ }, { "name": "indieauth/client", - "version": "0.1.4", + "version": "0.1.7", "source": { "type": "git", "url": "https://github.com/indieweb/indieauth-client-php.git", - "reference": "ecb74843c5c01ca49f118480d068087b5477e4f9" + "reference": "dec4f2f77ddaab11b00537db0a5fe52cc0bc3296" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/indieweb/indieauth-client-php/zipball/ecb74843c5c01ca49f118480d068087b5477e4f9", - "reference": "ecb74843c5c01ca49f118480d068087b5477e4f9", + "url": "https://api.github.com/repos/indieweb/indieauth-client-php/zipball/dec4f2f77ddaab11b00537db0a5fe52cc0bc3296", + "reference": "dec4f2f77ddaab11b00537db0a5fe52cc0bc3296", "shasum": "" }, "require": { + "barnabywalters/mf-cleaner": "0.*", "indieweb/link-rel-parser": "0.1.1", "mf2/mf2": "0.2.*", "php": ">5.3.0" @@ -85,7 +126,7 @@ } ], "description": "IndieAuth Client Library", - "time": "2014-08-15 18:03:06" + "time": "2014-10-06 06:25:22" }, { "name": "indieweb/date-formatter", diff --git a/controllers/auth.php b/controllers/auth.php index f405d86..0ecdf80 100644 --- a/controllers/auth.php +++ b/controllers/auth.php @@ -61,6 +61,18 @@ function hostname($url) { return parse_url($url, PHP_URL_HOST); } +function add_hcard_info($user, $hCard) { + if($user && $hCard) { + // Update the user's h-card info if present + if(array_key_exists('name', $hCard)) { + $user->name = $hCard['name']; + } + if(array_key_exists('photo', $hCard)) { + $user->photo_url = $hCard['photo']; + } + } +} + $app->get('/', function($format='html') use($app) { $res = $app->response(); @@ -95,6 +107,7 @@ $app->get('/auth/start', function() use($app) { $authorizationEndpoint = IndieAuth\Client::discoverAuthorizationEndpoint($me); $tokenEndpoint = IndieAuth\Client::discoverTokenEndpoint($me); $micropubEndpoint = IndieAuth\Client::discoverMicropubEndpoint($me); + $hCard = IndieAuth\Client::getHCard($me); // Generate a "state" parameter for the request $state = IndieAuth\Client::generateStateParameter(); @@ -111,8 +124,10 @@ $app->get('/auth/start', function() use($app) { // 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', hostname($me))->find_one(); + if($user && $user->access_token && !array_key_exists('restart', $params)) { + add_hcard_info($user, $hCard); $user->micropub_endpoint = $micropubEndpoint; $user->authorization_endpoint = $authorizationEndpoint; $user->token_endpoint = $tokenEndpoint; @@ -125,6 +140,7 @@ $app->get('/auth/start', function() use($app) { if(!$user) $user = ORM::for_table('users')->create(); + add_hcard_info($user, $hCard); $user->url = hostname($me); $user->date_created = date('Y-m-d H:i:s'); $user->micropub_endpoint = $micropubEndpoint; diff --git a/lib/config.template.php b/lib/config.template.php index 0c7b052..3f200dc 100644 --- a/lib/config.template.php +++ b/lib/config.template.php @@ -10,5 +10,7 @@ class Config { public static $dbPassword = ''; public static $jwtSecret = 'xxx'; + + public static $mf2Debug = false; } diff --git a/views/layout.php b/views/layout.php index 21bf081..ed2ebb5 100644 --- a/views/layout.php +++ b/views/layout.php @@ -52,9 +52,11 @@ fetch($this->page . '.php') ?> -
-
-
+ +
+
+
+