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