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.

rcube_enriched.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | This file is part of the Roundcube Webmail client |
  5. | Copyright (C) 2005-2012, The Roundcube Dev Team |
  6. | |
  7. | Licensed under the GNU General Public License version 3 or |
  8. | any later version with exceptions for skins & plugins. |
  9. | See the README file for a full license statement. |
  10. | |
  11. | PURPOSE: |
  12. | Helper class to convert Enriched to HTML format (RFC 1523, 1896) |
  13. +-----------------------------------------------------------------------+
  14. | Author: Aleksander Machniak <alec@alec.pl> |
  15. | Author: Ryo Chijiiwa (IlohaMail) |
  16. +-----------------------------------------------------------------------+
  17. */
  18. /**
  19. * Class for Enriched to HTML conversion
  20. *
  21. * @package Framework
  22. * @subpackage Utils
  23. */
  24. class rcube_enriched
  25. {
  26. protected static function convert_newlines($body)
  27. {
  28. // remove single newlines, convert N newlines to N-1
  29. $body = str_replace("\r\n", "\n", $body);
  30. $len = strlen($body);
  31. $nl = 0;
  32. $out = '';
  33. for ($i=0; $i<$len; $i++) {
  34. $c = $body[$i];
  35. if (ord($c) == 10)
  36. $nl++;
  37. if ($nl && ord($c) != 10)
  38. $nl = 0;
  39. if ($nl != 1)
  40. $out .= $c;
  41. else
  42. $out .= ' ';
  43. }
  44. return $out;
  45. }
  46. protected static function convert_formatting($body)
  47. {
  48. $replace = array(
  49. '<bold>' => '<b>', '</bold>' => '</b>',
  50. '<italic>' => '<i>', '</italic>' => '</i>',
  51. '<fixed>' => '<tt>', '</fixed>' => '</tt>',
  52. '<smaller>' => '<font size=-1>', '</smaller>'=> '</font>',
  53. '<bigger>' => '<font size=+1>', '</bigger>' => '</font>',
  54. '<underline>' => '<span style="text-decoration: underline">', '</underline>' => '</span>',
  55. '<flushleft>' => '<span style="text-align: left">', '</flushleft>' => '</span>',
  56. '<flushright>' => '<span style="text-align: right">', '</flushright>' => '</span>',
  57. '<flushboth>' => '<span style="text-align: justified">', '</flushboth>' => '</span>',
  58. '<indent>' => '<span style="padding-left: 20px">', '</indent>' => '</span>',
  59. '<indentright>' => '<span style="padding-right: 20px">', '</indentright>' => '</span>',
  60. );
  61. return str_ireplace(array_keys($replace), array_values($replace), $body);
  62. }
  63. protected static function convert_font($body)
  64. {
  65. $pattern = '/(.*)\<fontfamily\>\<param\>(.*)\<\/param\>(.*)\<\/fontfamily\>(.*)/ims';
  66. while (preg_match($pattern, $body, $a)) {
  67. if (count($a) != 5)
  68. continue;
  69. $body = $a[1].'<span style="font-family: '.$a[2].'">'.$a[3].'</span>'.$a[4];
  70. }
  71. return $body;
  72. }
  73. protected static function convert_color($body)
  74. {
  75. $pattern = '/(.*)\<color\>\<param\>(.*)\<\/param\>(.*)\<\/color\>(.*)/ims';
  76. while (preg_match($pattern, $body, $a)) {
  77. if (count($a) != 5)
  78. continue;
  79. // extract color (either by name, or ####,####,####)
  80. if (strpos($a[2],',')) {
  81. $rgb = explode(',',$a[2]);
  82. $color = '#';
  83. for ($i=0; $i<3; $i++)
  84. $color .= substr($rgb[$i], 0, 2); // just take first 2 bytes
  85. }
  86. else {
  87. $color = $a[2];
  88. }
  89. // put it all together
  90. $body = $a[1].'<span style="color: '.$color.'">'.$a[3].'</span>'.$a[4];
  91. }
  92. return $body;
  93. }
  94. protected static function convert_excerpt($body)
  95. {
  96. $pattern = '/(.*)\<excerpt\>(.*)\<\/excerpt\>(.*)/i';
  97. while (preg_match($pattern, $body, $a)) {
  98. if (count($a) != 4)
  99. continue;
  100. $quoted = '';
  101. $lines = explode('<br>', $a[2]);
  102. foreach ($lines as $line)
  103. $quoted .= '&gt;'.$line.'<br>';
  104. $body = $a[1].'<span class="quotes">'.$quoted.'</span>'.$a[3];
  105. }
  106. return $body;
  107. }
  108. /**
  109. * Converts Enriched text into HTML format
  110. *
  111. * @param string $body Enriched text
  112. *
  113. * @return string HTML text
  114. */
  115. public static function to_html($body)
  116. {
  117. $body = str_replace('<<','&lt;',$body);
  118. $body = self::convert_newlines($body);
  119. $body = str_replace("\n", '<br>', $body);
  120. $body = self::convert_formatting($body);
  121. $body = self::convert_color($body);
  122. $body = self::convert_font($body);
  123. $body = self::convert_excerpt($body);
  124. //$body = nl2br($body);
  125. return $body;
  126. }
  127. }