MDL-37283 import tinymce spellchecker 2.0.6.1
[moodle.git] / lib / editor / tinymce / tiny_mce / 3.5.1.1 / plugins / spellchecker / classes / GoogleSpell.php
1 <?php\r
2 /**\r
3  * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $\r
4  *\r
5  * @package MCManager.includes\r
6  * @author Moxiecode\r
7  * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.\r
8  */\r
9 \r
10 class GoogleSpell extends SpellChecker {\r
11         /**\r
12          * Spellchecks an array of words.\r
13          *\r
14          * @param {String} $lang Language code like sv or en.\r
15          * @param {Array} $words Array of words to spellcheck.\r
16          * @return {Array} Array of misspelled words.\r
17          */\r
18         function &checkWords($lang, $words) {\r
19                 $wordstr = implode(' ', $words);\r
20                 $matches = $this->_getMatches($lang, $wordstr);\r
21                 $words = array();\r
22 \r
23                 for ($i=0; $i<count($matches); $i++)\r
24                         $words[] = $this->_unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));\r
25 \r
26                 return $words;\r
27         }\r
28 \r
29         /**\r
30          * Returns suggestions of for a specific word.\r
31          *\r
32          * @param {String} $lang Language code like sv or en.\r
33          * @param {String} $word Specific word to get suggestions for.\r
34          * @return {Array} Array of suggestions for the specified word.\r
35          */\r
36         function &getSuggestions($lang, $word) {\r
37                 $sug = array();\r
38                 $osug = array();\r
39                 $matches = $this->_getMatches($lang, $word);\r
40 \r
41                 if (count($matches) > 0)\r
42                         $sug = explode("\t", $this->_unhtmlentities($matches[0][4]));\r
43 \r
44                 // Remove empty\r
45                 foreach ($sug as $item) {\r
46                         if ($item)\r
47                                 $osug[] = $item;\r
48                 }\r
49 \r
50                 return $osug;\r
51         }\r
52 \r
53         function &_getMatches($lang, $str) {\r
54                 $lang = preg_replace('/[^a-z\-]/i', '', $lang); // Sanitize, remove everything but a-z or -\r
55                 $str = preg_replace('/[\x00-\x1F\x7F]/', '', $str); // Sanitize, remove all control characters\r
56                 $server = "www.google.com";\r
57                 $port = 443;\r
58                 $path = "/tbproxy/spell?lang=" . $lang . "&hl=en";\r
59                 $host = "www.google.com";\r
60                 $url = "https://" . $server;\r
61 \r
62                 // Setup XML request\r
63                 $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $str . '</text></spellrequest>';\r
64 \r
65                 $header  = "POST ".$path." HTTP/1.0 \r\n";\r
66                 $header .= "MIME-Version: 1.0 \r\n";\r
67                 $header .= "Content-type: application/PTI26 \r\n";\r
68                 $header .= "Content-length: ".strlen($xml)." \r\n";\r
69                 $header .= "Content-transfer-encoding: text \r\n";\r
70                 $header .= "Request-number: 1 \r\n";\r
71                 $header .= "Document-type: Request \r\n";\r
72                 $header .= "Interface-Version: Test 1.4 \r\n";\r
73                 $header .= "Connection: close \r\n\r\n";\r
74                 $header .= $xml;\r
75 \r
76                 // Use curl if it exists\r
77                 if (function_exists('curl_init')) {\r
78                         // Use curl\r
79                         $ch = curl_init();\r
80                         curl_setopt($ch, CURLOPT_URL,$url);\r
81                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r
82                         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);\r
83                         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);\r
84                         if (!empty($this->_config['GoogleSpell.proxyhost'])) {\r
85                                 if (!empty($this->_config['GoogleSpell.proxytype']) and ($this->_config['GoogleSpell.proxytype'] === 'SOCKS5')) {\r
86                     curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);\r
87                                 } else {\r
88                     curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTML);\r
89                     curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, FALSE);\r
90                 }\r
91                                 if (empty($this->_config['GoogleSpell.proxyport'])) {\r
92                                         curl_setopt($ch, CURLOPT_PROXY, $this->_config['GoogleSpell.proxyhost']);\r
93                                 } else {\r
94                                         curl_setopt($ch, CURLOPT_PROXY, $this->_config['GoogleSpell.proxyhost'].':'.$this->_config['GoogleSpell.proxyport']);\r
95                                 }\r
96                                 if (!empty($this->_config['GoogleSpell.proxyuser']) and !empty($this->_config['GoogleSpell.proxypassword'])) {\r
97                                         curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->_config['GoogleSpell.proxyuser'].':'.$this->_config['GoogleSpell.proxypassword']);\r
98                                         if (defined('CURLOPT_PROXYAUTH')) {\r
99                                                 // any proxy authentication if PHP 5.1\r
100                                                 curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);\r
101                                         }\r
102                                 }\r
103                         }\r
104                         $xml = curl_exec($ch);\r
105                         curl_close($ch);\r
106                 } else {\r
107                         // Use raw sockets\r
108                         $fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);\r
109                         if ($fp) {\r
110                                 // Send request\r
111                                 fwrite($fp, $header);\r
112 \r
113                                 // Read response\r
114                                 $xml = "";\r
115                                 while (!feof($fp))\r
116                                         $xml .= fgets($fp, 128);\r
117 \r
118                                 fclose($fp);\r
119                         } else\r
120                                 echo "Could not open SSL connection to google.";\r
121                 }\r
122 \r
123                 // Grab and parse content\r
124                 $matches = array();\r
125                 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);\r
126 \r
127                 return $matches;\r
128         }\r
129 \r
130         function _unhtmlentities($string) {\r
131                 $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);\r
132                 $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);\r
133 \r
134                 $trans_tbl = get_html_translation_table(HTML_ENTITIES);\r
135                 $trans_tbl = array_flip($trans_tbl);\r
136 \r
137                 return strtr($string, $trans_tbl);\r
138         }\r
139 }\r
140 \r
141 // Patch in multibyte support\r
142 if (!function_exists('mb_substr')) {\r
143         function mb_substr($str, $start, $len = '', $encoding="UTF-8"){\r
144                 $limit = strlen($str);\r
145 \r
146                 for ($s = 0; $start > 0;--$start) {// found the real start\r
147                         if ($s >= $limit)\r
148                                 break;\r
149 \r
150                         if ($str[$s] <= "\x7F")\r
151                                 ++$s;\r
152                         else {\r
153                                 ++$s; // skip length\r
154 \r
155                                 while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")\r
156                                         ++$s;\r
157                         }\r
158                 }\r
159 \r
160                 if ($len == '')\r
161                         return substr($str, $s);\r
162                 else\r
163                         for ($e = $s; $len > 0; --$len) {//found the real end\r
164                                 if ($e >= $limit)\r
165                                         break;\r
166 \r
167                                 if ($str[$e] <= "\x7F")\r
168                                         ++$e;\r
169                                 else {\r
170                                         ++$e;//skip length\r
171 \r
172                                         while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)\r
173                                                 ++$e;\r
174                                 }\r
175                         }\r
176 \r
177                 return substr($str, $s, $e - $s);\r
178         }\r
179 }\r
180 \r
181 ?>\r