source: trunk/library/ckeditor/plugins/aspell/spellerpages/server-scripts/spellchecker.php @ 7655

Revision 7655, 5.9 KB checked in by douglasz, 11 years ago (diff)

Ticket #3236 - Melhorias de performance no codigo do Expresso.

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