4 * Tiny Spelling Interface for TinyMCE Spell Checking.
\r
6 * Copyright © 2006 Moxiecode Systems AB
\r
9 class TinyGoogleSpell {
\r
13 function TinyGoogleSpell(& $config, $lang, $mode, $spelling, $jargon, $encoding) {
\r
14 $this->lang = $lang;
\r
15 $this->spellurl = $config['googlespell.url'];
\r
18 // Returns array with bad words or false if failed.
\r
19 function checkWords($word_array) {
\r
21 $wordstr = implode(' ', $word_array);
\r
23 $matches = $this->_getMatches($wordstr);
\r
25 for ($i = 0; $i < count($matches); $i++)
\r
26 $words[] = $this->unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));
\r
31 function unhtmlentities($string) {
\r
32 $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
\r
33 $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
\r
35 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
\r
36 $trans_tbl = array_flip($trans_tbl);
\r
38 return strtr($string, $trans_tbl);
\r
41 // Returns array with suggestions or false if failed.
\r
42 function getSuggestion($word) {
\r
45 $matches = $this->_getMatches($word);
\r
47 if (count($matches) > 0)
\r
48 $sug = explode("\t", utf8_encode($this->unhtmlentities($matches[0][4])));
\r
53 function _xmlChars($string) {
\r
54 $trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
\r
56 foreach ($trans as $k => $v)
\r
57 $trans[$k] = "&#" . ord($k) . ";";
\r
59 return strtr($string, $trans);
\r
62 function _getMatches($word_list) {
\r
63 $url = $this->spellurl . "&" . $this->lang;
\r
65 $path = preg_replace("/^https?:\/\//i", "", $url);
\r
67 // Setup XML request
\r
68 $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $word_list . '</text></spellrequest>';
\r
70 $header = "POST " . $path . " HTTP/1.0 \r\n";
\r
71 $header .= "MIME-Version: 1.0 \r\n";
\r
72 $header .= "Content-type: application/PTI26 \r\n";
\r
73 $header .= "Content-length: " . strlen($xml) . " \r\n";
\r
74 $header .= "Content-transfer-encoding: text \r\n";
\r
75 $header .= "Request-number: 1 \r\n";
\r
76 $header .= "Document-type: Request \r\n";
\r
77 $header .= "Interface-Version: Test 1.4 \r\n";
\r
78 $header .= "Connection: close \r\n\r\n";
\r
80 //$this->_debugData($xml);
\r
83 curl_setopt($ch, CURLOPT_URL, $url);
\r
84 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
\r
85 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
\r
86 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
\r
87 $xml = curl_exec($ch);
\r
90 //$this->_debugData($xml);
\r
92 // Grab and parse content
\r
93 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
\r
98 function _debugData($data) {
\r
99 $fh = @ fopen("debug.log", 'a+');
\r
100 @ fwrite($fh, $data);
\r
105 // Setup classname, should be the same as the name of the spellchecker class
\r
106 $spellCheckerConfig['class'] = "TinyGoogleSpell";
\r