<?php
-
/**
- * @file src/Content/Smilies.php
- * @brief This file contains the Smilies class which contains functions to handle smiles
+ * @copyright Copyright (C) 2010-2024, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
*
- * @todo Use the shortcodes from here:
- * https://github.com/iamcal/emoji-data/blob/master/emoji_pretty.json?raw=true
- * https://raw.githubusercontent.com/emojione/emojione/master/extras/alpha-codes/eac.json?raw=true
- * https://github.com/johannhof/emoji-helper/blob/master/data/emoji.json?raw=true
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
*
- * Have also a look here:
- * https://www.webpagefx.com/tools/emoji-cheat-sheet/
*/
+
namespace Friendica\Content;
-use Friendica\Core\Config;
+use Friendica\Content\Text\BBCode;
use Friendica\Core\Hook;
-use Friendica\Core\PConfig;
-use Friendica\Core\System;
+use Friendica\DI;
use Friendica\Util\Strings;
/**
class Smilies
{
/**
- * @brief Replaces/adds the emoticon list
+ * 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(&$b, $smiley, $representation)
+ public static function add(array &$b, string $smiley, string $representation)
{
$found = array_search($smiley, $b['texts']);
}
/**
- * @brief Function to list all smilies
+ * Function to list all smilies
*
* Get an array of all smilies, both internal and from addons.
*
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @hook smilie ('texts' => smilies texts array, 'icons' => smilies html array)
*/
- public static function getList()
+ public static function getList(): array
{
- $texts = [
+ $texts = [
'<3',
'</3',
'<\\3',
];
+ $baseUrl = (string)DI::baseUrl();
+
$icons = [
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-heart.gif" alt="<3" title="<3" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-brokenheart.gif" alt="</3" title="</3" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-brokenheart.gif" alt="<\\3" title="<\\3" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-smile.gif" alt=":-)" title=":-)" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-wink.gif" alt=";-)" title=";-)" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-frown.gif" alt=":-(" title=":-(" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-tongue-out.gif" alt=":-P" title=":-P" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-tongue-out.gif" alt=":-p" title=":-P" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-x" title=":-x" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-X" title=":-X" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-laughing.gif" alt=":-D" title=":-D" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt="8-|" title="8-|" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt="8-O" title="8-O" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt=":-O" title="8-O" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-thumbsup.gif" alt="\\o/" title="\\o/" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="o.O" title="o.O" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="O.o" title="O.o" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="o_O" title="o_O" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="O_o" title="O_o" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-cry.gif" alt=":\'(" title=":\'("/>',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-foot-in-mouth.gif" alt=":-!" title=":-!" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-undecided.gif" alt=":-/" title=":-/" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-embarassed.gif" alt=":-[" title=":-[" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-cool.gif" alt="8-)" title="8-)" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/beer_mug.gif" alt=":beer" title=":beer" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/beer_mug.gif" alt=":homebrew" title=":homebrew" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/coffee.gif" alt=":coffee" title=":coffee" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-facepalm.gif" alt=":facepalm" title=":facepalm" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/like.gif" alt=":like" title=":like" />',
- '<img class="smiley" src="' . System::baseUrl() . '/images/dislike.gif" alt=":dislike" title=":dislike" />',
- '<a href="https://friendi.ca">~friendica <img class="smiley" src="' . System::baseUrl() . '/images/friendica-16.png" alt="~friendica" title="~friendica" /></a>',
- '<a href="http://redmatrix.me/">red<img class="smiley" src="' . System::baseUrl() . '/images/rm-16.png" alt="red#" title="red#" />matrix</a>',
- '<a href="http://redmatrix.me/">red<img class="smiley" src="' . System::baseUrl() . '/images/rm-16.png" alt="red#matrix" title="red#matrix" />matrix</a>'
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-heart.gif" alt="<3" title="<3" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-brokenheart.gif" alt="</3" title="</3" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-brokenheart.gif" alt="<\\3" title="<\\3" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-smile.gif" alt=":-)" title=":-)" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-wink.gif" alt=";-)" title=";-)" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-frown.gif" alt=":-(" title=":-(" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-tongue-out.gif" alt=":-P" title=":-P" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-tongue-out.gif" alt=":-p" title=":-P" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-x" title=":-x" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-X" title=":-X" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-laughing.gif" alt=":-D" title=":-D" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt="8-|" title="8-|" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt="8-O" title="8-O" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt=":-O" title="8-O" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-thumbsup.gif" alt="\\o/" title="\\o/" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="o.O" title="o.O" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="O.o" title="O.o" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="o_O" title="o_O" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="O_o" title="O_o" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-cry.gif" alt=":\'(" title=":\'("/>',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-foot-in-mouth.gif" alt=":-!" title=":-!" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-undecided.gif" alt=":-/" title=":-/" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-embarrassed.gif" alt=":-[" title=":-[" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-cool.gif" alt="8-)" title="8-)" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/beer_mug.gif" alt=":beer" title=":beer" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/beer_mug.gif" alt=":homebrew" title=":homebrew" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/coffee.gif" alt=":coffee" title=":coffee" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/smiley-facepalm.gif" alt=":facepalm" title=":facepalm" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/like.gif" alt=":like" title=":like" />',
+ '<img class="smiley" src="' . $baseUrl . '/images/dislike.gif" alt=":dislike" title=":dislike" />',
+ '<a href="https://friendi.ca">~friendica <img class="smiley" width="16" height="16" src="' . $baseUrl . '/images/friendica.svg" alt="~friendica" title="~friendica" /></a>',
+ '<a href="http://redmatrix.me/">red<img class="smiley" src="' . $baseUrl . '/images/rm-16.png" alt="red#" title="red#" />matrix</a>',
+ '<a href="http://redmatrix.me/">red<img class="smiley" src="' . $baseUrl . '/images/rm-16.png" alt="red#matrix" title="red#matrix" />matrix</a>'
];
$params = ['texts' => $texts, 'icons' => $icons];
return $params;
}
+ /**
+ * 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::getList();
+ $normalized = [];
+ return self::performForEachWordMatch(
+ array_combine($smilies['texts'], $smilies['icons']),
+ $text,
+ function (string $name, string $image) use($normalized, &$emojis) {
+ if (array_key_exists($name, $normalized)) {
+ return $normalized[$name];
+ }
+ if (preg_match('/src="(.+?)"/', $image, $match)) {
+ $url = $match[1];
+ // Image smilies, which should be normalized instead of being embedded for some protocols like ActivityPub.
+ // Normalize name
+ $norm = preg_replace('/[\s\-:#~]/', '', $name);
+ if (!ctype_alnum($norm)) {
+ if (preg_match('#/smiley-(\w+)\.gif#', $url, $match)) {
+ $norm = $match[1];
+ } else {
+ $norm = 'smiley' . count($normalized);
+ }
+ }
+ $shortcode = ':' . $norm . ':';
+ $normalized[$name] = $shortcode;
+ $emojis[$norm] = $url;
+ return $shortcode;
+ } else {
+ $normalized[$name] = $image;
+ // Probably text-substitution smilies (e.g., Unicode ones).
+ return $image;
+ }
+ },
+ );
+ });
+ });
+
+ 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) {
+ continue;
+ }
+ $ord1 = ord($word[0]);
+ $ord2 = ord($word[1]);
+ // A smiley shortcode must not begin or end with whitespaces.
+ if (ctype_space($word[0]) || ctype_space($word[strlen($word) - 1])) {
+ continue;
+ }
+ $ord1_bitset |= 1 << ($ord1 & 31);
+ $ord2_bitset |= 1 << ($ord2 & 31);
+ if (!array_key_exists($word[0], $prefixes)) {
+ $prefixes[$word[0]] = [];
+ }
+ $prefixes[$word[0]][] = $word;
+ }
+
+ $slength = strlen($subject);
+ $result = '';
+ // $processed is used to delay string concatenation since appending a char every loop is inefficient.
+ $processed = 0;
+ // 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 < $slength - 1; $i++) {
+ $c = $subject[$i];
+ $d = $subject[$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 (substr($subject, $i, $wlength) === $word) {
+ // Check for boundaries
+ if (($i === 0 || ctype_space($subject[$i - 1]) || ctype_punct($subject[$i - 1]))
+ && ($i + $wlength >= $slength || ctype_space($subject[$i + $wlength]) || ctype_punct($subject[$i + $wlength]))) {
+ $result .= substr($subject, $processed, $i - $processed);
+ $result .= call_user_func($callback, $word, $words[$word]);
+ $i += $wlength;
+ $processed = $i;
+ $i--;
+ break;
+ }
+ }
+ }
+ }
+ }
+ if ($processed < $slength) {
+ $result .= substr($subject, $processed);
+ }
+ return $result;
+ }
/**
* Copied from http://php.net/manual/en/function.str-replace.php#88569
*
* @return string $subject with all substrings in the $search array replaced by the values in the $replace array
*/
- private static function strOrigReplace($search, $replace, $subject)
+ private static function strOrigReplace(array $search, array $replace, string $subject): string
{
- return strtr($subject, array_combine($search, $replace));
+ return self::performForEachWordMatch(
+ array_combine($search, $replace),
+ $subject,
+ function (string $_, string $value) {
+ return $value;
+ }
+ );
}
/**
* function from being executed by the prepare_text() routine when preparing
* bbcode source for HTML display
*
- * @brief Replaces text emoticons with graphical images
* @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($s, $no_images = false)
+ public static function replace(string $s, bool $no_images = false): string
{
$smilies = self::getList();
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.
*
* @return string
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
- public static function replaceFromArray($text, array $smilies, $no_images = false)
+ public static function replaceFromArray(string $text, array $smilies, bool $no_images = false): string
{
- if (intval(Config::get('system', 'no_smilies'))
- || (local_user() && intval(PConfig::get(local_user(), 'system', 'no_smilies')))
- ) {
+ if (self::noSmilies()) {
return $text;
}
- $text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', 'self::encode', $text);
- $text = preg_replace_callback('/<(code)>(.*?)<\/code>/ism', 'self::encode', $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' => []];
$smilies = $cleaned;
}
- $text = preg_replace_callback('/<(3+)/', 'self::pregHeart', $text);
+ $text = preg_replace_callback('/\B<3+?\b/', [self::class, 'heartReplaceCallback'], $text);
$text = self::strOrigReplace($smilies['texts'], $smilies['icons'], $text);
- $text = preg_replace_callback('/<(code)>(.*?)<\/code>/ism', 'self::decode', $text);
- $text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', 'self::decode', $text);
+ $text = preg_replace_callback('/<(code)>(.*?)<\/code>/ism', [self::class, 'decode'], $text);
+ $text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', [self::class, 'decode'], $text);
return $text;
}
/**
- * @param string $m string
+ * Encodes smiley match array to BASE64 string
*
+ * @param array $m Match array
* @return string base64 encoded string
*/
- private static function encode($m)
+ private static function encode(array $m): string
{
return '<' . $m[1] . '>' . Strings::base64UrlEncode($m[2]) . '</' . $m[1] . '>';
}
/**
- * @param string $m string
+ * 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($m)
+ private static function decode(array $m): string
{
return '<' . $m[1] . '>' . Strings::base64UrlDecode($m[2]) . '</' . $m[1] . '>';
}
/**
- * @brief expand <3333 to the correct number of hearts
- *
- * @param string $x string
+ * 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
*
- * @throws \Friendica\Network\HTTPException\InternalServerErrorException
- * @todo : Rework because it doesn't work correctly
+ * @param string $body Possibly-HTML post body
+ * @return boolean
*/
- private static function pregHeart($x)
+ public static function isEmojiPost(string $body): bool
{
- if (strlen($x[1]) == 1) {
- return $x[0];
+ // Strips all whitespace
+ $conv = preg_replace('#\s#u', '', html_entity_decode($body));
+ if (empty($conv)) {
+ return false;
}
- $t = '';
- for ($cnt = 0; $cnt < strlen($x[1]); $cnt ++) {
- $t .= '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-heart.gif" alt="<3" />';
+
+ 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;
+ }
}
- $r = str_replace($x[0], $t, $x[0]);
- return $r;
+ return true;
}
}