PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/htmlarea/plugins/SpellChecker/spell-check-logic.php

http://github.com/s9y/Serendipity
PHP | 172 lines | 139 code | 18 blank | 15 comment | 10 complexity | 7e41f549b83bfa4ac6bbb6bd1933f3fc MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. <?php
  2. die('disabled in s9y');
  3. $text = stripslashes($_POST['content']);
  4. // Convert UTF-8 multi-bytes into decimal character entities. This is because
  5. // aspell isn't fully utf8-aware - ticket:120 raises the possibility
  6. // that this is not required (any more) and so you can turn it off
  7. // with editor.config.SpellChecker.utf8_to_entities = false
  8. if(!isset($_REQUEST['utf8_to_entitis']) || $_REQUEST['utf8_to_entities'])
  9. {
  10. $text = preg_replace('/([\xC0-\xDF][\x80-\xBF])/e', "'&#' . utf8_ord('\$1') . ';'", $text);
  11. $text = preg_replace('/([\xE0-\xEF][\x80-\xBF][\x80-\xBF])/e', "'&#' . utf8_ord('\$1') . ';'", $text);
  12. $text = preg_replace('/([\xF0-\xF7][\x80-\xBF][\x80-\xBF][\x80-\xBF])/e', "'&#' . utf8_ord('\$1') . ';'", $text);
  13. }
  14. function utf8_ord($chr)
  15. {
  16. switch(strlen($chr))
  17. {
  18. case 1 :
  19. return ord($chr);
  20. case 2 :
  21. $ord = ord($chr{1}) & 63;
  22. $ord = $ord | ((ord($chr{0}) & 31) << 6);
  23. return $ord;
  24. case 3 :
  25. $ord = ord($chr{2}) & 63;
  26. $ord = $ord | ((ord($chr{1}) & 63) << 6);
  27. $ord = $ord | ((ord($chr{0}) & 15) << 12);
  28. return $ord;
  29. case 4 :
  30. $ord = ord($chr{3}) & 63;
  31. $ord = $ord | ((ord($chr{2}) & 63) << 6);
  32. $ord = $ord | ((ord($chr{1}) & 63) << 12);
  33. $ord = $ord | ((ord($chr{0}) & 7) << 18);
  34. return $ord;
  35. default :
  36. trigger_error('Character not utf-8', E_USER_ERROR);
  37. }
  38. }
  39. require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'aspell_setup.php');
  40. ##############################################################################
  41. header('Content-Type: text/html; charset=utf-8');
  42. echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  43. <html>
  44. <head>
  45. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  46. <link rel="stylesheet" type="text/css" media="all" href="spell-check-style.css" />';
  47. // Lets define some values outside the condition below, in case we have an empty
  48. // document.
  49. $textarray = array();
  50. $varlines = '<script type="text/javascript">var suggested_words = { ';
  51. $infolines = 'var spellcheck_info = {';
  52. $counter = 0;
  53. $suggest_count = 0;
  54. if (trim($text) != "")
  55. {
  56. if ($fd = fopen($temptext, 'w'))
  57. {
  58. $textarray = explode("\n", $text);
  59. fwrite ($fd, "!\n");
  60. foreach ($textarray as $key=>$value)
  61. {
  62. // adding the carat to each line prevents the use of aspell commands within the text...
  63. fwrite($fd, "^$value\n");
  64. }
  65. fclose($fd);
  66. chmod($temptext, 0777);
  67. // next run aspell
  68. $return = shell_exec($aspellcommand . ' 2>&1');
  69. // echo $return;
  70. unlink($temptext);
  71. $returnarray = explode("\n", $return);
  72. $returnlines = count($returnarray);
  73. //print_r(htmlentities($return));
  74. $textlines = count($textarray);
  75. $lineindex = -1;
  76. $poscorrect = 0;
  77. foreach ($returnarray as $key=>$value)
  78. {
  79. // if there is a correction here, processes it, else move the $textarray pointer to the next line
  80. if (substr($value, 0, 1) == '&')
  81. {
  82. $counter=$counter+1;
  83. $correction = explode(' ', $value);
  84. $word = $correction[1];
  85. $suggest_count += $correction[2];
  86. $absposition = substr($correction[3], 0, -1) - 1;
  87. $position = $absposition + $poscorrect;
  88. $niceposition = $lineindex.','.$absposition;
  89. $suggstart = strpos($value, ':') + 2;
  90. $suggestions = substr($value, $suggstart);
  91. $suggestionarray = explode(', ', $suggestions);
  92. $beforeword = substr($textarray[$lineindex], 0, $position);
  93. $afterword = substr($textarray[$lineindex], $position + strlen($word));
  94. $textarray[$lineindex] = $beforeword.'<span class="HA-spellcheck-error">'.$word.'</span>'.$afterword;
  95. $suggestion_list = '';
  96. foreach ($suggestionarray as $key=>$value)
  97. {
  98. $suggestion_list .= $value.',';
  99. }
  100. $suggestion_list = substr($suggestion_list, 0, strlen($suggestion_list) - 1);
  101. $varlines .= '"'.$word.'":"'.$suggestion_list.'",';
  102. $poscorrect = $poscorrect + 41;
  103. }
  104. elseif (substr($value, 0, 1) == '#')
  105. {
  106. $correction = explode(' ', $value);
  107. $word = $correction[1];
  108. $absposition = $correction[2] - 1;
  109. $position = $absposition + $poscorrect;
  110. $niceposition = $lineindex.','.$absposition;
  111. $beforeword = substr($textarray[$lineindex], 0, $position);
  112. $afterword = substr($textarray[$lineindex], $position + strlen($word));
  113. $textarray[$lineindex] = $beforeword.$word.$afterword;
  114. $textarray[$lineindex] = $beforeword.'<span class="HA-spellcheck-error">'.$word.'</span><span class="HA-spellcheck-suggestions">'.$word.'</span>'.$afterword;
  115. // $poscorrect = $poscorrect;
  116. $poscorrect = $poscorrect + 88 + strlen($word);
  117. }
  118. else
  119. {
  120. //print "Done with line $lineindex, next line...<br><br>";
  121. $poscorrect = 0;
  122. $lineindex = $lineindex + 1;
  123. }
  124. }
  125. }
  126. else
  127. {
  128. // This one isnt used for anything at the moment!
  129. $return = 'failed to open!';
  130. }
  131. }
  132. else
  133. {
  134. $returnlines=0;
  135. }
  136. $infolines .= '"Language Used":"'.$lang.'",';
  137. $infolines .= '"Mispelled words":"'.$counter.'",';
  138. $infolines .= '"Total words suggested":"'.$suggest_count.'",';
  139. $infolines .= '"Total Lines Checked":"'.$returnlines.'"';
  140. $infolines .= '};';
  141. $varlines = substr($varlines, 0, strlen($varlines) - 1);
  142. echo $varlines.'};'.$infolines.'</script>';
  143. echo '</head>
  144. <body onload="window.parent.finishedSpellChecking();">';
  145. foreach ($textarray as $key=>$value)
  146. {
  147. echo $value;
  148. }
  149. $dictionaries = str_replace(chr(10),",", shell_exec($aspelldictionaries));
  150. if(ereg(",$",$dictionaries))
  151. $dictionaries = ereg_replace(",$","",$dictionaries);
  152. echo '<div id="HA-spellcheck-dictionaries">'.$dictionaries.'</div>';
  153. echo '</body></html>';
  154. ?>