Archived
1
0

Initial commit as of 2018-10-16

This commit is contained in:
Marcel
2018-10-16 18:28:42 +02:00
commit 29d7c2ffdc
3601 changed files with 358427 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
<?php
/**
* EnchantEngine.php
*
* Copyright, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
class TinyMCE_SpellChecker_EnchantEngine extends TinyMCE_SpellChecker_Engine {
/**
* Spellchecks an array of words.
*
* @param String $lang Selected language code (like en_US or de_DE). Shortcodes like "en" and "de" work with enchant >= 1.4.1
* @param Array $words Array of words to check.
* @return Name/value object with arrays of suggestions.
*/
public function getSuggestions($lang, $words) {
$suggestions = array();
$enchant = enchant_broker_init();
$config = $this->getConfig();
if (isset($config["enchant_dicts_path"])) {
enchant_broker_set_dict_path($enchant, ENCHANT_MYSPELL, $config["enchant_dicts_path"]);
enchant_broker_set_dict_path($enchant, ENCHANT_ISPELL, $config["enchant_dicts_path"]);
}
if (!enchant_broker_describe($enchant)) {
throw new Exception("Enchant spellchecker not find any backends.");
}
$lang = $this->normalizeLangCode($enchant, $lang);
if (enchant_broker_dict_exists($enchant, $lang)) {
$dict = enchant_broker_request_dict($enchant, $lang);
foreach ($words as $word) {
if (!enchant_dict_check($dict, $word)) {
$suggs = enchant_dict_suggest($dict, $word);
if (!is_array($suggs)) {
$suggs = array();
}
$suggestions[$word] = $suggs;
}
}
enchant_broker_free_dict($dict);
enchant_broker_free($enchant);
} else {
enchant_broker_free($enchant);
throw new Exception("Enchant spellchecker could not find dictionary for language: " . $lang);
}
return $suggestions;
}
/**
* Return true/false if the engine is supported by the server.
*
* @return boolean True/false if the engine is supported.
*/
public function isSupported() {
return function_exists("enchant_broker_init");
}
private function normalizeLangCode($enchant, $lang) {
$variants = array(
"en" => array("en_US", "en_GB")
);
if (isset($variants[$lang])) {
array_unshift($variants, $lang);
foreach ($variants[$lang] as $variant) {
if (enchant_broker_dict_exists($enchant, $variant)) {
return $variant;
}
}
}
return $lang;
}
}
TinyMCE_Spellchecker_Engine::add("enchant", "TinyMCE_SpellChecker_EnchantEngine");
?>

View File

@@ -0,0 +1,168 @@
<?php
/**
* Engine.php
*
* Copyright, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*
* Base class for all spellcheckers this takes in the words to check
* spelling on and returns the suggestions.
*/
class TinyMCE_SpellChecker_Engine {
private static $engines = array();
private $config;
public function __constructor($config) {
$this->config = $config;
}
/**
* Spellchecks an array of words.
*
* @param String $lang Selected language code (like en_US or de_DE). Shortcodes like "en" and "de" work with enchant >= 1.4.1
* @param Array $words Array of words to check.
* @return Name/value object with arrays of suggestions.
*/
public function getSuggestions($lang, $words) {
return array();
}
/**
* Return true/false if the engine is supported by the server.
*
* @return boolean True/false if the engine is supported.
*/
public function isSupported() {
return true;
}
/**
* Sets the config array used to create the instance.
*
* @param Array $config Name/value array with config options.
*/
public function setConfig($config) {
$this->config = $config;
}
/**
* Returns the config array used to create the instance.
*
* @return Array Name/value array with config options.
*/
public function getConfig() {
return $this->config;
}
// Static methods
public static function processRequest($tinymceSpellcheckerConfig) {
$engine = self::get($tinymceSpellcheckerConfig["engine"]);
$engine = new $engine();
$engine->setConfig($tinymceSpellcheckerConfig);
header('Content-Type: application/json');
header('Content-Encoding: UTF-8');
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
$method = self::getParam("method", "spellcheck");
$lang = self::getParam("lang", "en_US");
$text = self::getParam("text");
if ($method == "spellcheck") {
try {
if (!$text) {
throw new Exception("Missing input parameter 'text'.");
}
if (!$engine->isSupported()) {
throw new Exception("Current spellchecker isn't supported.");
}
$words = self::getWords($text);
echo json_encode((object) array(
"words" => (object) $engine->getSuggestions($lang, $words)
));
} catch (Exception $e) {
echo json_encode((object) array(
"error" => $e->getMessage()
));
}
} else {
echo json_encode((object) array(
"error" => "Invalid JSON input"
));
}
}
/**
* Returns an request value by name without magic quoting.
*
* @param String $name Name of parameter to get.
* @param String $default_value Default value to return if value not found.
* @return String request value by name without magic quoting or default value.
*/
public static function getParam($name, $default_value = false) {
if (isset($_POST[$name])) {
$req = $_POST;
} else if (isset($_GET[$name])) {
$req = $_GET;
} else {
return $default_value;
}
// Handle magic quotes
if (ini_get("magic_quotes_gpc")) {
if (is_array($req[$name])) {
$out = array();
foreach ($req[$name] as $name => $value) {
$out[stripslashes($name)] = stripslashes($value);
}
return $out;
}
return stripslashes($req[$name]);
}
return $req[$name];
}
public static function add($name, $className) {
self::$engines[$name] = $className;
}
public static function get($name) {
if (!isset(self::$engines[$name])) {
return null;
}
return self::$engines[$name];
}
public static function getWords($text) {
preg_match_all('(\w{3,})u', $text, $matches);
$words = $matches[0];
for ($i = count($words) - 1; $i >= 0; $i--) {
// Exclude words with numbers in them
if (preg_match('/[0-9]+/', $words[$i])) {
array_splice($words, $i, 1);
}
}
return $words;
}
}
?>

View File

@@ -0,0 +1,70 @@
<?php
/**
* PSpellEngine.php
*
* Copyright, Moxiecode Systems AB
* Released under LGPL License.
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
class TinyMCE_SpellChecker_PSpellEngine extends TinyMCE_SpellChecker_Engine {
/**
* Spellchecks an array of words.
*
* @param String $lang Selected language code (like en_US or de_DE). Shortcodes like "en" and "de" work with enchant >= 1.4.1
* @param Array $words Array of words to check.
* @return Name/value object with arrays of suggestions.
*/
public function getSuggestions($lang, $words) {
$config = $this->getConfig();
switch ($config['PSpell.mode']) {
case "fast":
$mode = PSPELL_FAST;
break;
case "slow":
$mode = PSPELL_SLOW;
break;
default:
$mode = PSPELL_NORMAL;
}
// Setup PSpell link
$plink = pspell_new(
$lang,
$config['pspell.spelling'],
$config['pspell.jargon'],
$config['pspell.encoding'],
$mode
);
if (!$plink) {
throw new Exception("No PSpell link found opened.");
}
$outWords = array();
foreach ($words as $word) {
if (!pspell_check($plink, trim($word))) {
$outWords[] = utf8_encode($word);
}
}
return $outWords;
}
/**
* Return true/false if the engine is supported by the server.
*
* @return boolean True/false if the engine is supported.
*/
public function isSupported() {
return function_exists("pspell_new");
}
}
TinyMCE_Spellchecker_Engine::add("pspell", "TinyMCE_SpellChecker_PSpellEngine");
?>