public static function parse($extUrl, $ownUrl) { $mf = \Mf2\fetch($extUrl); $entry = null; foreach ($mf['items'] as $item) { if (in_array('h-entry', $item['type'])) { if (isset($entry)) { throw new \Exception("Page is expected to have only a single entry."); } $entry = $item['properties']; } } if (!isset($entry)) { throw new \Exception("Page is expected to have an MF2-marked up entry."); } if (isset($entry['repost-of'])) { // Repost foreach ($entry['repost-of'] as $r) { if (is_string($r) && $r == $ownUrl) { return new ExternalPost(ExternalPost::TYPE_REPOST, $extUrl, self::parseAuthor($entry)); } } } else { if (isset($entry['in-reply-to'])) { // Reply foreach ($entry['in-reply-to'] as $r) { if (is_string($r) && $r == $ownUrl) { return new ExternalPost(ExternalPost::TYPE_REPLY, $extUrl, self::parseAuthor($entry)); } if (is_array($r) && isset($r['properties']) && isset($r['properties']['url']) && isset($r['properties']['url']) == $ownUrl) { return new ExternalPost(ExternalPost::TYPE_REPLY, $extUrl, self::parseAuthor($entry)); } } } else { if (isset($entry['like-of'])) { // Like foreach ($entry['like-of'] as $r) { if (is_string($r) && $r == $ownUrl) { return new ExternalPost(ExternalPost::TYPE_LIKE, $extUrl, self::parseAuthor($entry)); } if (is_array($r) && isset($r['properties']) && isset($r['properties']['url']) && isset($r['properties']['url']) == $ownUrl) { return new ExternalPost(ExternalPost::TYPE_LIKE, $extUrl, self::parseAuthor($entry)); } } } } } throw new \Exception("No related post found on the URL."); }
public function start() { $src = get('source'); $target = get('target'); if (!v::url($src)) { throw new Exception('Invalid source'); } if (!v::url($target)) { throw new Exception('Invalid target'); } if (!str::contains($target, site()->url())) { throw new Exception('Invalid target'); } require_once dirname(__DIR__) . DS . 'vendor' . DS . 'mf2.php'; require_once dirname(__DIR__) . DS . 'vendor' . DS . 'comments.php'; $data = \Mf2\fetch($src); $result = \IndieWeb\comments\parse($data['items'][0], $src); if (empty($result)) { throw new Exception('Probably spam'); } $path = ltrim(str_replace(site()->url(), '', $target), '/'); if (!empty($path) and $page = page($path)) { if (!empty($result['published'])) { $time = strtotime($result['published']); } else { $time = time(); $result['published'] = date('c'); } $json = json_encode($result); $hash = sha1($json); $file = $page->root() . DS . '.webmentions' . DS . $time . '-' . $hash . '.json'; f::write($file, $json); return true; } else { throw new Exception('Invalid page'); } }
private function parse($url) { return Mf2\fetch($url); }
/** * Checks HTML code to find a representative hCard * * @return string|null the hCard or null */ public function getRepresentativeHCard() { foreach ($this->react as $link) { if (in_array($link->rel, array("http://microformats.org/profile/hcard", "http://webfinger.net/rel/profile-page"))) { $mfs = \Mf2\fetch($link->href); if ($mfs && ($hCard = \BarnabyWalters\Mf2\getRepresentativeHCard($mfs, $link->href))) { return $hCard; } } } return null; }