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

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

Ticket #3236 - Correcoes para Performance: Function Within Loop Declaration.

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