]> git.donarmstrong.com Git - roundcube.git/blobdiff - program/js/tiny_mce/plugins/spellchecker/tinyspell.php
Imported Upstream version 0.1~rc2
[roundcube.git] / program / js / tiny_mce / plugins / spellchecker / tinyspell.php
diff --git a/program/js/tiny_mce/plugins/spellchecker/tinyspell.php b/program/js/tiny_mce/plugins/spellchecker/tinyspell.php
new file mode 100644 (file)
index 0000000..18345e6
--- /dev/null
@@ -0,0 +1,142 @@
+<?php\r
+/**\r
+ * $RCSfile: tinyspell.php,v $\r
+ * $Revision: 1.1 $\r
+ * $Date: 2006/03/14 17:33:47 $\r
+ *\r
+ * @author Moxiecode\r
+ * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.\r
+ */\r
+\r
+       // Ignore the Notice errors for now.\r
+       error_reporting(E_ALL ^ E_NOTICE);\r
+\r
+       require_once("config.php");\r
+\r
+       $id = sanitize($_POST['id'], "loose");\r
+\r
+       if (!$spellCheckerConfig['enabled']) {\r
+               header('Content-type: text/xml; charset=utf-8');\r
+               echo '<?xml version="1.0" encoding="utf-8" ?><res id="' . $id . '" error="true" msg="You must enable the spellchecker by modifying the config.php file." />';\r
+               die;\r
+       }\r
+\r
+       // Basic config\r
+       $defaultLanguage = $spellCheckerConfig['default.language'];\r
+       $defaultMode = $spellCheckerConfig['default.mode'];\r
+\r
+       // Normaly not required to configure\r
+       $defaultSpelling = $spellCheckerConfig['default.spelling'];\r
+       $defaultJargon = $spellCheckerConfig['default.jargon'];\r
+       $defaultEncoding = $spellCheckerConfig['default.encoding'];\r
+       $outputType = "xml"; // Do not change\r
+\r
+       // Get input parameters.\r
+\r
+       $check = urldecode($_REQUEST['check']);\r
+       $cmd = sanitize($_REQUEST['cmd']);\r
+       $lang = sanitize($_REQUEST['lang'], "strict");\r
+       $mode = sanitize($_REQUEST['mode'], "strict");\r
+       $spelling = sanitize($_REQUEST['spelling'], "strict");\r
+       $jargon = sanitize($_REQUEST['jargon'], "strict");\r
+       $encoding = sanitize($_REQUEST['encoding'], "strict");\r
+       $sg = sanitize($_REQUEST['sg'], "bool");\r
+       $words = array();\r
+\r
+       $validRequest = true;\r
+\r
+       if (empty($check))\r
+               $validRequest = false;\r
+\r
+       if (empty($lang))\r
+               $lang = $defaultLanguage;\r
+\r
+       if (empty($mode))\r
+               $mode = $defaultMode;\r
+\r
+       if (empty($spelling))\r
+               $spelling = $defaultSpelling;\r
+\r
+       if (empty($jargon))\r
+               $jargon = $defaultJargon;\r
+\r
+       if (empty($encoding))\r
+               $encoding = $defaultEncoding;\r
+\r
+       function sanitize($str, $type="strict") {\r
+               switch ($type) {\r
+                       case "strict":\r
+                               $str = preg_replace("/[^a-zA-Z0-9_\-]/i", "", $str);\r
+                       break;\r
+                       case "loose":\r
+                               $str = preg_replace("/</i", "&gt;", $str);\r
+                               $str = preg_replace("/>/i", "&lt;", $str);\r
+                       break;\r
+                       case "bool":\r
+                               if ($str == "true" || $str == true)\r
+                                       $str = true;\r
+                               else\r
+                                       $str = false;\r
+                       break;\r
+               }\r
+\r
+               return $str;\r
+       }\r
+\r
+       $result = array();\r
+       $tinyspell = new $spellCheckerConfig['class']($spellCheckerConfig, $lang, $mode, $spelling, $jargon, $encoding);\r
+\r
+       if (count($tinyspell->errorMsg) == 0) {\r
+               switch($cmd) {\r
+                       case "spell":\r
+                               // Space for non-exec version and \n for the exec version.\r
+                               $words = preg_split("/ |\n/", $check, -1, PREG_SPLIT_NO_EMPTY);\r
+                               $result = $tinyspell->checkWords($words);\r
+                       break;\r
+       \r
+                       case "suggest":\r
+                               $result = $tinyspell->getSuggestion($check);\r
+                       break;\r
+\r
+                       default:\r
+                               // Just use this for now.\r
+                               $tinyspell->errorMsg[] = "No command.";\r
+                               $outputType = $outputType . "error";\r
+                       break;\r
+               }\r
+       } else\r
+               $outputType = $outputType . "error";\r
+\r
+       if (!$result)\r
+               $result = array();\r
+\r
+       // Output data\r
+       switch($outputType) {\r
+               case "xml":\r
+                       header('Content-type: text/xml; charset=utf-8');\r
+                       $body  = '<?xml version="1.0" encoding="utf-8" ?>';\r
+                       $body .= "\n";\r
+                       \r
+                       if (count($result) == 0)\r
+                               $body .= '<res id="' . $id . '" cmd="'. $cmd .'" />';\r
+                       else\r
+                               $body .= '<res id="' . $id . '" cmd="'. $cmd .'">'. urlencode(implode(" ", $result)) .'</res>';\r
+\r
+                       echo $body;\r
+               break;\r
+               case "xmlerror";\r
+                       header('Content-type: text/xml; charset=utf-8');\r
+                       $body  = '<?xml version="1.0" encoding="utf-8" ?>';\r
+                       $body .= "\n";\r
+                       $body .= '<res id="' . $id . '" cmd="'. $cmd .'" error="true" msg="'. implode(" ", $tinyspell->errorMsg) .'" />';\r
+                       echo $body;\r
+               break;\r
+               case "html":\r
+                       var_dump($result);\r
+               break;\r
+               case "htmlerror":\r
+                       echo "Error";\r
+               break;\r
+       }\r
+\r
+?>\r