Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

250 líneas
8.8 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\App;
  16. use Friendica\Core\Config;
  17. use Friendica\Core\PConfig;
  18. use Friendica\Core\System;
  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. * @hook smilie ('texts' => smilies texts array, 'icons' => smilies html array)
  55. */
  56. public static function getList()
  57. {
  58. $texts = array(
  59. '&lt;3',
  60. '&lt;/3',
  61. '&lt;\\3',
  62. ':-)',
  63. ';-)',
  64. ':-(',
  65. ':-P',
  66. ':-p',
  67. ':-"',
  68. ':-&quot;',
  69. ':-x',
  70. ':-X',
  71. ':-D',
  72. '8-|',
  73. '8-O',
  74. ':-O',
  75. '\\o/',
  76. 'o.O',
  77. 'O.o',
  78. 'o_O',
  79. 'O_o',
  80. ":'(",
  81. ":-!",
  82. ":-/",
  83. ":-[",
  84. "8-)",
  85. ':beer',
  86. ':homebrew',
  87. ':coffee',
  88. ':facepalm',
  89. ':like',
  90. ':dislike',
  91. '~friendica',
  92. 'red#',
  93. 'red#matrix'
  94. );
  95. $icons = array(
  96. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-heart.gif" alt="&lt;3" title="&lt;3" />',
  97. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-brokenheart.gif" alt="&lt;/3" title="&lt;/3" />',
  98. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-brokenheart.gif" alt="&lt;\\3" title="&lt;\\3" />',
  99. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-smile.gif" alt=":-)" title=":-)" />',
  100. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-wink.gif" alt=";-)" title=";-)" />',
  101. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-frown.gif" alt=":-(" title=":-(" />',
  102. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-tongue-out.gif" alt=":-P" title=":-P" />',
  103. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-tongue-out.gif" alt=":-p" title=":-P" />',
  104. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
  105. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
  106. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-x" title=":-x" />',
  107. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-kiss.gif" alt=":-X" title=":-X" />',
  108. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-laughing.gif" alt=":-D" title=":-D" />',
  109. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt="8-|" title="8-|" />',
  110. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt="8-O" title="8-O" />',
  111. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-surprised.gif" alt=":-O" title="8-O" />',
  112. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-thumbsup.gif" alt="\\o/" title="\\o/" />',
  113. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="o.O" title="o.O" />',
  114. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="O.o" title="O.o" />',
  115. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="o_O" title="o_O" />',
  116. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-Oo.gif" alt="O_o" title="O_o" />',
  117. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-cry.gif" alt=":\'(" title=":\'("/>',
  118. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-foot-in-mouth.gif" alt=":-!" title=":-!" />',
  119. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-undecided.gif" alt=":-/" title=":-/" />',
  120. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-embarassed.gif" alt=":-[" title=":-[" />',
  121. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-cool.gif" alt="8-)" title="8-)" />',
  122. '<img class="smiley" src="' . System::baseUrl() . '/images/beer_mug.gif" alt=":beer" title=":beer" />',
  123. '<img class="smiley" src="' . System::baseUrl() . '/images/beer_mug.gif" alt=":homebrew" title=":homebrew" />',
  124. '<img class="smiley" src="' . System::baseUrl() . '/images/coffee.gif" alt=":coffee" title=":coffee" />',
  125. '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-facepalm.gif" alt=":facepalm" title=":facepalm" />',
  126. '<img class="smiley" src="' . System::baseUrl() . '/images/like.gif" alt=":like" title=":like" />',
  127. '<img class="smiley" src="' . System::baseUrl() . '/images/dislike.gif" alt=":dislike" title=":dislike" />',
  128. '<a href="https://friendi.ca">~friendica <img class="smiley" src="' . System::baseUrl() . '/images/friendica-16.png" alt="~friendica" title="~friendica" /></a>',
  129. '<a href="http://redmatrix.me/">red<img class="smiley" src="' . System::baseUrl() . '/images/rm-16.png" alt="red#" title="red#" />matrix</a>',
  130. '<a href="http://redmatrix.me/">red<img class="smiley" src="' . System::baseUrl() . '/images/rm-16.png" alt="red#matrix" title="red#matrix" />matrix</a>'
  131. );
  132. $params = array('texts' => $texts, 'icons' => $icons);
  133. call_hooks('smilie', $params);
  134. return $params;
  135. }
  136. /**
  137. * @brief Replaces text emoticons with graphical images
  138. *
  139. * It is expected that this function will be called using HTML text.
  140. * We will escape text between HTML pre and code blocks from being
  141. * processed.
  142. *
  143. * At a higher level, the bbcode [nosmile] tag can be used to prevent this
  144. * function from being executed by the prepare_text() routine when preparing
  145. * bbcode source for HTML display
  146. *
  147. * @param string $s Text that should be replaced
  148. * @param boolean $sample optional, default false
  149. * @param boolean $no_images Only replace emoticons without images
  150. *
  151. * @return string HML Output of the Smilie
  152. */
  153. public static function replace($s, $sample = false, $no_images = false)
  154. {
  155. if (intval(Config::get('system', 'no_smilies'))
  156. || (local_user() && intval(PConfig::get(local_user(), 'system', 'no_smilies')))
  157. ) {
  158. return $s;
  159. }
  160. $s = preg_replace_callback('/<pre>(.*?)<\/pre>/ism', 'self::encode', $s);
  161. $s = preg_replace_callback('/<code>(.*?)<\/code>/ism', 'self::encode', $s);
  162. $params = self::getList();
  163. if ($no_images) {
  164. $cleaned = array('texts' => array(), 'icons' => array());
  165. $icons = $params['icons'];
  166. foreach ($icons as $key => $icon) {
  167. if (!strstr($icon, '<img ')) {
  168. $cleaned['texts'][] = $params['texts'][$key];
  169. $cleaned['icons'][] = $params['icons'][$key];
  170. }
  171. }
  172. $params = $cleaned;
  173. }
  174. $params['string'] = $s;
  175. if ($sample) {
  176. $s = '<div class="smiley-sample">';
  177. for ($x = 0; $x < count($params['texts']); $x ++) {
  178. $s .= '<dl><dt>' . $params['texts'][$x] . '</dt><dd>' . $params['icons'][$x] . '</dd></dl>';
  179. }
  180. } else {
  181. $params['string'] = preg_replace_callback('/&lt;(3+)/', 'self::pregHeart', $params['string']);
  182. $s = str_replace($params['texts'], $params['icons'], $params['string']);
  183. }
  184. $s = preg_replace_callback('/<pre>(.*?)<\/pre>/ism', 'self::decode', $s);
  185. $s = preg_replace_callback('/<code>(.*?)<\/code>/ism', 'self::decode', $s);
  186. return $s;
  187. }
  188. /**
  189. * @param string $m string
  190. *
  191. * @return string base64 encoded string
  192. */
  193. private static function encode($m)
  194. {
  195. return(str_replace($m[1], base64url_encode($m[1]), $m[0]));
  196. }
  197. /**
  198. * @param string $m string
  199. *
  200. * @return string base64 decoded string
  201. */
  202. private static function decode($m)
  203. {
  204. return(str_replace($m[1], base64url_decode($m[1]), $m[0]));
  205. }
  206. /**
  207. * @brief expand <3333 to the correct number of hearts
  208. *
  209. * @param string $x string
  210. *
  211. * @return string HTML Output
  212. *
  213. * @todo: Rework because it doesn't work correctly
  214. */
  215. private static function pregHeart($x)
  216. {
  217. if (strlen($x[1]) == 1) {
  218. return $x[0];
  219. }
  220. $t = '';
  221. for ($cnt = 0; $cnt < strlen($x[1]); $cnt ++) {
  222. $t .= '<img class="smiley" src="' . System::baseUrl() . '/images/smiley-heart.gif" alt="&lt;3" />';
  223. }
  224. $r = str_replace($x[0], $t, $x[0]);
  225. return $r;
  226. }
  227. }