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