import torrus 1.0.9
[freeside.git] / httemplate / elements / fckeditor / editor / dialog / fck_spellerpages / spellerpages / server-scripts / spellchecker.pl
1 #!/usr/bin/perl\r
2 \r
3 use CGI qw/ :standard /;\r
4 use File::Temp qw/ tempfile tempdir /;\r
5 \r
6 # my $spellercss = '/speller/spellerStyle.css';                                 # by FredCK\r
7 my $spellercss = '../spellerStyle.css';                                                 # by FredCK\r
8 # my $wordWindowSrc = '/speller/wordWindow.js';                                 # by FredCK\r
9 my $wordWindowSrc = '../wordWindow.js';                                                 # by FredCK\r
10 my @textinputs = param( 'textinputs[]' ); # array\r
11 # my $aspell_cmd = 'aspell';                                                                    # by FredCK (for Linux)\r
12 my $aspell_cmd = '"C:\Program Files\Aspell\bin\aspell.exe"';    # by FredCK (for Windows)\r
13 my $lang = 'en_US';\r
14 # my $aspell_opts = "-a --lang=$lang --encoding=utf-8";                 # by FredCK\r
15 my $aspell_opts = "-a --lang=$lang --encoding=utf-8 -H --rem-sgml-check=alt";           # by FredCK\r
16 my $input_separator = "A";\r
17 \r
18 # set the 'wordtext' JavaScript variable to the submitted text.\r
19 sub printTextVar {\r
20         for( my $i = 0; $i <= $#textinputs; $i++ ) {\r
21                 print "textinputs[$i] = decodeURIComponent('" . escapeQuote( $textinputs[$i] ) . "')\n";\r
22         }\r
23 }\r
24 \r
25 sub printTextIdxDecl {\r
26         my $idx = shift;\r
27         print "words[$idx] = [];\n";\r
28         print "suggs[$idx] = [];\n";\r
29 }\r
30 \r
31 sub printWordsElem {\r
32         my( $textIdx, $wordIdx, $word ) = @_;\r
33         print "words[$textIdx][$wordIdx] = '" . escapeQuote( $word ) . "';\n";\r
34 }\r
35 \r
36 sub printSuggsElem {\r
37         my( $textIdx, $wordIdx, @suggs ) = @_;\r
38         print "suggs[$textIdx][$wordIdx] = [";\r
39         for my $i ( 0..$#suggs ) {\r
40                 print "'" . escapeQuote( $suggs[$i] ) . "'";\r
41                 if( $i < $#suggs ) {\r
42                         print ", ";\r
43                 }\r
44         }\r
45         print "];\n";\r
46 }\r
47 \r
48 sub printCheckerResults {\r
49         my $textInputIdx = -1;\r
50         my $wordIdx = 0;\r
51         my $unhandledText;\r
52         # create temp file\r
53         my $dir = tempdir( CLEANUP => 1 );\r
54         my( $fh, $tmpfilename ) = tempfile( DIR => $dir );\r
55 \r
56         # temp file was created properly?\r
57 \r
58         # open temp file, add the submitted text.\r
59         for( my $i = 0; $i <= $#textinputs; $i++ ) {\r
60                 $text = url_decode( $textinputs[$i] );\r
61                 # Strip all tags for the text. (by FredCK - #339 / #681)\r
62                 $text =~ s/<[^>]+>/ /g;\r
63                 @lines = split( /\n/, $text );\r
64                 print $fh "\%\n"; # exit terse mode\r
65                 print $fh "^$input_separator\n";\r
66                 print $fh "!\n";  # enter terse mode\r
67                 for my $line ( @lines ) {\r
68                         # use carat on each line to escape possible aspell commands\r
69                         print $fh "^$line\n";\r
70                 }\r
71 \r
72         }\r
73         # exec aspell command\r
74         my $cmd = "$aspell_cmd $aspell_opts < $tmpfilename 2>&1";\r
75         open ASPELL, "$cmd |" or handleError( "Could not execute `$cmd`\\n$!" ) and return;\r
76         # parse each line of aspell return\r
77         for my $ret ( <ASPELL> ) {\r
78                 chomp( $ret );\r
79                 # if '&', then not in dictionary but has suggestions\r
80                 # if '#', then not in dictionary and no suggestions\r
81                 # if '*', then it is a delimiter between text inputs\r
82                 if( $ret =~ /^\*/ ) {\r
83                         $textInputIdx++;\r
84                         printTextIdxDecl( $textInputIdx );\r
85                         $wordIdx = 0;\r
86 \r
87                 } elsif( $ret =~ /^(&|#)/ ) {\r
88                         my @tokens = split( " ", $ret, 5 );\r
89                         printWordsElem( $textInputIdx, $wordIdx, $tokens[1] );\r
90                         my @suggs = ();\r
91                         if( $tokens[4] ) {\r
92                                 @suggs = split( ", ", $tokens[4] );\r
93                         }\r
94                         printSuggsElem( $textInputIdx, $wordIdx, @suggs );\r
95                         $wordIdx++;\r
96                 } else {\r
97                         $unhandledText .= $ret;\r
98                 }\r
99         }\r
100         close ASPELL or handleError( "Error executing `$cmd`\\n$unhandledText" ) and return;\r
101 }\r
102 \r
103 sub escapeQuote {\r
104         my $str = shift;\r
105         $str =~ s/'/\\'/g;\r
106         return $str;\r
107 }\r
108 \r
109 sub handleError {\r
110         my $err = shift;\r
111         print "error = '" . escapeQuote( $err ) . "';\n";\r
112 }\r
113 \r
114 sub url_decode {\r
115         local $_ = @_ ? shift : $_;\r
116         defined or return;\r
117         # change + signs to spaces\r
118         tr/+/ /;\r
119         # change hex escapes to the proper characters\r
120         s/%([a-fA-F0-9]{2})/pack "H2", $1/eg;\r
121         return $_;\r
122 }\r
123 \r
124 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #\r
125 # Display HTML\r
126 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #\r
127 \r
128 print <<EOF;\r
129 Content-type: text/html; charset=utf-8\r
130 \r
131 <html>\r
132 <head>\r
133 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">\r
134 <link rel="stylesheet" type="text/css" href="$spellercss"/>\r
135 <script src="$wordWindowSrc"></script>\r
136 <script type="text/javascript">\r
137 var suggs = new Array();\r
138 var words = new Array();\r
139 var textinputs = new Array();\r
140 var error;\r
141 EOF\r
142 \r
143 printTextVar();\r
144 \r
145 printCheckerResults();\r
146 \r
147 print <<EOF;\r
148 var wordWindowObj = new wordWindow();\r
149 wordWindowObj.originalSpellings = words;\r
150 wordWindowObj.suggestions = suggs;\r
151 wordWindowObj.textInputs = textinputs;\r
152 \r
153 \r
154 function init_spell() {\r
155         // check if any error occured during server-side processing\r
156         if( error ) {\r
157                 alert( error );\r
158         } else {\r
159                 // call the init_spell() function in the parent frameset\r
160                 if (parent.frames.length) {\r
161                         parent.init_spell( wordWindowObj );\r
162                 } else {\r
163                         error = "This page was loaded outside of a frameset. ";\r
164                         error += "It might not display properly";\r
165                         alert( error );\r
166                 }\r
167         }\r
168 }\r
169 \r
170 </script>\r
171 \r
172 </head>\r
173 <body onLoad="init_spell();">\r
174 \r
175 <script type="text/javascript">\r
176 wordWindowObj.writeBody();\r
177 </script>\r
178 \r
179 </body>\r
180 </html>\r
181 EOF\r