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.

227 lines
8.5 KiB

4 years ago
  1. <?php
  2. /**
  3. * @file include/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. use Friendica\App;
  15. use Friendica\Core\Config;
  16. use Friendica\Core\PConfig;
  17. use Friendica\Core\System;
  18. /**
  19. * This class contains functions to handle smiles
  20. */
  21. class Smilies {
  22. /**
  23. * @brief Replaces/adds the emoticon list
  24. *
  25. * This function should be used whenever emoticons are added
  26. *
  27. * @param array $b Array of emoticons
  28. * @param string $smiley The text smilie
  29. * @param string $representation The replacement
  30. */
  31. public static function add(&$b, $smiley, $representation) {
  32. $found = array_search($smiley, $b['texts']);
  33. if (!is_int($found)) {
  34. $b['texts'][] = $smiley;
  35. $b['icons'][] = $representation;
  36. } else {
  37. $b['icons'][$found] = $representation;
  38. }
  39. }
  40. /**
  41. * @brief Function to list all smilies
  42. *
  43. * Get an array of all smilies, both internal and from addons.
  44. *
  45. * @return array
  46. * 'texts' => smilie shortcut
  47. * 'icons' => icon in html
  48. *
  49. * @hook smilie ('texts' => smilies texts array, 'icons' => smilies html array)
  50. */
  51. public static function get_list() {
  52. $texts = array(
  53. '&lt;3',
  54. '&lt;/3',
  55. '&lt;\\3',
  56. ':-)',
  57. ';-)',
  58. ':-(',
  59. ':-P',
  60. ':-p',
  61. ':-"',
  62. ':-&quot;',
  63. ':-x',
  64. ':-X',
  65. ':-D',
  66. '8-|',
  67. '8-O',
  68. ':-O',
  69. '\\o/',
  70. 'o.O',
  71. 'O.o',
  72. 'o_O',
  73. 'O_o',
  74. ":'(",
  75. ":-!",
  76. ":-/",
  77. ":-[",
  78. "8-)",
  79. ':beer',
  80. ':homebrew',
  81. ':coffee',
  82. ':facepalm',
  83. ':like',
  84. ':dislike',
  85. '~friendica',
  86. 'red#',
  87. 'red#matrix'
  88. );
  89. $icons = array(
  90. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-heart.gif" alt="&lt;3" title="&lt;3" />',
  91. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-brokenheart.gif" alt="&lt;/3" title="&lt;/3" />',
  92. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-brokenheart.gif" alt="&lt;\\3" title="&lt;\\3" />',
  93. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-smile.gif" alt=":-)" title=":-)" />',
  94. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-wink.gif" alt=";-)" title=";-)" />',
  95. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-frown.gif" alt=":-(" title=":-(" />',
  96. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-tongue-out.gif" alt=":-P" title=":-P" />',
  97. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-tongue-out.gif" alt=":-p" title=":-P" />',
  98. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
  99. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
  100. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-x" title=":-x" />',
  101. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-X" title=":-X" />',
  102. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-laughing.gif" alt=":-D" title=":-D" />',
  103. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt="8-|" title="8-|" />',
  104. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt="8-O" title="8-O" />',
  105. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt=":-O" title="8-O" />',
  106. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-thumbsup.gif" alt="\\o/" title="\\o/" />',
  107. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="o.O" title="o.O" />',
  108. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="O.o" title="O.o" />',
  109. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="o_O" title="o_O" />',
  110. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="O_o" title="O_o" />',
  111. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-cry.gif" alt=":\'(" title=":\'("/>',
  112. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-foot-in-mouth.gif" alt=":-!" title=":-!" />',
  113. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-undecided.gif" alt=":-/" title=":-/" />',
  114. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-embarassed.gif" alt=":-[" title=":-[" />',
  115. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-cool.gif" alt="8-)" title="8-)" />',
  116. '<img class="smiley" src="' . System::baseUrl() . '/images/beer_mug.gif" alt=":beer" title=":beer" />',
  117. '<img class="smiley" src="' . System::baseUrl() . '/images/beer_mug.gif" alt=":homebrew" title=":homebrew" />',
  118. '<img class="smiley" src="' . System::baseUrl() . '/images/coffee.gif" alt=":coffee" title=":coffee" />',
  119. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-facepalm.gif" alt=":facepalm" title=":facepalm" />',
  120. '<img class="smiley" src="' . System::baseUrl() . '/images/like.gif" alt=":like" title=":like" />',
  121. '<img class="smiley" src="' . System::baseUrl() . '/images/dislike.gif" alt=":dislike" title=":dislike" />',
  122. '<a href="https://friendi.ca">~friendica <img class="smiley" src="' . System::baseUrl() . '/images/friendica-16.png" alt="~friendica" title="~friendica" /></a>',
  123. '<a href="http://redmatrix.me/">red<img class="smiley" src="' . System::baseUrl() . '/images/rm-16.png" alt="red#" title="red#" />matrix</a>',
  124. '<a href="http://redmatrix.me/">red<img class="smiley" src="' . System::baseUrl() . '/images/rm-16.png" alt="red#matrix" title="red#matrix" />matrix</a>'
  125. );
  126. $params = array('texts' => $texts, 'icons' => $icons);
  127. call_hooks('smilie', $params);
  128. return $params;
  129. }
  130. /**
  131. * @brief Replaces text emoticons with graphical images
  132. *
  133. * It is expected that this function will be called using HTML text.
  134. * We will escape text between HTML pre and code blocks from being
  135. * processed.
  136. *
  137. * At a higher level, the bbcode [nosmile] tag can be used to prevent this
  138. * function from being executed by the prepare_text() routine when preparing
  139. * bbcode source for HTML display
  140. *
  141. * @param string $s Text that should be replaced
  142. * @param boolean $sample
  143. * @param boolean $no_images Only replace emoticons without images
  144. *
  145. * @return string HML Output of the Smilie
  146. */
  147. public static function replace($s, $sample = false, $no_images = false) {
  148. if(intval(Config::get('system','no_smilies'))
  149. || (local_user() && intval(PConfig::get(local_user(),'system','no_smilies'))))
  150. return $s;
  151. $s = preg_replace_callback('/<pre>(.*?)<\/pre>/ism','self::encode',$s);
  152. $s = preg_replace_callback('/<code>(.*?)<\/code>/ism','self::encode',$s);
  153. $params = self::get_list();
  154. if ($no_images) {
  155. $cleaned = array('texts' => array(), 'icons' => array());
  156. $icons = $params['icons'];
  157. foreach ($icons AS $key => $icon) {
  158. if (!strstr($icon, '<img ')) {
  159. $cleaned['texts'][] = $params['texts'][$key];
  160. $cleaned['icons'][] = $params['icons'][$key];
  161. }
  162. }
  163. $params = $cleaned;
  164. }
  165. $params['string'] = $s;
  166. if($sample) {
  167. $s = '<div class="smiley-sample">';
  168. for($x = 0; $x < count($params['texts']); $x ++) {
  169. $s .= '<dl><dt>' . $params['texts'][$x] . '</dt><dd>' . $params['icons'][$x] . '</dd></dl>';
  170. }
  171. }
  172. else {
  173. $params['string'] = preg_replace_callback('/&lt;(3+)/','self::preg_heart',$params['string']);
  174. $s = str_replace($params['texts'],$params['icons'],$params['string']);
  175. }
  176. $s = preg_replace_callback('/<pre>(.*?)<\/pre>/ism','self::decode',$s);
  177. $s = preg_replace_callback('/<code>(.*?)<\/code>/ism','self::decode',$s);
  178. return $s;
  179. }
  180. private static function encode($m) {
  181. return(str_replace($m[1],base64url_encode($m[1]),$m[0]));
  182. }
  183. private static function decode($m) {
  184. return(str_replace($m[1],base64url_decode($m[1]),$m[0]));
  185. }
  186. /**
  187. * @brief expand <3333 to the correct number of hearts
  188. *
  189. * @param string $x
  190. * @return string HTML Output
  191. *
  192. * @todo: Rework because it doesn't work correctly
  193. */
  194. private static function preg_heart($x) {
  195. if(strlen($x[1]) == 1)
  196. return $x[0];
  197. $t = '';
  198. for($cnt = 0; $cnt < strlen($x[1]); $cnt ++)
  199. $t .= '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-heart.gif" alt="&lt;3" />';
  200. $r = str_replace($x[0],$t,$x[0]);
  201. return $r;
  202. }
  203. }