aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Lib/Webfinger.php
diff options
context:
space:
mode:
authorzotlabs <mike@macgirvin.com>2018-08-06 17:43:22 -0700
committerzotlabs <mike@macgirvin.com>2018-08-06 17:43:22 -0700
commit166879b8b04f8656a0ef105c319b8b4a82626bd9 (patch)
tree4244b1a57ab90d7588c57789c027beeffeb6ede2 /Zotlabs/Lib/Webfinger.php
parentaeb9d5cd90a3bcb79c8d0b1645ece47e8756f422 (diff)
downloadvolse-hubzilla-166879b8b04f8656a0ef105c319b8b4a82626bd9.tar.gz
volse-hubzilla-166879b8b04f8656a0ef105c319b8b4a82626bd9.tar.bz2
volse-hubzilla-166879b8b04f8656a0ef105c319b8b4a82626bd9.zip
bring some Zot6 libraries and interfaces to red/hubzilla
Diffstat (limited to 'Zotlabs/Lib/Webfinger.php')
-rw-r--r--Zotlabs/Lib/Webfinger.php109
1 files changed, 109 insertions, 0 deletions
diff --git a/Zotlabs/Lib/Webfinger.php b/Zotlabs/Lib/Webfinger.php
new file mode 100644
index 000000000..c2364ac4d
--- /dev/null
+++ b/Zotlabs/Lib/Webfinger.php
@@ -0,0 +1,109 @@
+<?php
+
+namespace Zotlabs\Lib;
+
+/**
+ * @brief Fetch and return a webfinger for a resource
+ *
+ * @param string $resource - The resource
+ * @return boolean|string false or associative array from result JSON
+ */
+
+class Webfinger {
+
+ static private $server = EMPTY_STR;
+ static private $resource = EMPTY_STR;
+
+ static function exec($resource) {
+
+ if(! $resource) {
+ return false;
+ }
+
+ self::parse_resource($resource);
+
+ if(! ( self::$server && self::$resource)) {
+ return false;
+ }
+
+ if(! check_siteallowed(self::$server)) {
+ logger('blacklisted: ' . self::$server);
+ return false;
+ }
+
+ btlogger('fetching resource: ' . self::$resource . ' from ' . self::$server, LOGGER_DEBUG, LOG_INFO);
+
+ $url = 'https://' . self::$server . '/.well-known/webfinger?f=&resource=' . self::$resource ;
+
+ $counter = 0;
+ $s = z_fetch_url($url, false, $counter, [ 'headers' => [ 'Accept: application/jrd+json, */*' ] ]);
+
+ if($s['success']) {
+ $j = json_decode($s['body'], true);
+ return($j);
+ }
+
+ return false;
+ }
+
+ static function parse_resource($resource) {
+
+ self::$resource = urlencode($resource);
+
+ if(strpos($resource,'http') === 0) {
+ $m = parse_url($resource);
+ if($m) {
+ if($m['scheme'] !== 'https') {
+ return false;
+ }
+ self::$server = $m['host'] . (($m['port']) ? ':' . $m['port'] : '');
+ }
+ else {
+ return false;
+ }
+ }
+ elseif(strpos($resource,'tag:') === 0) {
+ $arr = explode(':',$resource); // split the tag
+ $h = explode(',',$arr[1]); // split the host,date
+ self::$server = $h[0];
+ }
+ else {
+ $x = explode('@',$resource);
+ $username = $x[0];
+ if(count($x) > 1) {
+ self::$server = $x[1];
+ }
+ else {
+ return false;
+ }
+ if(strpos($resource,'acct:') !== 0) {
+ self::$resource = urlencode('acct:' . $resource);
+ }
+ }
+
+ }
+
+ /**
+ * @brief fetch a webfinger resource and return a zot6 discovery url if present
+ *
+ */
+
+ static function zot_url($resource) {
+
+ $arr = self::exec($resource);
+
+ if(is_array($arr) && array_key_exists('links',$arr)) {
+ foreach($arr['links'] as $link) {
+ if(array_key_exists('rel',$link) && $link['rel'] === PROTOCOL_ZOT6) {
+ if(array_key_exists('href',$link) && $link['href'] !== EMPTY_STR) {
+ return $link['href'];
+ }
+ }
+ }
+ }
+ return false;
+ }
+
+
+
+} \ No newline at end of file