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.

173 lines
5.4KB

  1. <?php
  2. /**
  3. * @file src/Model/ItemContent.php
  4. */
  5. namespace Friendica\Model;
  6. use Friendica\BaseObject;
  7. use Friendica\Content\Text;
  8. use Friendica\Core\PConfig;
  9. use Friendica\Core\Protocol;
  10. class ItemContent extends BaseObject
  11. {
  12. /**
  13. * @brief Convert a message into plaintext for connectors to other networks
  14. *
  15. * @param array $item The message array that is about to be posted
  16. * @param int $limit The maximum number of characters when posting to that network
  17. * @param bool $includedlinks Has an attached link to be included into the message?
  18. * @param int $htmlmode This controls the behavior of the BBCode conversion
  19. * @param string $target_network Name of the network where the post should go to.
  20. *
  21. * @see \Friendica\Content\Text\BBCode::getAttachedData
  22. *
  23. * @return array Same array structure than \Friendica\Content\Text\BBCode::getAttachedData
  24. */
  25. public static function getPlaintextPost($item, $limit = 0, $includedlinks = false, $htmlmode = 2, $target_network = '')
  26. {
  27. // Remove hashtags
  28. $URLSearchString = '^\[\]';
  29. $body = preg_replace("/([#@])\[url\=([$URLSearchString]*)\](.*?)\[\/url\]/ism", '$1$3', $item['body']);
  30. // Add an URL element if the text contains a raw link
  31. $body = preg_replace('/([^\]\=\'"]|^)(https?\:\/\/[a-zA-Z0-9\:\/\-\?\&\;\.\=\_\~\#\%\$\!\+\,]+)/ism',
  32. '$1[url]$2[/url]', $body);
  33. // Remove the abstract
  34. $body = Text\BBCode::stripAbstract($body);
  35. // At first look at data that is attached via "type-..." stuff
  36. // This will hopefully replaced with a dedicated bbcode later
  37. //$post = self::getAttachedData($b['body']);
  38. $post = Text\BBCode::getAttachedData($body, $item);
  39. if (($item['title'] != '') && ($post['text'] != '')) {
  40. $post['text'] = trim($item['title'] . "\n\n" . $post['text']);
  41. } elseif ($item['title'] != '') {
  42. $post['text'] = trim($item['title']);
  43. }
  44. $abstract = '';
  45. // Fetch the abstract from the given target network
  46. if ($target_network != '') {
  47. $default_abstract = Text\BBCode::getAbstract($item['body']);
  48. $abstract = Text\BBCode::getAbstract($item['body'], $target_network);
  49. // If we post to a network with no limit we only fetch
  50. // an abstract exactly for this network
  51. if (($limit == 0) && ($abstract == $default_abstract)) {
  52. $abstract = '';
  53. }
  54. } else {// Try to guess the correct target network
  55. switch ($htmlmode) {
  56. case 8:
  57. $abstract = Text\BBCode::getAbstract($item['body'], Protocol::TWITTER);
  58. break;
  59. case 7:
  60. $abstract = Text\BBCode::getAbstract($item['body'], Protocol::STATUSNET);
  61. break;
  62. default: // We don't know the exact target.
  63. // We fetch an abstract since there is a posting limit.
  64. if ($limit > 0) {
  65. $abstract = Text\BBCode::getAbstract($item['body']);
  66. }
  67. }
  68. }
  69. if ($abstract != '') {
  70. $post['text'] = $abstract;
  71. if ($post['type'] == 'text') {
  72. $post['type'] = 'link';
  73. $post['url'] = $item['plink'];
  74. }
  75. }
  76. $html = Text\BBCode::convert($post['text'] . defaults($post, 'after', ''), false, $htmlmode);
  77. $msg = Text\HTML::toPlaintext($html, 0, true);
  78. $msg = trim(html_entity_decode($msg, ENT_QUOTES, 'UTF-8'));
  79. $link = '';
  80. if ($includedlinks) {
  81. if ($post['type'] == 'link') {
  82. $link = $post['url'];
  83. } elseif ($post['type'] == 'text') {
  84. $link = defaults($post, 'url', '');
  85. } elseif ($post['type'] == 'video') {
  86. $link = $post['url'];
  87. } elseif ($post['type'] == 'photo') {
  88. $link = $post['image'];
  89. }
  90. if (($msg == '') && isset($post['title'])) {
  91. $msg = trim($post['title']);
  92. }
  93. if (($msg == '') && isset($post['description'])) {
  94. $msg = trim($post['description']);
  95. }
  96. // If the link is already contained in the post, then it neeedn't to be added again
  97. // But: if the link is beyond the limit, then it has to be added.
  98. if (($link != '') && strstr($msg, $link)) {
  99. $pos = strpos($msg, $link);
  100. // Will the text be shortened in the link?
  101. // Or is the link the last item in the post?
  102. if (($limit > 0) && ($pos < $limit) && (($pos + 23 > $limit) || ($pos + strlen($link) == strlen($msg)))) {
  103. $msg = trim(str_replace($link, '', $msg));
  104. } elseif (($limit == 0) || ($pos < $limit)) {
  105. // The limit has to be increased since it will be shortened - but not now
  106. // Only do it with Twitter (htmlmode = 8)
  107. if (($limit > 0) && (strlen($link) > 23) && ($htmlmode == 8)) {
  108. $limit = $limit - 23 + strlen($link);
  109. }
  110. $link = '';
  111. if ($post['type'] == 'text') {
  112. unset($post['url']);
  113. }
  114. }
  115. }
  116. }
  117. if ($limit > 0) {
  118. // Reduce multiple spaces
  119. // When posted to a network with limited space, we try to gain space where possible
  120. while (strpos($msg, ' ') !== false) {
  121. $msg = str_replace(' ', ' ', $msg);
  122. }
  123. // Twitter is using its own limiter, so we always assume that shortened links will have this length
  124. if (iconv_strlen($link, 'UTF-8') > 0) {
  125. $limit = $limit - 23;
  126. }
  127. if (iconv_strlen($msg, 'UTF-8') > $limit) {
  128. if (($post['type'] == 'text') && isset($post['url'])) {
  129. $post['url'] = $item['plink'];
  130. } elseif (!isset($post['url'])) {
  131. $limit = $limit - 23;
  132. $post['url'] = $item['plink'];
  133. } elseif (strpos($item['body'], '[share') !== false) {
  134. $post['url'] = $item['plink'];
  135. } elseif (PConfig::get($item['uid'], 'system', 'no_intelligent_shortening')) {
  136. $post['url'] = $item['plink'];
  137. }
  138. $msg = Text\Plaintext::shorten($msg, $limit);
  139. }
  140. }
  141. $post['text'] = trim($msg);
  142. return $post;
  143. }
  144. }