dont die if html parsing fails. slack grin instead

This commit is contained in:
Alex Grintsvayg 2019-10-09 14:28:25 -04:00
parent c343d0c2b4
commit a83245afbc
No known key found for this signature in database
GPG key ID: AEB3F089F86A22B5
2 changed files with 24 additions and 12 deletions

View file

@ -13,4 +13,12 @@ class Slack
Curl::post($slackErrorNotificationUrl, ['text' => ($alert ? '<!channel> ' : '') . Request::getRelativeUri() . "\n" . $e], ['json_data' => true]); Curl::post($slackErrorNotificationUrl, ['text' => ($alert ? '<!channel> ' : '') . Request::getRelativeUri() . "\n" . $e], ['json_data' => true]);
} }
} }
public static function slackGrin()
{
$slackErrorNotificationUrl = Config::get(Config::SLACK_ERROR_NOTIFICATION_URL);
if ($slackErrorNotificationUrl) {
Curl::post($slackErrorNotificationUrl, ['text' => '<@U1R0URBC1> Failed to parse html on ' . Request::getRelativeUri()], ['json_data' => true]);
}
}
} }

View file

@ -179,21 +179,25 @@ class View
public static function safeExternalLinks(string $html, string $domain): string public static function safeExternalLinks(string $html, string $domain): string
{ {
return $html; try {
$parser = new Masterminds\HTML5();
$parser = new Masterminds\HTML5(); $dom = $parser->loadHTML($html);
$dom = $parser->loadHTML($html); $links = $dom->getElementsByTagName('body') ?
$links = $dom->getElementsByTagName('body') ? $dom->getElementsByTagName('body')[0]->getElementsByTagName('a') :
$dom->getElementsByTagName('body')[0]->getElementsByTagName('a') : $dom->getElementsByTagName('a');
$dom->getElementsByTagName('a');
foreach ($links as $link) { foreach ($links as $link) {
if ($link->getAttribute('href') && static::isLinkExternal($link->getAttribute('href'), $domain)) { if ($link->getAttribute('href') && static::isLinkExternal($link->getAttribute('href'), $domain)) {
$link->setAttribute('rel', "noopener noreferrer"); $link->setAttribute('rel', "noopener noreferrer");
}
} }
}
return $parser->saveHTML($dom); return $parser->saveHTML($dom);
}
catch (Error $e) {
Slack::slackGrin();
return $html;
}
} }
public static function isLinkExternal(string $url, string $domain): bool public static function isLinkExternal(string $url, string $domain): bool