+ /**
+ * @brief Switch the scheme of an url between http and https
+ *
+ * @param string $url URL
+ *
+ * @return string switched URL
+ */
+ private static function switchScheme($url) {
+ $parts = parse_url($url);
+
+ if (!isset($parts['scheme'])) {
+ return $url;
+ }
+
+ if ($parts['scheme'] == 'http') {
+ $url = str_replace('http://', 'https://', $url);
+ } elseif ($parts['scheme'] == 'https') {
+ $url = str_replace('https://', 'http://', $url);
+ }
+
+ return $url;
+ }
+
+ /**
+ * @brief Checks if a profile url should be OStatus but only provides partial information
+ *
+ * @param array $webfinger Webfinger data
+ * @param string $lrdd Path template for webfinger request
+ *
+ * @return array fixed webfinger data
+ */
+ private static function fixOstatus($webfinger, $lrdd) {
+ if (empty($webfinger['links']) || empty($webfinger['subject'])) {
+ return $webfinger;
+ }
+
+ $is_ostatus = false;
+ $has_key = false;
+
+ foreach ($webfinger['links'] as $link) {
+ if ($link['rel'] == NAMESPACE_OSTATUSSUB) {
+ $is_ostatus = true;
+ }
+ if ($link['rel'] == 'magic-public-key') {
+ $has_key = true;
+ }
+ }
+
+ if (!$is_ostatus || $has_key) {
+ return $webfinger;
+ }
+
+ $url = self::switchScheme($webfinger['subject']);
+ $path = str_replace('{uri}', urlencode($url), $lrdd);
+ $webfinger2 = self::webfinger($path);
+
+ // Is the new webfinger detectable as OStatus?
+ if (self::ostatus($webfinger2, true)) {
+ $webfinger = $webfinger2;
+ }
+
+ return $webfinger;
+ }
+