3 * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
6 * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
9 class GoogleSpell extends SpellChecker {
11 * Spellchecks an array of words.
13 * @param {String} $lang Language code like sv or en.
14 * @param {Array} $words Array of words to spellcheck.
15 * @return {Array} Array of misspelled words.
17 function &checkWords($lang, $words) {
18 $wordstr = implode(' ', $words);
19 $matches = $this->_getMatches($lang, $wordstr);
22 for ($i=0; $i<count($matches); $i++)
23 $words[] = $this->_unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));
29 * Returns suggestions of for a specific word.
31 * @param {String} $lang Language code like sv or en.
32 * @param {String} $word Specific word to get suggestions for.
33 * @return {Array} Array of suggestions for the specified word.
35 function &getSuggestions($lang, $word) {
38 $matches = $this->_getMatches($lang, $word);
40 if (count($matches) > 0)
41 $sug = explode("\t", utf8_encode($this->_unhtmlentities($matches[0][4])));
44 foreach ($sug as $item) {
52 function &_getMatches($lang, $str) {
53 $server = "www.google.com";
55 $path = "/tbproxy/spell?lang=" . $lang . "&hl=en";
56 $host = "www.google.com";
57 $url = "https://" . $server;
60 $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $str . '</text></spellrequest>';
62 $header = "POST ".$path." HTTP/1.0 \r\n";
63 $header .= "MIME-Version: 1.0 \r\n";
64 $header .= "Content-type: application/PTI26 \r\n";
65 $header .= "Content-length: ".strlen($xml)." \r\n";
66 $header .= "Content-transfer-encoding: text \r\n";
67 $header .= "Request-number: 1 \r\n";
68 $header .= "Document-type: Request \r\n";
69 $header .= "Interface-Version: Test 1.4 \r\n";
70 $header .= "Connection: close \r\n\r\n";
73 // Use curl if it exists
74 if (function_exists('curl_init')) {
77 curl_setopt($ch, CURLOPT_URL,$url);
78 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
79 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
80 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
81 $xml = curl_exec($ch);
85 $fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
93 $xml .= fgets($fp, 128);
97 echo "Could not open SSL connection to google.";
100 // Grab and parse content
102 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
107 function _unhtmlentities($string) {
108 $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
109 $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
111 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
112 $trans_tbl = array_flip($trans_tbl);
114 return strtr($string, $trans_tbl);
118 // Patch in multibyte support
119 if (!function_exists('mb_substr')) {
120 function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
121 $limit = strlen($str);
123 for ($s = 0; $start > 0;--$start) {// found the real start
127 if ($str[$s] <= "\x7F")
132 while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
138 return substr($str, $s);
140 for ($e = $s; $len > 0; --$len) {//found the real end
144 if ($str[$e] <= "\x7F")
149 while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
154 return substr($str, $s, $e - $s);