5 * Squirrelspell module.
7 * Copyright (c) 1999-2002 The SquirrelMail development team
8 * Licensed under the GNU GPL. For full terms see the file COPYING.
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.
16 * @author Konstantin Riabitsev <icon@duke.edu> ($Author$)
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.
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.
30 function SpellLink($jscode, $title, $link) {
31 echo "<td><a href=\"javascript:$jscode\" "
32 . "title=\"$title\">$ln</a>"
37 * Declaring globals for users with E_ALL set.
39 global $sqspell_text, $SQSPELL_APP, $sqspell_use_app, $attachment_dir,
40 $username, $SQSPELL_EREG, $color;
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.
50 $sqspell_raw_lines = explode("\n", $sqspell_text);
51 for ($i=0; $i<sizeof($sqspell_raw_lines); $i++){
53 * See if the signature is starting, which will be a "--" on the
54 * single line (after trimming).
56 if (trim($sqspell_raw_lines[$i]) == '--'){
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
64 if(substr($sqspell_raw_lines[$i], 0, 1) != '>'){
65 $sqspell_new_lines[$i] = ' ' . $sqspell_raw_lines[$i];
67 $sqspell_new_lines[$i] = '';
71 * $sqspell_new_lines array now contains the lines to submit to the
74 $sqspell_new_text=implode("\n", $sqspell_new_lines);
77 * Define the command used to spellcheck the document.
79 $sqspell_command=$SQSPELL_APP[$sqspell_use_app];
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.
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
93 $floc = "$attachment_dir/" . md5($sqspell_new_text . microtime());
94 } while (file_exists($floc));
96 * Write the contents to the file.
98 $fp=fopen($floc, 'w');
99 fwrite($fp, $sqspell_new_text);
102 * Execute ispell/aspell and catch the output.
104 exec("cat $floc | $sqspell_command", $sqspell_output);
106 * Remove the temp file.
111 * Load the user dictionary.
113 $words=sqspell_getLang(sqspell_getWords(), $sqspell_use_app);
115 * Define some variables to be used during the processing.
118 $missed_words=Array();
120 $locations = Array();
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
129 for ($i=0; $i<sizeof($sqspell_output); $i++){
130 switch (substr($sqspell_output[$i], 0, 1)){
133 * Ispell adds empty lines when an end of line is reached
139 * Line begins with "&".
140 * This means there's a misspelled word and a few suggestions.
143 list($left, $right) = explode(": ", $sqspell_output[$i]);
144 $tmparray = explode(" ", $left);
145 $sqspell_word=$tmparray[1];
147 * Check if the word is in user dictionary.
149 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
150 $sqspell_symb=intval($tmparray[3])-1;
151 if (!$misses[$sqspell_word]) {
152 $misses[$sqspell_word] = $right;
153 $missed_words[$errors] = $sqspell_word;
156 if ($locations[$sqspell_word]){
157 $locations[$sqspell_word] .= ', ';
159 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
163 * Line begins with "#".
164 * This means a misspelled word and no suggestions.
167 $tmparray = explode(" ", $sqspell_output[$i]);
168 $sqspell_word=$tmparray[1];
171 * Check if the word is in user dictionary.
173 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
174 $sqspell_symb=intval($tmparray[2])-1;
175 if (!$misses[$sqspell_word]) {
176 $misses[$sqspell_word] = '_NONE';
177 $missed_words[$errors] = $sqspell_word;
180 if ($locations[$sqspell_word]) $locations[$sqspell_word] .= ', ';
181 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
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
196 . '<title>' . _("SquirrelSpell Results") . '</title>';
198 * Check if there are user-defined stylesheets.
200 if ($theme_css != '') {
201 echo "<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"$theme_css\">\n";
204 * Load the spelling errors into JavaScript arrays
207 echo "<script type=\"text/javascript\">\n"
210 $sqspell_lines = explode("\n", $sqspell_text);
212 * The javascript array sqspell_lines[] contains all lines of
213 * the message we've been checking.
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";
223 * The javascript array misses[] contais all misspelled words.
225 echo "var misses=new Array();\n";
226 for ($i=0; $i<sizeof($missed_words); $i++){
227 echo "misses[$i] = \"" . $missed_words[$i] . "\";\n";
232 * Suggestions are (guess what!) suggestions for misspellings
234 echo "var suggestions = new Array();\n";
236 while (list($word, $value) = each($misses)){
237 if ($value=='_NONE') $value='';
238 echo "suggestions[$i] = \"$value\";\n";
244 * Locations are where those misspellings are located, line:symbol
246 echo "var locations= new Array();\n";
248 while (list($word, $value) = each($locations)){
249 echo "locations[$i] = \"$value\";\n";
254 * Add some strings so they can be i18n'd.
256 echo "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
258 echo "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
259 echo "var ui_wait = \""
260 . _("Now saving your personal dictionary... Please wait.")
265 * Did I mention that I hate dots on the end of contcatenated lines?
266 * Dots at the beginning make so much more sense!
270 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n"
273 echo "<body bgcolor=\"$color[4]\" text=\"$color[8]\" link=\"$color[7]\" "
274 . "alink=\"$color[7]\" vlink=\"$color[7]\" "
275 . "onload=\"populateSqspellForm()\">\n";
277 <table width="100%" border="0" cellpadding="2">
279 <td bgcolor="<?php echo $color[9] ?>" align="center">
281 <?php printf( _("Found %s errors"), $errors ) ?>
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%">
301 $sptag = "<span style=\"background-color: $color[9]\">";
302 echo $sptag . _("Line with an error:") . '</span>';
305 <textarea name="sqspell_line_area" cols="50" rows="3"
306 wrap="hard" onfocus="this.blur()"></textarea>
310 <td align="right" width="25%">
312 echo $sptag . _("Error:") . '</span>';
315 <td align="left" width="25%">
316 <input name="sqspell_error" size="10" value=""
317 onfocus="this.blur()" />
319 <td align="right" width="25%">
321 echo $sptag . _("Suggestions:") . '</span>';
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">
328 echo '<option>' . _("Suggestions") . '</option>';
336 echo $sptag . _("Change to:") . '</span>';
340 <input name="sqspell_oruse" size="15" value=""
341 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value">
345 echo $sptag . _("Occurs times:") . '</span>';
349 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()">
352 <!-- hello? What is this? </td></tr> -->
354 <td colspan="4"><hr></td>
358 <table border="0" cellpadding="0" cellspacing="3" width="100%">
359 <tr align="center" bgcolor="<?php echo $color[9] ?>">
361 SpellLink('sqspellChange()',
362 _("Change this word"),
364 SpellLink('sqspellChangeAll()',
365 _("Change ALL occurances of this word"),
367 SpellLink('sqspellIgnore()',
368 _("Ignore this word"),
370 SpellLink('sqspellIgnoreAll()',
371 _("Ignore ALL occurances this word"),
373 SpellLink('sqspellRemember()',
374 _("Add this word to your personal dictionary"),
382 <td colspan="4"><hr></td>
385 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
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()">';
409 * AREN'T YOU SUCH A KNOW-IT-ALL!
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);