public function testFindNoLinksInHTML() { $html = '<div><p>Hello World</p></div>'; $mf2 = Mf2\parse($html, 'http://source.example.net/'); $links = IndieWeb\MentionClientTest::findOutgoingLinks($mf2); $this->assertEquals(array(), $links); }
public function testMultipleHCardsURLNoRelMe() { $html = '<div class="h-card"><a href="http://example.com/" class="u-url">Example</a></div> <div class="h-card"><a href="http://example.com/" class="u-url">Example</a></div>'; $parsed = Mf2\parse($html); $representative = Mf2\HCard\representative($parsed, 'http://example.com/'); $this->assertFalse($representative); }
public function get_outgoing_links(Request $request, Response $response) { if (!$this->_is_logged_in($request, $response)) { return $response; } $sourceURL = $request->get('url'); $client = new IndieWeb\MentionClient(); $source = $this->http->get($sourceURL); $parsed = \Mf2\parse($source['body'], $sourceURL); $links = array_values($client->findOutgoingLinks($parsed)); // Remove the source URL from the list if present $links = array_filter($links, function ($link) use($sourceURL) { return $link != $sourceURL; }); $response->headers->set('Content-Type', 'application/json'); $response->setContent(json_encode(['links' => $links])); return $response; }
private static function mf2parse($content, $url) { $data = array(); $host = extract_domain_name($url); switch ($host) { case 'twitter.com': $parsed = Mf2\Shim\parseTwitter($content, $url); break; default: $parsed = Mf2\parse($content, $url); } if (mf2_cleaner::isMicroformatCollection($parsed)) { $entries = mf2_cleaner::findMicroformatsByType($parsed, 'h-entry'); if ($entries) { $entry = $entries[0]; if (mf2_cleaner::isMicroformat($entry)) { foreach ($entry['properties'] as $key => $value) { $data[$key] = mf2_cleaner::getPlaintext($entry, $key); } $data['published'] = mf2_cleaner::getPublished($entry); $data['updated'] = mf2_cleaner::getUpdated($entry); $data['name'] = mf2_cleaner::getPlaintext($entry, 'name'); $data['content'] = mf2_cleaner::getHtml($entry, 'content'); $data['summary'] = mf2_cleaner::getHtml($entry, 'summary'); $data['name'] = trim(preg_replace('/https?:\\/\\/([^ ]+|$)/', '', $data['name'])); $author = mf2_cleaner::getAuthor($entry); if ($author) { $data['author'] = array(); foreach ($author['properties'] as $key => $value) { $data['author'][$key] = mf2_cleaner::getPlaintext($author, $key); } $data['author'] = array_filter($data['author']); } } } } return array_filter($data); }
/** * main worker function: reads all bookmarks sorted by owner; * gets them, processes them, sends the new entries * */ public function worker() { static::debug('worker started', 6); $args = array('orderby' => 'owner', 'order' => 'ASC', 'limit' => -1); $bookmarks = get_bookmarks($args); $currowner = $owner = false; foreach ($bookmarks as $bookmark) { /* print_r ($bookmark); stdClass Object ( [link_id] => 1 [link_url] => http://devopsreactions.tumblr.com/ [link_name] => http://devopsreactions.tumblr.com/ [link_image] => [link_target] => [link_description] => [link_visible] => Y [link_owner] => 1 [link_rating] => 0 [link_updated] => 0000-00-00 00:00:00 [link_rel] => [link_notes] => [link_rss] => http://devopsreactions.tumblr.com/rss ) */ //if ( $currowner != $bookmark->link_owner) { $currowner = $bookmark->link_owner; $owner = get_userdata($currowner); $owner = $owner->data; /* print_r ( $owner ); stdClass Object ( [ID] => 1 [user_login] => cadeyrn [user_pass] => $P$B.QI9GiDNyfXC7S75jJ4pcrQjx3awy/ [user_nicename] => cadeyrn [user_email] => hello@petermolnar.eu [user_url] => https://petermolnar.eu/ [user_registered] => 2014-04-28 21:36:35 [user_activation_key] => [user_status] => 0 [display_name] => Peter Molnar ) */ //} $export_yaml[$owner->user_nicename][] = array('name' => $bookmark->link_name, 'url' => $bookmark->link_url, 'rss' => $bookmark->link_rss, 'description' => $bookmark->link_description, 'lastfetched' => $bookmark->link_updated); if (!empty($bookmark->link_rss)) { $this->do_rss($bookmark, $owner); } else { static::debug('Switcing into HTML mode', 6); $url = htmlspecialchars_decode($bookmark->link_url); static::debug(" fetching {$url}", 6); $q = wp_remote_get($url); if (is_wp_error($q)) { static::debug(' something went wrong: ' . $q->get_error_message(), 4); continue; } if (!is_array($q)) { continue; } if (!isset($q['headers']) || !is_array($q['headers'])) { continue; } if (!isset($q['body']) || empty($q['body'])) { continue; } $ctype = isset($q['headers']['content-type']) ? $q['headers']['content-type'] : 'text/html'; if ($ctype == "application/json") { static::debug(" content is json", 6); $content = json_decode($q['body'], true); } else { static::debug(" content is html", 6); $content = Mf2\parse($q['body'], $url); } static::debug(" sending it to mf parser", 6); $this->parse_mf($bookmark, $owner, $content); } } if (function_exists('yaml_emit')) { $flatroot = WP_CONTENT_DIR . DIRECTORY_SEPARATOR . 'flat'; if (!is_dir($flatroot)) { if (!mkdir($flatroot)) { static::debug_log('Failed to create ' . $flatroot . ', exiting YAML creation', 4); } } foreach ($export_yaml as $owner => $bookmarks) { $export = yaml_emit($bookmarks, YAML_UTF8_ENCODING); $export_file = $flatroot . DIRECTORY_SEPARATOR . 'bookmarks_' . $owner . '.yml'; file_put_contents($export_file, $export); } } }
/** * try to get contents of webmention originator * * @param int $post_id ID of post * @param string $source Originator URL * @param string $target Target URL * * @return bool|array false on error; plain array or Mf2 parsed (and * flattened ) array of remote content on success */ protected static function try_receive_remote($post_id, $source, $target) { $content = false; $q = static::_wp_remote_get($source); if (false === $q) { return false; } $targets = array($target, wp_get_shortlink($post_id), get_permalink($post_id)); $found = false; foreach ($targets as $k => $t) { $t = preg_replace('/https?:\\/\\/(?:www.)?/', '', $t); $t = preg_replace('/#.*/', '', $t); $t = untrailingslashit($t); //$targets[ $k ] = $t; if (!stristr($q['body'], $t)) { $found = true; } } // check if source really links to target // this could be a temporary error, so we'll retry later this one as well if (false == $found) { static::debug(" missing link to {$t} in remote body", 6); return false; } $ctype = isset($q['headers']['content-type']) ? $q['headers']['content-type'] : 'text/html'; if ("text/plain" == $ctype) { static::debug(" interesting, plain text webmention. I'm not prepared for this yet", 6); return false; } elseif ($ctype == "application/json") { static::debug(" content is JSON", 6); $content = json_decode($q['body'], true); } else { static::debug(" content is (probably) html, trying to parse it with MF2", 6); try { $content = Mf2\parse($q['body'], $source); } catch (Exception $e) { static::debug(" parsing MF2 failed: " . $e->getMessage(), 4); return false; } $content = static::flatten_mf2_array($content); } return $content; }
public function testHasRel() { $html = '<div class="h-card"><a href="http://example.com/" class="u-url" rel="me"></a></div>'; $parsed = Mf2\parse($html); $this->assertTrue(Mf2\HCard\has_rel($parsed, 'me', 'http://example.com/')); }
public function sendMentions($sourceURL, $sourceBody = false) { if ($sourceBody) { $this->_sourceBody = $sourceBody; $this->_links = self::findOutgoingLinks($sourceBody); } else { $body = static::_get($sourceURL); $this->_sourceBody = $body['body']; $parsed = \Mf2\parse($this->_sourceBody, $sourceURL); $this->_links = self::findOutgoingLinks($parsed); } $totalAccepted = 0; foreach ($this->_links as $target) { self::_debug("Checking {$target} for webmention and pingback endpoints"); if ($this->sendFirstSupportedMention($sourceURL, $target)) { $totalAccepted++; } } return $totalAccepted; }
private function getViewModel($mention) { $source_base = $this->getBaseUrl($mention['source']); return new \Aruna\PostViewModel(\Mf2\parse($mention['html'], $source_base)); }
public function get_outgoing_links(Request $request, Response $response) { if (!$this->_is_logged_in($request, $response)) { return $response; } $sourceURL = $request->get('url'); $client = new IndieWeb\MentionClient(); $source = $this->http->get($sourceURL); $parsed = \Mf2\parse($source['body'], $sourceURL); $links = $client->findOutgoingLinks($parsed); $response->headers->set('Content-Type', 'application/json'); $response->setContent(json_encode(['links' => array_values($links)])); return $response; }