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