MDL-29081 add proxy support to googlespell
[moodle.git] / lib / editor / tinymce / tiny_mce / 3.4.5 / plugins / spellchecker / classes / GoogleSpell.php
CommitLineData
fb916bf0
PS
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);
8f9f70fb
PS
82 if (!empty($this->_config['GoogleSpell.proxyhost'])) {
83 if (!empty($this->_config['GoogleSpell.proxytype']) and ($this->_config['GoogleSpell.proxytype'] === 'SOCKS5')) {
84 curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
85 } else {
86 curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTML);
87 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, FALSE);
88 }
89 if (empty($this->_config['GoogleSpell.proxyport'])) {
90 curl_setopt($ch, CURLOPT_PROXY, $this->_config['GoogleSpell.proxyhost']);
91 } else {
92 curl_setopt($ch, CURLOPT_PROXY, $this->_config['GoogleSpell.proxyhost'].':'.$this->_config['GoogleSpell.proxyport']);
93 }
94 if (!empty($this->_config['GoogleSpell.proxyuser']) and !empty($this->_config['GoogleSpell.proxypassword'])) {
95 curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->_config['GoogleSpell.proxyuser'].':'.$this->_config['GoogleSpell.proxypassword']);
96 if (defined('CURLOPT_PROXYAUTH')) {
97 // any proxy authentication if PHP 5.1
98 curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
99 }
100 }
101 }
fb916bf0
PS
102 $xml = curl_exec($ch);
103 curl_close($ch);
104 } else {
105 // Use raw sockets
106 $fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
107 if ($fp) {
108 // Send request
109 fwrite($fp, $header);
110
111 // Read response
112 $xml = "";
113 while (!feof($fp))
114 $xml .= fgets($fp, 128);
115
116 fclose($fp);
117 } else
118 echo "Could not open SSL connection to google.";
119 }
120
121 // Grab and parse content
122 $matches = array();
123 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
124
125 return $matches;
126 }
127
128 function _unhtmlentities($string) {
129 $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
130 $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
131
132 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
133 $trans_tbl = array_flip($trans_tbl);
134
135 return strtr($string, $trans_tbl);
136 }
137}
138
139// Patch in multibyte support
140if (!function_exists('mb_substr')) {
141 function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
142 $limit = strlen($str);
143
144 for ($s = 0; $start > 0;--$start) {// found the real start
145 if ($s >= $limit)
146 break;
147
148 if ($str[$s] <= "\x7F")
149 ++$s;
150 else {
151 ++$s; // skip length
152
153 while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
154 ++$s;
155 }
156 }
157
158 if ($len == '')
159 return substr($str, $s);
160 else
161 for ($e = $s; $len > 0; --$len) {//found the real end
162 if ($e >= $limit)
163 break;
164
165 if ($str[$e] <= "\x7F")
166 ++$e;
167 else {
168 ++$e;//skip length
169
170 while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
171 ++$e;
172 }
173 }
174
175 return substr($str, $s, $e - $s);
176 }
177}
178
179?>