]> git.donarmstrong.com Git - roundcube.git/blobdiff - program/js/tiny_mce/plugins/spellchecker/classes/TinyGoogleSpell.class.php
Imported Upstream version 0.1~rc2
[roundcube.git] / program / js / tiny_mce / plugins / spellchecker / classes / TinyGoogleSpell.class.php
diff --git a/program/js/tiny_mce/plugins/spellchecker/classes/TinyGoogleSpell.class.php b/program/js/tiny_mce/plugins/spellchecker/classes/TinyGoogleSpell.class.php
new file mode 100644 (file)
index 0000000..7be9297
--- /dev/null
@@ -0,0 +1,107 @@
+<?php\r
+\r
+/* *\r
+ * Tiny Spelling Interface for TinyMCE Spell Checking.\r
+ *\r
+ * Copyright © 2006 Moxiecode Systems AB\r
+ */\r
+\r
+class TinyGoogleSpell {\r
+       var $lang;\r
+       var $spellurl;\r
+\r
+       function TinyGoogleSpell(& $config, $lang, $mode, $spelling, $jargon, $encoding) {\r
+               $this->lang = $lang;\r
+               $this->spellurl = $config['googlespell.url'];\r
+       }\r
+\r
+       // Returns array with bad words or false if failed.\r
+       function checkWords($word_array) {\r
+               $words = array ();\r
+               $wordstr = implode(' ', $word_array);\r
+\r
+               $matches = $this->_getMatches($wordstr);\r
+\r
+               for ($i = 0; $i < count($matches); $i++)\r
+                       $words[] = $this->unhtmlentities(mb_substr($wordstr, $matches[$i][1], $matches[$i][2], "UTF-8"));\r
+\r
+               return $words;\r
+       }\r
+\r
+       function unhtmlentities($string) {\r
+               $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);\r
+               $string = preg_replace('~&#([0-9]+);~e', 'chr(\\1)', $string);\r
+\r
+               $trans_tbl = get_html_translation_table(HTML_ENTITIES);\r
+               $trans_tbl = array_flip($trans_tbl);\r
+\r
+               return strtr($string, $trans_tbl);\r
+       }\r
+\r
+       // Returns array with suggestions or false if failed.\r
+       function getSuggestion($word) {\r
+               $sug = array ();\r
+\r
+               $matches = $this->_getMatches($word);\r
+\r
+               if (count($matches) > 0)\r
+                       $sug = explode("\t", utf8_encode($this->unhtmlentities($matches[0][4])));\r
+\r
+               return $sug;\r
+       }\r
+\r
+       function _xmlChars($string) {\r
+               $trans = get_html_translation_table(HTML_ENTITIES, ENT_QUOTES);\r
+\r
+               foreach ($trans as $k => $v)\r
+                       $trans[$k] = "&#" . ord($k) . ";";\r
+\r
+               return strtr($string, $trans);\r
+       }\r
+\r
+       function _getMatches($word_list) {\r
+               $url = $this->spellurl . "&" . $this->lang;\r
+\r
+               $path = preg_replace("/^https?:\/\//i", "", $url);\r
+\r
+               // Setup XML request\r
+           $xml = '<?xml version="1.0" encoding="utf-8" ?><spellrequest textalreadyclipped="0" ignoredups="0" ignoredigits="1" ignoreallcaps="1"><text>' . $word_list . '</text></spellrequest>';\r
+\r
+               $header = "POST " . $path . " HTTP/1.0 \r\n";\r
+               $header .= "MIME-Version: 1.0 \r\n";\r
+               $header .= "Content-type: application/PTI26 \r\n";\r
+               $header .= "Content-length: " . strlen($xml) . " \r\n";\r
+               $header .= "Content-transfer-encoding: text \r\n";\r
+               $header .= "Request-number: 1 \r\n";\r
+               $header .= "Document-type: Request \r\n";\r
+               $header .= "Interface-Version: Test 1.4 \r\n";\r
+               $header .= "Connection: close \r\n\r\n";\r
+               $header .= $xml;\r
+               //$this->_debugData($xml);\r
+\r
+               $ch = curl_init();\r
+               curl_setopt($ch, CURLOPT_URL, $url);\r
+               curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);\r
+               curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $header);\r
+               curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);\r
+               $xml = curl_exec($ch);\r
+               curl_close($ch);\r
+\r
+               //$this->_debugData($xml);\r
+\r
+               // Grab and parse content\r
+               preg_match_all('/<c o="([^"]*)" l="([^"]*)" s="([^"]*)">([^<]*)<\/c>/', $xml, $matches, PREG_SET_ORDER);\r
+\r
+               return $matches;\r
+       }\r
+\r
+       function _debugData($data) {\r
+               $fh = @ fopen("debug.log", 'a+');\r
+               @ fwrite($fh, $data);\r
+               @ fclose($fh);\r
+       }\r
+}\r
+\r
+// Setup classname, should be the same as the name of the spellchecker class\r
+$spellCheckerConfig['class'] = "TinyGoogleSpell";\r
+?>\r