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