Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

287 lines
10 KiB

  1. <?php
  2. /**
  3. * @file src/Content/Smilies.php
  4. * @brief This file contains the Smilies class which contains functions to handle smiles
  5. *
  6. * @todo Use the shortcodes from here:
  7. * https://github.com/iamcal/emoji-data/blob/master/emoji_pretty.json?raw=true
  8. * https://raw.githubusercontent.com/emojione/emojione/master/extras/alpha-codes/eac.json?raw=true
  9. * https://github.com/johannhof/emoji-helper/blob/master/data/emoji.json?raw=true
  10. *
  11. * Have also a look here:
  12. * https://www.webpagefx.com/tools/emoji-cheat-sheet/
  13. */
  14. namespace Friendica\Content;
  15. use Friendica\Core\Config;
  16. use Friendica\Core\Hook;
  17. use Friendica\DI;
  18. use Friendica\Util\Strings;
  19. /**
  20. * This class contains functions to handle smiles
  21. */
  22. class Smilies
  23. {
  24. /**
  25. * @brief Replaces/adds the emoticon list
  26. *
  27. * This function should be used whenever emoticons are added
  28. *
  29. * @param array $b Array of emoticons
  30. * @param string $smiley The text smilie
  31. * @param string $representation The replacement
  32. *
  33. * @return void
  34. */
  35. public static function add(&$b, $smiley, $representation)
  36. {
  37. $found = array_search($smiley, $b['texts']);
  38. if (!is_int($found)) {
  39. $b['texts'][] = $smiley;
  40. $b['icons'][] = $representation;
  41. } else {
  42. $b['icons'][$found] = $representation;
  43. }
  44. }
  45. /**
  46. * @brief Function to list all smilies
  47. *
  48. * Get an array of all smilies, both internal and from addons.
  49. *
  50. * @return array
  51. * 'texts' => smilie shortcut
  52. * 'icons' => icon in html
  53. *
  54. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  55. * @hook smilie ('texts' => smilies texts array, 'icons' => smilies html array)
  56. */
  57. public static function getList()
  58. {
  59. $texts = [
  60. '&lt;3',
  61. '&lt;/3',
  62. '&lt;\\3',
  63. ':-)',
  64. ';-)',
  65. ':-(',
  66. ':-P',
  67. ':-p',
  68. ':-"',
  69. ':-&quot;',
  70. ':-x',
  71. ':-X',
  72. ':-D',
  73. '8-|',
  74. '8-O',
  75. ':-O',
  76. '\\o/',
  77. 'o.O',
  78. 'O.o',
  79. 'o_O',
  80. 'O_o',
  81. ":'(",
  82. ":-!",
  83. ":-/",
  84. ":-[",
  85. "8-)",
  86. ':beer',
  87. ':homebrew',
  88. ':coffee',
  89. ':facepalm',
  90. ':like',
  91. ':dislike',
  92. '~friendica',
  93. 'red#',
  94. 'red#matrix'
  95. ];
  96. $baseUrl = DI::baseUrl();
  97. $icons = [
  98. '<img class="smiley" src="' . $baseUrl . '/images/smiley-heart.gif" alt="&lt;3" title="&lt;3" />',
  99. '<img class="smiley" src="' . $baseUrl . '/images/smiley-brokenheart.gif" alt="&lt;/3" title="&lt;/3" />',
  100. '<img class="smiley" src="' . $baseUrl . '/images/smiley-brokenheart.gif" alt="&lt;\\3" title="&lt;\\3" />',
  101. '<img class="smiley" src="' . $baseUrl . '/images/smiley-smile.gif" alt=":-)" title=":-)" />',
  102. '<img class="smiley" src="' . $baseUrl . '/images/smiley-wink.gif" alt=";-)" title=";-)" />',
  103. '<img class="smiley" src="' . $baseUrl . '/images/smiley-frown.gif" alt=":-(" title=":-(" />',
  104. '<img class="smiley" src="' . $baseUrl . '/images/smiley-tongue-out.gif" alt=":-P" title=":-P" />',
  105. '<img class="smiley" src="' . $baseUrl . '/images/smiley-tongue-out.gif" alt=":-p" title=":-P" />',
  106. '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
  107. '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
  108. '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-x" title=":-x" />',
  109. '<img class="smiley" src="' . $baseUrl . '/images/smiley-kiss.gif" alt=":-X" title=":-X" />',
  110. '<img class="smiley" src="' . $baseUrl . '/images/smiley-laughing.gif" alt=":-D" title=":-D" />',
  111. '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt="8-|" title="8-|" />',
  112. '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt="8-O" title="8-O" />',
  113. '<img class="smiley" src="' . $baseUrl . '/images/smiley-surprised.gif" alt=":-O" title="8-O" />',
  114. '<img class="smiley" src="' . $baseUrl . '/images/smiley-thumbsup.gif" alt="\\o/" title="\\o/" />',
  115. '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="o.O" title="o.O" />',
  116. '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="O.o" title="O.o" />',
  117. '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="o_O" title="o_O" />',
  118. '<img class="smiley" src="' . $baseUrl . '/images/smiley-Oo.gif" alt="O_o" title="O_o" />',
  119. '<img class="smiley" src="' . $baseUrl . '/images/smiley-cry.gif" alt=":\'(" title=":\'("/>',
  120. '<img class="smiley" src="' . $baseUrl . '/images/smiley-foot-in-mouth.gif" alt=":-!" title=":-!" />',
  121. '<img class="smiley" src="' . $baseUrl . '/images/smiley-undecided.gif" alt=":-/" title=":-/" />',
  122. '<img class="smiley" src="' . $baseUrl . '/images/smiley-embarassed.gif" alt=":-[" title=":-[" />',
  123. '<img class="smiley" src="' . $baseUrl . '/images/smiley-cool.gif" alt="8-)" title="8-)" />',
  124. '<img class="smiley" src="' . $baseUrl . '/images/beer_mug.gif" alt=":beer" title=":beer" />',
  125. '<img class="smiley" src="' . $baseUrl . '/images/beer_mug.gif" alt=":homebrew" title=":homebrew" />',
  126. '<img class="smiley" src="' . $baseUrl . '/images/coffee.gif" alt=":coffee" title=":coffee" />',
  127. '<img class="smiley" src="' . $baseUrl . '/images/smiley-facepalm.gif" alt=":facepalm" title=":facepalm" />',
  128. '<img class="smiley" src="' . $baseUrl . '/images/like.gif" alt=":like" title=":like" />',
  129. '<img class="smiley" src="' . $baseUrl . '/images/dislike.gif" alt=":dislike" title=":dislike" />',
  130. '<a href="https://friendi.ca">~friendica <img class="smiley" src="' . $baseUrl . '/images/friendica-16.png" alt="~friendica" title="~friendica" /></a>',
  131. '<a href="http://redmatrix.me/">red<img class="smiley" src="' . $baseUrl . '/images/rm-16.png" alt="red#" title="red#" />matrix</a>',
  132. '<a href="http://redmatrix.me/">red<img class="smiley" src="' . $baseUrl . '/images/rm-16.png" alt="red#matrix" title="red#matrix" />matrix</a>'
  133. ];
  134. $params = ['texts' => $texts, 'icons' => $icons];
  135. Hook::callAll('smilie', $params);
  136. return $params;
  137. }
  138. /**
  139. * Copied from http://php.net/manual/en/function.str-replace.php#88569
  140. * Modified for camel caps: renamed stro_replace -> strOrigReplace
  141. *
  142. * When using str_replace(...), values that did not exist in the original string (but were put there by previous
  143. * replacements) will be replaced continuously. This string replacement function is designed to replace the values
  144. * in $search with those in $replace while not factoring in prior replacements. Note that this function will
  145. * always look for the longest possible match first and then work its way down to individual characters.
  146. *
  147. * @param array $search list of strings or characters that need to be replaced
  148. * @param array $replace list of strings or characters that will replace the corresponding values in $search
  149. * @param string $subject the string on which this operation is being performed
  150. *
  151. * @return string $subject with all substrings in the $search array replaced by the values in the $replace array
  152. */
  153. private static function strOrigReplace($search, $replace, $subject)
  154. {
  155. return strtr($subject, array_combine($search, $replace));
  156. }
  157. /**
  158. * Replaces text emoticons with graphical images
  159. *
  160. * It is expected that this function will be called using HTML text.
  161. * We will escape text between HTML pre and code blocks from being
  162. * processed.
  163. *
  164. * At a higher level, the bbcode [nosmile] tag can be used to prevent this
  165. * function from being executed by the prepare_text() routine when preparing
  166. * bbcode source for HTML display
  167. *
  168. * @brief Replaces text emoticons with graphical images
  169. * @param string $s Text that should be replaced
  170. * @param boolean $no_images Only replace emoticons without images
  171. *
  172. * @return string HTML Output of the Smilie
  173. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  174. */
  175. public static function replace($s, $no_images = false)
  176. {
  177. $smilies = self::getList();
  178. $s = self::replaceFromArray($s, $smilies, $no_images);
  179. return $s;
  180. }
  181. /**
  182. * Replaces emoji shortcodes in a string from a structured array of searches and replaces.
  183. *
  184. * Depends on system.no_smilies config value, skips <pre> and <code> tags.
  185. *
  186. * @param string $text An HTML string
  187. * @param array $smilies An string replacement array with the following structure: ['texts' => [], 'icons' => []]
  188. * @param bool $no_images Only replace shortcodes without image replacement (e.g. Unicode characters)
  189. * @return string
  190. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  191. */
  192. public static function replaceFromArray($text, array $smilies, $no_images = false)
  193. {
  194. if (intval(Config::get('system', 'no_smilies'))
  195. || (local_user() && intval(DI::pConfig()->get(local_user(), 'system', 'no_smilies')))
  196. ) {
  197. return $text;
  198. }
  199. $text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', 'self::encode', $text);
  200. $text = preg_replace_callback('/<(code)>(.*?)<\/code>/ism', 'self::encode', $text);
  201. if ($no_images) {
  202. $cleaned = ['texts' => [], 'icons' => []];
  203. $icons = $smilies['icons'];
  204. foreach ($icons as $key => $icon) {
  205. if (!strstr($icon, '<img ')) {
  206. $cleaned['texts'][] = $smilies['texts'][$key];
  207. $cleaned['icons'][] = $smilies['icons'][$key];
  208. }
  209. }
  210. $smilies = $cleaned;
  211. }
  212. $text = preg_replace_callback('/&lt;(3+)/', 'self::pregHeart', $text);
  213. $text = self::strOrigReplace($smilies['texts'], $smilies['icons'], $text);
  214. $text = preg_replace_callback('/<(code)>(.*?)<\/code>/ism', 'self::decode', $text);
  215. $text = preg_replace_callback('/<(pre)>(.*?)<\/pre>/ism', 'self::decode', $text);
  216. return $text;
  217. }
  218. /**
  219. * @param string $m string
  220. *
  221. * @return string base64 encoded string
  222. */
  223. private static function encode($m)
  224. {
  225. return '<' . $m[1] . '>' . Strings::base64UrlEncode($m[2]) . '</' . $m[1] . '>';
  226. }
  227. /**
  228. * @param string $m string
  229. *
  230. * @return string base64 decoded string
  231. * @throws \Exception
  232. */
  233. private static function decode($m)
  234. {
  235. return '<' . $m[1] . '>' . Strings::base64UrlDecode($m[2]) . '</' . $m[1] . '>';
  236. }
  237. /**
  238. * @brief expand <3333 to the correct number of hearts
  239. *
  240. * @param string $x string
  241. *
  242. * @return string HTML Output
  243. *
  244. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  245. */
  246. private static function pregHeart($x)
  247. {
  248. if (strlen($x[1]) == 1) {
  249. return $x[0];
  250. }
  251. $t = '';
  252. for ($cnt = 0; $cnt < strlen($x[1]); $cnt ++) {
  253. $t .= '❤';
  254. }
  255. $r = str_replace($x[0], $t, $x[0]);
  256. return $r;
  257. }
  258. }