]> git.donarmstrong.com Git - roundcube.git/blob - program/js/tiny_mce/plugins/spellchecker/classes/TinyGoogleSpell.class.php
7be929731f36e4b605212240ff240202ef8d6a89
[roundcube.git] / program / js / tiny_mce / plugins / spellchecker / classes / TinyGoogleSpell.class.php
1 <?php\r
2 \r
3 /* *\r
4  * Tiny Spelling Interface for TinyMCE Spell Checking.\r
5  *\r
6  * Copyright © 2006 Moxiecode Systems AB\r
7  */\r
8 \r
9 class TinyGoogleSpell {\r
10         var $lang;\r
11         var $spellurl;\r
12 \r
13         function TinyGoogleSpell(& $config, $lang, $mode, $spelling, $jargon, $encoding) {\r
14                 $this->lang = $lang;\r
15                 $this->spellurl = $config['googlespell.url'];\r
16         }\r
17 \r
18         // Returns array with bad words or false if failed.\r
19         function checkWords($word_array) {\r
20                 $words = array ();\r
21                 $wordstr = implode(' ', $word_array);\r
22 \r
23                 $matches = $this->_getMatches($wordstr);\r
24 \r
25                 for ($i = 0; $i < count($matches); $i++)\r
26                         $words[] = $this->unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));\r
27 \r
28                 return $words;\r
29         }\r
30 \r
31         function unhtmlentities($string) {\r
32                 $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);\r
33                 $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);\r
34 \r
35                 $trans_tbl = get_html_translation_table(HTML_ENTITIES);\r
36                 $trans_tbl = array_flip($trans_tbl);\r
37 \r
38                 return strtr($string, $trans_tbl);\r
39         }\r
40 \r
41         // Returns array with suggestions or false if failed.\r
42         function getSuggestion($word) {\r
43                 $sug = array ();\r
44 \r
45                 $matches = $this->_getMatches($word);\r
46 \r
47                 if (count($matches) > 0)\r
48                         $sug = explode("\t", utf8_encode($this->unhtmlentities($matches[0][4])));\r
49 \r
50                 return $sug;\r
51         }\r
52 \r
53         function _xmlChars($string) {\r
54                 $trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);\r
55 \r
56                 foreach ($trans as $k => $v)\r
57                         $trans[$k] = "&#" . ord($k) . ";";\r
58 \r
59                 return strtr($string, $trans);\r
60         }\r
61 \r
62         function _getMatches($word_list) {\r
63                 $url = $this->spellurl . "&" . $this->lang;\r
64 \r
65                 $path = preg_replace("/^https?:\/\//i", "", $url);\r
66 \r
67                 // Setup XML request\r
68             $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $word_list . '</text></spellrequest>';\r
69 \r
70                 $header = "POST " . $path . " HTTP/1.0 \r\n";\r
71                 $header .= "MIME-Version: 1.0 \r\n";\r
72                 $header .= "Content-type: application/PTI26 \r\n";\r
73                 $header .= "Content-length: " . strlen($xml) . " \r\n";\r
74                 $header .= "Content-transfer-encoding: text \r\n";\r
75                 $header .= "Request-number: 1 \r\n";\r
76                 $header .= "Document-type: Request \r\n";\r
77                 $header .= "Interface-Version: Test 1.4 \r\n";\r
78                 $header .= "Connection: close \r\n\r\n";\r
79                 $header .= $xml;\r
80                 //$this->_debugData($xml);\r
81 \r
82                 $ch = curl_init();\r
83                 curl_setopt($ch, CURLOPT_URL, $url);\r
84                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r
85                 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);\r
86                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);\r
87                 $xml = curl_exec($ch);\r
88                 curl_close($ch);\r
89 \r
90                 //$this->_debugData($xml);\r
91 \r
92                 // Grab and parse content\r
93                 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);\r
94 \r
95                 return $matches;\r
96         }\r
97 \r
98         function _debugData($data) {\r
99                 $fh = @ fopen("debug.log", 'a+');\r
100                 @ fwrite($fh, $data);\r
101                 @ fclose($fh);\r
102         }\r
103 }\r
104 \r
105 // Setup classname, should be the same as the name of the spellchecker class\r
106 $spellCheckerConfig['class'] = "TinyGoogleSpell";\r
107 ?>\r