One more
[squirrelmail.git] / plugins / squirrelspell / modules / check_me.mod.php
CommitLineData
849bdf42 1<?php
2
3 /**
4 CHECK_ME.MOD.PHP
5 -----------------
6 This module is the main workhorse of SquirrelSpell. It submits
7 the message to the spell-checker, parses the output, and loads
8 the interface window.
9 **/
10// Declaring globals for E_ALL.
11global $sqspell_text, $SQSPELL_APP, $sqspell_use_app, $attachment_dir,
12 $username, $SQSPELL_EREG, $color;
13
14 // Now we explode the lines for three reasons:
15 // 1) So we can ignore lines starting with ">" (reply's)
16 // 2) So we can stop processing when we get to "--" on a single line,
17 // which means that the signature is starting
18 // 3) So we can add an extra space at the beginning of each line. This way
19 // ispell/aspell don't treat these as command characters.
20 $sqspell_raw_lines = explode("\n", $sqspell_text);
21 for ($i=0; $i<sizeof($sqspell_raw_lines); $i++){
22 if (trim($sqspell_raw_lines[$i]) == "--") break;
23 if(substr($sqspell_raw_lines[$i], 0, 1) != ">")
24 $sqspell_new_lines[$i] = " " . $sqspell_raw_lines[$i];
25 else $sqspell_new_lines[$i] = "";
26 }
27 $sqspell_new_text=implode("\n", $sqspell_new_lines);
28
29 // Define the command used to spellcheck the document.
30 $sqspell_command=$SQSPELL_APP[$sqspell_use_app];
31 // For the simplicity's sake we'll put all text into a file
32 // in attachment_dir directory, then cat it and pipe it to sqspell_command.
33 // There are other ways to do it, including popen(), but it's unidirectional
34 // and no fun at all.
35 // NOTE: This will probably change in future releases of squirrelspell
36 // for privacy reasons.
37 //
38 $floc = "$attachment_dir/$username" . "_sqspell_data.txt";
39 $fp=fopen($floc, "w");
40 fwrite($fp, $sqspell_new_text);
41 fclose($fp);
42 exec("cat $floc | $sqspell_command", $sqspell_output);
43 unlink($floc);
44
45 // Load the user dictionary.
46 $words=sqspell_getLang(sqspell_getWords(), $sqspell_use_app);
47 // define some variables.
48 $current_line=0;
49 $missed_words=Array();
50 $misses = Array();
51 $locations = Array();
52 $errors=0;
53 // Now we process the output of sqspell_command (ispell or aspell
54 // in ispell compatibility mode, whichever).
55 for ($i=0; $i<sizeof($sqspell_output); $i++){
56 switch (substr($sqspell_output[$i], 0, 1)){
57 case "":
58 // Ispell adds empty lines when an end of line is reached
59 $current_line++;
60 break;
61
62 case "&":
63 // This means there's a misspelled word and a few suggestions.
64 list($left, $right) = explode(": ", $sqspell_output[$i]);
65 $tmparray = explode(" ", $left);
66 $sqspell_word=$tmparray[1];
67 // Check if the word is in user dictionary.
68 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
69 $sqspell_symb=intval($tmparray[3])-1;
70 if (!$misses[$sqspell_word]) {
71 $misses[$sqspell_word] = $right;
72 $missed_words[$errors] = $sqspell_word;
73 $errors++;
74 }
75 if ($locations[$sqspell_word]) $locations[$sqspell_word] .= ", ";
76 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
77 }
78 break;
79
80 case "#":
81 // This means a misspelled word and no suggestions.
82 $tmparray = explode(" ", $sqspell_output[$i]);
83 $sqspell_word=$tmparray[1];
84 // Check if the word is in user dictionary.
85 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
86 $sqspell_symb=intval($tmparray[2])-1;
87 if (!$misses[$sqspell_word]) {
88 $misses[$sqspell_word] = "_NONE";
89 $missed_words[$errors] = $sqspell_word;
90 $errors++;
91 }
92 if ($locations[$sqspell_word]) $locations[$sqspell_word] .= ", ";
93 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
94 }
95 break;
96 }
97 }
98
99 if ($errors){
100 // So, there are errors
101 // This is the only place where the generic GUI-wrapper is not
102 // called, but generated right here. This is due to the complexity
103 // of the output.
104 ?>
105 <html>
106 <head>
107 <title>SquirrelSpell Results</title>
108 <script type="text/javascript">
109 // Load the spelling errors into JavaScript arrays
110 <!--
111 <?php
112 $sqspell_lines = explode("\n", $sqspell_text);
113 echo "// All lines of the message
114 var sqspell_lines=new Array();\n";
115 for ($i=0; $i<sizeof($sqspell_lines); $i++){
116 echo "sqspell_lines[$i] = \"" . chop(addslashes($sqspell_lines[$i])) . "\";\n";
117 }
118
119 echo "\n\n";
120 echo "// Misses are all misspelled words
121 var misses=new Array();\n";
122 for ($i=0; $i<sizeof($missed_words); $i++){
123 echo "misses[$i] = \"" . $missed_words[$i] . "\";\n";
124 }
125
126 echo "\n\n";
127 echo "// Suggestions are (guess what!) suggestions for misspellings
128 var suggestions = new Array();\n";
129 $i=0;
130 while (list($word, $value) = each($misses)){
131 if ($value=="_NONE") $value="";
132 echo "suggestions[$i] = \"$value\";\n";
133 $i++;
134 }
135
136 echo "\n\n";
137 echo "// Locations are where those misspellings are located, line:symbol
138 var locations= new Array();\n";
139 $i=0;
140 while (list($word, $value) = each($locations)){
141 echo "locations[$i] = \"$value\";\n";
142 $i++;
143 }
144 ?>
145 // Why isn't there a booger fairy?
146 //-->
147 </script>
148 <script src="js/check_me.js" type="text/javascript"></script>
149 </head>
150 <?php
151 printf('<body bgcolor="%s" text="%s" link="%s" alink="%s" vlink="%s" onload="populateSqspellForm()">', $color[4], $color[8], $color[7], $color[7], $color[7]);
152 ?>
153 <table width="100%" border="0" cellpadding="2">
154 <tr><td bgcolor="<?php echo $color[9] ?>" align="center"><b>Found <?php
155 echo $errors ?> errors</b></td></tr>
156 <tr><td><hr></td></tr>
157 <tr><td>
158 <form method="post">
159 <input type="hidden" name="MOD" value="forget_me_not">
160 <input type="hidden" name="words" value="">
161 <input type="hidden" name="sqspell_use_app" value="<?php echo $sqspell_use_app ?>">
162 <table border="0" width="100%">
163 <tr align="center">
164 <td colspan="4">
165 <span style="background-color:<?php echo $color[9] ?>">Line with an error:</span><br>
166 <textarea name="sqspell_line_area" cols="50" rows="3" wrap="hard" onfocus="this.blur()"></textarea>
167 </td>
168 </tr>
169 <tr valign="middle">
170 <td align="right" width="25%">
171 <span style="background-color: <?php echo $color[9] ?>">Error:</span>
172 </td>
173 <td align="left" width="25%">
174 <input name="sqspell_error" size="10" value="" onfocus="this.blur()">
175 </td>
176 <td align="right" width="25%">
177 <span style="background-color: <?php echo $color[9] ?>">Suggestions:</span>
178 </td>
179 <td align="left" width="25%">
180 <select name="sqspell_suggestion" onchange="if (this.options[this.selectedIndex].value != '_NONE') document.forms[0].sqspell_oruse.value=this.options[this.selectedIndex].value">
181 <option>Suggestions</option>
182 </select>
183 </td>
184 </tr>
185 <tr>
186 <td align="right">
187 <span style="background-color: <?php echo $color[9] ?>">Change to:</span>
188 </td>
189 <td align="left">
190 <input name="sqspell_oruse" size="15" value=""
191 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value">
192 </td>
193 <td align="right">
194 <span style="background-color: <?php echo $color[9] ?>">Occurs times:</span>
195 </td>
196 <td align="left">
197 <input name="sqspell_likethis" size=3 value=""
198 onfocus="this.blur()">
199 </td>
200 </tr>
201 </td></tr>
202 <tr><td colspan="4"><hr></td></tr>
203 <tr>
204 <td colspan="4">
205 <table border="0" cellpadding="0" cellspacing="3" width="100%">
206 <tr align="center" bgcolor="<?php echo $color[9] ?>">
207 <td>
208 <a href="javascript:sqspellChange()"
209 title="Change this word">Change</a>
210 </td>
211 <td>
212 <a href="javascript:sqspellChangeAll()"
213 title="Change ALL occurances of this word">Change All</a>
214 </td>
215 <td>
216 <a href="javascript:sqspellIgnore()"
217 title="Ignore this word">Ignore</a>
218 </td>
219 <td>
220 <a href="javascript:sqspellIgnoreAll()"
221 title="Ignore ALL occurances of this word">Ignore All</a>
222 </td>
223 <td>
224 <a href="javascript:sqspellRemember()"
225 title="Add this word to your personal dictionary">Add to Dic</a>
226 </td>
227 </tr>
228 </table>
229 </td>
230 </tr>
231 <tr><td colspan="4"><hr></td></tr>
232 <tr>
233 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
234 <input type="button" value=" Close and Commit " onclick="if (confirm('The spellcheck is not finished. Really close and commit changes?')) sqspellCommitChanges()">
235 <input type="button" value=" Close and Cancel " onclick="if (confirm('The spellcheck is not finished. Really close and discard changes?')) self.close()">
236 </td>
237 </tr>
238 </table>
239 </form>
240 </td></tr>
241 </table>
242 </body>
243 </html>
244 <?php
245 } else {
246 // AREN'T YOU SUCH A KNOW-IT-ALL!
247 $msg="<form onsubmit=\"return false\"><div align=\"center\"><input type=\"submit\" value=\" Close \" onclick=\"self.close()\"></div></form>";
248 sqspell_makeWindow(null, "No errors found", null, $msg);
249 }
250?>