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