]> git.donarmstrong.com Git - roundcube.git/blob - program/js/tiny_mce/plugins/spellchecker/classes/PSpellShell.php
Imported Upstream version 0.3.1
[roundcube.git] / program / js / tiny_mce / plugins / spellchecker / classes / PSpellShell.php
1 <?php\r
2 /**\r
3  * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $\r
4  *\r
5  * @author Moxiecode\r
6  * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.\r
7  */\r
8 \r
9 class PSpellShell extends SpellChecker {\r
10         /**\r
11          * Spellchecks an array of words.\r
12          *\r
13          * @param {String} $lang Language code like sv or en.\r
14          * @param {Array} $words Array of words to spellcheck.\r
15          * @return {Array} Array of misspelled words.\r
16          */\r
17         function &checkWords($lang, $words) {\r
18                 $cmd = $this->_getCMD($lang);\r
19 \r
20                 if ($fh = fopen($this->_tmpfile, "w")) {\r
21                         fwrite($fh, "!\n");\r
22 \r
23                         foreach($words as $key => $value)\r
24                                 fwrite($fh, "^" . $value . "\n");\r
25 \r
26                         fclose($fh);\r
27                 } else\r
28                         $this->throwError("PSpell support was not found.");\r
29 \r
30                 $data = shell_exec($cmd);\r
31                 @unlink($this->_tmpfile);\r
32 \r
33                 $returnData = array();\r
34                 $dataArr = preg_split("/[\r\n]/", $data, -1, PREG_SPLIT_NO_EMPTY);\r
35 \r
36                 foreach ($dataArr as $dstr) {\r
37                         $matches = array();\r
38 \r
39                         // Skip this line.\r
40                         if (strpos($dstr, "@") === 0)\r
41                                 continue;\r
42 \r
43                         preg_match("/\& ([^ ]+) .*/i", $dstr, $matches);\r
44 \r
45                         if (!empty($matches[1]))\r
46                                 $returnData[] = utf8_encode(trim($matches[1]));\r
47                 }\r
48 \r
49                 return $returnData;\r
50         }\r
51 \r
52         /**\r
53          * Returns suggestions of for a specific word.\r
54          *\r
55          * @param {String} $lang Language code like sv or en.\r
56          * @param {String} $word Specific word to get suggestions for.\r
57          * @return {Array} Array of suggestions for the specified word.\r
58          */\r
59         function &getSuggestions($lang, $word) {\r
60                 $cmd = $this->_getCMD($lang);\r
61 \r
62         if (function_exists("mb_convert_encoding"))\r
63             $word = mb_convert_encoding($word, "ISO-8859-1", mb_detect_encoding($word, "UTF-8"));\r
64         else\r
65             $word = utf8_encode($word);\r
66 \r
67                 if ($fh = fopen($this->_tmpfile, "w")) {\r
68                         fwrite($fh, "!\n");\r
69                         fwrite($fh, "^$word\n");\r
70                         fclose($fh);\r
71                 } else\r
72                         $this->throwError("Error opening tmp file.");\r
73 \r
74                 $data = shell_exec($cmd);\r
75                 @unlink($this->_tmpfile);\r
76 \r
77                 $returnData = array();\r
78                 $dataArr = preg_split("/\n/", $data, -1, PREG_SPLIT_NO_EMPTY);\r
79 \r
80                 foreach($dataArr as $dstr) {\r
81                         $matches = array();\r
82 \r
83                         // Skip this line.\r
84                         if (strpos($dstr, "@") === 0)\r
85                                 continue;\r
86 \r
87                         preg_match("/\&[^:]+:(.*)/i", $dstr, $matches);\r
88 \r
89                         if (!empty($matches[1])) {\r
90                                 $words = array_slice(explode(',', $matches[1]), 0, 10);\r
91 \r
92                                 for ($i=0; $i<count($words); $i++)\r
93                                         $words[$i] = trim($words[$i]);\r
94 \r
95                                 return $words;\r
96                         }\r
97                 }\r
98 \r
99                 return array();\r
100         }\r
101 \r
102         function _getCMD($lang) {\r
103                 $this->_tmpfile = tempnam($this->_config['PSpellShell.tmp'], "tinyspell");\r
104 \r
105                 if(preg_match("#win#i", php_uname()))\r
106                         return $this->_config['PSpellShell.aspell'] . " -a --lang=". escapeshellarg($lang) . " --encoding=utf-8 -H < " . $this->_tmpfile . " 2>&1";\r
107 \r
108                 return "cat ". $this->_tmpfile ." | " . $this->_config['PSpellShell.aspell'] . " -a --encoding=utf-8 -H --lang=". escapeshellarg($lang);\r
109         }\r
110 }\r
111 \r
112 ?>\r