raw
mp-wp_genesis           1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * $Id: editor_plugin_src.js 201 2007-02-12 15:56:56Z spocke $
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package MCManager.includes
mp-wp_genesis 6 * @author Moxiecode
mp-wp_genesis 7 */
mp-wp_genesis 8
mp-wp_genesis 9 class GoogleSpell extends SpellChecker {
mp-wp_genesis 10 /**
mp-wp_genesis 11 * Spellchecks an array of words.
mp-wp_genesis 12 *
mp-wp_genesis 13 * @param {String} $lang Language code like sv or en.
mp-wp_genesis 14 * @param {Array} $words Array of words to spellcheck.
mp-wp_genesis 15 * @return {Array} Array of misspelled words.
mp-wp_genesis 16 */
mp-wp_genesis 17 function &checkWords($lang, $words) {
mp-wp_genesis 18 $wordstr = implode(' ', $words);
mp-wp_genesis 19 $matches = $this->_getMatches($lang, $wordstr);
mp-wp_genesis 20 $words = array();
mp-wp_genesis 21
mp-wp_genesis 22 for ($i=0; $i<count($matches); $i++)
mp-wp_genesis 23 $words[] = $this->_unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));
mp-wp_genesis 24
mp-wp_genesis 25 return $words;
mp-wp_genesis 26 }
mp-wp_genesis 27
mp-wp_genesis 28 /**
mp-wp_genesis 29 * Returns suggestions of for a specific word.
mp-wp_genesis 30 *
mp-wp_genesis 31 * @param {String} $lang Language code like sv or en.
mp-wp_genesis 32 * @param {String} $word Specific word to get suggestions for.
mp-wp_genesis 33 * @return {Array} Array of suggestions for the specified word.
mp-wp_genesis 34 */
mp-wp_genesis 35 function &getSuggestions($lang, $word) {
mp-wp_genesis 36 $sug = array();
mp-wp_genesis 37 $osug = array();
mp-wp_genesis 38 $matches = $this->_getMatches($lang, $word);
mp-wp_genesis 39
mp-wp_genesis 40 if (count($matches) > 0)
mp-wp_genesis 41 $sug = explode("\t", utf8_encode($this->_unhtmlentities($matches[0][4])));
mp-wp_genesis 42
mp-wp_genesis 43 // Remove empty
mp-wp_genesis 44 foreach ($sug as $item) {
mp-wp_genesis 45 if ($item)
mp-wp_genesis 46 $osug[] = $item;
mp-wp_genesis 47 }
mp-wp_genesis 48
mp-wp_genesis 49 return $osug;
mp-wp_genesis 50 }
mp-wp_genesis 51
mp-wp_genesis 52 function &_getMatches($lang, $str) {
mp-wp_genesis 53 $server = "www.google.com";
mp-wp_genesis 54 $port = 443;
mp-wp_genesis 55 $path = "/tbproxy/spell?lang=" . $lang . "&hl=en";
mp-wp_genesis 56 $host = "www.google.com";
mp-wp_genesis 57 $url = "https://" . $server;
mp-wp_genesis 58
mp-wp_genesis 59 // Setup XML request
mp-wp_genesis 60 $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $str . '</text></spellrequest>';
mp-wp_genesis 61
mp-wp_genesis 62 $header = "POST ".$path." HTTP/1.0 \r\n";
mp-wp_genesis 63 $header .= "MIME-Version: 1.0 \r\n";
mp-wp_genesis 64 $header .= "Content-type: application/PTI26 \r\n";
mp-wp_genesis 65 $header .= "Content-length: ".strlen($xml)." \r\n";
mp-wp_genesis 66 $header .= "Content-transfer-encoding: text \r\n";
mp-wp_genesis 67 $header .= "Request-number: 1 \r\n";
mp-wp_genesis 68 $header .= "Document-type: Request \r\n";
mp-wp_genesis 69 $header .= "Interface-Version: Test 1.4 \r\n";
mp-wp_genesis 70 $header .= "Connection: close \r\n\r\n";
mp-wp_genesis 71 $header .= $xml;
mp-wp_genesis 72
mp-wp_genesis 73 // Use curl if it exists
mp-wp_genesis 74 if (function_exists('curl_init')) {
mp-wp_genesis 75 // Use curl
mp-wp_genesis 76 $ch = curl_init();
mp-wp_genesis 77 curl_setopt($ch, CURLOPT_URL,$url);
mp-wp_genesis 78 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
mp-wp_genesis 79 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);
mp-wp_genesis 80 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
mp-wp_genesis 81 $xml = curl_exec($ch);
mp-wp_genesis 82 curl_close($ch);
mp-wp_genesis 83 } else {
mp-wp_genesis 84 // Use raw sockets
mp-wp_genesis 85 $fp = fsockopen("ssl://" . $server, $port, $errno, $errstr, 30);
mp-wp_genesis 86 if ($fp) {
mp-wp_genesis 87 // Send request
mp-wp_genesis 88 fwrite($fp, $header);
mp-wp_genesis 89
mp-wp_genesis 90 // Read response
mp-wp_genesis 91 $xml = "";
mp-wp_genesis 92 while (!feof($fp))
mp-wp_genesis 93 $xml .= fgets($fp, 128);
mp-wp_genesis 94
mp-wp_genesis 95 fclose($fp);
mp-wp_genesis 96 } else
mp-wp_genesis 97 echo "Could not open SSL connection to google.";
mp-wp_genesis 98 }
mp-wp_genesis 99
mp-wp_genesis 100 // Grab and parse content
mp-wp_genesis 101 $matches = array();
mp-wp_genesis 102 preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);
mp-wp_genesis 103
mp-wp_genesis 104 return $matches;
mp-wp_genesis 105 }
mp-wp_genesis 106
mp-wp_genesis 107 function _unhtmlentities($string) {
mp-wp_genesis 108 $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
mp-wp_genesis 109 $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);
mp-wp_genesis 110
mp-wp_genesis 111 $trans_tbl = get_html_translation_table(HTML_ENTITIES);
mp-wp_genesis 112 $trans_tbl = array_flip($trans_tbl);
mp-wp_genesis 113
mp-wp_genesis 114 return strtr($string, $trans_tbl);
mp-wp_genesis 115 }
mp-wp_genesis 116 }
mp-wp_genesis 117
mp-wp_genesis 118 // Patch in multibyte support
mp-wp_genesis 119 if (!function_exists('mb_substr')) {
mp-wp_genesis 120 function mb_substr($str, $start, $len = '', $encoding="UTF-8"){
mp-wp_genesis 121 $limit = strlen($str);
mp-wp_genesis 122
mp-wp_genesis 123 for ($s = 0; $start > 0;--$start) {// found the real start
mp-wp_genesis 124 if ($s >= $limit)
mp-wp_genesis 125 break;
mp-wp_genesis 126
mp-wp_genesis 127 if ($str[$s] <= "\x7F")
mp-wp_genesis 128 ++$s;
mp-wp_genesis 129 else {
mp-wp_genesis 130 ++$s; // skip length
mp-wp_genesis 131
mp-wp_genesis 132 while ($str[$s] >= "\x80" && $str[$s] <= "\xBF")
mp-wp_genesis 133 ++$s;
mp-wp_genesis 134 }
mp-wp_genesis 135 }
mp-wp_genesis 136
mp-wp_genesis 137 if ($len == '')
mp-wp_genesis 138 return substr($str, $s);
mp-wp_genesis 139 else
mp-wp_genesis 140 for ($e = $s; $len > 0; --$len) {//found the real end
mp-wp_genesis 141 if ($e >= $limit)
mp-wp_genesis 142 break;
mp-wp_genesis 143
mp-wp_genesis 144 if ($str[$e] <= "\x7F")
mp-wp_genesis 145 ++$e;
mp-wp_genesis 146 else {
mp-wp_genesis 147 ++$e;//skip length
mp-wp_genesis 148
mp-wp_genesis 149 while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit)
mp-wp_genesis 150 ++$e;
mp-wp_genesis 151 }
mp-wp_genesis 152 }
mp-wp_genesis 153
mp-wp_genesis 154 return substr($str, $s, $e - $s);
mp-wp_genesis 155 }
mp-wp_genesis 156 }
mp-wp_genesis 157
mp-wp_genesis 158 ?>