PageRenderTime 1542ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/opencore/js/thirdparty/xinha/plugins/SpellChecker/spell-check-logic.php

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