2020-06-07 19:04:43 +03:00
|
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
namespace openvk\Web\Models\Entities\Traits;
|
|
|
|
|
|
|
|
|
|
trait TRichText
|
|
|
|
|
{
|
|
|
|
|
private function formatEmojis(string $text): string
|
|
|
|
|
{
|
2021-11-17 22:41:44 +03:00
|
|
|
|
$contentColumn = property_exists($this, "overrideContentColumn") ? $this->overrideContentColumn : "content";
|
|
|
|
|
if(iconv_strlen($this->getRecord()->{$contentColumn}) > OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["emojiProcessingLimit"])
|
2020-08-20 15:58:40 +03:00
|
|
|
|
return $text;
|
|
|
|
|
|
2020-06-26 11:36:56 +03:00
|
|
|
|
$emojis = \Emoji\detect_emoji($text);
|
2020-08-20 15:58:40 +03:00
|
|
|
|
$replaced = []; # OVK-113
|
2020-06-07 19:04:43 +03:00
|
|
|
|
foreach($emojis as $emoji) {
|
2020-08-20 15:58:40 +03:00
|
|
|
|
$point = explode("-", strtolower($emoji["hex_str"]))[0];
|
|
|
|
|
if(in_array($point, $replaced))
|
|
|
|
|
continue;
|
|
|
|
|
else
|
|
|
|
|
$replaced[] = $point;
|
|
|
|
|
|
2020-06-26 12:30:15 +03:00
|
|
|
|
$image = "https://abs.twimg.com/emoji/v2/72x72/$point.png";
|
2020-06-07 19:04:43 +03:00
|
|
|
|
$image = "<img src='$image' alt='$emoji[emoji]' ";
|
2020-06-26 12:30:15 +03:00
|
|
|
|
$image .= "style='max-height:12px; padding-left: 2pt; padding-right: 2pt; vertical-align: bottom;' />";
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
|
|
$text = str_replace($emoji["emoji"], $image, $text);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-20 15:58:40 +03:00
|
|
|
|
return $text;
|
2020-06-07 19:04:43 +03:00
|
|
|
|
}
|
2021-11-17 22:41:44 +03:00
|
|
|
|
|
|
|
|
|
private function formatLinks(string &$text): string
|
|
|
|
|
{
|
|
|
|
|
return preg_replace_callback(
|
|
|
|
|
"%(([A-z]++):\/\/(\S*?\.\S*?))([\s)\[\]{},;\"\'<]|\.\s|$)%",
|
|
|
|
|
(function (array $matches): string {
|
|
|
|
|
$href = str_replace("#", "#", $matches[1]);
|
|
|
|
|
$link = str_replace("#", "#", $matches[3]);
|
|
|
|
|
$rel = $this->isAd() ? "sponsored" : "ugc";
|
|
|
|
|
|
|
|
|
|
return "<a href='$href' rel='$rel' target='_blank'>$link</a>" . htmlentities($matches[4]);
|
|
|
|
|
}),
|
|
|
|
|
$text
|
|
|
|
|
);
|
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
|
|
private function removeZalgo(string $text): string
|
|
|
|
|
{
|
|
|
|
|
return preg_replace("%[\x{0300}-\x{036F}]{3,}%Xu", "<EFBFBD>", $text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getText(bool $html = true): string
|
|
|
|
|
{
|
2021-11-17 22:39:22 +03:00
|
|
|
|
$contentColumn = property_exists($this, "overrideContentColumn") ? $this->overrideContentColumn : "content";
|
|
|
|
|
|
|
|
|
|
$text = htmlentities($this->getRecord()->{$contentColumn}, ENT_DISALLOWED | ENT_XHTML);
|
|
|
|
|
$proc = iconv_strlen($this->getRecord()->{$contentColumn}) <= OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["postSizes"]["processingLimit"];
|
2020-06-07 19:04:43 +03:00
|
|
|
|
if($html) {
|
2020-08-20 15:58:40 +03:00
|
|
|
|
if($proc) {
|
|
|
|
|
$rel = $this->isAd() ? "sponsored" : "ugc";
|
2020-08-24 14:37:47 +03:00
|
|
|
|
$text = $this->formatLinks($text);
|
2020-08-20 15:58:40 +03:00
|
|
|
|
$text = preg_replace("%@(id|club)([0-9]++) \(([\p{L} 0-9]+)\)%Xu", "[$1$2|$3]", $text);
|
|
|
|
|
$text = preg_replace("%@(id|club)([0-9]++)%Xu", "[$1$2|@$1$2]", $text);
|
|
|
|
|
$text = preg_replace("%\[(id|club)([0-9]++)\|([\p{L} 0-9@]+)\]%Xu", "<a href='/$1$2'>$3</a>", $text);
|
|
|
|
|
$text = preg_replace("%(#([\p{L}_-]++[0-9]*[\p{L}_-]*))%Xu", "<a href='/feed/hashtag/$2'>$1</a>", $text);
|
|
|
|
|
$text = $this->formatEmojis($text);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-07 19:04:43 +03:00
|
|
|
|
$text = $this->removeZalgo($text);
|
|
|
|
|
$text = nl2br($text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
}
|