]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/js/tinymce/plugins/spellchecker/classes/TinyGoogleSpell.class.php
Wordpress 2.3.2-scripts
[autoinstalls/wordpress.git] / wp-includes / js / tinymce / plugins / spellchecker / classes / TinyGoogleSpell.class.php
1 <?php
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                 // Use curl if it exists
82                 if (function_exists('curl_init')) {
83                         // Use curl
84                         $ch = curl_init();
85                         curl_setopt($ch, CURLOPT_URL,$url);
86                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
87                         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
88                         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
89                         $xml = curl_exec($ch);
90                         curl_close($ch);
91                 } else {
92                         // Use raw sockets
93                         $fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
94                         if ($fp) {
95                                 // Send request
96                                 fwrite($fp, $header);
97
98                                 // Read response
99                                 $xml = "";
100                                 while (!feof($fp))
101                                         $xml .= fgets($fp, 128);
102
103                                 fclose($fp);
104                         } else 
105                                 echo "Could not open SSL connection to google.";
106                 }
107
108                 //$this->_debugData($xml);
109
110                 // Grab and parse content
111                 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
112
113                 return $matches;
114         }
115
116         function _debugData($data) {
117                 $fh = @fopen("debug.log", 'a+');
118                 @fwrite($fh, $data);
119                 @fclose($fh);
120         }
121 }
122
123 // Setup classname, should be the same as the name of the spellchecker class
124 $spellCheckerConfig['class'] = "TinyGoogleSpell";
125
126 ?>