5 * Squirrelspell module.
7 * Copyright (c) 1999-2004 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_APP, $attachment_dir, $SQSPELL_EREG, $color;
41 $sqspell_text = $_POST['sqspell_text'];
42 $sqspell_use_app = $_POST['sqspell_use_app'];
45 * Now we explode the lines for three reasons:
46 * 1) So we can ignore lines starting with ">" (reply's)
47 * 2) So we can stop processing when we get to "--" on a single line,
48 * which means that the signature is starting
49 * 3) So we can add an extra space at the beginning of each line. This way
50 * ispell/aspell don't treat these as command characters.
52 $sqspell_raw_lines = explode("\n", $sqspell_text);
53 for ($i=0; $i<sizeof($sqspell_raw_lines); $i++){
55 * See if the signature is starting, which will be a "--" on the
56 * single line (after trimming).
58 if (trim($sqspell_raw_lines[$i]) == '--'){
62 * See if this is quoted text. Don't check the quoted text, since
63 * it's no business of ours how badly our correspondents misspell
66 if(substr($sqspell_raw_lines[$i], 0, 1) != '>'){
67 $sqspell_new_lines[$i] = ' ' . $sqspell_raw_lines[$i];
69 $sqspell_new_lines[$i] = ' ';
73 * $sqspell_new_lines array now contains the lines to submit to the
76 $sqspell_new_text=implode("\n", $sqspell_new_lines);
79 * Define the command used to spellcheck the document.
81 $sqspell_command=$SQSPELL_APP[$sqspell_use_app];
83 * If you have php >= 4.3.0, we can use proc_open and safe mode
84 * and not mess w/ temp files. Otherwise we will do it the old
85 * way, (minus the uneeded call to cat that messes up Wintel
87 * Thanks Ray Ferguson for providing this patch.
89 if( check_php_version ( 4, 3 ) ) {
90 $descriptorspec = array(
91 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from
92 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
93 2 => array('pipe', 'w'), // stderr is a pipe that the child will write to
95 $spell_proc=proc_open($sqspell_command, $descriptorspec, $pipes);
96 fwrite($pipes[0], $sqspell_new_text);
98 $sqspell_output = array();
99 for($i=1; $i<=2; $i++){
100 while(!feof($pipes[$i]))
101 array_push($sqspell_output, rtrim(fgetss($pipes[$i],999),"\n"));
104 $sqspell_exitcode=proc_close($spell_proc);
107 $floc = "$attachment_dir/" . md5($sqspell_new_text . microtime());
108 } while (file_exists($floc));
109 $fp=fopen($floc, 'w');
110 fwrite($fp, $sqspell_new_text);
112 exec("$sqspell_command < $floc 2>&1", $sqspell_output, $sqspell_exitcode);
117 * Check if the execution was successful. Bail out if it wasn't.
119 if ($sqspell_exitcode){
120 $msg= "<div align='center'>"
121 . sprintf(_("I tried to execute '%s', but it returned:"),
122 $sqspell_command) . "<pre>"
123 . htmlspecialchars(join("\n", $sqspell_output)) . '</pre>'
124 . '<form onsubmit="return false">'
125 . '<input type="submit" value=" ' . _("Close")
126 . ' " onclick="self.close()" /></form></div>';
127 sqspell_makeWindow(null, _("SquirrelSpell is misconfigured."), null, $msg);
132 * Load the user dictionary.
134 $words=sqspell_getLang(sqspell_getWords(), $sqspell_use_app);
136 * Define some variables to be used during the processing.
139 $missed_words=Array();
141 $locations = Array();
144 * Now we process the output of sqspell_command (ispell or aspell in
145 * ispell compatibility mode, whichever). I'm going to be scarce on
146 * comments here, since you can just look at the ispell/aspell output
147 * and figure out what's going on. ;) The best way to describe this is
150 for ($i=0; $i<sizeof($sqspell_output); $i++){
151 switch (substr($sqspell_output[$i], 0, 1)){
154 * Ispell adds empty lines when an end of line is reached
160 * Line begins with "&".
161 * This means there's a misspelled word and a few suggestions.
164 list($left, $right) = explode(": ", $sqspell_output[$i]);
165 $tmparray = explode(" ", $left);
166 $sqspell_word=$tmparray[1];
168 * Check if the word is in user dictionary.
170 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
171 $sqspell_symb=intval($tmparray[3])-1;
172 if (!isset($misses[$sqspell_word])) {
173 $misses[$sqspell_word] = $right;
174 $missed_words[$errors] = $sqspell_word;
177 if (isset($locations[$sqspell_word])){
178 $locations[$sqspell_word] .= ', ';
180 $locations[$sqspell_word] = '';
182 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
186 * Line begins with "#".
187 * This means a misspelled word and no suggestions.
190 $tmparray = explode(" ", $sqspell_output[$i]);
191 $sqspell_word=$tmparray[1];
194 * Check if the word is in user dictionary.
196 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
197 $sqspell_symb=intval($tmparray[2])-1;
198 if (!isset($misses[$sqspell_word])) {
199 $misses[$sqspell_word] = '_NONE';
200 $missed_words[$errors] = $sqspell_word;
203 if (isset($locations[$sqspell_word])) {
204 $locations[$sqspell_word] .= ', ';
206 $locations[$sqspell_word] = '';
208 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
216 * Load the spelling errors into JavaScript arrays
219 $extrajs="<script type=\"text/javascript\">\n"
222 $sqspell_lines = explode("\n", $sqspell_text);
224 * The javascript array sqspell_lines[] contains all lines of
225 * the message we've been checking.
227 $extrajs.= "var sqspell_lines=new Array();\n";
228 for ($i=0; $i<sizeof($sqspell_lines); $i++){
229 $extrajs.= "sqspell_lines[$i] = \""
230 . chop(addslashes($sqspell_lines[$i])) . "\";\n";
235 * The javascript array misses[] contais all misspelled words.
237 $extrajs.= "var misses=new Array();\n";
238 for ($i=0; $i<sizeof($missed_words); $i++){
239 $extrajs.= "misses[$i] = \"" . $missed_words[$i] . "\";\n";
244 * Suggestions are (guess what!) suggestions for misspellings
246 $extrajs.= "var suggestions = new Array();\n";
248 while (list($word, $value) = each($misses)){
249 if ($value=='_NONE') $value='';
250 $extrajs.= "suggestions[$i] = \"$value\";\n";
256 * Locations are where those misspellings are located, line:symbol
258 $extrajs.= "var locations= new Array();\n";
260 while (list($word, $value) = each($locations)){
261 $extrajs.= "locations[$i] = \"$value\";\n";
266 * Add some strings so they can be i18n'd.
268 $extrajs.= "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
270 $extrajs.= "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
271 $extrajs.= "var ui_wait = \""
272 . _("Now saving your personal dictionary... Please wait.")
277 * Did I mention that I hate dots on the end of contcatenated lines?
278 * Dots at the beginning make so much more sense!
282 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n";
285 displayHtmlHeader(_("SquirrelSpell Results"),$extrajs);
287 echo "<body bgcolor=\"$color[4]\" text=\"$color[8]\" link=\"$color[7]\" "
288 . "alink=\"$color[7]\" vlink=\"$color[7]\" "
289 . "onload=\"populateSqspellForm()\">\n";
291 <table width="100%" border="0" cellpadding="2">
293 <td bgcolor="<?php echo $color[9] ?>" align="center">
295 <?php printf( _("Found %s errors"), $errors ) ?>
307 <input type="hidden" name="MOD" value="forget_me_not" />
308 <input type="hidden" name="words" value="" />
309 <input type="hidden" name="sqspell_use_app"
310 value="<?php echo $sqspell_use_app ?>" />
311 <table border="0" width="100%">
315 $sptag = "<span style=\"background-color: $color[9]\">";
316 echo $sptag . _("Line with an error:") . '</span>';
319 <textarea name="sqspell_line_area" cols="50" rows="3"
320 wrap="hard" onfocus="this.blur()"></textarea>
324 <td align="right" width="25%">
326 echo $sptag . _("Error:") . '</span>';
329 <td align="left" width="25%">
330 <input name="sqspell_error" size="10" value=""
331 onfocus="this.blur()" />
333 <td align="right" width="25%">
335 echo $sptag . _("Suggestions:") . '</span>';
338 <td align="left" width="25%">
339 <select name="sqspell_suggestion"
340 onchange="if (this.options[this.selectedIndex].value != '_NONE') document.forms[0].sqspell_oruse.value=this.options[this.selectedIndex].value">
342 echo '<option>' . _("Suggestions") . '</option>';
350 echo $sptag . _("Change to:") . '</span>';
354 <input name="sqspell_oruse" size="15" value=""
355 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value" />
359 echo $sptag . _("Occurs times:") . '</span>';
363 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()" />
366 <!-- hello? What is this? </td></tr> -->
368 <td colspan="4"><hr /></td>
372 <table border="0" cellpadding="0" cellspacing="3" width="100%">
373 <tr align="center" bgcolor="<?php echo $color[9] ?>">
375 SpellLink('sqspellChange()',
376 _("Change this word"),
378 SpellLink('sqspellChangeAll()',
379 _("Change ALL occurances of this word"),
381 SpellLink('sqspellIgnore()',
382 _("Ignore this word"),
384 SpellLink('sqspellIgnoreAll()',
385 _("Ignore ALL occurances this word"),
387 SpellLink('sqspellRemember()',
388 _("Add this word to your personal dictionary"),
396 <td colspan="4"><hr /></td>
399 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
401 echo '<input type="button" value=" '
402 . _("Close and Commit")
403 . ' " onclick="if (confirm(\''
404 . _("The spellcheck is not finished. Really close and commit changes?")
405 . '\')) sqspellCommitChanges()" />'
406 . ' <input type="button" value=" '
407 . _("Close and Cancel")
408 . ' " onclick="if (confirm(\''
409 . _("The spellcheck is not finished. Really close and discard changes?")
410 . '\')) self.close()" />';
423 * AREN'T YOU SUCH A KNOW-IT-ALL!
425 $msg='<form onsubmit="return false"><div align="center">'.
426 '<input type="submit" value=" ' . _("Close") .
427 ' " onclick="self.close()" /></div></form>';
428 sqspell_makeWindow(null, _("No errors found"), null, $msg);
436 * vim: syntax=php et ts=4