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\">$link</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 2>&1", $sqspell_output, $sqspell_exitcode);
106 * Remove the temp file.
111 * Check if the execution was successful. Bail out if it wasn't.
113 if ($sqspell_exitcode){
114 $msg= "<div align='center'>"
115 . sprintf(_("I tried to execute '%s', but it returned:"),
116 $sqspell_command) . "<pre>"
117 . nl2br(join("\n", $sqspell_output)) . "</pre>"
118 . "<form onsubmit=\"return false\">"
119 . "<input type=\"submit\" value=\" " . _("Close")
120 . " \" onclick=\"self.close()\"></form></div>";
121 sqspell_makeWindow(null, _("SquirrelSpell is misconfigured."), null, $msg);
126 * Load the user dictionary.
128 $words=sqspell_getLang(sqspell_getWords(), $sqspell_use_app);
130 * Define some variables to be used during the processing.
133 $missed_words=Array();
135 $locations = Array();
138 * Now we process the output of sqspell_command (ispell or aspell in
139 * ispell compatibility mode, whichever). I'm going to be scarce on
140 * comments here, since you can just look at the ispell/aspell output
141 * and figure out what's going on. ;) The best way to describe this is
144 for ($i=0; $i<sizeof($sqspell_output); $i++){
145 switch (substr($sqspell_output[$i], 0, 1)){
148 * Ispell adds empty lines when an end of line is reached
154 * Line begins with "&".
155 * This means there's a misspelled word and a few suggestions.
158 list($left, $right) = explode(": ", $sqspell_output[$i]);
159 $tmparray = explode(" ", $left);
160 $sqspell_word=$tmparray[1];
162 * Check if the word is in user dictionary.
164 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
165 $sqspell_symb=intval($tmparray[3])-1;
166 if (!$misses[$sqspell_word]) {
167 $misses[$sqspell_word] = $right;
168 $missed_words[$errors] = $sqspell_word;
171 if ($locations[$sqspell_word]){
172 $locations[$sqspell_word] .= ', ';
174 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
178 * Line begins with "#".
179 * This means a misspelled word and no suggestions.
182 $tmparray = explode(" ", $sqspell_output[$i]);
183 $sqspell_word=$tmparray[1];
186 * Check if the word is in user dictionary.
188 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
189 $sqspell_symb=intval($tmparray[2])-1;
190 if (!$misses[$sqspell_word]) {
191 $misses[$sqspell_word] = '_NONE';
192 $missed_words[$errors] = $sqspell_word;
195 if ($locations[$sqspell_word]) $locations[$sqspell_word] .= ', ';
196 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
204 * So, there are errors
205 * This is the only place where the generic GUI-wrapper is not
206 * called, but generated right here. This is due to the complexity
211 . '<title>' . _("SquirrelSpell Results") . '</title>';
213 * Check if there are user-defined stylesheets.
215 if ($theme_css != '') {
216 echo "<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"$theme_css\">\n";
219 * Load the spelling errors into JavaScript arrays
222 echo "<script type=\"text/javascript\">\n"
225 $sqspell_lines = explode("\n", $sqspell_text);
227 * The javascript array sqspell_lines[] contains all lines of
228 * the message we've been checking.
230 echo "var sqspell_lines=new Array();\n";
231 for ($i=0; $i<sizeof($sqspell_lines); $i++){
232 echo "sqspell_lines[$i] = \""
233 . chop(addslashes($sqspell_lines[$i])) . "\";\n";
238 * The javascript array misses[] contais all misspelled words.
240 echo "var misses=new Array();\n";
241 for ($i=0; $i<sizeof($missed_words); $i++){
242 echo "misses[$i] = \"" . $missed_words[$i] . "\";\n";
247 * Suggestions are (guess what!) suggestions for misspellings
249 echo "var suggestions = new Array();\n";
251 while (list($word, $value) = each($misses)){
252 if ($value=='_NONE') $value='';
253 echo "suggestions[$i] = \"$value\";\n";
259 * Locations are where those misspellings are located, line:symbol
261 echo "var locations= new Array();\n";
263 while (list($word, $value) = each($locations)){
264 echo "locations[$i] = \"$value\";\n";
269 * Add some strings so they can be i18n'd.
271 echo "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
273 echo "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
274 echo "var ui_wait = \""
275 . _("Now saving your personal dictionary... Please wait.")
280 * Did I mention that I hate dots on the end of contcatenated lines?
281 * Dots at the beginning make so much more sense!
285 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n"
288 echo "<body bgcolor=\"$color[4]\" text=\"$color[8]\" link=\"$color[7]\" "
289 . "alink=\"$color[7]\" vlink=\"$color[7]\" "
290 . "onload=\"populateSqspellForm()\">\n";
292 <table width="100%" border="0" cellpadding="2">
294 <td bgcolor="<?php echo $color[9] ?>" align="center">
296 <?php printf( _("Found %s errors"), $errors ) ?>
308 <input type="hidden" name="MOD" value="forget_me_not" />
309 <input type="hidden" name="words" value="" />
310 <input type="hidden" name="sqspell_use_app"
311 value="<?php echo $sqspell_use_app ?>" />
312 <table border="0" width="100%">
316 $sptag = "<span style=\"background-color: $color[9]\">";
317 echo $sptag . _("Line with an error:") . '</span>';
320 <textarea name="sqspell_line_area" cols="50" rows="3"
321 wrap="hard" onfocus="this.blur()"></textarea>
325 <td align="right" width="25%">
327 echo $sptag . _("Error:") . '</span>';
330 <td align="left" width="25%">
331 <input name="sqspell_error" size="10" value=""
332 onfocus="this.blur()" />
334 <td align="right" width="25%">
336 echo $sptag . _("Suggestions:") . '</span>';
339 <td align="left" width="25%">
340 <select name="sqspell_suggestion"
341 onchange="if (this.options[this.selectedIndex].value != '_NONE') document.forms[0].sqspell_oruse.value=this.options[this.selectedIndex].value">
343 echo '<option>' . _("Suggestions") . '</option>';
351 echo $sptag . _("Change to:") . '</span>';
355 <input name="sqspell_oruse" size="15" value=""
356 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value">
360 echo $sptag . _("Occurs times:") . '</span>';
364 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()">
367 <!-- hello? What is this? </td></tr> -->
369 <td colspan="4"><hr></td>
373 <table border="0" cellpadding="0" cellspacing="3" width="100%">
374 <tr align="center" bgcolor="<?php echo $color[9] ?>">
376 SpellLink('sqspellChange()',
377 _("Change this word"),
379 SpellLink('sqspellChangeAll()',
380 _("Change ALL occurances of this word"),
382 SpellLink('sqspellIgnore()',
383 _("Ignore this word"),
385 SpellLink('sqspellIgnoreAll()',
386 _("Ignore ALL occurances this word"),
388 SpellLink('sqspellRemember()',
389 _("Add this word to your personal dictionary"),
397 <td colspan="4"><hr></td>
400 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
402 echo '<input type="button" value=" '
403 . _("Close and Commit")
404 . ' " onclick="if (confirm(\''
405 . _("The spellcheck is not finished. Really close and commit changes?")
406 . '\')) sqspellCommitChanges()">'
407 . ' <input type="button" value=" '
408 . _("Close and Cancel")
409 . ' " onclick="if (confirm(\''
410 . _("The spellcheck is not finished. Really close and discard changes?")
411 . '\')) self.close()">';
424 * AREN'T YOU SUCH A KNOW-IT-ALL!
426 $msg="<form onsubmit=\"return false\"><div align=\"center\">"
427 . "<input type=\"submit\" value=\" " . _("Close")
428 . " \" onclick=\"self.close()\"></div></form>";
429 sqspell_makeWindow(null, _("No errors found"), null, $msg);