commit | author | age
|
6649b1
|
1 |
<?php
|
S |
2 |
/* *
|
|
3 |
* Tiny Spelling Interface for TinyMCE Spell Checking.
|
|
4 |
*
|
|
5 |
* Copyright © 2006 Moxiecode Systems AB
|
|
6 |
*/
|
|
7 |
|
|
8 |
class TinyGoogleSpell {
|
|
9 |
var $lang;
|
|
10 |
|
|
11 |
function TinyGoogleSpell(&$config, $lang, $mode, $spelling, $jargon, $encoding) {
|
|
12 |
$this->lang = $lang;
|
|
13 |
}
|
|
14 |
|
|
15 |
// Returns array with bad words or false if failed.
|
|
16 |
function checkWords($word_array) {
|
|
17 |
$words = array();
|
|
18 |
$wordstr = implode(' ', $word_array);
|
|
19 |
|
|
20 |
$matches = $this->_getMatches($wordstr);
|
|
21 |
|
|
22 |
for ($i=0; $i<count($matches); $i++)
|
|
23 |
$words[] = $this->unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));
|
|
24 |
|
|
25 |
return $words;
|
|
26 |
}
|
|
27 |
|
|
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);
|
|
31 |
|
|
32 |
$trans_tbl = get_html_translation_table(HTML_ENTITIES);
|
|
33 |
$trans_tbl = array_flip($trans_tbl);
|
|
34 |
|
|
35 |
return strtr($string, $trans_tbl);
|
|
36 |
}
|
|
37 |
|
|
38 |
// Returns array with suggestions or false if failed.
|
|
39 |
function getSuggestion($word) {
|
|
40 |
$sug = array();
|
|
41 |
|
|
42 |
$matches = $this->_getMatches($word);
|
|
43 |
|
|
44 |
if (count($matches) > 0)
|
|
45 |
$sug = explode("\t", utf8_encode($this->unhtmlentities($matches[0][4])));
|
|
46 |
|
|
47 |
return $sug;
|
|
48 |
}
|
|
49 |
|
|
50 |
function _xmlChars($string) {
|
|
51 |
$trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);
|
|
52 |
|
|
53 |
foreach ($trans as $k => $v)
|
|
54 |
$trans[$k] = "&#".ord($k).";";
|
|
55 |
|
|
56 |
return strtr($string, $trans);
|
|
57 |
}
|
|
58 |
|
|
59 |
function _getMatches($word_list) {
|
|
60 |
$server = "www.google.com";
|
|
61 |
$port = 443;
|
|
62 |
$path = "/tbproxy/spell?lang=" . $this->lang . "&hl=en";
|
|
63 |
$host = "www.google.com";
|
|
64 |
$url = "https://" . $server;
|
|
65 |
|
|
66 |
// Setup XML request
|
|
67 |
$xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $word_list . '</text></spellrequest>';
|
|
68 |
|
|
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";
|
|
78 |
$header .= $xml;
|
|
79 |
//$this->_debugData($xml);
|
|
80 |
|
|
81 |
$ch = curl_init();
|
|
82 |
curl_setopt($ch, CURLOPT_URL,$url);
|
|
83 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
84 |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
|
|
85 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
|
|
86 |
$xml = curl_exec($ch);
|
|
87 |
curl_close($ch);
|
|
88 |
|
|
89 |
//$this->_debugData($xml);
|
|
90 |
|
|
91 |
// Grab and parse content
|
|
92 |
preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
|
|
93 |
|
|
94 |
return $matches;
|
|
95 |
}
|
|
96 |
|
|
97 |
function _debugData($data) {
|
|
98 |
$fh = @fopen("debug.log", 'a+');
|
|
99 |
@fwrite($fh, $data);
|
|
100 |
@fclose($fh);
|
|
101 |
}
|
|
102 |
}
|
|
103 |
|
|
104 |
// Setup classname, should be the same as the name of the spellchecker class
|
|
105 |
$spellCheckerConfig['class'] = "TinyGoogleSpell";
|
|
106 |
|
|
107 |
?>
|