cmd strings. No longer using temp file piping or redirection. Actual data // is passed into spawned aspell application via STDIN. function TinyPspellShell(&$config, $lang, $mode, $spelling, $jargon, $encoding) { $this->lang = $lang; $this->mode = $mode; $this->error = false; $this->errorMsg = array(); $this->tmpfile = tempnam($config['tinypspellshell.tmp'], "tinyspell"); if(preg_match("#win#i",php_uname())) // $this->cmd = $config['tinypspellshell.aspell'] . " -a --lang=". $this->lang." --encoding=utf-8 -H < $this->tmpfile 2>&1"; $this->cmd = $config['tinypspellshell.aspell'] . " -a --lang=". $this->lang." --encoding=utf-8 -H "; else //$this->cmd = "cat ". $this->tmpfile ." | " . $config['tinypspellshell.aspell'] . " -a --encoding=utf-8 -H --lang=". $this->lang; $this->cmd = $config['tinypspellshell.aspell'] . " -a --encoding=utf-8 -H --lang=". $this->lang; } // Returns array with bad words or false if failed. // // Modified by Roth // 2007.06.24 // -No longer using Temporary Files. // -No longer using shell_exec(). Calling into specialized Win32 aware Shell_Exec() function. function checkWords($wordArray) { /* if ($fh = fopen($this->tmpfile, "w")) { fwrite($fh, "!\n"); foreach($wordArray as $key => $value) fwrite($fh, "^" . $value . "\n"); fclose($fh); } else { $this->errorMsg[] = "PSpell not found."; return array(); } */ // $data = shell_exec($this->cmd); $data = Win32Shell_Exec( $this->cmd, $wordArray ); @unlink($this->tmpfile); $returnData = array(); $dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY); foreach($dataArr as $dstr) { $matches = array(); // Skip this line. if (strpos($dstr, "@") === 0) continue; preg_match("/\& (.*) .* .*: .*/i", $dstr, $matches); if (!empty($matches[1])) $returnData[] = $matches[1]; } return $returnData; } // Returns array with suggestions or false if failed. // // Modified by Roth // 2007.06.24 // -No longer using Temporary Files. // -No longer using shell_exec(). Calling into specialized Win32 aware Shell_Exec() function. function getSuggestion($word) { if (function_exists("mb_convert_encoding")) $word = mb_convert_encoding($word, "ISO-8859-1", mb_detect_encoding($word, "UTF-8")); else $word = utf8_encode($word); /* if ($fh = fopen($this->tmpfile, "w")) { fwrite($fh, "!\n"); fwrite($fh, "^$word\n"); fclose($fh); } else die("Error opening tmp file."); */ # $data = shell_exec($this->cmd); $data = Win32Shell_Exec( $this->cmd, $word ); @unlink($this->tmpfile); $returnData = array(); $dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY); foreach($dataArr as $dstr) { $matches = array(); // Skip this line. if (strpos($dstr, "@") === 0) continue; preg_match("/\& .* .* .*: (.*)/i", $dstr, $matches); if (!empty($matches[1])) { // For some reason, the exec version seems to add commas? $returnData[] = str_replace(",", "", $matches[1]); } } return $returnData; } function _debugData($data) { $fh = @fopen("debug.log", 'a+'); @fwrite($fh, $data); @fclose($fh); } } // Roth Changes // 2007.06.24 // -Fixed spell checker working on Win32 IIS machines. Needed to NOT shell out to the aspell.exe but // instead use proc_open() using pipes for the child process' STDIN, STDOUT, STDERR. // -Not using temporary files any more. Now just piping the temp file data to the child process via // STDIN. function Win32Shell_Exec( $Command, $WordObject ) { $descriptorspec = array( 0 => array( "pipe", "r"), 1 => array("pipe", "w"), // stdout is a pipe that the child will write to 2 => array("file", $TempFile . ".error-output", "w") // stderr is a file to write to ); $process = proc_open( $Command, $descriptorspec, $pipes); if( is_resource( $process ) ) { $ProcessInputData = ""; // $pipes now looks like this: // 0 => writeable handle connected to child stdin // 1 => readable handle connected to child stdout // Any error output will be appended to /tmp/error-output.txt if( is_array( $WordObject ) ) { $ProcessInputData = "!\n"; foreach( $WordObject as $Key => $Value) { $ProcessInputData .= "^" . $Value . "\n"; } } else { $ProcessInputData = "!\n^$WordObject\n"; } fwrite( $pipes[0], $ProcessInputData ); fclose( $pipes[0] ); while( !feof( $pipes[1] ) ) { $myData .= fgets($pipes[1], 1024); } fclose($pipes[1]); // It is important that you close any pipes before calling // proc_close in order to avoid a deadlock $return_value = proc_close($process); } else { $myData = "COULD NOT CREATE PROCESS"; } return( $myData ); } // Setup classname, should be the same as the name of the spellchecker class $spellCheckerConfig['class'] = "TinyPspellShell"; ?>