Some changes. En-route. Most likely broken. ;)
[squirrelmail.git] / plugins / squirrelspell / modules / check_me.mod
CommitLineData
849bdf42 1<?php
d112ed5a 2/**
3 * check_me.mod
4 * -------------
5 * Squirrelspell module.
6 *
7 * Copyright (c) 1999-2002 The SquirrelMail development team
8 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 *
10 * This module is the main workhorse of SquirrelSpell. It submits
11 * the message to the spell-checker, parses the output, and loads
12 * the interface window.
13 *
14 * $Id$
15 *
16 * @author Konstantin Riabitsev <icon@duke.edu> ($Author$)
17 * @version $Date$
18 */
158d478f 19
d112ed5a 20/**
21 * This function makes a javascript-powered link. Not sure why
22 * Philippe decided to move it outside the main code, but hey. ;)
23 * I bet for the i18n reasons.
24 *
25 * @param $jscode Javascript code to include in the link.
26 * @param $title A little pop-up title to provide for the links.
27 * @param $link The content of the link.
28 * @return void, since this just draws the content.
29 */
30function SpellLink($jscode, $title, $link) {
31 echo "<td><a href=\"javascript:$jscode\" "
32 . "title=\"$title\">$ln</a>"
33 . '</td>';
158d478f 34}
35
d112ed5a 36/**
37 * Declaring globals for users with E_ALL set.
38 */
849bdf42 39global $sqspell_text, $SQSPELL_APP, $sqspell_use_app, $attachment_dir,
d112ed5a 40 $username, $SQSPELL_EREG, $color;
849bdf42 41
d112ed5a 42/**
43 * Now we explode the lines for three reasons:
44 * 1) So we can ignore lines starting with ">" (reply's)
45 * 2) So we can stop processing when we get to "--" on a single line,
46 * which means that the signature is starting
47 * 3) So we can add an extra space at the beginning of each line. This way
48 * ispell/aspell don't treat these as command characters.
49 */
50$sqspell_raw_lines = explode("\n", $sqspell_text);
51for ($i=0; $i<sizeof($sqspell_raw_lines); $i++){
52 /**
53 * See if the signature is starting, which will be a "--" on the
54 * single line (after trimming).
55 */
56 if (trim($sqspell_raw_lines[$i]) == '--'){
57 break;
58 }
59 /**
60 * See if this is quoted text. Don't check the quoted text, since
61 * it's no business of ours how badly our correspondents misspell
62 * stuff.
63 */
64 if(substr($sqspell_raw_lines[$i], 0, 1) != '>'){
158d478f 65 $sqspell_new_lines[$i] = ' ' . $sqspell_raw_lines[$i];
d112ed5a 66 } else {
67 $sqspell_new_lines[$i] = '';
68 }
69}
70/**
71 * $sqspell_new_lines array now contains the lines to submit to the
72 * spellchecker.
73 */
74$sqspell_new_text=implode("\n", $sqspell_new_lines);
849bdf42 75
d112ed5a 76/**
77 * Define the command used to spellcheck the document.
78 */
79$sqspell_command=$SQSPELL_APP[$sqspell_use_app];
80/**
81 * For the simplicity's sake we'll put all text into a file in
82 * attachment_dir directory, then cat it and pipe it to
83 * sqspell_command. There are other ways to do it, including popen(),
84 * but it's unidirectional and no fun at all.
85 *
86 * The name of the file is an md5 hash of the message itself plus
87 * microtime. This prevents symlink attacks. The loop is here to
88 * further enhance this feature, and make sure we don't overwrite
89 * someone else's data, although the possibility of this happening is
90 * QUITE remote.
91 */
92do {
93 $floc = "$attachment_dir/" . md5($sqspell_new_text . microtime());
94} while (file_exists($floc));
95/**
96 * Write the contents to the file.
97 */
98$fp=fopen($floc, 'w');
99fwrite($fp, $sqspell_new_text);
100fclose($fp);
101/**
102 * Execute ispell/aspell and catch the output.
103 */
104exec("cat $floc | $sqspell_command", $sqspell_output);
105/**
106 * Remove the temp file.
107 */
108unlink($floc);
849bdf42 109
d112ed5a 110/**
111 * Load the user dictionary.
112 */
113$words=sqspell_getLang(sqspell_getWords(), $sqspell_use_app);
114/**
115 * Define some variables to be used during the processing.
116 */
117$current_line=0;
118$missed_words=Array();
119$misses = Array();
120$locations = Array();
121$errors=0;
122/**
123 * Now we process the output of sqspell_command (ispell or aspell in
124 * ispell compatibility mode, whichever). I'm going to be scarce on
125 * comments here, since you can just look at the ispell/aspell output
126 * and figure out what's going on. ;) The best way to describe this is
127 * "Dark Magic".
128 */
129for ($i=0; $i<sizeof($sqspell_output); $i++){
849bdf42 130 switch (substr($sqspell_output[$i], 0, 1)){
d112ed5a 131 /**
132 * Line is empty.
133 * Ispell adds empty lines when an end of line is reached
134 */
158d478f 135 case '':
849bdf42 136 $current_line++;
d112ed5a 137 break;
138 /**
139 * Line begins with "&".
140 * This means there's a misspelled word and a few suggestions.
141 */
158d478f 142 case '&':
849bdf42 143 list($left, $right) = explode(": ", $sqspell_output[$i]);
144 $tmparray = explode(" ", $left);
145 $sqspell_word=$tmparray[1];
d112ed5a 146 /**
147 * Check if the word is in user dictionary.
148 */
158d478f 149 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
d112ed5a 150 $sqspell_symb=intval($tmparray[3])-1;
151 if (!$misses[$sqspell_word]) {
158d478f 152 $misses[$sqspell_word] = $right;
153 $missed_words[$errors] = $sqspell_word;
154 $errors++;
d112ed5a 155 }
156 if ($locations[$sqspell_word]){
158d478f 157 $locations[$sqspell_word] .= ', ';
d112ed5a 158 }
159 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
849bdf42 160 }
d112ed5a 161 break;
162 /**
163 * Line begins with "#".
164 * This means a misspelled word and no suggestions.
165 */
158d478f 166 case '#':
849bdf42 167 $tmparray = explode(" ", $sqspell_output[$i]);
168 $sqspell_word=$tmparray[1];
d112ed5a 169 /**
170 *
171 * Check if the word is in user dictionary.
172 */
849bdf42 173 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
d112ed5a 174 $sqspell_symb=intval($tmparray[2])-1;
175 if (!$misses[$sqspell_word]) {
176 $misses[$sqspell_word] = '_NONE';
177 $missed_words[$errors] = $sqspell_word;
178 $errors++;
179 }
180 if ($locations[$sqspell_word]) $locations[$sqspell_word] .= ', ';
181 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
849bdf42 182 }
d112ed5a 183 break;
849bdf42 184 }
d112ed5a 185}
158d478f 186
d112ed5a 187if ($errors){
188 /**
189 * So, there are errors
190 * This is the only place where the generic GUI-wrapper is not
191 * called, but generated right here. This is due to the complexity
192 * of the output.
193 */
194 echo "<html>\n"
195 . "<head>\n"
196 . '<title>' . _("SquirrelSpell Results") . '</title>';
197 /**
198 * Check if there are user-defined stylesheets.
199 */
200 if ($theme_css != '') {
201 echo "<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"$theme_css\">\n";
202 }
203 /**
204 * Load the spelling errors into JavaScript arrays
205 * (More dark magic!)
206 */
207 echo "<script type=\"text/javascript\">\n"
208 . "<!--\n";
209
210 $sqspell_lines = explode("\n", $sqspell_text);
211 /**
212 * The javascript array sqspell_lines[] contains all lines of
213 * the message we've been checking.
214 */
215 echo "var sqspell_lines=new Array();\n";
216 for ($i=0; $i<sizeof($sqspell_lines); $i++){
217 echo "sqspell_lines[$i] = \""
218 . chop(addslashes($sqspell_lines[$i])) . "\";\n";
219 }
220 echo "\n\n";
158d478f 221
d112ed5a 222 /**
223 * The javascript array misses[] contais all misspelled words.
224 */
225 echo "var misses=new Array();\n";
226 for ($i=0; $i<sizeof($missed_words); $i++){
227 echo "misses[$i] = \"" . $missed_words[$i] . "\";\n";
228 }
229 echo "\n\n";
230
231 /**
232 * Suggestions are (guess what!) suggestions for misspellings
233 */
234 echo "var suggestions = new Array();\n";
235 $i=0;
236 while (list($word, $value) = each($misses)){
237 if ($value=='_NONE') $value='';
238 echo "suggestions[$i] = \"$value\";\n";
239 $i++;
240 }
241 echo "\n\n";
849bdf42 242
d112ed5a 243 /**
244 * Locations are where those misspellings are located, line:symbol
245 */
246 echo "var locations= new Array();\n";
247 $i=0;
248 while (list($word, $value) = each($locations)){
249 echo "locations[$i] = \"$value\";\n";
250 $i++;
251 }
849bdf42 252
d112ed5a 253 /**
254 * Add some strings so they can be i18n'd.
255 */
256 echo "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
257 . "\";\n";
258 echo "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
259 echo "var ui_wait = \""
260 . _("Now saving your personal dictionary... Please wait.")
261 . "\";\n";
262
158d478f 263
d112ed5a 264 /**
265 * Did I mention that I hate dots on the end of contcatenated lines?
266 * Dots at the beginning make so much more sense!
267 */
268 echo "//-->\n"
269 . "</script>\n"
270 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n"
271 . "</head>\n";
272
273 echo "<body bgcolor=\"$color[4]\" text=\"$color[8]\" link=\"$color[7]\" "
274 . "alink=\"$color[7]\" vlink=\"$color[7]\" "
275 . "onload=\"populateSqspellForm()\">\n";
276 ?>
277 <table width="100%" border="0" cellpadding="2">
278 <tr>
279 <td bgcolor="<?php echo $color[9] ?>" align="center">
280 <b>
281 <?php printf( _("Found %s errors"), $errors ) ?>
282 </b>
283 </td>
284 </tr>
285 <tr>
286 <td>
287 <hr>
288 </td>
289 </tr>
290 <tr>
291 <td>
292 <form method="post">
293 <input type="hidden" name="MOD" value="forget_me_not" />
294 <input type="hidden" name="words" value="" />
295 <input type="hidden" name="sqspell_use_app"
296 value="<?php echo $sqspell_use_app ?>" />
297 <table border="0" width="100%">
298 <tr align="center">
299 <td colspan="4">
300 <?php
301 $sptag = "<span style=\"background-color: $color[9]\">";
302 echo $sptag . _("Line with an error:") . '</span>';
303 ?>
304 <br />
305 <textarea name="sqspell_line_area" cols="50" rows="3"
306 wrap="hard" onfocus="this.blur()"></textarea>
307 </td>
308 </tr>
309 <tr valign="middle">
310 <td align="right" width="25%">
311 <?php
312 echo $sptag . _("Error:") . '</span>';
313 ?>
314 </td>
315 <td align="left" width="25%">
316 <input name="sqspell_error" size="10" value=""
317 onfocus="this.blur()" />
318 </td>
319 <td align="right" width="25%">
320 <?php
321 echo $sptag . _("Suggestions:") . '</span>';
322 ?>
323 </td>
324 <td align="left" width="25%">
325 <select name="sqspell_suggestion"
326 onchange="if (this.options[this.selectedIndex].value != '_NONE') document.forms[0].sqspell_oruse.value=this.options[this.selectedIndex].value">
327 <?php
328 echo '<option>' . _("Suggestions") . '</option>';
329 ?>
330 </select>
331 </td>
332 </tr>
333 <tr>
334 <td align="right">
335 <?php
336 echo $sptag . _("Change to:") . '</span>';
337 ?>
338 </td>
339 <td align="left">
340 <input name="sqspell_oruse" size="15" value=""
341 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value">
342 </td>
343 <td align="right">
344 <?php
345 echo $sptag . _("Occurs times:") . '</span>';
346 ?>
347 </td>
348 <td align="left">
349 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()">
350 </td>
351 </tr>
352 <!-- hello? What is this? </td></tr> -->
353 <tr>
354 <td colspan="4"><hr></td>
355 </tr>
356 <tr>
357 <td colspan="4">
358 <table border="0" cellpadding="0" cellspacing="3" width="100%">
359 <tr align="center" bgcolor="<?php echo $color[9] ?>">
360 <?php
361 SpellLink('sqspellChange()',
362 _("Change this word"),
363 _("Change"));
364 SpellLink('sqspellChangeAll()',
365 _("Change ALL occurances of this word"),
366 _("Change All"));
367 SpellLink('sqspellIgnore()',
368 _("Ignore this word"),
369 _("Ignore"));
370 SpellLink('sqspellIgnoreAll()',
371 _("Ignore ALL occurances this word"),
372 _("Ignore All"));
373 SpellLink('sqspellRemember()',
374 _("Add this word to your personal dictionary"),
375 _("Add to Dic"));
376 ?>
377 </tr>
378 </table>
379 </td>
380 </tr>
381 <tr>
382 <td colspan="4"><hr></td>
383 </tr>
384 <tr>
385 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
386 <?php
387 echo '<input type="button" value=" '
388 . _("Close and Commit")
389 . ' " onclick="if (confirm(\''
390 . _("The spellcheck is not finished. Really close and commit changes?")
391 . '\')) sqspellCommitChanges()">'
392 . ' <input type="button" value=" '
393 . _("Close and Cancel")
394 . ' " onclick="if (confirm(\''
395 . _("The spellcheck is not finished. Really close and discard changes?")
396 . '\')) self.close()">';
397 ?>
398 </td>
849bdf42 399 </tr>
400 </table>
d112ed5a 401 </form>
402 </td>
403 </tr>
849bdf42 404 </table>
d112ed5a 405 </body></html>
849bdf42 406 <?php
d112ed5a 407} else {
408 /**
409 * AREN'T YOU SUCH A KNOW-IT-ALL!
410 */
411 $msg="<form onsubmit=\"return false\"><div align=\"center\">"
412 . "<input type=\"submit\" value=\" " . _("Close")
413 . " \" onclick=\"self.close()\"></div></form>";
414 sqspell_makeWindow(null, _("No errors found"), null, $msg);
415}
416
417/**
418 * For Emacs weenies:
419 * Local variables:
420 * mode: php
421 * End:
422 */
849bdf42 423?>