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