yllar
2006-12-14 38bf9d3b71067a51ffc9a915ea288929d1fb08e4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/* * 
 * Tiny Spelling Interface for TinyMCE Spell Checking.
 *
 * Copyright © 2006 Moxiecode Systems AB
 *
 */
 
 
class TinyPspellShell {
    var $lang;
    var $mode;
    var $string;
    var $error;
    var $errorMsg;
 
    var $cmd;
    var $tmpfile;
 
    var $jargon;
    var $spelling;
    var $encoding;
 
    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";
        else
            $this->cmd = "cat ". $this->tmpfile ." | " . $config['tinypspellshell.aspell'] . " -a --encoding=utf-8 -H --lang=". $this->lang;
    }
 
    // Returns array with bad words or false if failed.
    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);
        @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.
    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);
 
        @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);
    }
 
}
 
// Setup classname, should be the same as the name of the spellchecker class
$spellCheckerConfig['class'] = "TinyPspellShell";
 
?>