3 * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
5 * @package MCManager.includes
7 * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
10 class GoogleSpell extends SpellChecker {
12 * Spellchecks an array of words.
14 * @param {String} $lang Language code like sv or en.
15 * @param {Array} $words Array of words to spellcheck.
16 * @return {Array} Array of misspelled words.
18 function &checkWords($lang, $words) {
19 $wordstr = implode(' ', $words);
20 $matches = $this->_getMatches($lang, $wordstr);
23 for ($i=0; $i<count($matches); $i++)
24 $words[] = $this->_unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));
30 * Returns suggestions of for a specific word.
32 * @param {String} $lang Language code like sv or en.
33 * @param {String} $word Specific word to get suggestions for.
34 * @return {Array} Array of suggestions for the specified word.
36 function &getSuggestions($lang, $word) {
39 $matches = $this->_getMatches($lang, $word);
41 if (count($matches) > 0)
42 $sug = explode("\t", utf8_encode($this->_unhtmlentities($matches[0][4])));
45 foreach ($sug as $item) {
53 protected function &_getMatches($lang, $str) {
54 $lang = preg_replace('/[^a-z\-]/i', '', $lang);
55 $str = preg_replace('/[\x00-\x1F\x7F]/', '', $str);
56 $server = "www.google.com";
58 $path = "/tbproxy/spell?lang=" . $lang . "&hl=en";
59 $host = "www.google.com";
60 $url = "https://" . $server;
63 $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $str . '</text></spellrequest>';
65 $header = "POST ".$path." HTTP/1.0 \r\n";
66 $header .= "MIME-Version: 1.0 \r\n";
67 $header .= "Content-type: application/PTI26 \r\n";
68 $header .= "Content-length: ".strlen($xml)." \r\n";
69 $header .= "Content-transfer-encoding: text \r\n";
70 $header .= "Request-number: 1 \r\n";
71 $header .= "Document-type: Request \r\n";
72 $header .= "Interface-Version: Test 1.4 \r\n";
73 $header .= "Connection: close \r\n\r\n";
76 // Use curl if it exists
77 if (function_exists('curl_init')) {
80 curl_setopt($ch, CURLOPT_URL,$url);
81 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
82 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
83 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
84 $xml = curl_exec($ch);
88 $fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
96 $xml .= fgets($fp, 128);
100 echo "Could not open SSL connection to google.";
103 // Grab and parse content
105 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
110 protected function _unhtmlentities($string) {
111 $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
112 $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
114 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
115 $trans_tbl = array_flip($trans_tbl);
117 return strtr($string, $trans_tbl);
121 // Patch in multibyte support
122 if (!function_exists('mb_substr')) {
123 function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
124 $limit = strlen($str);
126 for ($s = 0; $start > 0;--$start) {// found the real start
130 if ($str[$s] <= "\x7F")
135 while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
141 return substr($str, $s);
143 for ($e = $s; $len > 0; --$len) {//found the real end
147 if ($str[$e] <= "\x7F")
152 while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
157 return substr($str, $s, $e - $s);