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