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