source: trunk/www.guidonia.net/wp/wp-includes/js/tinymce/plugins/spellchecker/classes/GoogleSpell.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 4.0 KB
Line 
1<?php
2/**
3 * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
4 *
5 * @package MCManager.includes
6 * @author Moxiecode
7 * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
8 */
9
10class GoogleSpell extends SpellChecker {
11 /**
12 * Spellchecks an array of words.
13 *
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.
17 */
18 function &checkWords($lang, $words) {
19 $wordstr = implode(' ', $words);
20 $matches = $this->_getMatches($lang, $wordstr);
21 $words = array();
22
23 for ($i=0; $i<count($matches); $i++)
24 $words[] = $this->_unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));
25
26 return $words;
27 }
28
29 /**
30 * Returns suggestions of for a specific word.
31 *
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.
35 */
36 function &getSuggestions($lang, $word) {
37 $sug = array();
38 $osug = array();
39 $matches = $this->_getMatches($lang, $word);
40
41 if (count($matches) > 0)
42 $sug = explode("\t", utf8_encode($this->_unhtmlentities($matches[0][4])));
43
44 // Remove empty
45 foreach ($sug as $item) {
46 if ($item)
47 $osug[] = $item;
48 }
49
50 return $osug;
51 }
52
53 function &_getMatches($lang, $str) {
54 $server = "www.google.com";
55 $port = 443;
56 $path = "/tbproxy/spell?lang=" . $lang . "&hl=en";
57 $host = "www.google.com";
58 $url = "https://" . $server;
59
60 // Setup XML request
61 $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $str . '</text></spellrequest>';
62
63 $header = "POST ".$path." HTTP/1.0 \r\n";
64 $header .= "MIME-Version: 1.0 \r\n";
65 $header .= "Content-type: application/PTI26 \r\n";
66 $header .= "Content-length: ".strlen($xml)." \r\n";
67 $header .= "Content-transfer-encoding: text \r\n";
68 $header .= "Request-number: 1 \r\n";
69 $header .= "Document-type: Request \r\n";
70 $header .= "Interface-Version: Test 1.4 \r\n";
71 $header .= "Connection: close \r\n\r\n";
72 $header .= $xml;
73
74 // Use curl if it exists
75 if (function_exists('curl_init')) {
76 // Use curl
77 $ch = curl_init();
78 curl_setopt($ch, CURLOPT_URL,$url);
79 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
80 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
81 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
82 $xml = curl_exec($ch);
83 curl_close($ch);
84 } else {
85 // Use raw sockets
86 $fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
87 if ($fp) {
88 // Send request
89 fwrite($fp, $header);
90
91 // Read response
92 $xml = "";
93 while (!feof($fp))
94 $xml .= fgets($fp, 128);
95
96 fclose($fp);
97 } else
98 echo "Could not open SSL connection to google.";
99 }
100
101 // Grab and parse content
102 $matches = array();
103 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
104
105 return $matches;
106 }
107
108 function _unhtmlentities($string) {
109 $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
110 $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
111
112 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
113 $trans_tbl = array_flip($trans_tbl);
114
115 return strtr($string, $trans_tbl);
116 }
117}
118
119// Patch in multibyte support
120if (!function_exists('mb_substr')) {
121 function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
122 $limit = strlen($str);
123
124 for ($s = 0; $start > 0;--$start) {// found the real start
125 if ($s >= $limit)
126 break;
127
128 if ($str[$s] <= "\x7F")
129 ++$s;
130 else {
131 ++$s; // skip length
132
133 while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
134 ++$s;
135 }
136 }
137
138 if ($len == '')
139 return substr($str, $s);
140 else
141 for ($e = $s; $len > 0; --$len) {//found the real end
142 if ($e >= $limit)
143 break;
144
145 if ($str[$e] <= "\x7F")
146 ++$e;
147 else {
148 ++$e;//skip length
149
150 while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
151 ++$e;
152 }
153 }
154
155 return substr($str, $s, $e - $s);
156 }
157}
158
159?>
Note: See TracBrowser for help on using the repository browser.