source: branches/1.2/workflow/js/fckeditor/editor/dialog/fck_spellerpages/spellerpages/server-scripts/spellchecker.php @ 1349

Revision 1349, 5.4 KB checked in by niltonneto, 15 years ago (diff)

Ticket #561 - Inclusão do módulo Workflow faltante nessa versão.

  • Property svn:executable set to *
Line 
1<?php
2header('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";                // 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
23function 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
32function 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
38function 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
44function 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
58function escape_quote( $str ) {
59        return preg_replace ( "/'/", "\\'", $str );
60}
61
62
63# handle a server-side error.
64function error_handler( $err ) {
65        echo "error = '" . escape_quote( $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
70function 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                        $lines = explode( "\n", $text );
86                        fwrite ( $fh, "%\n" ); # exit terse mode
87                        fwrite ( $fh, "^$input_separator\n" );
88                        fwrite ( $fh, "!\n" ); # enter terse mode
89                        foreach( $lines as $key=>$value ) {
90                                # use carat on each line to escape possible aspell commands
91                                fwrite( $fh, "^$value\n" );
92                        }
93                }
94                fclose( $fh );
95
96                # exec aspell command - redirect STDERR to STDOUT
97                $cmd = "$aspell_prog $aspell_opts < $tempfile 2>&1";
98                if( $aspellret = shell_exec( $cmd )) {
99                        $linesout = explode( "\n", $aspellret );
100                        $index = 0;
101                        $text_input_index = -1;
102                        # parse each line of aspell return
103                        foreach( $linesout as $key=>$val ) {
104                                $chardesc = substr( $val, 0, 1 );
105                                # if '&', then not in dictionary but has suggestions
106                                # if '#', then not in dictionary and no suggestions
107                                # if '*', then it is a delimiter between text inputs
108                                # if '@' then version info
109                                if( $chardesc == '&' || $chardesc == '#' ) {
110                                        $line = explode( " ", $val, 5 );
111                                        print_words_elem( $line[1], $index, $text_input_index );
112                                        if( isset( $line[4] )) {
113                                                $suggs = explode( ", ", $line[4] );
114                                        } else {
115                                                $suggs = array();
116                                        }
117                                        print_suggs_elem( $suggs, $index, $text_input_index );
118                                        $index++;
119                                } elseif( $chardesc == '*' ) {
120                                        $text_input_index++;
121                                        print_textindex_decl( $text_input_index );
122                                        $index = 0;
123                                } elseif( $chardesc != '@' && $chardesc != "" ) {
124                                        # assume this is error output
125                                        $aspell_err .= $val;
126                                }
127                        }
128                        if( $aspell_err ) {
129                                $aspell_err = "Error executing `$cmd`\\n$aspell_err";
130                                error_handler( $aspell_err );
131                        }
132                } else {
133                        error_handler( "System error: Aspell program execution failed (`$cmd`)" );
134                }
135        } else {
136                error_handler( "System error: Could not open file '$tempfile' for writing" );
137        }
138
139        # close temp file, delete file
140        unlink( $tempfile );
141}
142
143
144?>
145<html>
146<head>
147<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
148<link rel="stylesheet" type="text/css" href="<?php echo $spellercss ?>" />
149<script language="javascript" src="<?php echo $word_win_src ?>"></script>
150<script language="javascript">
151var suggs = new Array();
152var words = new Array();
153var textinputs = new Array();
154var error;
155<?php
156
157print_textinputs_var();
158
159print_checker_results();
160
161?>
162
163var wordWindowObj = new wordWindow();
164wordWindowObj.originalSpellings = words;
165wordWindowObj.suggestions = suggs;
166wordWindowObj.textInputs = textinputs;
167
168function init_spell() {
169        // check if any error occured during server-side processing
170        if( error ) {
171                alert( error );
172        } else {
173                // call the init_spell() function in the parent frameset
174                if (parent.frames.length) {
175                        parent.init_spell( wordWindowObj );
176                } else {
177                        alert('This page was loaded outside of a frameset. It might not display properly');
178                }
179        }
180}
181
182
183
184</script>
185
186</head>
187<!-- <body onLoad="init_spell();">              by FredCK -->
188<body onLoad="init_spell();" bgcolor="#ffffff">
189
190<script type="text/javascript">
191wordWindowObj.writeBody();
192</script>
193
194</body>
195</html>
196
Note: See TracBrowser for help on using the repository browser.