3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Content;
24 use Friendica\Core\Hook;
26 use Friendica\Util\Strings;
29 * This class contains functions to handle smiles
35 * Replaces/adds the emoticon list
37 * This function should be used whenever emoticons are added
39 * @param array $b Array of emoticons
40 * @param string $smiley The text smilie
41 * @param string $representation The replacement
44 public static function add(array &$b, string $smiley, string $representation)
46 $found = array_search($smiley, $b['texts']);
48 if (!is_int($found)) {
49 $b['texts'][] = $smiley;
50 $b['icons'][] = $representation;
52 $b['icons'][$found] = $representation;
57 * Function to list all smilies
59 * Get an array of all smilies, both internal and from addons.
62 * 'texts' => smilie shortcut
63 * 'icons' => icon in html
65 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
66 * @hook smilie ('texts' => smilies texts array, 'icons' => smilies html array)
68 public static function getList(): array
109 $baseUrl = DI::baseUrl();
112 '<img class="smiley" src="' . $baseUrl . '/images/smiley-heart.gif" alt="<3" title="<3" />',
113 '<img class="smiley" src="' . $baseUrl . '/images/smiley-brokenheart.gif" alt="</3" title="</3" />',
114 '<img class="smiley" src="' . $baseUrl . '/images/smiley-brokenheart.gif" alt="<\\3" title="<\\3" />',
115 '<img class="smiley" src="' . $baseUrl . '/images/smiley-smile.gif" alt=":-)" title=":-)" />',
116 '<img class="smiley" src="' . $baseUrl . '/images/smiley-wink.gif" alt=";-)" title=";-)" />',
117 '<img class="smiley" src="' . $baseUrl . '/images/smiley-frown.gif" alt=":-(" title=":-(" />',
118 '<img class="smiley" src="' . $baseUrl . '/images/smiley-tongue-out.gif" alt=":-P" title=":-P" />',
119 '<img class="smiley" src="' . $baseUrl . '/images/smiley-tongue-out.gif" alt=":-p" title=":-P" />',
120 '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
121 '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
122 '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-x" title=":-x" />',
123 '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-X" title=":-X" />',
124 '<img class="smiley" src="' . $baseUrl . '/images/smiley-laughing.gif" alt=":-D" title=":-D" />',
125 '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt="8-|" title="8-|" />',
126 '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt="8-O" title="8-O" />',
127 '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt=":-O" title="8-O" />',
128 '<img class="smiley" src="' . $baseUrl . '/images/smiley-thumbsup.gif" alt="\\o/" title="\\o/" />',
129 '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="o.O" title="o.O" />',
130 '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="O.o" title="O.o" />',
131 '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="o_O" title="o_O" />',
132 '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="O_o" title="O_o" />',
133 '<img class="smiley" src="' . $baseUrl . '/images/smiley-cry.gif" alt=":\'(" title=":\'("/>',
134 '<img class="smiley" src="' . $baseUrl . '/images/smiley-foot-in-mouth.gif" alt=":-!" title=":-!" />',
135 '<img class="smiley" src="' . $baseUrl . '/images/smiley-undecided.gif" alt=":-/" title=":-/" />',
136 '<img class="smiley" src="' . $baseUrl . '/images/smiley-embarassed.gif" alt=":-[" title=":-[" />',
137 '<img class="smiley" src="' . $baseUrl . '/images/smiley-cool.gif" alt="8-)" title="8-)" />',
138 '<img class="smiley" src="' . $baseUrl . '/images/beer_mug.gif" alt=":beer" title=":beer" />',
139 '<img class="smiley" src="' . $baseUrl . '/images/beer_mug.gif" alt=":homebrew" title=":homebrew" />',
140 '<img class="smiley" src="' . $baseUrl . '/images/coffee.gif" alt=":coffee" title=":coffee" />',
141 '<img class="smiley" src="' . $baseUrl . '/images/smiley-facepalm.gif" alt=":facepalm" title=":facepalm" />',
142 '<img class="smiley" src="' . $baseUrl . '/images/like.gif" alt=":like" title=":like" />',
143 '<img class="smiley" src="' . $baseUrl . '/images/dislike.gif" alt=":dislike" title=":dislike" />',
144 '<a href="https://friendi.ca">~friendica <img class="smiley" width="16" height="16" src="' . $baseUrl . '/images/friendica.svg" alt="~friendica" title="~friendica" /></a>',
145 '<a href="http://redmatrix.me/">red<img class="smiley" src="' . $baseUrl . '/images/rm-16.png" alt="red#" title="red#" />matrix</a>',
146 '<a href="http://redmatrix.me/">red<img class="smiley" src="' . $baseUrl . '/images/rm-16.png" alt="red#matrix" title="red#matrix" />matrix</a>'
149 $params = ['texts' => $texts, 'icons' => $icons];
150 Hook::callAll('smilie', $params);
157 * Copied from http://php.net/manual/en/function.str-replace.php#88569
158 * Modified for camel caps: renamed stro_replace -> strOrigReplace
160 * When using str_replace(...), values that did not exist in the original string (but were put there by previous
161 * replacements) will be replaced continuously. This string replacement function is designed to replace the values
162 * in $search with those in $replace while not factoring in prior replacements. Note that this function will
163 * always look for the longest possible match first and then work its way down to individual characters.
165 * @param array $search list of strings or characters that need to be replaced
166 * @param array $replace list of strings or characters that will replace the corresponding values in $search
167 * @param string $subject the string on which this operation is being performed
169 * @return string $subject with all substrings in the $search array replaced by the values in the $replace array
171 private static function strOrigReplace(array $search, array $replace, string $subject): string
173 return strtr($subject, array_combine($search, $replace));
177 * Replaces text emoticons with graphical images
179 * It is expected that this function will be called using HTML text.
180 * We will escape text between HTML pre and code blocks from being
183 * At a higher level, the bbcode [nosmile] tag can be used to prevent this
184 * function from being executed by the prepare_text() routine when preparing
185 * bbcode source for HTML display
187 * @param string $s Text that should be replaced
188 * @param boolean $no_images Only replace emoticons without images
190 * @return string HTML Output of the Smilie
191 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
193 public static function replace(string $s, bool $no_images = false): string
195 $smilies = self::getList();
197 $s = self::replaceFromArray($s, $smilies, $no_images);
203 * Replaces emoji shortcodes in a string from a structured array of searches and replaces.
205 * Depends on system.no_smilies config value, skips <pre> and <code> tags.
207 * @param string $text An HTML string
208 * @param array $smilies An string replacement array with the following structure: ['texts' => [], 'icons' => []]
209 * @param bool $no_images Only replace shortcodes without image replacement (e.g. Unicode characters)
211 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
213 public static function replaceFromArray(string $text, array $smilies, bool $no_images = false): string
215 if (intval(DI::config()->get('system', 'no_smilies'))
216 || (DI::userSession()->getLocalUserId() && intval(DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'no_smilies')))
221 $text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', [self::class, 'encode'], $text);
222 $text = preg_replace_callback('/<(code)>(.*?)<\/code>/ism', [self::class, 'encode'], $text);
225 $cleaned = ['texts' => [], 'icons' => []];
226 $icons = $smilies['icons'];
227 foreach ($icons as $key => $icon) {
228 if (!strstr($icon, '<img ')) {
229 $cleaned['texts'][] = $smilies['texts'][$key];
230 $cleaned['icons'][] = $smilies['icons'][$key];
236 $text = preg_replace_callback('/<(3+)/', [self::class, 'heartReplaceCallback'], $text);
237 $text = self::strOrigReplace($smilies['texts'], $smilies['icons'], $text);
239 $text = preg_replace_callback('/<(code)>(.*?)<\/code>/ism', [self::class, 'decode'], $text);
240 $text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', [self::class, 'decode'], $text);
246 * Encodes smiley match array to BASE64 string
248 * @param array $m Match array
249 * @return string base64 encoded string
251 private static function encode(array $m): string
253 return '<' . $m[1] . '>' . Strings::base64UrlEncode($m[2]) . '</' . $m[1] . '>';
257 * Decodes a previously BASE64-encoded match array to a string
259 * @param array $m Matches array
260 * @return string base64 decoded string
263 private static function decode(array $m): string
265 return '<' . $m[1] . '>' . Strings::base64UrlDecode($m[2]) . '</' . $m[1] . '>';
270 * expand <3333 to the correct number of hearts
272 * @param array $matches
273 * @return string HTML Output
275 private static function heartReplaceCallback(array $matches): string
277 if (strlen($matches[1]) == 1) {
282 for ($cnt = 0; $cnt < strlen($matches[1]); $cnt ++) {
286 return str_replace($matches[0], $t, $matches[0]);