5 * Squirrelspell module.
7 * Copyright (c) 1999-2005 The SquirrelMail Project 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_DEFAULT, $SQSPELL_APP, $attachment_dir, $color;
41 if (! sqgetGlobalVar('sqspell_text',$sqspell_text,SQ_POST)) {
44 if (! sqgetGlobalVar('sqspell_use_app',$sqspell_use_app,SQ_POST)) {
45 $sqspell_use_app = $SQSPELL_APP_DEFAULT;
49 * Now we explode the lines for three reasons:
50 * 1) So we can ignore lines starting with ">" (reply's)
51 * 2) So we can stop processing when we get to "--" on a single line,
52 * which means that the signature is starting
53 * 3) So we can add an extra space at the beginning of each line. This way
54 * ispell/aspell don't treat these as command characters.
56 $sqspell_raw_lines = explode("\n", $sqspell_text);
57 for ($i=0; $i<sizeof($sqspell_raw_lines); $i++){
59 * See if the signature is starting, which will be a "--" on the
60 * single line (after trimming).
62 if (trim($sqspell_raw_lines[$i]) == '--'){
66 * See if this is quoted text. Don't check the quoted text, since
67 * it's no business of ours how badly our correspondents misspell
70 if(substr($sqspell_raw_lines[$i], 0, 1) != '>'){
71 $sqspell_new_lines[$i] = ' ' . $sqspell_raw_lines[$i];
73 $sqspell_new_lines[$i] = ' ';
77 * $sqspell_new_lines array now contains the lines to submit to the
80 $sqspell_new_text=implode("\n", $sqspell_new_lines);
83 * Define the command used to spellcheck the document.
85 $sqspell_command=$SQSPELL_APP[$sqspell_use_app];
87 * If you have php >= 4.3.0, we can use proc_open and safe mode
88 * and not mess w/ temp files. Otherwise we will do it the old
89 * way, (minus the uneeded call to cat that messes up Wintel
91 * Thanks Ray Ferguson for providing this patch.
93 if( check_php_version ( 4, 3 ) ) {
94 $descriptorspec = array(
95 0 => array('pipe', 'r'), // stdin is a pipe that the child will read from
96 1 => array('pipe', 'w'), // stdout is a pipe that the child will write to
97 2 => array('pipe', 'w'), // stderr is a pipe that the child will write to
99 $spell_proc = @proc_open($sqspell_command, $descriptorspec, $pipes);
100 if ( ! is_resource ( $spell_proc ) ) {
101 // TODO: replace error_box() with sqspell_makeWindow()
102 error_box ( sprintf(_("Could not run the spellchecker command (%s)."),
103 htmlspecialchars($sqspell_command) ) , $color );
104 // close html tags and abort script.
105 echo "</body></html>";
108 if ( ! @fwrite($pipes[0], $sqspell_new_text) ) {
109 // TODO: replace error_box() with sqspell_makeWindow()
110 error_box ( _("Error while writing to pipe.") , $color );
111 // close all three $pipes here.
112 for($i=0; $i<=2; $i++) {
113 // disable all fclose error messages
116 // close html tags and abort script.
117 echo "</body></html>";
121 $sqspell_output = array();
122 for($i=1; $i<=2; $i++) {
123 while(!feof($pipes[$i])) {
124 array_push($sqspell_output, rtrim(fgetss($pipes[$i],999),"\n"));
128 $sqspell_exitcode=proc_close($spell_proc);
130 // add slash to attachment directory, if it does not end with slash.
131 if (substr($attachment_dir, -1) != '/')
132 $attachment_dir = $attachment_dir . '/';
134 // find unused file in attachment directory
136 $floc = $attachment_dir . md5($sqspell_new_text . microtime());
137 } while (file_exists($floc));
139 $fp = @fopen($floc, 'w');
140 if ( ! is_resource ($fp) ) {
141 // TODO: replace error_box() with sqspell_makeWindow()
142 error_box ( sprintf(_("Could not open temporary file '%s'."),
143 htmlspecialchars($floc) ) , $color );
144 // failed to open temp file. abort script.
145 echo "</body></html>";
148 if ( ! @fwrite($fp, $sqspell_new_text) ) {
149 // TODO: replace error_box() with sqspell_makeWindow()
150 error_box ( sprintf(_("Error while writing to temporary file '%s'."),
151 htmlspecialchars($floc) ) , $color );
152 // close file descriptor
154 // failed writing to temp file. abort script.
155 echo "</body></html>";
159 exec("$sqspell_command < $floc 2>&1", $sqspell_output, $sqspell_exitcode);
164 * Check if the execution was successful. Bail out if it wasn't.
166 if ($sqspell_exitcode){
167 $msg= "<div align='center'>"
168 . sprintf(_("I tried to execute '%s', but it returned:"),
169 $sqspell_command) . "<pre>"
170 . htmlspecialchars(join("\n", $sqspell_output)) . '</pre>'
171 . '<form onsubmit="return false">'
172 . '<input type="submit" value=" ' . _("Close")
173 . ' " onclick="self.close()" /></form></div>';
174 sqspell_makeWindow(null, _("SquirrelSpell is misconfigured."), null, $msg);
179 * Load the user dictionary.
181 $words=sqspell_getLang($sqspell_use_app);
183 * Define some variables to be used during the processing.
186 $missed_words=Array();
188 $locations = Array();
191 * Now we process the output of sqspell_command (ispell or aspell in
192 * ispell compatibility mode, whichever). I'm going to be scarce on
193 * comments here, since you can just look at the ispell/aspell output
194 * and figure out what's going on. ;) The best way to describe this is
197 for ($i=0; $i<sizeof($sqspell_output); $i++){
198 switch (substr($sqspell_output[$i], 0, 1)){
201 * Ispell adds empty lines when an end of line is reached
207 * Line begins with "&".
208 * This means there's a misspelled word and a few suggestions.
211 list($left, $right) = explode(": ", $sqspell_output[$i]);
212 $tmparray = explode(" ", $left);
213 $sqspell_word=$tmparray[1];
215 * Check if the word is in user dictionary.
217 if (! in_array($sqspell_word,$words)){
218 $sqspell_symb=intval($tmparray[3])-1;
219 if (!isset($misses[$sqspell_word])) {
220 $misses[$sqspell_word] = $right;
221 $missed_words[$errors] = $sqspell_word;
224 if (isset($locations[$sqspell_word])){
225 $locations[$sqspell_word] .= ', ';
227 $locations[$sqspell_word] = '';
229 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
233 * Line begins with "#".
234 * This means a misspelled word and no suggestions.
237 $tmparray = explode(" ", $sqspell_output[$i]);
238 $sqspell_word=$tmparray[1];
241 * Check if the word is in user dictionary.
243 if (!in_array($sqspell_word,$words)){
244 $sqspell_symb=intval($tmparray[2])-1;
245 if (!isset($misses[$sqspell_word])) {
246 $misses[$sqspell_word] = '_NONE';
247 $missed_words[$errors] = $sqspell_word;
250 if (isset($locations[$sqspell_word])) {
251 $locations[$sqspell_word] .= ', ';
253 $locations[$sqspell_word] = '';
255 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
263 * Load the spelling errors into JavaScript arrays
266 $extrajs="<script type=\"text/javascript\">\n"
269 $sqspell_lines = explode("\n", $sqspell_text);
271 * The javascript array sqspell_lines[] contains all lines of
272 * the message we've been checking.
274 $extrajs.= "var sqspell_lines=new Array();\n";
275 for ($i=0; $i<sizeof($sqspell_lines); $i++){
276 $extrajs.= "sqspell_lines[$i] = \""
277 . chop(addslashes($sqspell_lines[$i])) . "\";\n";
282 * The javascript array misses[] contais all misspelled words.
284 $extrajs.= "var misses=new Array();\n";
285 for ($i=0; $i<sizeof($missed_words); $i++){
286 $extrajs.= "misses[$i] = \"" . $missed_words[$i] . "\";\n";
291 * Suggestions are (guess what!) suggestions for misspellings
293 $extrajs.= "var suggestions = new Array();\n";
295 while (list($word, $value) = each($misses)){
296 if ($value=='_NONE') $value='';
297 $extrajs.= "suggestions[$i] = \"$value\";\n";
303 * Locations are where those misspellings are located, line:symbol
305 $extrajs.= "var locations= new Array();\n";
307 while (list($word, $value) = each($locations)){
308 $extrajs.= "locations[$i] = \"$value\";\n";
313 * Add some strings so they can be i18n'd.
315 $extrajs.= "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
317 $extrajs.= "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
318 $extrajs.= "var ui_wait = \""
319 . _("Now saving your personal dictionary... Please wait.")
324 * Did I mention that I hate dots on the end of contcatenated lines?
325 * Dots at the beginning make so much more sense!
329 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n";
332 displayHtmlHeader(_("SquirrelSpell Results"),$extrajs);
334 echo "<body bgcolor=\"$color[4]\" text=\"$color[8]\" link=\"$color[7]\" "
335 . "alink=\"$color[7]\" vlink=\"$color[7]\" "
336 . "onload=\"populateSqspellForm()\">\n";
338 <table width="100%" border="0" cellpadding="2">
340 <td bgcolor="<?php echo $color[9] ?>" align="center">
342 <?php printf( ngettext(_("Found %d error"),_("Found %d errors"),$errors), $errors ) ?>
354 <input type="hidden" name="MOD" value="forget_me_not" />
355 <input type="hidden" name="words" value="" />
356 <input type="hidden" name="sqspell_use_app"
357 value="<?php echo $sqspell_use_app ?>" />
358 <table border="0" width="100%">
362 $sptag = "<span style=\"background-color: $color[9]\">";
363 echo $sptag . _("Line with an error:") . '</span>';
366 <textarea name="sqspell_line_area" cols="50" rows="3"
367 wrap="hard" onfocus="this.blur()"></textarea>
371 <td align="right" width="25%">
373 echo $sptag . _("Error:") . '</span>';
376 <td align="left" width="25%">
377 <input name="sqspell_error" size="10" value=""
378 onfocus="this.blur()" />
380 <td align="right" width="25%">
382 echo $sptag . _("Suggestions:") . '</span>';
385 <td align="left" width="25%">
386 <select name="sqspell_suggestion"
387 onchange="if (this.options[this.selectedIndex].value != '_NONE') document.forms[0].sqspell_oruse.value=this.options[this.selectedIndex].value">
389 echo '<option>' . _("Suggestions") . '</option>';
397 echo $sptag . _("Change to:") . '</span>';
401 <input name="sqspell_oruse" size="15" value=""
402 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value" />
406 echo $sptag . _("Occurs times:") . '</span>';
410 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()" />
413 <!-- hello? What is this? </td></tr> -->
415 <td colspan="4"><hr /></td>
419 <table border="0" cellpadding="0" cellspacing="3" width="100%">
420 <tr align="center" bgcolor="<?php echo $color[9] ?>">
422 SpellLink('sqspellChange()',
423 _("Change this word"),
425 SpellLink('sqspellChangeAll()',
426 _("Change ALL occurances of this word"),
428 SpellLink('sqspellIgnore()',
429 _("Ignore this word"),
431 SpellLink('sqspellIgnoreAll()',
432 _("Ignore ALL occurances this word"),
434 SpellLink('sqspellRemember()',
435 _("Add this word to your personal dictionary"),
443 <td colspan="4"><hr /></td>
446 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
448 echo '<input type="button" value=" '
449 . _("Close and Commit")
450 . ' " onclick="if (confirm(\''
451 . _("The spellcheck is not finished. Really close and commit changes?")
452 . '\')) sqspellCommitChanges()" />'
453 . ' <input type="button" value=" '
454 . _("Close and Cancel")
455 . ' " onclick="if (confirm(\''
456 . _("The spellcheck is not finished. Really close and discard changes?")
457 . '\')) self.close()" />';
470 * AREN'T YOU SUCH A KNOW-IT-ALL!
472 $msg='<form onsubmit="return false"><div align="center">' .
473 '<input type="submit" value=" ' . _("Close") .
474 ' " onclick="self.close()" /></div></form>';
475 sqspell_makeWindow(null, _("No errors found"), null, $msg);
483 * vim: syntax=php et ts=4