.
*
*/
namespace Friendica\Content;
use Friendica\Content\Text\BBCode;
use Friendica\Core\Hook;
use Friendica\DI;
use Friendica\Util\Strings;
/**
* This class contains functions to handle smiles
*/
class Smilies
{
/**
* Replaces/adds the emoticon list
*
* This function should be used whenever emoticons are added
*
* @param array $b Array of emoticons
* @param string $smiley The text smilie
* @param string $representation The replacement
* @return void
*/
public static function add(array &$b, string $smiley, string $representation)
{
$found = array_search($smiley, $b['texts']);
if (!is_int($found)) {
$b['texts'][] = $smiley;
$b['icons'][] = $representation;
} else {
$b['icons'][$found] = $representation;
}
}
/**
* Function to list all smilies
*
* Get an array of all smilies, both internal and from addons.
*
* @return array
* 'texts' => smilie shortcut
* 'icons' => icon in html
*
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @hook smilie ('texts' => smilies texts array, 'icons' => smilies html array)
*/
public static function getList(): array
{
$texts = [
'<3',
'</3',
'<\\3',
':-)',
';-)',
':-(',
':-P',
':-p',
':-"',
':-"',
':-x',
':-X',
':-D',
'8-|',
'8-O',
':-O',
'\\o/',
'o.O',
'O.o',
'o_O',
'O_o',
":'(",
":-!",
":-/",
":-[",
"8-)",
':beer',
':homebrew',
':coffee',
':facepalm',
':like',
':dislike',
'~friendica',
'red#',
'red#matrix'
];
$baseUrl = (string)DI::baseUrl();
$icons = [
'',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'
',
'~friendica
',
'red
matrix',
'red
matrix'
];
$params = ['texts' => $texts, 'icons' => $icons];
Hook::callAll('smilie', $params);
return $params;
}
/**
* Normalizes smiley shortcodes into texts with no special symbols.
*
* @return array
* 'texts' => smilie shortcut
* 'icons' => icon url or an empty string
* 'norms' => normalized shortcut
*/
public static function getNormalizedList(): array
{
$smilies = self::getList();
$norms = [];
$icons = $smilies['icons'];
foreach ($smilies['texts'] as $i => $shortcode) {
// Extract urls
$icon = $icons[$i];
if (preg_match('/src="(.+?)"/', $icon, $match)) {
$icon = $match[1];
} else {
$icon = '';
}
$icons[$i] = $icon;
// Normalize name
$norm = preg_replace('/[\s\-:#~]/', '', $shortcode);
if (ctype_alnum($norm)) {
$norms[] = $norm;
} elseif (preg_match('#/smiley-(\w+)\.gif#', $icon, $match)) {
$norms[] = $match[1];
} else {
$norms[] = 'smiley' . $i;
}
}
$smilies['norms'] = $norms;
return $smilies;
}
/**
* Finds all used smilies (denoted by quoting colons like :heart:) in the provided text and normalizes their usages.
*
* @param string $text that might contain smiley usages
* @return array with smilie codes (colon included) as the keys, their image urls as values;
* the normalized string is put under the '' (empty string) key
*/
public static function extractUsedSmilies(string $text, string &$normalized = null): array
{
$emojis = [];
$normalized = BBCode::performWithEscapedTags($text, ['code'], function ($text) use (&$emojis) {
return BBCode::performWithEscapedTags($text, ['noparse', 'nobb', 'pre'], function ($text) use (&$emojis) {
if (strpos($text, '[nosmile]') !== false || self::noSmilies()) {
return $text;
}
$smilies = self::getNormalizedList();
$normalized = array_combine($smilies['texts'], $smilies['norms']);
return self::performForEachWordMatch(
array_combine($smilies['texts'], $smilies['icons']),
$text,
function (string $name, string $image) use($normalized, &$emojis) {
$name = $normalized[$name];
if (preg_match('/src="(.+?)"/', $image, $match)) {
$image = $match[1];
$emojis[$name] = $image;
}
return ':' . $name . ':';
},
);
});
});
return $emojis;
}
/**
* Similar to strtr but matches only whole words and replaces texts with $callback.
*
* @param array $words
* @param string $subject
* @param callable $callback ($offset, $value)
* @return string
*/
private static function performForEachWordMatch(array $words, string $subject, callable $callback): string
{
$ord1_bitset = 0;
$ord2_bitset = 0;
$prefixes = [];
foreach ($words as $word => $_) {
if (strlen($word) < 2 || !ctype_graph($word)) {
continue;
}
$ord1 = ord($word);
$ord2 = ord($word[1]);
$ord1_bitset |= 1 << ($ord1 & 31);
$ord2_bitset |= 1 << ($ord2 & 31);
if (!array_key_exists($word[0], $prefixes)) {
$prefixes[$word[0]] = [];
}
$prefixes[$word[0]][] = $word;
}
$result = '';
$processed = 0;
$s_start = 0; // Segment start
// No spaces are allowed in smilies, so they can serve as delimiters.
// Splitting by some delimiters may not necessary though?
while (true) {
if ($s_start >= strlen($subject)) {
$result .= substr($subject, $processed);
break;
}
if (preg_match('/\s+?(?=\S|$)/', $subject, $match, PREG_OFFSET_CAPTURE, $s_start)) {
[$whitespaces, $s_end] = $match[0];
} else {
$s_end = strlen($subject);
$whitespaces = '';
}
$s_length = $s_end - $s_start;
if ($s_length > 1) {
$segment = substr($subject, $s_start, $s_length);
// Find possible starting points for smilies.
// For built-in smilies, the two bitsets should make attempts quite efficient.
// However, presuming custom smilies follow the format of ":shortcode" or ":shortcode:",
// if the user adds more smilies (with addons), the second bitset may eventually become useless.
for ($i = 0; $i < $s_length - 1; $i++) {
$c = $segment[$i];
$d = $segment[$i + 1];
if (($ord1_bitset & (1 << (ord($c) & 31))) && ($ord2_bitset & (1 << (ord($d) & 31))) && array_key_exists($c, $prefixes)) {
foreach ($prefixes[$c] as $word) {
$wlength = strlen($word);
if ($wlength <= $s_length - $i && substr($segment, $i, $wlength) === $word) {
// Check for boundaries
if (($i === 0 || ctype_space($segment[$i - 1]) || ctype_punct($segment[$i - 1]))
&& ($i + $wlength >= $s_length || ctype_space($segment[$i + $wlength]) || ctype_punct($segment[$i + $wlength]))) {
$result .= substr($subject, $processed, $s_start - $processed + $i);
$result .= call_user_func($callback, $word, $words[$word]);
$i += $wlength;
$processed = $s_start + $i;
$i--;
break;
}
}
}
}
}
}
$s_start = $s_end + strlen($whitespaces);
}
return $result;
}
/**
* Copied from http://php.net/manual/en/function.str-replace.php#88569
* Modified for camel caps: renamed stro_replace -> strOrigReplace
*
* When using str_replace(...), values that did not exist in the original string (but were put there by previous
* replacements) will be replaced continuously. This string replacement function is designed to replace the values
* in $search with those in $replace while not factoring in prior replacements. Note that this function will
* always look for the longest possible match first and then work its way down to individual characters.
*
* @param array $search list of strings or characters that need to be replaced
* @param array $replace list of strings or characters that will replace the corresponding values in $search
* @param string $subject the string on which this operation is being performed
*
* @return string $subject with all substrings in the $search array replaced by the values in the $replace array
*/
private static function strOrigReplace(array $search, array $replace, string $subject): string
{
return self::performForEachWordMatch(
array_combine($search, $replace),
$subject,
function (string $_, string $value) {
return $value;
}
);
}
/**
* Replaces text emoticons with graphical images
*
* It is expected that this function will be called using HTML text.
* We will escape text between HTML pre and code blocks from being
* processed.
*
* At a higher level, the bbcode [nosmile] tag can be used to prevent this
* function from being executed by the prepare_text() routine when preparing
* bbcode source for HTML display
*
* @param string $s Text that should be replaced
* @param boolean $no_images Only replace emoticons without images
*
* @return string HTML Output of the Smilie
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function replace(string $s, bool $no_images = false): string
{
$smilies = self::getList();
$s = self::replaceFromArray($s, $smilies, $no_images);
return $s;
}
private static function noSmilies(): bool {
return (intval(DI::config()->get('system', 'no_smilies')) ||
(DI::userSession()->getLocalUserId() &&
intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'no_smilies'))));
}
/**
* Replaces emoji shortcodes in a string from a structured array of searches and replaces.
*
* Depends on system.no_smilies config value, skips
and tags.
*
* @param string $text An HTML string
* @param array $smilies An string replacement array with the following structure: ['texts' => [], 'icons' => []]
* @param bool $no_images Only replace shortcodes without image replacement (e.g. Unicode characters)
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function replaceFromArray(string $text, array $smilies, bool $no_images = false): string
{
if (self::noSmilies()) {
return $text;
}
$text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', [self::class, 'encode'], $text);
$text = preg_replace_callback('/<(code)>(.*?)<\/code>/ism', [self::class, 'encode'], $text);
if ($no_images) {
$cleaned = ['texts' => [], 'icons' => []];
$icons = $smilies['icons'];
foreach ($icons as $key => $icon) {
if (!strstr($icon, '
(.*?)<\/code>/ism', [self::class, 'decode'], $text);
$text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', [self::class, 'decode'], $text);
return $text;
}
/**
* Encodes smiley match array to BASE64 string
*
* @param array $m Match array
* @return string base64 encoded string
*/
private static function encode(array $m): string
{
return '<' . $m[1] . '>' . Strings::base64UrlEncode($m[2]) . '' . $m[1] . '>';
}
/**
* Decodes a previously BASE64-encoded match array to a string
*
* @param array $m Matches array
* @return string base64 decoded string
* @throws \Exception
*/
private static function decode(array $m): string
{
return '<' . $m[1] . '>' . Strings::base64UrlDecode($m[2]) . '' . $m[1] . '>';
}
/**
* expand <3333 to the correct number of hearts
*
* @param array $matches
* @return string HTML Output
*/
private static function heartReplaceCallback(array $matches): string
{
return str_repeat('❤', strlen($matches[0]) - 4);
}
/**
* Checks if the body doesn't contain any alphanumeric characters
*
* @param string $body Possibly-HTML post body
* @return boolean
*/
public static function isEmojiPost(string $body): bool
{
// Strips all whitespace
$conv = preg_replace('#\s#u', '', html_entity_decode($body));
if (empty($conv)) {
return false;
}
if (!class_exists('IntlChar')) {
// Most Emojis are 4 byte Unicode characters, so this is a good workaround, when IntlChar does not exist on the system
return strlen($conv) / mb_strlen($conv) == 4;
}
for ($i = 0; $i < mb_strlen($conv); $i++) {
$character = mb_substr($conv, $i, 1);
if (\IntlChar::isalnum($character) || \IntlChar::ispunct($character) || \IntlChar::isgraph($character) && (strlen($character) <= 2)) {
return false;
}
}
return true;
}
}