Back to index

moin  1.9.0~rc2
spellchecker.php
Go to the documentation of this file.
00001 <?php
00002 header('Content-type: text/html; charset=utf-8');
00003 
00004 // The following variables values must reflect your installation needs.
00005 
00006 $aspell_prog  = '"C:\Program Files\Aspell\bin\aspell.exe"';    // by FredCK (for Windows)
00007 //$aspell_prog       = 'aspell';                                                                  // by FredCK (for Linux)
00008 
00009 $lang                = 'en_US';
00010 $aspell_opts  = "-a --lang=$lang --encoding=utf-8 -H --rem-sgml-check=alt";         // by FredCK
00011 
00012 $tempfiledir  = "./";
00013 
00014 $spellercss          = '../spellerStyle.css';                                       // by FredCK
00015 $word_win_src = '../wordWindow.js';                                                 // by FredCK
00016 
00017 $textinputs          = $_POST['textinputs']; # array
00018 $input_separator = "A";
00019 
00020 # set the JavaScript variable to the submitted text.
00021 # textinputs is an array, each element corresponding to the (url-encoded)
00022 # value of the text control submitted for spell-checking
00023 function print_textinputs_var() {
00024        global $textinputs;
00025        foreach( $textinputs as $key=>$val ) {
00026               # $val = str_replace( "'", "%27", $val );
00027               echo "textinputs[$key] = decodeURIComponent(\"" . $val . "\");\n";
00028        }
00029 }
00030 
00031 # make declarations for the text input index
00032 function print_textindex_decl( $text_input_idx ) {
00033        echo "words[$text_input_idx] = [];\n";
00034        echo "suggs[$text_input_idx] = [];\n";
00035 }
00036 
00037 # set an element of the JavaScript 'words' array to a misspelled word
00038 function print_words_elem( $word, $index, $text_input_idx ) {
00039        echo "words[$text_input_idx][$index] = '" . escape_quote( $word ) . "';\n";
00040 }
00041 
00042 
00043 # set an element of the JavaScript 'suggs' array to a list of suggestions
00044 function print_suggs_elem( $suggs, $index, $text_input_idx ) {
00045        echo "suggs[$text_input_idx][$index] = [";
00046        foreach( $suggs as $key=>$val ) {
00047               if( $val ) {
00048                      echo "'" . escape_quote( $val ) . "'";
00049                      if ( $key+1 < count( $suggs )) {
00050                             echo ", ";
00051                      }
00052               }
00053        }
00054        echo "];\n";
00055 }
00056 
00057 # escape single quote
00058 function escape_quote( $str ) {
00059        return preg_replace ( "/'/", "\\'", $str );
00060 }
00061 
00062 
00063 # handle a server-side error.
00064 function error_handler( $err ) {
00065        echo "error = '" . preg_replace( "/['\\\\]/", "\\\\$0", $err ) . "';\n";
00066 }
00067 
00068 ## get the list of misspelled words. Put the results in the javascript words array
00069 ## for each misspelled word, get suggestions and put in the javascript suggs array
00070 function print_checker_results() {
00071 
00072        global $aspell_prog;
00073        global $aspell_opts;
00074        global $tempfiledir;
00075        global $textinputs;
00076        global $input_separator;
00077        $aspell_err = "";
00078        # create temp file
00079        $tempfile = tempnam( $tempfiledir, 'aspell_data_' );
00080 
00081        # open temp file, add the submitted text.
00082        if( $fh = fopen( $tempfile, 'w' )) {
00083               for( $i = 0; $i < count( $textinputs ); $i++ ) {
00084                      $text = urldecode( $textinputs[$i] );
00085 
00086                      // Strip all tags for the text. (by FredCK - #339 / #681)
00087                      $text = preg_replace( "/<[^>]+>/", " ", $text ) ;
00088 
00089                      $lines = explode( "\n", $text );
00090                      fwrite ( $fh, "%\n" ); # exit terse mode
00091                      fwrite ( $fh, "^$input_separator\n" );
00092                      fwrite ( $fh, "!\n" ); # enter terse mode
00093                      foreach( $lines as $key=>$value ) {
00094                             # use carat on each line to escape possible aspell commands
00095                             fwrite( $fh, "^$value\n" );
00096                      }
00097               }
00098               fclose( $fh );
00099 
00100               # exec aspell command - redirect STDERR to STDOUT
00101               $cmd = "$aspell_prog $aspell_opts < $tempfile 2>&1";
00102               if( $aspellret = shell_exec( $cmd )) {
00103                      $linesout = explode( "\n", $aspellret );
00104                      $index = 0;
00105                      $text_input_index = -1;
00106                      # parse each line of aspell return
00107                      foreach( $linesout as $key=>$val ) {
00108                             $chardesc = substr( $val, 0, 1 );
00109                             # if '&', then not in dictionary but has suggestions
00110                             # if '#', then not in dictionary and no suggestions
00111                             # if '*', then it is a delimiter between text inputs
00112                             # if '@' then version info
00113                             if( $chardesc == '&' || $chardesc == '#' ) {
00114                                    $line = explode( " ", $val, 5 );
00115                                    print_words_elem( $line[1], $index, $text_input_index );
00116                                    if( isset( $line[4] )) {
00117                                           $suggs = explode( ", ", $line[4] );
00118                                    } else {
00119                                           $suggs = array();
00120                                    }
00121                                    print_suggs_elem( $suggs, $index, $text_input_index );
00122                                    $index++;
00123                             } elseif( $chardesc == '*' ) {
00124                                    $text_input_index++;
00125                                    print_textindex_decl( $text_input_index );
00126                                    $index = 0;
00127                             } elseif( $chardesc != '@' && $chardesc != "" ) {
00128                                    # assume this is error output
00129                                    $aspell_err .= $val;
00130                             }
00131                      }
00132                      if( $aspell_err ) {
00133                             $aspell_err = "Error executing `$cmd`\\n$aspell_err";
00134                             error_handler( $aspell_err );
00135                      }
00136               } else {
00137                      error_handler( "System error: Aspell program execution failed (`$cmd`)" );
00138               }
00139        } else {
00140               error_handler( "System error: Could not open file '$tempfile' for writing" );
00141        }
00142 
00143        # close temp file, delete file
00144        unlink( $tempfile );
00145 }
00146 
00147 
00148 ?>
00149 <html>
00150 <head>
00151 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
00152 <link rel="stylesheet" type="text/css" href="<?php echo $spellercss ?>" />
00153 <script language="javascript" src="<?php echo $word_win_src ?>"></script>
00154 <script language="javascript">
00155 var suggs = new Array();
00156 var words = new Array();
00157 var textinputs = new Array();
00158 var error;
00159 <?php
00160 
00161 print_textinputs_var();
00162 
00163 print_checker_results();
00164 
00165 ?>
00166 
00167 var wordWindowObj = new wordWindow();
00168 wordWindowObj.originalSpellings = words;
00169 wordWindowObj.suggestions = suggs;
00170 wordWindowObj.textInputs = textinputs;
00171 
00172 function init_spell() {
00173        // check if any error occured during server-side processing
00174        if( error ) {
00175               alert( error );
00176        } else {
00177               // call the init_spell() function in the parent frameset
00178               if (parent.frames.length) {
00179                      parent.init_spell( wordWindowObj );
00180               } else {
00181                      alert('This page was loaded outside of a frameset. It might not display properly');
00182               }
00183        }
00184 }
00185 
00186 
00187 
00188 </script>
00189 
00190 </head>
00191 <!-- <body onLoad="init_spell();">        by FredCK -->
00192 <body onLoad="init_spell();" bgcolor="#ffffff">
00193 
00194 <script type="text/javascript">
00195 wordWindowObj.writeBody();
00196 </script>
00197 
00198 </body>
00199 </html>