Created
August 1, 2013 10:18
-
-
Save colymba/6130162 to your computer and use it in GitHub Desktop.
TinyMCE GoogleSpell PHP RPC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* @package MCManager.includes | |
* @author Thierry Francois @colymba | |
* @author Moxiecode | |
* @copyright Copyright © 2004-2007, Moxiecode Systems AB, All rights reserved. | |
*/ | |
class GoogleSpell extends SpellChecker { | |
/** | |
* Spellchecks an array of words. | |
* | |
* @param {String} $lang Language code like sv or en. | |
* @param {Array} $words Array of words to spellcheck. | |
* @return {Array} Array of misspelled words. | |
*/ | |
function &checkWords($lang, $words) { | |
$wordstr = implode(' ', $words); | |
$matches = $this->_getMatches($lang, $wordstr); | |
$words = array(); | |
for ($i=0; $i<count($matches); $i++) | |
$words[] = $this->_unhtmlentities(mb_substr($wordstr, $matches[$i]['charStart'], $matches[$i]['charLength'], "UTF-8")); | |
return $words; | |
} | |
/** | |
* Returns suggestions of for a specific word. | |
* | |
* @param {String} $lang Language code like sv or en. | |
* @param {String} $word Specific word to get suggestions for. | |
* @return {Array} Array of suggestions for the specified word. | |
*/ | |
function &getSuggestions($lang, $word) { | |
$sug = array(); | |
$osug = array(); | |
$matches = $this->_getMatches($lang, $word); | |
foreach ($matches[0]['suggestions'][0] as $type => $word) { | |
array_push($sug, $word); | |
} | |
return $osug; | |
} | |
function &_getMatches($lang, $str) { | |
$server = "www.googleapis.com"; | |
$port = 443; | |
$path = "/rpc"; | |
$url = "https://" . $server . ":" . $port . $path; | |
$requestData = json_encode( array( | |
"method" => "spelling.check", | |
"apiVersion" => "v2", | |
"params" => array( | |
"language" => $lang, | |
"text" => $str, | |
"key" => "AIzaSyCLlKc60a3z7lo8deV-hAyDU7rHYgL4HZg" | |
) | |
)); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL,$url); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestData); | |
$response = curl_exec($ch); | |
curl_close($ch); | |
$response = json_decode( $response, true ); | |
return $response['result']['spellingCheckResponse']['misspellings']; | |
} | |
function _unhtmlentities($string) { | |
$string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string); | |
$string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string); | |
$trans_tbl = get_html_translation_table(HTML_ENTITIES); | |
$trans_tbl = array_flip($trans_tbl); | |
return strtr($string, $trans_tbl); | |
} | |
} | |
// Patch in multibyte support | |
if (!function_exists('mb_substr')) { | |
function mb_substr($str, $start, $len = '', $encoding="UTF-8"){ | |
$limit = strlen($str); | |
for ($s = 0; $start > 0;--$start) {// found the real start | |
if ($s >= $limit) | |
break; | |
if ($str[$s] <= "\x7F") | |
++$s; | |
else { | |
++$s; // skip length | |
while ($str[$s] >= "\x80" && $str[$s] <= "\xBF") | |
++$s; | |
} | |
} | |
if ($len == '') | |
return substr($str, $s); | |
else | |
for ($e = $s; $len > 0; --$len) {//found the real end | |
if ($e >= $limit) | |
break; | |
if ($str[$e] <= "\x7F") | |
++$e; | |
else { | |
++$e;//skip length | |
while ($str[$e] >= "\x80" && $str[$e] <= "\xBF" && $e < $limit) | |
++$e; | |
} | |
} | |
return substr($str, $s, $e - $s); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment