]> git.donarmstrong.com Git - roundcube.git/blob - program/js/tiny_mce/plugins/spellchecker/tinyspell.php
Imported Upstream version 0.1~rc2
[roundcube.git] / program / js / tiny_mce / plugins / spellchecker / tinyspell.php
1 <?php\r
2 /**\r
3  * $RCSfile: tinyspell.php,v $\r
4  * $Revision: 1.1 $\r
5  * $Date: 2006/03/14 17:33:47 $\r
6  *\r
7  * @author Moxiecode\r
8  * @copyright Copyright © 2004-2006, Moxiecode Systems AB, All rights reserved.\r
9  */\r
10 \r
11         // Ignore the Notice errors for now.\r
12         error_reporting(E_ALL ^ E_NOTICE);\r
13 \r
14         require_once("config.php");\r
15 \r
16         $id = sanitize($_POST['id'], "loose");\r
17 \r
18         if (!$spellCheckerConfig['enabled']) {\r
19                 header('Content-type: text/xml; charset=utf-8');\r
20                 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
21                 die;\r
22         }\r
23 \r
24         // Basic config\r
25         $defaultLanguage = $spellCheckerConfig['default.language'];\r
26         $defaultMode = $spellCheckerConfig['default.mode'];\r
27 \r
28         // Normaly not required to configure\r
29         $defaultSpelling = $spellCheckerConfig['default.spelling'];\r
30         $defaultJargon = $spellCheckerConfig['default.jargon'];\r
31         $defaultEncoding = $spellCheckerConfig['default.encoding'];\r
32         $outputType = "xml"; // Do not change\r
33 \r
34         // Get input parameters.\r
35 \r
36         $check = urldecode($_REQUEST['check']);\r
37         $cmd = sanitize($_REQUEST['cmd']);\r
38         $lang = sanitize($_REQUEST['lang'], "strict");\r
39         $mode = sanitize($_REQUEST['mode'], "strict");\r
40         $spelling = sanitize($_REQUEST['spelling'], "strict");\r
41         $jargon = sanitize($_REQUEST['jargon'], "strict");\r
42         $encoding = sanitize($_REQUEST['encoding'], "strict");\r
43         $sg = sanitize($_REQUEST['sg'], "bool");\r
44         $words = array();\r
45 \r
46         $validRequest = true;\r
47 \r
48         if (empty($check))\r
49                 $validRequest = false;\r
50 \r
51         if (empty($lang))\r
52                 $lang = $defaultLanguage;\r
53 \r
54         if (empty($mode))\r
55                 $mode = $defaultMode;\r
56 \r
57         if (empty($spelling))\r
58                 $spelling = $defaultSpelling;\r
59 \r
60         if (empty($jargon))\r
61                 $jargon = $defaultJargon;\r
62 \r
63         if (empty($encoding))\r
64                 $encoding = $defaultEncoding;\r
65 \r
66         function sanitize($str, $type="strict") {\r
67                 switch ($type) {\r
68                         case "strict":\r
69                                 $str = preg_replace("/[^a-zA-Z0-9_\-]/i", "", $str);\r
70                         break;\r
71                         case "loose":\r
72                                 $str = preg_replace("/</i", "&gt;", $str);\r
73                                 $str = preg_replace("/>/i", "&lt;", $str);\r
74                         break;\r
75                         case "bool":\r
76                                 if ($str == "true" || $str == true)\r
77                                         $str = true;\r
78                                 else\r
79                                         $str = false;\r
80                         break;\r
81                 }\r
82 \r
83                 return $str;\r
84         }\r
85 \r
86         $result = array();\r
87         $tinyspell = new $spellCheckerConfig['class']($spellCheckerConfig, $lang, $mode, $spelling, $jargon, $encoding);\r
88 \r
89         if (count($tinyspell->errorMsg) == 0) {\r
90                 switch($cmd) {\r
91                         case "spell":\r
92                                 // Space for non-exec version and \n for the exec version.\r
93                                 $words = preg_split("/ |\n/", $check, -1, PREG_SPLIT_NO_EMPTY);\r
94                                 $result = $tinyspell->checkWords($words);\r
95                         break;\r
96         \r
97                         case "suggest":\r
98                                 $result = $tinyspell->getSuggestion($check);\r
99                         break;\r
100 \r
101                         default:\r
102                                 // Just use this for now.\r
103                                 $tinyspell->errorMsg[] = "No command.";\r
104                                 $outputType = $outputType . "error";\r
105                         break;\r
106                 }\r
107         } else\r
108                 $outputType = $outputType . "error";\r
109 \r
110         if (!$result)\r
111                 $result = array();\r
112 \r
113         // Output data\r
114         switch($outputType) {\r
115                 case "xml":\r
116                         header('Content-type: text/xml; charset=utf-8');\r
117                         $body  = '<?xml version="1.0" encoding="utf-8" ?>';\r
118                         $body .= "\n";\r
119                         \r
120                         if (count($result) == 0)\r
121                                 $body .= '<res id="' . $id . '" cmd="'. $cmd .'" />';\r
122                         else\r
123                                 $body .= '<res id="' . $id . '" cmd="'. $cmd .'">'. urlencode(implode(" ", $result)) .'</res>';\r
124 \r
125                         echo $body;\r
126                 break;\r
127                 case "xmlerror";\r
128                         header('Content-type: text/xml; charset=utf-8');\r
129                         $body  = '<?xml version="1.0" encoding="utf-8" ?>';\r
130                         $body .= "\n";\r
131                         $body .= '<res id="' . $id . '" cmd="'. $cmd .'" error="true" msg="'. implode(" ", $tinyspell->errorMsg) .'" />';\r
132                         echo $body;\r
133                 break;\r
134                 case "html":\r
135                         var_dump($result);\r
136                 break;\r
137                 case "htmlerror":\r
138                         echo "Error";\r
139                 break;\r
140         }\r
141 \r
142 ?>\r