3 * Tiny Spelling Interface for TinyMCE Spell Checking.
5 * Copyright © 2006 Moxiecode Systems AB
10 class TinyPspellShell {
24 function TinyPspellShell(&$config, $lang, $mode, $spelling, $jargon, $encoding) {
28 $this->errorMsg = array();
30 $this->tmpfile = tempnam($config['tinypspellshell.tmp'], "tinyspell");
32 if(preg_match("#win#i",php_uname()))
33 $this->cmd = $config['tinypspellshell.aspell'] . " -a --lang=". $this->lang." --encoding=utf-8 -H < $this->tmpfile 2>&1";
35 $this->cmd = "cat ". $this->tmpfile ." | " . $config['tinypspellshell.aspell'] . " -a --encoding=utf-8 -H --lang=". $this->lang;
38 // Returns array with bad words or false if failed.
39 function checkWords($wordArray) {
40 if ($fh = fopen($this->tmpfile, "w")) {
42 foreach($wordArray as $key => $value)
43 fwrite($fh, "^" . $value . "\n");
46 $this->errorMsg[] = "PSpell not found.";
50 $data = shell_exec($this->cmd);
51 @unlink($this->tmpfile);
53 $returnData = array();
54 $dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
56 foreach($dataArr as $dstr) {
60 if (strpos($dstr, "@") === 0)
63 preg_match("/\& (.*) .* .*: .*/i", $dstr, $matches);
65 if (!empty($matches[1]))
66 $returnData[] = $matches[1];
72 // Returns array with suggestions or false if failed.
73 function getSuggestion($word) {
74 if (function_exists("mb_convert_encoding"))
75 $word = mb_convert_encoding($word, "ISO-8859-1", mb_detect_encoding($word, "UTF-8"));
77 $word = utf8_encode($word);
79 if ($fh = fopen($this->tmpfile, "w")) {
81 fwrite($fh, "^$word\n");
84 die("Error opening tmp file.");
86 $data = shell_exec($this->cmd);
88 @unlink($this->tmpfile);
90 $returnData = array();
91 $dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
93 foreach($dataArr as $dstr) {
97 if (strpos($dstr, "@") === 0)
100 preg_match("/\& .* .* .*: (.*)/i", $dstr, $matches);
102 if (!empty($matches[1])) {
103 // For some reason, the exec version seems to add commas?
104 $returnData[] = str_replace(",", "", $matches[1]);
110 function _debugData($data) {
111 $fh = @fopen("debug.log", 'a+');
118 // Setup classname, should be the same as the name of the spellchecker class
119 $spellCheckerConfig['class'] = "TinyPspellShell";