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