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