]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/spellchecker/classes/GoogleSpell.php
Wordpress 3.5
[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  * @package MCManager.includes
6  * @author Moxiecode
7  * @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved.
8  */
9
10 class 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         protected function &_getMatches($lang, $str) {
54                 $lang = preg_replace('/[^a-z\-]/i', '', $lang);
55                 $str = preg_replace('/[\x00-\x1F\x7F]/', '', $str);
56                 $server = "www.google.com";
57                 $port = 443;
58                 $path = "/tbproxy/spell?lang=" . $lang . "&hl=en";
59                 $host = "www.google.com";
60                 $url = "https://" . $server;
61
62                 // Setup XML request
63                 $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $str . '</text></spellrequest>';
64
65                 $header  = "POST ".$path." HTTP/1.0 \r\n";
66                 $header .= "MIME-Version: 1.0 \r\n";
67                 $header .= "Content-type: application/PTI26 \r\n";
68                 $header .= "Content-length: ".strlen($xml)." \r\n";
69                 $header .= "Content-transfer-encoding: text \r\n";
70                 $header .= "Request-number: 1 \r\n";
71                 $header .= "Document-type: Request \r\n";
72                 $header .= "Interface-Version: Test 1.4 \r\n";
73                 $header .= "Connection: close \r\n\r\n";
74                 $header .= $xml;
75
76                 // Use curl if it exists
77                 if (function_exists('curl_init')) {
78                         // Use curl
79                         $ch = curl_init();
80                         curl_setopt($ch, CURLOPT_URL,$url);
81                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
82                         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
83                         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
84                         $xml = curl_exec($ch);
85                         curl_close($ch);
86                 } else {
87                         // Use raw sockets
88                         $fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
89                         if ($fp) {
90                                 // Send request
91                                 fwrite($fp, $header);
92
93                                 // Read response
94                                 $xml = "";
95                                 while (!feof($fp))
96                                         $xml .= fgets($fp, 128);
97
98                                 fclose($fp);
99                         } else
100                                 echo "Could not open SSL connection to google.";
101                 }
102
103                 // Grab and parse content
104                 $matches = array();
105                 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
106
107                 return $matches;
108         }
109
110         protected function _unhtmlentities($string) {
111                 $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
112                 $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
113
114                 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
115                 $trans_tbl = array_flip($trans_tbl);
116
117                 return strtr($string, $trans_tbl);
118         }
119 }
120
121 // Patch in multibyte support
122 if (!function_exists('mb_substr')) {
123         function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
124                 $limit = strlen($str);
125
126                 for ($s = 0; $start > 0;--$start) {// found the real start
127                         if ($s >= $limit)
128                                 break;
129
130                         if ($str[$s] <= "\x7F")
131                                 ++$s;
132                         else {
133                                 ++$s; // skip length
134
135                                 while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
136                                         ++$s;
137                         }
138                 }
139
140                 if ($len == '')
141                         return substr($str, $s);
142                 else
143                         for ($e = $s; $len > 0; --$len) {//found the real end
144                                 if ($e >= $limit)
145                                         break;
146
147                                 if ($str[$e] <= "\x7F")
148                                         ++$e;
149                                 else {
150                                         ++$e;//skip length
151
152                                         while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
153                                                 ++$e;
154                                 }
155                         }
156
157                 return substr($str, $s, $e - $s);
158         }
159 }
160
161 ?>