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