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 (!isset($misses[$sqspell_word])) {
167 $misses[$sqspell_word] = $right;
168 $missed_words[$errors] = $sqspell_word;
171 if (isset($locations[$sqspell_word])){
172 $locations[$sqspell_word] .= ', ';
174 $locations[$sqspell_word] = '';
176 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
180 * Line begins with "#".
181 * This means a misspelled word and no suggestions.
184 $tmparray = explode(" ", $sqspell_output[$i]);
185 $sqspell_word=$tmparray[1];
188 * Check if the word is in user dictionary.
190 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
191 $sqspell_symb=intval($tmparray[2])-1;
192 if (!$misses[$sqspell_word]) {
193 $misses[$sqspell_word] = '_NONE';
194 $missed_words[$errors] = $sqspell_word;
197 if ($locations[$sqspell_word]) $locations[$sqspell_word] .= ', ';
198 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
206 * So, there are errors
207 * This is the only place where the generic GUI-wrapper is not
208 * called, but generated right here. This is due to the complexity
213 . '<title>' . _("SquirrelSpell Results") . '</title>';
215 * Check if there are user-defined stylesheets.
217 if ($theme_css != '') {
218 echo "<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"$theme_css\">\n";
221 * Load the spelling errors into JavaScript arrays
224 echo "<script type=\"text/javascript\">\n"
227 $sqspell_lines = explode("\n", $sqspell_text);
229 * The javascript array sqspell_lines[] contains all lines of
230 * the message we've been checking.
232 echo "var sqspell_lines=new Array();\n";
233 for ($i=0; $i<sizeof($sqspell_lines); $i++){
234 echo "sqspell_lines[$i] = \""
235 . chop(addslashes($sqspell_lines[$i])) . "\";\n";
240 * The javascript array misses[] contais all misspelled words.
242 echo "var misses=new Array();\n";
243 for ($i=0; $i<sizeof($missed_words); $i++){
244 echo "misses[$i] = \"" . $missed_words[$i] . "\";\n";
249 * Suggestions are (guess what!) suggestions for misspellings
251 echo "var suggestions = new Array();\n";
253 while (list($word, $value) = each($misses)){
254 if ($value=='_NONE') $value='';
255 echo "suggestions[$i] = \"$value\";\n";
261 * Locations are where those misspellings are located, line:symbol
263 echo "var locations= new Array();\n";
265 while (list($word, $value) = each($locations)){
266 echo "locations[$i] = \"$value\";\n";
271 * Add some strings so they can be i18n'd.
273 echo "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
275 echo "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
276 echo "var ui_wait = \""
277 . _("Now saving your personal dictionary... Please wait.")
282 * Did I mention that I hate dots on the end of contcatenated lines?
283 * Dots at the beginning make so much more sense!
287 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n"
290 echo "<body bgcolor=\"$color[4]\" text=\"$color[8]\" link=\"$color[7]\" "
291 . "alink=\"$color[7]\" vlink=\"$color[7]\" "
292 . "onload=\"populateSqspellForm()\">\n";
294 <table width="100%" border="0" cellpadding="2">
296 <td bgcolor="<?php echo $color[9] ?>" align="center">
298 <?php printf( _("Found %s errors"), $errors ) ?>
310 <input type="hidden" name="MOD" value="forget_me_not" />
311 <input type="hidden" name="words" value="" />
312 <input type="hidden" name="sqspell_use_app"
313 value="<?php echo $sqspell_use_app ?>" />
314 <table border="0" width="100%">
318 $sptag = "<span style=\"background-color: $color[9]\">";
319 echo $sptag . _("Line with an error:") . '</span>';
322 <textarea name="sqspell_line_area" cols="50" rows="3"
323 wrap="hard" onfocus="this.blur()"></textarea>
327 <td align="right" width="25%">
329 echo $sptag . _("Error:") . '</span>';
332 <td align="left" width="25%">
333 <input name="sqspell_error" size="10" value=""
334 onfocus="this.blur()" />
336 <td align="right" width="25%">
338 echo $sptag . _("Suggestions:") . '</span>';
341 <td align="left" width="25%">
342 <select name="sqspell_suggestion"
343 onchange="if (this.options[this.selectedIndex].value != '_NONE') document.forms[0].sqspell_oruse.value=this.options[this.selectedIndex].value">
345 echo '<option>' . _("Suggestions") . '</option>';
353 echo $sptag . _("Change to:") . '</span>';
357 <input name="sqspell_oruse" size="15" value=""
358 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value">
362 echo $sptag . _("Occurs times:") . '</span>';
366 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()">
369 <!-- hello? What is this? </td></tr> -->
371 <td colspan="4"><hr></td>
375 <table border="0" cellpadding="0" cellspacing="3" width="100%">
376 <tr align="center" bgcolor="<?php echo $color[9] ?>">
378 SpellLink('sqspellChange()',
379 _("Change this word"),
381 SpellLink('sqspellChangeAll()',
382 _("Change ALL occurances of this word"),
384 SpellLink('sqspellIgnore()',
385 _("Ignore this word"),
387 SpellLink('sqspellIgnoreAll()',
388 _("Ignore ALL occurances this word"),
390 SpellLink('sqspellRemember()',
391 _("Add this word to your personal dictionary"),
399 <td colspan="4"><hr></td>
402 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
404 echo '<input type="button" value=" '
405 . _("Close and Commit")
406 . ' " onclick="if (confirm(\''
407 . _("The spellcheck is not finished. Really close and commit changes?")
408 . '\')) sqspellCommitChanges()">'
409 . ' <input type="button" value=" '
410 . _("Close and Cancel")
411 . ' " onclick="if (confirm(\''
412 . _("The spellcheck is not finished. Really close and discard changes?")
413 . '\')) self.close()">';
426 * AREN'T YOU SUCH A KNOW-IT-ALL!
428 $msg="<form onsubmit=\"return false\"><div align=\"center\">"
429 . "<input type=\"submit\" value=\" " . _("Close")
430 . " \" onclick=\"self.close()\"></div></form>";
431 sqspell_makeWindow(null, _("No errors found"), null, $msg);