Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/editor/tinymce/tiny_mce/3.4.6/plugins/spellchecker/classes/GoogleSpell.php
Go to the documentation of this file.
00001 <?php
00010 class GoogleSpell extends SpellChecker {
00018         function &checkWords($lang, $words) {
00019                 $wordstr = implode(' ', $words);
00020                 $matches = $this->_getMatches($lang, $wordstr);
00021                 $words = array();
00022 
00023                 for ($i=0; $i<count($matches); $i++)
00024                         $words[] = $this->_unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));
00025 
00026                 return $words;
00027         }
00028 
00036         function &getSuggestions($lang, $word) {
00037                 $sug = array();
00038                 $osug = array();
00039                 $matches = $this->_getMatches($lang, $word);
00040 
00041                 if (count($matches) > 0)
00042                         $sug = explode("\t", utf8_encode($this->_unhtmlentities($matches[0][4])));
00043 
00044                 // Remove empty
00045                 foreach ($sug as $item) {
00046                         if ($item)
00047                                 $osug[] = $item;
00048                 }
00049 
00050                 return $osug;
00051         }
00052 
00053         function &_getMatches($lang, $str) {
00054                 $server = "www.google.com";
00055                 $port = 443;
00056                 $path = "/tbproxy/spell?lang=" . $lang . "&hl=en";
00057                 $host = "www.google.com";
00058                 $url = "https://" . $server;
00059 
00060                 // Setup XML request
00061                 $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $str . '</text></spellrequest>';
00062 
00063                 $header  = "POST ".$path." HTTP/1.0 \r\n";
00064                 $header .= "MIME-Version: 1.0 \r\n";
00065                 $header .= "Content-type: application/PTI26 \r\n";
00066                 $header .= "Content-length: ".strlen($xml)." \r\n";
00067                 $header .= "Content-transfer-encoding: text \r\n";
00068                 $header .= "Request-number: 1 \r\n";
00069                 $header .= "Document-type: Request \r\n";
00070                 $header .= "Interface-Version: Test 1.4 \r\n";
00071                 $header .= "Connection: close \r\n\r\n";
00072                 $header .= $xml;
00073 
00074                 // Use curl if it exists
00075                 if (function_exists('curl_init')) {
00076                         // Use curl
00077                         $ch = curl_init();
00078                         curl_setopt($ch, CURLOPT_URL,$url);
00079                         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
00080                         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
00081                         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
00082                         if (!empty($this->_config['GoogleSpell.proxyhost'])) {
00083                                 if (!empty($this->_config['GoogleSpell.proxytype']) and ($this->_config['GoogleSpell.proxytype'] === 'SOCKS5')) {
00084                     curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
00085                                 } else {
00086                     curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTML);
00087                     curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, FALSE);
00088                 }
00089                                 if (empty($this->_config['GoogleSpell.proxyport'])) {
00090                                         curl_setopt($ch, CURLOPT_PROXY, $this->_config['GoogleSpell.proxyhost']);
00091                                 } else {
00092                                         curl_setopt($ch, CURLOPT_PROXY, $this->_config['GoogleSpell.proxyhost'].':'.$this->_config['GoogleSpell.proxyport']);
00093                                 }
00094                                 if (!empty($this->_config['GoogleSpell.proxyuser']) and !empty($this->_config['GoogleSpell.proxypassword'])) {
00095                                         curl_setopt($ch, CURLOPT_PROXYUSERPWD, $this->_config['GoogleSpell.proxyuser'].':'.$this->_config['GoogleSpell.proxypassword']);
00096                                         if (defined('CURLOPT_PROXYAUTH')) {
00097                                                 // any proxy authentication if PHP 5.1
00098                                                 curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
00099                                         }
00100                                 }
00101                         }
00102                         $xml = curl_exec($ch);
00103                         curl_close($ch);
00104                 } else {
00105                         // Use raw sockets
00106                         $fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
00107                         if ($fp) {
00108                                 // Send request
00109                                 fwrite($fp, $header);
00110 
00111                                 // Read response
00112                                 $xml = "";
00113                                 while (!feof($fp))
00114                                         $xml .= fgets($fp, 128);
00115 
00116                                 fclose($fp);
00117                         } else
00118                                 echo "Could not open SSL connection to google.";
00119                 }
00120 
00121                 // Grab and parse content
00122                 $matches = array();
00123                 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
00124 
00125                 return $matches;
00126         }
00127 
00128         function _unhtmlentities($string) {
00129                 $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
00130                 $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
00131 
00132                 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
00133                 $trans_tbl = array_flip($trans_tbl);
00134 
00135                 return strtr($string, $trans_tbl);
00136         }
00137 }
00138 
00139 // Patch in multibyte support
00140 if (!function_exists('mb_substr')) {
00141         function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
00142                 $limit = strlen($str);
00143 
00144                 for ($s = 0; $start > 0;--$start) {// found the real start
00145                         if ($s >= $limit)
00146                                 break;
00147 
00148                         if ($str[$s] <= "\x7F")
00149                                 ++$s;
00150                         else {
00151                                 ++$s; // skip length
00152 
00153                                 while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
00154                                         ++$s;
00155                         }
00156                 }
00157 
00158                 if ($len == '')
00159                         return substr($str, $s);
00160                 else
00161                         for ($e = $s; $len > 0; --$len) {//found the real end
00162                                 if ($e >= $limit)
00163                                         break;
00164 
00165                                 if ($str[$e] <= "\x7F")
00166                                         ++$e;
00167                                 else {
00168                                         ++$e;//skip length
00169 
00170                                         while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
00171                                                 ++$e;
00172                                 }
00173                         }
00174 
00175                 return substr($str, $s, $e - $s);
00176         }
00177 }
00178 
00179 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations