5 * Squirrelspell module.
7 * Copyright (c) 1999-2005 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.
14 * @author Konstantin Riabitsev <icon@duke.edu>
17 * @subpackage squirrelspell
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 if ( ! is_resource ( $spell_proc ) ) {
97 error_box ( _("Could not run the spellchecker command (%s).",
98 htmlspecialchars($sqspell_command) ) , $color );
100 fwrite($pipes[0], $sqspell_new_text);
102 $sqspell_output = array();
103 for($i=1; $i<=2; $i++) {
104 while(!feof($pipes[$i])) {
105 array_push($sqspell_output, rtrim(fgetss($pipes[$i],999),"\n"));
109 $sqspell_exitcode=proc_close($spell_proc);
112 $floc = "$attachment_dir/" . md5($sqspell_new_text . microtime());
113 } while (file_exists($floc));
114 $fp = @fopen($floc, 'w');
115 if ( ! is_resource ($fp) ) {
116 error_box ( _("Could not open temporary file '%s'.",
117 htmlspecialchars($floc) ) , $color );
119 fwrite($fp, $sqspell_new_text);
121 exec("$sqspell_command < $floc 2>&1", $sqspell_output, $sqspell_exitcode);
126 * Check if the execution was successful. Bail out if it wasn't.
128 if ($sqspell_exitcode){
129 $msg= "<div align='center'>"
130 . sprintf(_("I tried to execute '%s', but it returned:"),
131 $sqspell_command) . "<pre>"
132 . htmlspecialchars(join("\n", $sqspell_output)) . '</pre>'
133 . '<form onsubmit="return false">'
134 . '<input type="submit" value=" ' . _("Close")
135 . ' " onclick="self.close()" /></form></div>';
136 sqspell_makeWindow(null, _("SquirrelSpell is misconfigured."), null, $msg);
141 * Load the user dictionary.
143 $words=sqspell_getLang(sqspell_getWords(), $sqspell_use_app);
145 * Define some variables to be used during the processing.
148 $missed_words=Array();
150 $locations = Array();
153 * Now we process the output of sqspell_command (ispell or aspell in
154 * ispell compatibility mode, whichever). I'm going to be scarce on
155 * comments here, since you can just look at the ispell/aspell output
156 * and figure out what's going on. ;) The best way to describe this is
159 for ($i=0; $i<sizeof($sqspell_output); $i++){
160 switch (substr($sqspell_output[$i], 0, 1)){
163 * Ispell adds empty lines when an end of line is reached
169 * Line begins with "&".
170 * This means there's a misspelled word and a few suggestions.
173 list($left, $right) = explode(": ", $sqspell_output[$i]);
174 $tmparray = explode(" ", $left);
175 $sqspell_word=$tmparray[1];
177 * Check if the word is in user dictionary.
179 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
180 $sqspell_symb=intval($tmparray[3])-1;
181 if (!isset($misses[$sqspell_word])) {
182 $misses[$sqspell_word] = $right;
183 $missed_words[$errors] = $sqspell_word;
186 if (isset($locations[$sqspell_word])){
187 $locations[$sqspell_word] .= ', ';
189 $locations[$sqspell_word] = '';
191 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
195 * Line begins with "#".
196 * This means a misspelled word and no suggestions.
199 $tmparray = explode(" ", $sqspell_output[$i]);
200 $sqspell_word=$tmparray[1];
203 * Check if the word is in user dictionary.
205 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
206 $sqspell_symb=intval($tmparray[2])-1;
207 if (!isset($misses[$sqspell_word])) {
208 $misses[$sqspell_word] = '_NONE';
209 $missed_words[$errors] = $sqspell_word;
212 if (isset($locations[$sqspell_word])) {
213 $locations[$sqspell_word] .= ', ';
215 $locations[$sqspell_word] = '';
217 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
225 * Load the spelling errors into JavaScript arrays
228 $extrajs="<script type=\"text/javascript\">\n"
231 $sqspell_lines = explode("\n", $sqspell_text);
233 * The javascript array sqspell_lines[] contains all lines of
234 * the message we've been checking.
236 $extrajs.= "var sqspell_lines=new Array();\n";
237 for ($i=0; $i<sizeof($sqspell_lines); $i++){
238 $extrajs.= "sqspell_lines[$i] = \""
239 . chop(addslashes($sqspell_lines[$i])) . "\";\n";
244 * The javascript array misses[] contais all misspelled words.
246 $extrajs.= "var misses=new Array();\n";
247 for ($i=0; $i<sizeof($missed_words); $i++){
248 $extrajs.= "misses[$i] = \"" . $missed_words[$i] . "\";\n";
253 * Suggestions are (guess what!) suggestions for misspellings
255 $extrajs.= "var suggestions = new Array();\n";
257 while (list($word, $value) = each($misses)){
258 if ($value=='_NONE') $value='';
259 $extrajs.= "suggestions[$i] = \"$value\";\n";
265 * Locations are where those misspellings are located, line:symbol
267 $extrajs.= "var locations= new Array();\n";
269 while (list($word, $value) = each($locations)){
270 $extrajs.= "locations[$i] = \"$value\";\n";
275 * Add some strings so they can be i18n'd.
277 $extrajs.= "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
279 $extrajs.= "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
280 $extrajs.= "var ui_wait = \""
281 . _("Now saving your personal dictionary... Please wait.")
286 * Did I mention that I hate dots on the end of contcatenated lines?
287 * Dots at the beginning make so much more sense!
291 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n";
294 displayHtmlHeader(_("SquirrelSpell Results"),$extrajs);
296 echo "<body bgcolor=\"$color[4]\" text=\"$color[8]\" link=\"$color[7]\" "
297 . "alink=\"$color[7]\" vlink=\"$color[7]\" "
298 . "onload=\"populateSqspellForm()\">\n";
300 <table width="100%" border="0" cellpadding="2">
302 <td bgcolor="<?php echo $color[9] ?>" align="center">
304 <?php printf( _("Found %s errors"), $errors ) ?>
316 <input type="hidden" name="MOD" value="forget_me_not" />
317 <input type="hidden" name="words" value="" />
318 <input type="hidden" name="sqspell_use_app"
319 value="<?php echo $sqspell_use_app ?>" />
320 <table border="0" width="100%">
324 $sptag = "<span style=\"background-color: $color[9]\">";
325 echo $sptag . _("Line with an error:") . '</span>';
328 <textarea name="sqspell_line_area" cols="50" rows="3"
329 wrap="hard" onfocus="this.blur()"></textarea>
333 <td align="right" width="25%">
335 echo $sptag . _("Error:") . '</span>';
338 <td align="left" width="25%">
339 <input name="sqspell_error" size="10" value=""
340 onfocus="this.blur()" />
342 <td align="right" width="25%">
344 echo $sptag . _("Suggestions:") . '</span>';
347 <td align="left" width="25%">
348 <select name="sqspell_suggestion"
349 onchange="if (this.options[this.selectedIndex].value != '_NONE') document.forms[0].sqspell_oruse.value=this.options[this.selectedIndex].value">
351 echo '<option>' . _("Suggestions") . '</option>';
359 echo $sptag . _("Change to:") . '</span>';
363 <input name="sqspell_oruse" size="15" value=""
364 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value" />
368 echo $sptag . _("Occurs times:") . '</span>';
372 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()" />
375 <!-- hello? What is this? </td></tr> -->
377 <td colspan="4"><hr /></td>
381 <table border="0" cellpadding="0" cellspacing="3" width="100%">
382 <tr align="center" bgcolor="<?php echo $color[9] ?>">
384 SpellLink('sqspellChange()',
385 _("Change this word"),
387 SpellLink('sqspellChangeAll()',
388 _("Change ALL occurances of this word"),
390 SpellLink('sqspellIgnore()',
391 _("Ignore this word"),
393 SpellLink('sqspellIgnoreAll()',
394 _("Ignore ALL occurances this word"),
396 SpellLink('sqspellRemember()',
397 _("Add this word to your personal dictionary"),
405 <td colspan="4"><hr /></td>
408 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
410 echo '<input type="button" value=" '
411 . _("Close and Commit")
412 . ' " onclick="if (confirm(\''
413 . _("The spellcheck is not finished. Really close and commit changes?")
414 . '\')) sqspellCommitChanges()" />'
415 . ' <input type="button" value=" '
416 . _("Close and Cancel")
417 . ' " onclick="if (confirm(\''
418 . _("The spellcheck is not finished. Really close and discard changes?")
419 . '\')) self.close()" />';
432 * AREN'T YOU SUCH A KNOW-IT-ALL!
434 $msg='<form onsubmit="return false"><div align="center">'.
435 '<input type="submit" value=" ' . _("Close") .
436 ' " onclick="self.close()" /></div></form>';
437 sqspell_makeWindow(null, _("No errors found"), null, $msg);
445 * vim: syntax=php et ts=4