PageRenderTime 1448ms CodeModel.GetById 48ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/speller/server-scripts/spellchecker.php

https://github.com/bobpuffer/LAE
PHP | 248 lines | 198 code | 29 blank | 21 comment | 24 complexity | 753e65f4ef1c407833a45a494b59b59a MD5 | raw file
  1. <?php // $Id$
  2. include_once("../../../config.php");
  3. require_login();
  4. if (empty($CFG->aspellpath)) {
  5. error('Spellchecker not configured');
  6. }
  7. header('Content-type: text/html; charset=utf-8');
  8. // Speller pages script http://spellerpages.sourceforge.net/
  9. // Modified by Marc Alier on August 2004 for the integration with moodle
  10. $aspell_prog = escapeshellarg($CFG->aspellpath);
  11. $spellercss = $CFG->wwwroot .'/lib/speller/spellerStyle.css';
  12. $word_win_src = $CFG->wwwroot .'/lib/speller/wordWindow.js';
  13. $textinputs = $_POST['textinputs']; // array
  14. if(!($lang = check_language($aspell_prog))) {
  15. error_handler("No suitable dictionary found installed on your server!");
  16. exit;
  17. }
  18. $aspell_opts = '-a -H --lang='. $lang .' --encoding=utf-8';
  19. if (!empty($CFG->aspellextradicts)) { // eg /usr/bin/.aspell.en.pws
  20. $aspell_opts .= ' --add-extra-dicts='.$CFG->aspellextradicts;
  21. }
  22. $tempfiledir = $CFG->dataroot; // Use dataroot since it must be writable
  23. $input_separator = 'A';
  24. function check_language($cmd) {
  25. /// return users current language if its
  26. /// dictionary is found installed in system
  27. /// and always return english if user's own
  28. /// language is not in the list. If english dictionary
  29. /// isn't found, then false is returned.
  30. global $CFG;
  31. clearstatcache();
  32. $current_lang = str_replace('_utf8', '', current_language());
  33. $output = '';
  34. if(!($handle = popen($cmd .' dump dicts', 'r'))) {
  35. error_handler("Couldn't create handle!");
  36. exit;
  37. }
  38. while(!feof($handle)) {
  39. $output .= fread($handle, 1024);
  40. }
  41. @pclose($handle);
  42. $dicts = explode(chr(10), strtolower($output));
  43. if(is_array($dicts)) {
  44. if(in_array($current_lang,$dicts)) {
  45. return $current_lang;
  46. }
  47. }
  48. if (!empty($CFG->editordictionary)) {
  49. return $CFG->editordictionary;
  50. }
  51. return false;
  52. }
  53. // set the JavaScript variable to the submitted text.
  54. // textinputs is an array, each element corresponding to the (url-encoded)
  55. // value of the text control submitted for spell-checking
  56. function print_textinputs_var() {
  57. global $textinputs;
  58. foreach( $textinputs as $key=>$val ) {
  59. // $val = str_replace( "'", "%27", $val );
  60. echo "textinputs[$key] = decodeURIComponent(\"" . $val . "\");\n";
  61. }
  62. }
  63. // make declarations for the text input index
  64. function print_textindex_decl( $text_input_idx ) {
  65. echo "words[$text_input_idx] = [];\n";
  66. echo "suggs[$text_input_idx] = [];\n";
  67. }
  68. // set an element of the JavaScript 'words' array to a misspelled word
  69. function print_words_elem( $word, $index, $text_input_idx ) {
  70. echo "words[$text_input_idx][$index] = '" . escape_quote( $word ) . "';\n";
  71. }
  72. // set an element of the JavaScript 'suggs' array to a list of suggestions
  73. function print_suggs_elem( $suggs, $index, $text_input_idx ) {
  74. echo "suggs[$text_input_idx][$index] = [";
  75. foreach( $suggs as $key=>$val ) {
  76. if( $val ) {
  77. echo "'" . escape_quote( $val ) . "'";
  78. if ( $key+1 < count( $suggs )) {
  79. echo ", ";
  80. }
  81. }
  82. }
  83. echo "];\n";
  84. }
  85. // escape single quote
  86. function escape_quote( $str ) {
  87. return preg_replace ( "/'/", "\\'", $str );
  88. }
  89. // handle a server-side error.
  90. function error_handler( $err ) {
  91. echo "error = '" . escape_quote( $err ) . "';\n";
  92. }
  93. // get the list of misspelled words. Put the results in the javascript words array
  94. // for each misspelled word, get suggestions and put in the javascript suggs array
  95. function print_checker_results() {
  96. global $aspell_prog;
  97. global $aspell_opts;
  98. global $tempfiledir;
  99. global $textinputs;
  100. global $input_separator;
  101. $aspell_err = "";
  102. // create temp file
  103. $tempfile = tempnam( $tempfiledir, 'aspell_data_' );
  104. // open temp file, add the submitted text.
  105. if( $fh = fopen( $tempfile, 'w' )) {
  106. for( $i = 0; $i < count( $textinputs ); $i++ ) {
  107. $text = urldecode( $textinputs[$i] );
  108. $lines = explode( "\n", $text );
  109. fwrite ( $fh, "%\n" ); // exit terse mode
  110. fwrite ( $fh, "^$input_separator\n" );
  111. fwrite ( $fh, "!\n" ); // enter terse mode
  112. foreach( $lines as $key=>$value ) {
  113. // use carat on each line to escape possible aspell commands
  114. fwrite( $fh, "^$value\n" );
  115. }
  116. }
  117. fclose( $fh );
  118. // exec aspell command - redirect STDERR to STDOUT
  119. $cmd = "$aspell_prog $aspell_opts < $tempfile 2>&1";
  120. if( $aspellret = shell_exec( $cmd )) {
  121. $linesout = explode( "\n", $aspellret );
  122. $index = 0;
  123. $text_input_index = -1;
  124. // parse each line of aspell return
  125. foreach( $linesout as $key=>$val ) {
  126. $chardesc = substr( $val, 0, 1 );
  127. // if '&', then not in dictionary but has suggestions
  128. // if '#', then not in dictionary and no suggestions
  129. // if '*', then it is a delimiter between text inputs
  130. // if '@' then version info
  131. if( $chardesc == '&' || $chardesc == '#' ) {
  132. $line = explode( " ", $val, 5 );
  133. print_words_elem( $line[1], $index, $text_input_index );
  134. if( isset( $line[4] )) {
  135. $suggs = explode( ", ", $line[4] );
  136. } else {
  137. $suggs = array();
  138. }
  139. print_suggs_elem( $suggs, $index, $text_input_index );
  140. $index++;
  141. } elseif( $chardesc == '*' ) {
  142. $text_input_index++;
  143. print_textindex_decl( $text_input_index );
  144. $index = 0;
  145. } elseif( $chardesc != '@' && $chardesc != "" ) {
  146. // assume this is error output
  147. $aspell_err .= $val;
  148. }
  149. }
  150. if( $aspell_err ) {
  151. $aspell_err = "Error executing `$cmd`\\n$aspell_err";
  152. error_handler( $aspell_err );
  153. }
  154. } else {
  155. error_handler( "System error: Aspell program execution failed (`$cmd`)" );
  156. }
  157. } else {
  158. error_handler( "System error: Could not open file '$tempfile' for writing" );
  159. }
  160. // close temp file, delete file
  161. unlink( $tempfile );
  162. }
  163. ?>
  164. <html>
  165. <head>
  166. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  167. <link rel="stylesheet" type="text/css" href="<?php echo $spellercss ?>" />
  168. <script type="text/javascript" src="<?php echo $word_win_src ?>"></script>
  169. <script type="text/javascript">
  170. //<![CDATA[
  171. var suggs = new Array();
  172. var words = new Array();
  173. var textinputs = new Array();
  174. var error;
  175. <?php
  176. print_textinputs_var();
  177. print_checker_results();
  178. ?>
  179. var wordWindowObj = new wordWindow();
  180. wordWindowObj.originalSpellings = words;
  181. wordWindowObj.suggestions = suggs;
  182. wordWindowObj.textInputs = textinputs;
  183. function init_spell() {
  184. // check if any error occured during server-side processing
  185. if( error ) {
  186. alert( error );
  187. } else {
  188. // call the init_spell() function in the parent frameset
  189. if (parent.frames.length) {
  190. parent.init_spell( wordWindowObj );
  191. } else {
  192. alert('This page was loaded outside of a frameset. It might not display properly');
  193. }
  194. }
  195. }
  196. //]]>
  197. </script>
  198. </head>
  199. <body onLoad="init_spell();">
  200. <script type="text/javascript">
  201. //<![CDATA[
  202. wordWindowObj.writeBody();
  203. //]]>
  204. </script>
  205. </body>
  206. </html>