spellchecker.php
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:6k
源码类别:

OA系统

开发平台:

C#

  1. <?php
  2. header('Content-type: text/html; charset=utf-8');
  3. //$spellercss = '/speller/spellerStyle.css'; // by FredCK
  4. $spellercss = '../spellerStyle.css'; // by FredCK
  5. //$word_win_src = '/speller/wordWindow.js'; // by FredCK
  6. $word_win_src = '../wordWindow.js'; // by FredCK
  7. $textinputs = $_POST['textinputs']; # array
  8. //$aspell_prog = 'aspell'; // by FredCK (for Linux)
  9. $aspell_prog = '"C:Program FilesAspellbinaspell.exe"'; // by FredCK (for Windows)
  10. $lang = 'en_US';
  11. //$aspell_opts = "-a --lang=$lang --encoding=utf-8"; // by FredCK
  12. $aspell_opts = "-a --lang=$lang --encoding=utf-8 -H"; // by FredCK
  13. $tempfiledir = "./";
  14. $input_separator = "A";
  15. # set the JavaScript variable to the submitted text.
  16. # textinputs is an array, each element corresponding to the (url-encoded)
  17. # value of the text control submitted for spell-checking
  18. function print_textinputs_var() {
  19. global $textinputs;
  20. foreach( $textinputs as $key=>$val ) {
  21. # $val = str_replace( "'", "%27", $val );
  22. echo "textinputs[$key] = decodeURIComponent("" . $val . "");n";
  23. }
  24. }
  25. # make declarations for the text input index
  26. function print_textindex_decl( $text_input_idx ) {
  27. echo "words[$text_input_idx] = [];n";
  28. echo "suggs[$text_input_idx] = [];n";
  29. }
  30. # set an element of the JavaScript 'words' array to a misspelled word
  31. function print_words_elem( $word, $index, $text_input_idx ) {
  32. echo "words[$text_input_idx][$index] = '" . escape_quote( $word ) . "';n";
  33. }
  34. # set an element of the JavaScript 'suggs' array to a list of suggestions
  35. function print_suggs_elem( $suggs, $index, $text_input_idx ) {
  36. echo "suggs[$text_input_idx][$index] = [";
  37. foreach( $suggs as $key=>$val ) {
  38. if( $val ) {
  39. echo "'" . escape_quote( $val ) . "'";
  40. if ( $key+1 < count( $suggs )) {
  41. echo ", ";
  42. }
  43. }
  44. }
  45. echo "];n";
  46. }
  47. # escape single quote
  48. function escape_quote( $str ) {
  49. return preg_replace ( "/'/", "\'", $str );
  50. }
  51. # handle a server-side error.
  52. function error_handler( $err ) {
  53. echo "error = '" . escape_quote( $err ) . "';n";
  54. }
  55. ## get the list of misspelled words. Put the results in the javascript words array
  56. ## for each misspelled word, get suggestions and put in the javascript suggs array
  57. function print_checker_results() {
  58. global $aspell_prog;
  59. global $aspell_opts;
  60. global $tempfiledir;
  61. global $textinputs;
  62. global $input_separator;
  63. $aspell_err = "";
  64. # create temp file
  65. $tempfile = tempnam( $tempfiledir, 'aspell_data_' );
  66. # open temp file, add the submitted text.
  67. if( $fh = fopen( $tempfile, 'w' )) {
  68. for( $i = 0; $i < count( $textinputs ); $i++ ) {
  69. $text = urldecode( $textinputs[$i] );
  70. $lines = explode( "n", $text );
  71. fwrite ( $fh, "%n" ); # exit terse mode
  72. fwrite ( $fh, "^$input_separatorn" );
  73. fwrite ( $fh, "!n" ); # enter terse mode
  74. foreach( $lines as $key=>$value ) {
  75. # use carat on each line to escape possible aspell commands
  76. fwrite( $fh, "^$valuen" );
  77. }
  78. }
  79. fclose( $fh );
  80. # exec aspell command - redirect STDERR to STDOUT
  81. $cmd = "$aspell_prog $aspell_opts < $tempfile 2>&1";
  82. if( $aspellret = shell_exec( $cmd )) {
  83. $linesout = explode( "n", $aspellret );
  84. $index = 0;
  85. $text_input_index = -1;
  86. # parse each line of aspell return
  87. foreach( $linesout as $key=>$val ) {
  88. $chardesc = substr( $val, 0, 1 );
  89. # if '&', then not in dictionary but has suggestions
  90. # if '#', then not in dictionary and no suggestions
  91. # if '*', then it is a delimiter between text inputs
  92. # if '@' then version info
  93. if( $chardesc == '&' || $chardesc == '#' ) {
  94. $line = explode( " ", $val, 5 );
  95. print_words_elem( $line[1], $index, $text_input_index );
  96. if( isset( $line[4] )) {
  97. $suggs = explode( ", ", $line[4] );
  98. } else {
  99. $suggs = array();
  100. }
  101. print_suggs_elem( $suggs, $index, $text_input_index );
  102. $index++;
  103. } elseif( $chardesc == '*' ) {
  104. $text_input_index++;
  105. print_textindex_decl( $text_input_index );
  106. $index = 0;
  107. } elseif( $chardesc != '@' && $chardesc != "" ) {
  108. # assume this is error output
  109. $aspell_err .= $val;
  110. }
  111. }
  112. if( $aspell_err ) {
  113. $aspell_err = "Error executing `$cmd`\n$aspell_err";
  114. error_handler( $aspell_err );
  115. }
  116. } else {
  117. error_handler( "System error: Aspell program execution failed (`$cmd`)" );
  118. }
  119. } else {
  120. error_handler( "System error: Could not open file '$tempfile' for writing" );
  121. }
  122. # close temp file, delete file
  123. unlink( $tempfile );
  124. }
  125. ?>
  126. <html>
  127. <head>
  128. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  129. <link rel="stylesheet" type="text/css" href="<?php echo $spellercss ?>" />
  130. <script language="javascript" src="<?php echo $word_win_src ?>"></script>
  131. <script language="javascript">
  132. var suggs = new Array();
  133. var words = new Array();
  134. var textinputs = new Array();
  135. var error;
  136. <?php
  137. print_textinputs_var();
  138. print_checker_results();
  139. ?>
  140. var wordWindowObj = new wordWindow();
  141. wordWindowObj.originalSpellings = words;
  142. wordWindowObj.suggestions = suggs;
  143. wordWindowObj.textInputs = textinputs;
  144. function init_spell() {
  145. // check if any error occured during server-side processing
  146. if( error ) {
  147. alert( error );
  148. } else {
  149. // call the init_spell() function in the parent frameset
  150. if (parent.frames.length) {
  151. parent.init_spell( wordWindowObj );
  152. } else {
  153. alert('This page was loaded outside of a frameset. It might not display properly');
  154. }
  155. }
  156. }
  157. </script>
  158. </head>
  159. <!-- <body onLoad="init_spell();"> by FredCK -->
  160. <body onLoad="init_spell();" bgcolor="#ffffff">
  161. <script type="text/javascript">
  162. wordWindowObj.writeBody();
  163. </script>
  164. </body>
  165. </html>