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.

182 lines
7.1 KiB

  1. <?php
  2. /**
  3. * @file include/Smilies.php
  4. * @brief This file contains the Smilies class which contains functions to handle smiles
  5. */
  6. /**
  7. * This class contains functions to handle smiles
  8. */
  9. class Smilies {
  10. /**
  11. * @brief Function to list all smilies
  12. *
  13. * Get an array of all smilies, both internal and from addons.
  14. *
  15. * @return array
  16. * 'texts' => smilie shortcut
  17. * 'icons' => icon in html
  18. *
  19. * @hook smilie ('texts' => smilies texts array, 'icons' => smilies html array)
  20. */
  21. public static function get_list() {
  22. $texts = array(
  23. '&lt;3',
  24. '&lt;/3',
  25. '&lt;\\3',
  26. ':-)',
  27. ';-)',
  28. ':-(',
  29. ':-P',
  30. ':-p',
  31. ':-"',
  32. ':-&quot;',
  33. ':-x',
  34. ':-X',
  35. ':-D',
  36. '8-|',
  37. '8-O',
  38. ':-O',
  39. '\\o/',
  40. 'o.O',
  41. 'O.o',
  42. 'o_O',
  43. 'O_o',
  44. ":'(",
  45. ":-!",
  46. ":-/",
  47. ":-[",
  48. "8-)",
  49. ':beer',
  50. ':homebrew',
  51. ':coffee',
  52. ':facepalm',
  53. ':like',
  54. ':dislike',
  55. '~friendica',
  56. 'red#',
  57. 'red#matrix'
  58. );
  59. $icons = array(
  60. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-heart.gif" alt="&lt;3" title="&lt;3" />',
  61. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-brokenheart.gif" alt="&lt;/3" title="&lt;/3" />',
  62. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-brokenheart.gif" alt="&lt;\\3" title="&lt;\\3" />',
  63. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-smile.gif" alt=":-)" title=":-)" />',
  64. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-wink.gif" alt=";-)" title=";-)" />',
  65. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-frown.gif" alt=":-(" title=":-(" />',
  66. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-tongue-out.gif" alt=":-P" title=":-P" />',
  67. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-tongue-out.gif" alt=":-p" title=":-P" />',
  68. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
  69. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-kiss.gif" alt=":-\" title=":-\" />',
  70. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-kiss.gif" alt=":-x" title=":-x" />',
  71. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-kiss.gif" alt=":-X" title=":-X" />',
  72. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-laughing.gif" alt=":-D" title=":-D" />',
  73. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-surprised.gif" alt="8-|" title="8-|" />',
  74. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-surprised.gif" alt="8-O" title="8-O" />',
  75. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-surprised.gif" alt=":-O" title="8-O" />',
  76. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-thumbsup.gif" alt="\\o/" title="\\o/" />',
  77. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-Oo.gif" alt="o.O" title="o.O" />',
  78. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-Oo.gif" alt="O.o" title="O.o" />',
  79. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-Oo.gif" alt="o_O" title="o_O" />',
  80. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-Oo.gif" alt="O_o" title="O_o" />',
  81. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-cry.gif" alt=":\'(" title=":\'("/>',
  82. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-foot-in-mouth.gif" alt=":-!" title=":-!" />',
  83. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-undecided.gif" alt=":-/" title=":-/" />',
  84. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-embarassed.gif" alt=":-[" title=":-[" />',
  85. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-cool.gif" alt="8-)" title="8-)" />',
  86. '<img class="smiley" src="' . app::get_baseurl() . '/images/beer_mug.gif" alt=":beer" title=":beer" />',
  87. '<img class="smiley" src="' . app::get_baseurl() . '/images/beer_mug.gif" alt=":homebrew" title=":homebrew" />',
  88. '<img class="smiley" src="' . app::get_baseurl() . '/images/coffee.gif" alt=":coffee" title=":coffee" />',
  89. '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-facepalm.gif" alt=":facepalm" title=":facepalm" />',
  90. '<img class="smiley" src="' . app::get_baseurl() . '/images/like.gif" alt=":like" title=":like" />',
  91. '<img class="smiley" src="' . app::get_baseurl() . '/images/dislike.gif" alt=":dislike" title=":dislike" />',
  92. '<a href="http://friendica.com">~friendica <img class="smiley" src="' . app::get_baseurl() . '/images/friendica-16.png" alt="~friendica" title="~friendica" /></a>',
  93. '<a href="http://redmatrix.me/">red<img class="smiley" src="' . app::get_baseurl() . '/images/rm-16.png" alt="red#" title="red#" />matrix</a>',
  94. '<a href="http://redmatrix.me/">red<img class="smiley" src="' . app::get_baseurl() . '/images/rm-16.png" alt="red#matrix" title="red#matrix" />matrix</a>'
  95. );
  96. $params = array('texts' => $texts, 'icons' => $icons);
  97. call_hooks('smilie', $params);
  98. return $params;
  99. }
  100. /**
  101. * @brief Replaces text emoticons with graphical images
  102. *
  103. * It is expected that this function will be called using HTML text.
  104. * We will escape text between HTML pre and code blocks from being
  105. * processed.
  106. *
  107. * At a higher level, the bbcode [nosmile] tag can be used to prevent this
  108. * function from being executed by the prepare_text() routine when preparing
  109. * bbcode source for HTML display
  110. *
  111. * @param string $s
  112. * @param boolean $sample
  113. *
  114. * @return string HML Output of the Smilie
  115. */
  116. public static function replace($s, $sample = false) {
  117. if(intval(get_config('system','no_smilies'))
  118. || (local_user() && intval(get_pconfig(local_user(),'system','no_smilies'))))
  119. return $s;
  120. $s = preg_replace_callback('/<pre>(.*?)<\/pre>/ism','self::encode',$s);
  121. $s = preg_replace_callback('/<code>(.*?)<\/code>/ism','self::encode',$s);
  122. $params = self::get_list();
  123. $params['string'] = $s;
  124. if($sample) {
  125. $s = '<div class="smiley-sample">';
  126. for($x = 0; $x < count($params['texts']); $x ++) {
  127. $s .= '<dl><dt>' . $params['texts'][$x] . '</dt><dd>' . $params['icons'][$x] . '</dd></dl>';
  128. }
  129. }
  130. else {
  131. $params['string'] = preg_replace_callback('/&lt;(3+)/','self::preg_heart',$params['string']);
  132. $s = str_replace($params['texts'],$params['icons'],$params['string']);
  133. }
  134. $s = preg_replace_callback('/<pre>(.*?)<\/pre>/ism','self::decode',$s);
  135. $s = preg_replace_callback('/<code>(.*?)<\/code>/ism','self::decode',$s);
  136. return $s;
  137. }
  138. private function encode($m) {
  139. return(str_replace($m[1],base64url_encode($m[1]),$m[0]));
  140. }
  141. private function decode($m) {
  142. return(str_replace($m[1],base64url_decode($m[1]),$m[0]));
  143. }
  144. /**
  145. * @brief expand <3333 to the correct number of hearts
  146. *
  147. * @param string $x
  148. * @return string HTML Output
  149. *
  150. * @todo: Rework because it doesn't work correctly
  151. */
  152. private function preg_heart($x) {
  153. if(strlen($x[1]) == 1)
  154. return $x[0];
  155. $t = '';
  156. for($cnt = 0; $cnt < strlen($x[1]); $cnt ++)
  157. $t .= '<img class="smiley" src="' . app::get_baseurl() . '/images/smiley-heart.gif" alt="&lt;3" />';
  158. $r = str_replace($x[0],$t,$x[0]);
  159. return $r;
  160. }
  161. }