function saveDataFromInput() { $page = Idno::site()->currentPage(); if (empty($this->_id)) { $new = true; } else { $new = false; } $this->likeof = $page->getInput('like-of'); if ($this->likeof) { foreach ((array) $this->likeof as $likeofurl) { $this->syndicatedto = Webmention::addSyndicatedReplyTargets($likeofurl, $this->syndicatedto); } } $this->description = $page->getInput('description'); if (empty($this->description)) { $result = \IdnoPlugins\Reactions\Pages\Fetch::fetch($this->likeof); if (!empty($result['description'])) { $this->description = $result['description']; } } $this->setAccess($page->getInput('access')); Idno::site()->logging()->log("saving like. new: {$new}"); if ($this->publish($new)) { Idno::site()->logging()->log("sending webmentions from {$this->getURL()} to {$this->likeof}"); Webmention::sendWebmentionPayload($this->getURL(), $this->likeof); } return true; }
function saveDataFromInput() { $page = Idno::site()->currentPage(); if (empty($this->_id)) { $new = true; } else { $new = false; } $this->repostof = $page->getInput('repost-of'); if ($this->repostof) { foreach ((array) $this->repostof as $repostofurl) { $this->syndicatedto = Webmention::addSyndicatedReplyTargets($repostofurl, $this->syndicatedto); } } $this->description = $page->getInput('description'); $this->body = $page->getInput('body'); if (empty($this->description) && empty($this->body)) { $result = \IdnoPlugins\Reactions\Pages\Fetch::fetch($this->repostof); if (isset($result['description'])) { $this->description = $result['description']; } if (isset($result['content'])) { $this->body = $result['content']; } } $this->setAccess($page->getInput('access')); if ($this->publish($new)) { Webmention::sendWebmentionPayload($this->getURL(), $this->repostof); } return true; }
/** * Add webmentions as annotations based on Microformats 2 data * * @param string $source The source URL * @param string $target The target URL (i.e., the page on this site that was pinged) * @param string $source_content The source page's HTML * @param array $source_mf2 Parsed Microformats 2 content from $source * @return bool Success */ function addWebmentions($source, $target, $source_content, $source_mf2) { if ($source_content['response'] == 410) { $this->removeAnnotation($source); $this->save(); } else { if (!empty($source_mf2) && !empty($source_mf2['items']) && is_array($source_mf2['items'])) { // At this point, we don't know who owns the page or what the content is. // First, we'll initialize some variables that we're interested in filling. $mentions = array('owner' => array(), 'mentions' => array()); // Content owner and usable webmention items $return = true; // Return value; // Get the page title from the source content $title = $source; try { $dom = new \DOMDocument(); $dom->loadHTML($source_content['content']); $xpath = new \DOMXPath($dom); if ($xpath_title = $xpath->query('//title')->item(0)->textContent) { $title = $xpath_title; } } catch (\Exception $e) { // Do nothing } // And then let's cycle through them! // A first pass for overall owner ... foreach ($source_mf2['items'] as $item) { // Figure out what kind of Microformats 2 item we have if (!empty($item['type']) && is_array($item['type'])) { foreach ($item['type'] as $type) { switch ($type) { case 'h-card': if (!empty($item['properties'])) { if (!empty($item['properties']['name'])) { $mentions['owner']['name'] = $item['properties']['name'][0]; } if (!empty($item['properties']['url'])) { $mentions['owner']['url'] = $item['properties']['url'][0]; } if (!empty($item['properties']['photo'])) { //$mentions['owner']['photo'] = $item['properties']['photo'][0]; $tmpfname = tempnam(sys_get_temp_dir(), 'webmention_avatar'); file_put_contents($tmpfname, \Idno\Core\Webservice::file_get_contents($item['properties']['photo'][0])); $name = md5($item['properties']['url'][0]); // TODO: Don't update the cache image for every webmention if ($icon = \Idno\Entities\File::createThumbnailFromFile($tmpfname, $name, 300)) { $mentions['owner']['photo'] = \Idno\Core\Idno::site()->config()->url . 'file/' . (string) $icon; } else { if ($icon = \Idno\Entities\File::createFromFile($tmpfname, $name)) { $mentions['owner']['photo'] = \Idno\Core\Idno::site()->config()->url . 'file/' . (string) $icon; } } unlink($tmpfname); } } break; } if (!empty($mentions['owner'])) { break; } } } } // And now a second pass for per-item owners and mentions ... foreach ($source_mf2['items'] as $item) { $mentions = $this->addWebmentionItem($item, $mentions, $source, $target, $title); if (!empty($item['type']) && is_array($item['type'])) { } } if (!empty($mentions['mentions']) && !empty($mentions['owner']) && !empty($mentions['owner']['url'])) { if (empty($mentions['owner']['photo'])) { $mentions['owner']['photo'] = ''; } if (empty($mentions['owner']['url'])) { $mentions['owner']['url'] = $source; } if (empty($mentions['owner']['name'])) { $mentions['owner']['name'] = 'Web user'; } if (empty($mentions['title'])) { $mentions['title'] = ''; } $this->removeAnnotation($source); foreach ($mentions['mentions'] as $mention) { if (!empty($mention['url'])) { $permalink = implode('', $mention['url']); } else { $permalink = $source; } // Special exemption for bridgy if (strpos($source, 'https://brid-gy.appspot.com/') !== false && in_array($mention['type'], array('like', 'share', 'rsvp'))) { $permalink = \Idno\Core\Idno::site()->template()->getURLWithVar('known_from', $source, implode('', $mention['url'])); } if (!$this->addAnnotation($mention['type'], $mentions['owner']['name'], $mentions['owner']['url'], $mentions['owner']['photo'], $mention['content'], $permalink, $mention['created'], $mention['title'])) { $return = false; } } $this->save(); if ($return) { if ($this->isReply()) { $webmentions = new Webmention(); if ($reply_urls = $this->getReplyToURLs()) { foreach ($reply_urls as $reply_url) { $webmentions->sendWebmentionPayload($this->getDisplayURL(), $reply_url); } } } } } return $return; } } return true; // There were no IndieWeb webmentions to add }
/** * Add webmentions as annotations based on Microformats 2 data * * @param string $source The source URL * @param string $target The target URL (i.e., the page on this site that was pinged) * @param array $source_response The Webservice response from fetching the source page * @param array $source_mf2 Parsed Microformats 2 content from $source * @return bool Success */ function addWebmentions($source, $target, $source_response, $source_mf2) { if ($source_response['response'] == 410) { $this->removeAnnotation($source); $this->save(); return true; } $return = false; if (!empty($source_mf2) && !empty($source_mf2['items']) && is_array($source_mf2['items'])) { // At this point, we don't know who owns the page or what the content is. // First, we'll initialize some variables that we're interested in filling. $mentions = array('owner' => array(), 'mentions' => array()); // Content owner and usable webmention items // Get the page title from the source content $title = Webmention::getTitleFromContent($source_response['content'], $source); // primary h-entry on the page $primary = Webmention::findRepresentativeHEntry($source_mf2, $source, ['h-entry']); $author = Webmention::findAuthorHCard($source_mf2, $source, $primary); // Convert the h-entry to one or more mentions if ($primary) { $mentions = $this->addWebmentionItem($primary, $author, $mentions, $source, $target, $title); } if (!empty($mentions['mentions']) && !empty($mentions['owner'])) { $return = true; if (empty($mentions['owner']['photo'])) { $mentions['owner']['photo'] = ''; } if (empty($mentions['owner']['url'])) { $mentions['owner']['url'] = $source; } if (empty($mentions['owner']['name'])) { $mentions['owner']['name'] = 'Web user'; } if (empty($mentions['title'])) { $mentions['title'] = ''; } $this->removeAnnotation($source); foreach ($mentions['mentions'] as $mention) { if (!empty($mention['url'])) { $permalink = $mention['url'][0]; //implode('', $mention['url']); } else { $permalink = $source; } // Special exemption for bridgy if (strpos($source, 'https://brid-gy.appspot.com/') !== false && in_array($mention['type'], array('like', 'share', 'rsvp'))) { $permalink = \Idno\Core\Idno::site()->template()->getURLWithVar('known_from', $source, implode('', $mention['url'])); } if (!$this->addAnnotation($mention['type'], $mentions['owner']['name'], $mentions['owner']['url'], $mentions['owner']['photo'], $mention['content'], $permalink, $mention['created'], $mention['title'])) { $return = false; } } $this->save(); if ($return && $this->isReply()) { if ($reply_urls = $this->getReplyToURLs()) { foreach ($reply_urls as $reply_url) { Webmention::sendWebmentionPayload($this->getDisplayURL(), $reply_url); } } } } } return $return; }