--- /dev/null
+<?php\r
+/**\r
+ * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $\r
+ *\r
+ * @author Moxiecode\r
+ * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.\r
+ */\r
+\r
+class GoogleSpell extends SpellChecker {\r
+ /**\r
+ * Spellchecks an array of words.\r
+ *\r
+ * @param {String} $lang Language code like sv or en.\r
+ * @param {Array} $words Array of words to spellcheck.\r
+ * @return {Array} Array of misspelled words.\r
+ */\r
+ function &checkWords($lang, $words) {\r
+ $wordstr = implode(' ', $words);\r
+ $matches = $this->_getMatches($lang, $wordstr);\r
+ $words = array();\r
+\r
+ for ($i=0; $i<count($matches); $i++)\r
+ $words[] = $this->_unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));\r
+\r
+ return $words;\r
+ }\r
+\r
+ /**\r
+ * Returns suggestions of for a specific word.\r
+ *\r
+ * @param {String} $lang Language code like sv or en.\r
+ * @param {String} $word Specific word to get suggestions for.\r
+ * @return {Array} Array of suggestions for the specified word.\r
+ */\r
+ function &getSuggestions($lang, $word) {\r
+ $sug = array();\r
+ $osug = array();\r
+ $matches = $this->_getMatches($lang, $word);\r
+\r
+ if (count($matches) > 0)\r
+ $sug = explode("\t", $this->_unhtmlentities($matches[0][4]));\r
+\r
+ // Remove empty\r
+ foreach ($sug as $item) {\r
+ if ($item)\r
+ $osug[] = $item;\r
+ }\r
+\r
+ return $osug;\r
+ }\r
+\r
+ function &_getMatches($lang, $str) {\r
+ $server = "www.google.com";\r
+ $port = 443;\r
+ $path = "/tbproxy/spell?lang=" . $lang . "&hl=en";\r
+ $ssl = true;\r
+ \r
+ // spell check uri is configured (added by RoundCube)\r
+ if (!empty($this->_config['rpc_uri'])) {\r
+ $a_uri = parse_url($this->_config['rpc_uri']);\r
+ $ssl = ($a_uri['scheme']=='https' || $a_uri['scheme']=='ssl');\r
+ $port = $a_uri['port'] ? $a_uri['port'] : ($ssl ? 443 : 80);\r
+ $server = $a_uri['host'];\r
+ $path = $a_uri['path'] . ($a_uri['query'] ? '?'.$a_uri['query'] : '') . $lang;\r
+ }\r
+\r
+ // Setup XML request\r
+ $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $str . '</text></spellrequest>';\r
+\r
+ $header = "POST ".$path." HTTP/1.0 \r\n";\r
+ $header .= "MIME-Version: 1.0 \r\n";\r
+ $header .= "Content-type: application/PTI26 \r\n";\r
+ $header .= "Content-length: ".strlen($xml)." \r\n";\r
+ $header .= "Content-transfer-encoding: text \r\n";\r
+ $header .= "Request-number: 1 \r\n";\r
+ $header .= "Document-type: Request \r\n";\r
+ $header .= "Interface-Version: Test 1.4 \r\n";\r
+ $header .= "Connection: close \r\n\r\n";\r
+ $header .= $xml;\r
+\r
+ // Use curl if it exists\r
+ if (function_exists('curl_init')) {\r
+ // Use curl\r
+ $ch = curl_init();\r
+ curl_setopt($ch, CURLOPT_URL, ($ssl ? "https://" : "http://") . $server);\r
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r
+ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);\r
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);\r
+ $xml = curl_exec($ch);\r
+ curl_close($ch);\r
+ } else {\r
+ // Use raw sockets\r
+ $fp = fsockopen(($ssl ? "ssl://" : "") . $server, $port, $errno, $errstr, 30);\r
+ if ($fp) {\r
+ // Send request\r
+ fwrite($fp, $header);\r
+\r
+ // Read response\r
+ $xml = "";\r
+ while (!feof($fp))\r
+ $xml .= fgets($fp, 128);\r
+\r
+ fclose($fp);\r
+ } else\r
+ echo "Could not open SSL connection to google.";\r
+ }\r
+\r
+ // Grab and parse content\r
+ $matches = array();\r
+ preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);\r
+\r
+ return $matches;\r
+ }\r
+\r
+ function _unhtmlentities($string) {\r
+ $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);\r
+ $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);\r
+\r
+ $trans_tbl = get_html_translation_table(HTML_ENTITIES);\r
+ $trans_tbl = array_flip($trans_tbl);\r
+\r
+ return strtr($string, $trans_tbl);\r
+ }\r
+}\r
+\r
+// Patch in multibyte support\r
+if (!function_exists('mb_substr')) {\r
+ function mb_substr($str, $start, $len = '', $encoding="UTF-8"){\r
+ $limit = strlen($str);\r
+\r
+ for ($s = 0; $start > 0;--$start) {// found the real start\r
+ if ($s >= $limit)\r
+ break;\r
+\r
+ if ($str[$s] <= "\x7F")\r
+ ++$s;\r
+ else {\r
+ ++$s; // skip length\r
+\r
+ while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")\r
+ ++$s;\r
+ }\r
+ }\r
+\r
+ if ($len == '')\r
+ return substr($str, $s);\r
+ else\r
+ for ($e = $s; $len > 0; --$len) {//found the real end\r
+ if ($e >= $limit)\r
+ break;\r
+\r
+ if ($str[$e] <= "\x7F")\r
+ ++$e;\r
+ else {\r
+ ++$e;//skip length\r
+\r
+ while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)\r
+ ++$e;\r
+ }\r
+ }\r
+\r
+ return substr($str, $s, $e - $s);\r
+ }\r
+}\r
+\r
+?>\r