Skip to content
Snippets Groups Projects
Commit 8f3ad43e authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

fixes #3990

Closes #3990

Merge request studip/studip!2839
parent fddbe499
No related branches found
No related tags found
No related merge requests found
...@@ -91,7 +91,7 @@ ...@@ -91,7 +91,7 @@
<span data-show-topic="<?= $post['topic_id'] ?>" data-topic-content="<?= $post['topic_id'] ?>" <?= $edit_posting != $post['topic_id'] ? '' : 'style="display: none;"' ?>> <span data-show-topic="<?= $post['topic_id'] ?>" data-topic-content="<?= $post['topic_id'] ?>" <?= $edit_posting != $post['topic_id'] ? '' : 'style="display: none;"' ?>>
<?= ForumHelpers::highlight($post['content'], $highlight) ?> <?= ForumHelpers::highlight($post['content'], $highlight) ?>
<?= OpenGraph::extract(formatReady(ForumEntry::removeQuotes($post['content_raw'])))->render() ?> <?= OpenGraph::extract(ForumEntry::removeQuotes($post['content_raw']))->render() ?>
</span> </span>
</div> </div>
......
...@@ -11,34 +11,71 @@ class OpenGraph ...@@ -11,34 +11,71 @@ class OpenGraph
/** /**
* Extracts urls and their according open graph infos from a given string * Extracts urls and their according open graph infos from a given string
* *
* @param String $string Text to extract urls and open graph infos from * @param string $string Text to extract urls and open graph infos from
* @return OpenGraphURLCollection containing the extracted urls * @return OpenGraphURLCollection containing the extracted urls
*/ */
public static function extract($string) public static function extract(string $string): OpenGraphURLCollection
{ {
$collection = new OpenGraphURLCollection; $collection = new OpenGraphURLCollection();
if (Config::get()->OPENGRAPH_ENABLE) { if (!Config::get()->OPENGRAPH_ENABLE) {
$regexp = StudipCoreFormat::getStudipMarkup('links')['start']; return $collection;
$matched = preg_match_all('/' . $regexp . '/ums', $string, $matches, PREG_SET_ORDER); }
foreach ($matches as $match) {
$url = $match[2]; if (Studip\Markup::isHtml($string)) {
$urls = self::extractUrlsFromHtml($string);
if (!$url) { } else {
continue; $urls = self::extractUrlsFromText($string);
} }
if (!isLinkIntern($url)) { foreach ($urls as $url) {
$og_url = OpenGraphURL::fromURL($url); $og_url = OpenGraphURL::fromURL($url);
if ($og_url && !$collection->find($og_url->id)) { if ($og_url && !$collection->find($og_url->id)) {
$og_url->store(); $og_url->store();
$collection[] = $og_url; $collection[] = $og_url;
}
}
} }
} }
return $collection; return $collection;
} }
public static function filterURLs(array $urls): array
{
return array_filter($urls, function (string $url): bool {
if (!$url) {
return false;
}
return !isLinkIntern($url);
});
}
public static function extractUrlsFromText(string $text): array
{
$regexp = StudipCoreFormat::getStudipMarkup('links')['start'];
preg_match_all('/' . $regexp . '/ums', $text, $matches, PREG_SET_ORDER);
$urls = array_column($matches, 2);
return self::filterURLs($urls);
}
public static function extractUrlsFromHtml(string $html): array
{
$document = new DOMDocument();
$document->loadHTML($html, LIBXML_NOWARNING | LIBXML_NOERROR);
$elements = $document->getElementsByTagName('a');
$urls = [];
foreach ($elements as $element) {
if (!$element->hasAttribute('href')) {
continue;
}
$urls[] = $element->getAttribute('href');
}
return self::filterURLs($urls);
}
} }
...@@ -18,12 +18,12 @@ if (Config::get()->LOAD_EXTERNAL_MEDIA === "proxy" && Seminar_Session::is_curren ...@@ -18,12 +18,12 @@ if (Config::get()->LOAD_EXTERNAL_MEDIA === "proxy" && Seminar_Session::is_curren
?> ?>
<div class="opengraph <? if (count($videofiles) > 0) echo 'video'; ?> <? if (count($audiofiles) > 0) echo 'audio'; ?>"> <div class="opengraph <? if (count($videofiles) > 0) echo 'video'; ?> <? if (count($audiofiles) > 0) echo 'audio'; ?>">
<? if ($og['image'] && count($videofiles) === 0): ?> <? if ($og['image'] && count($videofiles) === 0): ?>
<a href="<?= URLHelper::getLink($og['url'], [], false) ?>" class="image" <a href="<?= URLHelper::getLink($og['url'], [], true) ?>" class="image"
target="_blank" rel="noopener noreferrer" target="_blank" rel="noopener noreferrer"
style="background-image:url(<?= htmlReady($media_url_func($og['image'])) ?>)"> style="background-image:url(<?= htmlReady($media_url_func($og['image'])) ?>)">
</a> </a>
<? endif; ?> <? endif; ?>
<a href="<?= URLHelper::getLink($og['url'], [], false) ?>" class="info" <a href="<?= URLHelper::getLink($og['url'], [], true) ?>" class="info"
target="_blank" rel="noopener noreferrer"> target="_blank" rel="noopener noreferrer">
<strong><?= htmlReady($og['title']) ?></strong> <strong><?= htmlReady($og['title']) ?></strong>
<? if (!count($videofiles)) : ?> <? if (!count($videofiles)) : ?>
......
...@@ -10,7 +10,6 @@ ...@@ -10,7 +10,6 @@
*/ */
require_once 'lib/classes/SimpleORMap.class.php'; require_once 'lib/classes/SimpleORMap.class.php';
require_once 'lib/models/OpenGraphURL.class.php';
require_once 'lib/visual.inc.php'; require_once 'lib/visual.inc.php';
require_once 'lib/classes/Config.class.php'; require_once 'lib/classes/Config.class.php';
......
<?php
/**
* @author Jan-Hendrik Willms <tleilax+studip@gmail.com>
* @license GPL2 or any later version
*/
class OpenGraphTest extends \Codeception\Test\Unit
{
public function setUp(): void
{
static $config = [
'OPENGRAPH_ENABLE' => true,
];
Config::set(new Config($config));
}
public function testURLExtraction()
{
$text = 'this is a link: https://example.org?foo=bar&bar=foo - believe it or not';
$urls = OpenGraph::extractUrlsFromText($text);
$this->assertCount(1, $urls);
$this->assertEquals('https://example.org?foo=bar&bar=foo', $urls[0]);
}
public function testURLExtractionFromHTML()
{
$html = Studip\Markup::HTML_MARKER . '<a href="https://example.org?foo=bar&amp;bar=foo">this is a link</a> - believe it or not';
$urls = OpenGraph::extractUrlsFromHtml($html);
$this->assertCount(1, $urls);
$this->assertEquals('https://example.org?foo=bar&bar=foo', $urls[0]);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment