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.

132 lines
2.5KB

  1. <?php
  2. function convertquote($body, $reply)
  3. {
  4. // Convert Quotes
  5. $arrbody = explode("\n", trim($body));
  6. $arrlevel = array();
  7. for ($i = 0; $i < count($arrbody); $i++) {
  8. $quotelevel = 0;
  9. $quoteline = $arrbody[$i];
  10. while ((strlen($quoteline)>0) and ((substr($quoteline, 0, 1) == '>')
  11. || (substr($quoteline, 0, 1) == ' '))) {
  12. if (substr($quoteline, 0, 1) == '>')
  13. $quotelevel++;
  14. $quoteline = ltrim(substr($quoteline, 1));
  15. }
  16. //echo $quotelevel.'*'.$quoteline."\r\n";
  17. $arrlevel[$i] = $quotelevel;
  18. $arrbody[$i] = $quoteline;
  19. }
  20. $quotelevel = 0;
  21. $previousquote = 0;
  22. $arrbodyquoted = array();
  23. for ($i = 0; $i < count($arrbody); $i++) {
  24. $previousquote = $quotelevel;
  25. $quotelevel = $arrlevel[$i];
  26. $currline = $arrbody[$i];
  27. while ($previousquote < $quotelevel) {
  28. if ($sender != '') {
  29. $quote = "[quote title=$sender]";
  30. $sender = '';
  31. } else
  32. $quote = "[quote]";
  33. $arrbody[$i] = $quote.$arrbody[$i];
  34. $previousquote++;
  35. }
  36. while ($previousquote > $quotelevel) {
  37. $arrbody[$i] = '[/quote]'.$arrbody[$i];
  38. $previousquote--;
  39. }
  40. $arrbodyquoted[] = $arrbody[$i];
  41. }
  42. while ($quotelevel > 0) {
  43. $arrbodyquoted[] = '[/quote]';
  44. $quotelevel--;
  45. }
  46. $body = implode("\n", $arrbodyquoted);
  47. if (strlen($body) > 0)
  48. $body = $body."\n\n";
  49. if ($reply)
  50. $body = removetofu($body);
  51. return($body);
  52. }
  53. function removetofu($message)
  54. {
  55. $message = trim($message);
  56. do {
  57. $oldmessage = $message;
  58. $message = preg_replace('=\[/quote\][\s](.*?)\[quote\]=i', '$1', $message);
  59. $message = str_replace("[/quote][quote]", "", $message);
  60. } while ($message != $oldmessage);
  61. $quotes = array();
  62. $startquotes = 0;
  63. $start = 0;
  64. while (($pos = strpos($message, '[quote', $start)) > 0) {
  65. $quotes[$pos] = -1;
  66. $start = $pos + 7;
  67. $startquotes++;
  68. }
  69. $endquotes = 0;
  70. $start = 0;
  71. while (($pos = strpos($message, '[/quote]', $start)) > 0) {
  72. $start = $pos + 7;
  73. $endquotes++;
  74. }
  75. while ($endquotes < $startquotes) {
  76. $message .= '[/quote]';
  77. ++$endquotes;
  78. }
  79. $start = 0;
  80. while (($pos = strpos($message, '[/quote]', $start)) > 0) {
  81. $quotes[$pos] = 1;
  82. $start = $pos + 7;
  83. }
  84. if (strtolower(substr($message, -8)) != '[/quote]')
  85. return($message);
  86. krsort($quotes);
  87. $quotelevel = 0;
  88. $quotestart = 0;
  89. foreach ($quotes as $index => $quote) {
  90. $quotelevel += $quote;
  91. if (($quotelevel == 0) and ($quotestart == 0))
  92. $quotestart = $index;
  93. }
  94. if ($quotestart != 0) {
  95. $message = trim(substr($message, 0, $quotestart))."\n[spoiler]".substr($message, $quotestart+7, -8).'[/spoiler]';
  96. }
  97. return($message);
  98. }