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