2 header('Content-type: text/html; charset=utf-8');
4 // The following variables values must reflect your installation needs.
6 $aspell_prog = '"C:\Program Files\Aspell\bin\aspell.exe"'; // by FredCK (for Windows)
7 //$aspell_prog = 'aspell'; // by FredCK (for Linux)
10 $aspell_opts = "-a --lang=$lang --encoding=utf-8 -H --rem-sgml-check=alt"; // by FredCK
14 $spellercss = '../spellerStyle.css'; // by FredCK
15 $word_win_src = '../wordWindow.js'; // by FredCK
17 $textinputs = $_POST['textinputs']; # array
18 $input_separator = "A";
20 # set the JavaScript variable to the submitted text.
21 # textinputs is an array, each element corresponding to the (url-encoded)
22 # value of the text control submitted for spell-checking
23 function print_textinputs_var() {
25 foreach( $textinputs as $key=>$val ) {
26 # $val = str_replace( "'", "%27", $val );
27 echo "textinputs[$key] = decodeURIComponent(\"" . $val . "\");\n";
31 # make declarations for the text input index
32 function print_textindex_decl( $text_input_idx ) {
33 echo "words[$text_input_idx] = [];\n";
34 echo "suggs[$text_input_idx] = [];\n";
37 # set an element of the JavaScript 'words' array to a misspelled word
38 function print_words_elem( $word, $index, $text_input_idx ) {
39 echo "words[$text_input_idx][$index] = '" . escape_quote( $word ) . "';\n";
42 # set an element of the JavaScript 'suggs' array to a list of suggestions
43 function print_suggs_elem( $suggs, $index, $text_input_idx ) {
44 echo "suggs[$text_input_idx][$index] = [";
45 foreach( $suggs as $key=>$val ) {
47 echo "'" . escape_quote( $val ) . "'";
48 if ( $key+1 < count( $suggs )) {
57 function escape_quote( $str ) {
58 return preg_replace ( "/'/", "\\'", $str );
61 # handle a server-side error.
62 function error_handler( $err ) {
63 echo "error = '" . preg_replace( "/['\\\\]/", "\\\\$0", $err ) . "';\n";
66 ## get the list of misspelled words. Put the results in the javascript words array
67 ## for each misspelled word, get suggestions and put in the javascript suggs array
68 function print_checker_results() {
74 global $input_separator;
77 $tempfile = tempnam( $tempfiledir, 'aspell_data_' );
79 # open temp file, add the submitted text.
80 if( $fh = fopen( $tempfile, 'w' )) {
81 for( $i = 0; $i < count( $textinputs ); $i++ ) {
82 $text = urldecode( $textinputs[$i] );
84 // Strip all tags for the text. (by FredCK - #339 / #681)
85 $text = preg_replace( "/<[^>]+>/", " ", $text ) ;
87 $lines = explode( "\n", $text );
88 fwrite ( $fh, "%\n" ); # exit terse mode
89 fwrite ( $fh, "^$input_separator\n" );
90 fwrite ( $fh, "!\n" ); # enter terse mode
91 foreach( $lines as $key=>$value ) {
92 # use carat on each line to escape possible aspell commands
93 fwrite( $fh, "^$value\n" );
98 # exec aspell command - redirect STDERR to STDOUT
99 $cmd = "$aspell_prog $aspell_opts < $tempfile 2>&1";
100 if( $aspellret = shell_exec( $cmd )) {
101 $linesout = explode( "\n", $aspellret );
103 $text_input_index = -1;
104 # parse each line of aspell return
105 foreach( $linesout as $key=>$val ) {
106 $chardesc = substr( $val, 0, 1 );
107 # if '&', then not in dictionary but has suggestions
108 # if '#', then not in dictionary and no suggestions
109 # if '*', then it is a delimiter between text inputs
110 # if '@' then version info
111 if( $chardesc == '&' || $chardesc == '#' ) {
112 $line = explode( " ", $val, 5 );
113 print_words_elem( $line[1], $index, $text_input_index );
114 if( isset( $line[4] )) {
115 $suggs = explode( ", ", $line[4] );
119 print_suggs_elem( $suggs, $index, $text_input_index );
121 } elseif( $chardesc == '*' ) {
123 print_textindex_decl( $text_input_index );
125 } elseif( $chardesc != '@' && $chardesc != "" ) {
126 # assume this is error output
131 $aspell_err = "Error executing `$cmd`\\n$aspell_err";
132 error_handler( $aspell_err );
135 error_handler( "System error: Aspell program execution failed (`$cmd`)" );
138 error_handler( "System error: Could not open file '$tempfile' for writing" );
141 # close temp file, delete file
148 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
149 <link rel="stylesheet" type="text/css" href="<?php echo $spellercss ?>" />
150 <script language="javascript" src="<?php echo $word_win_src ?>"></script>
151 <script language="javascript">
152 var suggs = new Array();
153 var words = new Array();
154 var textinputs = new Array();
158 print_textinputs_var();
160 print_checker_results();
164 var wordWindowObj = new wordWindow();
165 wordWindowObj.originalSpellings = words;
166 wordWindowObj.suggestions = suggs;
167 wordWindowObj.textInputs = textinputs;
169 function init_spell() {
170 // check if any error occured during server-side processing
174 // call the init_spell() function in the parent frameset
175 if (parent.frames.length) {
176 parent.init_spell( wordWindowObj );
178 alert('This page was loaded outside of a frameset. It might not display properly');
186 <!-- <body onLoad="init_spell();"> by FredCK -->
187 <body onLoad="init_spell();" bgcolor="#ffffff">
189 <script type="text/javascript">
190 wordWindowObj.writeBody();