MDL-36212 rework html entity conversions
[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                 $server = "www.google.com";\r
55                 $port = 443;\r
56                 $path = "/tbproxy/spell?lang=" . $lang . "&hl=en";\r
57                 $host = "www.google.com";\r
58                 $url = "https://" . $server;\r
59 \r
60                 // Setup XML request\r
61                 $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $str . '</text></spellrequest>';\r
62 \r
63                 $header  = "POST ".$path." HTTP/1.0 \r\n";\r
64                 $header .= "MIME-Version: 1.0 \r\n";\r
65                 $header .= "Content-type: application/PTI26 \r\n";\r
66                 $header .= "Content-length: ".strlen($xml)." \r\n";\r
67                 $header .= "Content-transfer-encoding: text \r\n";\r
68                 $header .= "Request-number: 1 \r\n";\r
69                 $header .= "Document-type: Request \r\n";\r
70                 $header .= "Interface-Version: Test 1.4 \r\n";\r
71                 $header .= "Connection: close \r\n\r\n";\r
72                 $header .= $xml;\r
73 \r
74                 // Use curl if it exists\r
75                 if (function_exists('curl_init')) {\r
76                         // Use curl\r
77                         $ch = curl_init();\r
78                         curl_setopt($ch, CURLOPT_URL,$url);\r
79                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r
80                         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);\r
81                         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);\r
82                         if (!empty($this->_config['GoogleSpell.proxyhost'])) {\r
83                                 if (!empty($this->_config['GoogleSpell.proxytype']) and ($this->_config['GoogleSpell.proxytype'] === 'SOCKS5')) {\r
84                     curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);\r
85                                 } else {\r
86                     curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTML);\r
87                     curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, FALSE);\r
88                 }\r
89                                 if (empty($this->_config['GoogleSpell.proxyport'])) {\r
90                                         curl_setopt($ch, CURLOPT_PROXY, $this->_config['GoogleSpell.proxyhost']);\r
91                                 } else {\r
92                                         curl_setopt($ch, CURLOPT_PROXY, $this->_config['GoogleSpell.proxyhost'].':'.$this->_config['GoogleSpell.proxyport']);\r
93                                 }\r
94                                 if (!empty($this->_config['GoogleSpell.proxyuser']) and !empty($this->_config['GoogleSpell.proxypassword'])) {\r
95                                         curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->_config['GoogleSpell.proxyuser'].':'.$this->_config['GoogleSpell.proxypassword']);\r
96                                         if (defined('CURLOPT_PROXYAUTH')) {\r
97                                                 // any proxy authentication if PHP 5.1\r
98                                                 curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);\r
99                                         }\r
100                                 }\r
101                         }\r
102                         $xml = curl_exec($ch);\r
103                         curl_close($ch);\r
104                 } else {\r
105                         // Use raw sockets\r
106                         $fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);\r
107                         if ($fp) {\r
108                                 // Send request\r
109                                 fwrite($fp, $header);\r
110 \r
111                                 // Read response\r
112                                 $xml = "";\r
113                                 while (!feof($fp))\r
114                                         $xml .= fgets($fp, 128);\r
115 \r
116                                 fclose($fp);\r
117                         } else\r
118                                 echo "Could not open SSL connection to google.";\r
119                 }\r
120 \r
121                 // Grab and parse content\r
122                 $matches = array();\r
123                 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);\r
124 \r
125                 return $matches;\r
126         }\r
127 \r
128         function _unhtmlentities($string) {\r
129         return textlib::entities_to_utf8($string); // Moodle hack\r
130                 $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);\r
131                 $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);\r
132 \r
133                 $trans_tbl = get_html_translation_table(HTML_ENTITIES);\r
134                 $trans_tbl = array_flip($trans_tbl);\r
135 \r
136                 return strtr($string, $trans_tbl);\r
137         }\r
138 }\r
139 \r
140 // Patch in multibyte support\r
141 if (!function_exists('mb_substr')) {\r
142         function mb_substr($str, $start, $len = '', $encoding="UTF-8"){\r
143                 $limit = strlen($str);\r
144 \r
145                 for ($s = 0; $start > 0;--$start) {// found the real start\r
146                         if ($s >= $limit)\r
147                                 break;\r
148 \r
149                         if ($str[$s] <= "\x7F")\r
150                                 ++$s;\r
151                         else {\r
152                                 ++$s; // skip length\r
153 \r
154                                 while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")\r
155                                         ++$s;\r
156                         }\r
157                 }\r
158 \r
159                 if ($len == '')\r
160                         return substr($str, $s);\r
161                 else\r
162                         for ($e = $s; $len > 0; --$len) {//found the real end\r
163                                 if ($e >= $limit)\r
164                                         break;\r
165 \r
166                                 if ($str[$e] <= "\x7F")\r
167                                         ++$e;\r
168                                 else {\r
169                                         ++$e;//skip length\r
170 \r
171                                         while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)\r
172                                                 ++$e;\r
173                                 }\r
174                         }\r
175 \r
176                 return substr($str, $s, $e - $s);\r
177         }\r
178 }\r
179 \r
180 ?>\r