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