3 * Tiny Spelling Interface for TinyMCE Spell Checking.
5 * Copyright © 2006 Moxiecode Systems AB
8 class TinyGoogleSpell {
11 function TinyGoogleSpell(&$config, $lang, $mode, $spelling, $jargon, $encoding) {
15 // Returns array with bad words or false if failed.
16 function checkWords($word_array) {
18 $wordstr = implode(' ', $word_array);
20 $matches = $this->_getMatches($wordstr);
22 for ($i=0; $i<count($matches); $i++)
23 $words[] = $this->unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));
28 function unhtmlentities($string) {
29 $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
30 $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
32 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
33 $trans_tbl = array_flip($trans_tbl);
35 return strtr($string, $trans_tbl);
38 // Returns array with suggestions or false if failed.
39 function getSuggestion($word) {
42 $matches = $this->_getMatches($word);
44 if (count($matches) > 0)
45 $sug = explode("\t", utf8_encode($this->unhtmlentities($matches[0][4])));
50 function _xmlChars($string) {
51 $trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
53 foreach ($trans as $k => $v)
54 $trans[$k] = "&#".ord($k).";";
56 return strtr($string, $trans);
59 function _getMatches($word_list) {
60 $server = "www.google.com";
62 $path = "/tbproxy/spell?lang=" . $this->lang . "&hl=en";
63 $host = "www.google.com";
64 $url = "https://" . $server;
67 $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $word_list . '</text></spellrequest>';
69 $header = "POST ".$path." HTTP/1.0 \r\n";
70 $header .= "MIME-Version: 1.0 \r\n";
71 $header .= "Content-type: application/PTI26 \r\n";
72 $header .= "Content-length: ".strlen($xml)." \r\n";
73 $header .= "Content-transfer-encoding: text \r\n";
74 $header .= "Request-number: 1 \r\n";
75 $header .= "Document-type: Request \r\n";
76 $header .= "Interface-Version: Test 1.4 \r\n";
77 $header .= "Connection: close \r\n\r\n";
79 //$this->_debugData($xml);
81 // Use curl if it exists
82 if (function_exists('curl_init')) {
85 curl_setopt($ch, CURLOPT_URL,$url);
86 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
87 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
88 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
89 $xml = curl_exec($ch);
93 $fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
101 $xml .= fgets($fp, 128);
105 echo "Could not open SSL connection to google.";
108 //$this->_debugData($xml);
110 // Grab and parse content
111 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
116 function _debugData($data) {
117 $fh = @fopen("debug.log", 'a+');
123 // Setup classname, should be the same as the name of the spellchecker class
124 $spellCheckerConfig['class'] = "TinyGoogleSpell";