Wordpress 2.3.2
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / spellchecker / classes / TinyPspellShell.class.php
1 <?php
2 /* * 
3  * Tiny Spelling Interface for TinyMCE Spell Checking.
4  *
5  * Copyright © 2006 Moxiecode Systems AB
6  *
7  */
8
9
10 class TinyPspellShell {
11         var $lang;
12         var $mode;
13         var $string;
14         var $error;
15         var $errorMsg;
16
17         var $cmd;
18         var $tmpfile;
19
20         var $jargon;
21         var $spelling;
22         var $encoding;
23
24         function TinyPspellShell(&$config, $lang, $mode, $spelling, $jargon, $encoding) {
25                 $this->lang = $lang;
26                 $this->mode = $mode;
27                 $this->error = false;
28                 $this->errorMsg = array();
29
30                 $this->tmpfile = tempnam($config['tinypspellshell.tmp'], "tinyspell");
31
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";
34         else
35             $this->cmd = "cat ". $this->tmpfile ." | " . $config['tinypspellshell.aspell'] . " -a --encoding=utf-8 -H --lang=". $this->lang;
36         }
37
38         // Returns array with bad words or false if failed.
39         function checkWords($wordArray) {
40                 if ($fh = fopen($this->tmpfile, "w")) {
41                         fwrite($fh, "!\n");
42                         foreach($wordArray as $key => $value)
43                                 fwrite($fh, "^" . $value . "\n");
44                         fclose($fh);
45                 } else {
46                         $this->errorMsg[] = "PSpell not found.";
47                         return array();
48                 }
49
50                 $data = shell_exec($this->cmd);
51         @unlink($this->tmpfile);
52
53                 $returnData = array();
54                 $dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
55
56                 foreach($dataArr as $dstr) {
57                         $matches = array();
58
59                         // Skip this line.
60                         if (strpos($dstr, "@") === 0)
61                                 continue;
62
63                         preg_match("/\& (.*) .* .*: .*/i", $dstr, $matches);
64
65                         if (!empty($matches[1]))
66                                 $returnData[] = $matches[1];
67                 }
68
69                 return $returnData;
70         }
71
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"));
76         else
77             $word = utf8_encode($word);
78
79                 if ($fh = fopen($this->tmpfile, "w")) {
80                         fwrite($fh, "!\n");
81                         fwrite($fh, "^$word\n");
82                         fclose($fh);
83                 } else
84                         die("Error opening tmp file.");
85
86                 $data = shell_exec($this->cmd);
87
88         @unlink($this->tmpfile);
89
90                 $returnData = array();
91                 $dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);
92
93                 foreach($dataArr as $dstr) {
94                         $matches = array();
95
96                         // Skip this line.
97                         if (strpos($dstr, "@") === 0)
98                                 continue;
99
100                         preg_match("/\& .* .* .*: (.*)/i", $dstr, $matches);
101
102                         if (!empty($matches[1])) {
103                                 // For some reason, the exec version seems to add commas?
104                                 $returnData[] = str_replace(",", "", $matches[1]);
105                         }
106                 }
107                 return $returnData;
108         }
109
110         function _debugData($data) {
111                 $fh = @fopen("debug.log", 'a+');
112                 @fwrite($fh, $data);
113                 @fclose($fh);
114         }
115
116 }
117
118 // Setup classname, should be the same as the name of the spellchecker class
119 $spellCheckerConfig['class'] = "TinyPspellShell";
120
121 ?>