PageRenderTime 1521ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/generador-examenes-XHTML/fckeditor/editor/dialog/fck_spellerpages/spellerpages/server-scripts/spellchecker.php

https://bitbucket.org/mmorillo/sicogae
PHP | 201 lines | 157 code | 28 blank | 16 comment | 20 complexity | 76a5485074eff497fee6821568de857e MD5 | raw file
  1. <?php
  2. header('Content-type: text/html; charset=utf-8');
  3. // The following variables values must reflect your installation needs.
  4. $aspell_prog = '"C:\Program Files\Aspell\bin\aspell.exe"'; // by FredCK (for Windows)
  5. //$aspell_prog = 'aspell'; // by FredCK (for Linux)
  6. $lang = 'en_US';
  7. $aspell_opts = "-a --lang=$lang --encoding=utf-8 -H --rem-sgml-check=alt"; // by FredCK
  8. $tempfiledir = "./";
  9. $spellercss = '../spellerStyle.css'; // by FredCK
  10. $word_win_src = '../wordWindow.js'; // by FredCK
  11. $textinputs = $_POST['textinputs']; # array
  12. $input_separator = "A";
  13. # set the JavaScript variable to the submitted text.
  14. # textinputs is an array, each element corresponding to the (url-encoded)
  15. # value of the text control submitted for spell-checking
  16. function print_textinputs_var() {
  17. global $textinputs;
  18. foreach( $textinputs as $key=>$val ) {
  19. # $val = str_replace( "'", "%27", $val );
  20. echo "textinputs[$key] = decodeURIComponent(\"" . $val . "\");\n";
  21. }
  22. }
  23. # make declarations for the text input index
  24. function print_textindex_decl( $text_input_idx ) {
  25. echo "words[$text_input_idx] = [];\n";
  26. echo "suggs[$text_input_idx] = [];\n";
  27. }
  28. # set an element of the JavaScript 'words' array to a misspelled word
  29. function print_words_elem( $word, $index, $text_input_idx ) {
  30. echo "words[$text_input_idx][$index] = '" . escape_quote( $word ) . "';\n";
  31. }
  32. # set an element of the JavaScript 'suggs' array to a list of suggestions
  33. function print_suggs_elem( $suggs, $index, $text_input_idx ) {
  34. echo "suggs[$text_input_idx][$index] = [";
  35. foreach( $suggs as $key=>$val ) {
  36. if( $val ) {
  37. echo "'" . escape_quote( $val ) . "'";
  38. if ( $key+1 < count( $suggs )) {
  39. echo ", ";
  40. }
  41. }
  42. }
  43. echo "];\n";
  44. }
  45. # escape single quote
  46. function escape_quote( $str ) {
  47. return preg_replace ( "/'/", "\\'", $str );
  48. }
  49. # handle a server-side error.
  50. function error_handler( $err ) {
  51. echo "error = '" . escape_quote( $err ) . "';\n";
  52. }
  53. ## get the list of misspelled words. Put the results in the javascript words array
  54. ## for each misspelled word, get suggestions and put in the javascript suggs array
  55. function print_checker_results() {
  56. global $aspell_prog;
  57. global $aspell_opts;
  58. global $tempfiledir;
  59. global $textinputs;
  60. global $input_separator;
  61. $aspell_err = "";
  62. # create temp file
  63. $tempfile = tempnam( $tempfiledir, 'aspell_data_' );
  64. # open temp file, add the submitted text.
  65. if( $fh = fopen( $tempfile, 'w' )) {
  66. for( $i = 0; $i < count( $textinputs ); $i++ ) {
  67. $text = urldecode( $textinputs[$i] );
  68. // Strip all tags for the text. (by FredCK - #339 / #681)
  69. $text = preg_replace( "/<[^>]+>/", " ", $text ) ;
  70. $lines = explode( "\n", $text );
  71. fwrite ( $fh, "%\n" ); # exit terse mode
  72. fwrite ( $fh, "^$input_separator\n" );
  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, "^$value\n" );
  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>