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 error_box ( _("Could not run the spellchecker command (%s).",
102 htmlspecialchars($sqspell_command) ) , $color );
104 if ( ! @fwrite($pipes[0], $sqspell_new_text) ) {
105 error_box ( _("Error while writing to pipe.",
106 htmlspecialchars($floc) ) , $color );
109 $sqspell_output = array();
110 for($i=1; $i<=2; $i++) {
111 while(!feof($pipes[$i])) {
112 array_push($sqspell_output, rtrim(fgetss($pipes[$i],999),"\n"));
116 $sqspell_exitcode=proc_close($spell_proc);
119 $floc = "$attachment_dir/" . md5($sqspell_new_text . microtime());
120 } while (file_exists($floc));
121 $fp = @fopen($floc, 'w');
122 if ( ! is_resource ($fp) ) {
123 error_box ( _("Could not open temporary file '%s'.",
124 htmlspecialchars($floc) ) , $color );
126 if ( ! @fwrite($fp, $sqspell_new_text) ) {
127 error_box ( _("Error while writing to temporary file '%s'.",
128 htmlspecialchars($floc) ) , $color );
131 exec("$sqspell_command < $floc 2>&1", $sqspell_output, $sqspell_exitcode);
136 * Check if the execution was successful. Bail out if it wasn't.
138 if ($sqspell_exitcode){
139 $msg= "<div align='center'>"
140 . sprintf(_("I tried to execute '%s', but it returned:"),
141 $sqspell_command) . "<pre>"
142 . htmlspecialchars(join("\n", $sqspell_output)) . '</pre>'
143 . '<form onsubmit="return false">'
144 . '<input type="submit" value=" ' . _("Close")
145 . ' " onclick="self.close()" /></form></div>';
146 sqspell_makeWindow(null, _("SquirrelSpell is misconfigured."), null, $msg);
151 * Load the user dictionary.
153 $words=sqspell_getLang($sqspell_use_app);
155 * Define some variables to be used during the processing.
158 $missed_words=Array();
160 $locations = Array();
163 * Now we process the output of sqspell_command (ispell or aspell in
164 * ispell compatibility mode, whichever). I'm going to be scarce on
165 * comments here, since you can just look at the ispell/aspell output
166 * and figure out what's going on. ;) The best way to describe this is
169 for ($i=0; $i<sizeof($sqspell_output); $i++){
170 switch (substr($sqspell_output[$i], 0, 1)){
173 * Ispell adds empty lines when an end of line is reached
179 * Line begins with "&".
180 * This means there's a misspelled word and a few suggestions.
183 list($left, $right) = explode(": ", $sqspell_output[$i]);
184 $tmparray = explode(" ", $left);
185 $sqspell_word=$tmparray[1];
187 * Check if the word is in user dictionary.
189 if (! in_array($sqspell_word,$words)){
190 $sqspell_symb=intval($tmparray[3])-1;
191 if (!isset($misses[$sqspell_word])) {
192 $misses[$sqspell_word] = $right;
193 $missed_words[$errors] = $sqspell_word;
196 if (isset($locations[$sqspell_word])){
197 $locations[$sqspell_word] .= ', ';
199 $locations[$sqspell_word] = '';
201 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
205 * Line begins with "#".
206 * This means a misspelled word and no suggestions.
209 $tmparray = explode(" ", $sqspell_output[$i]);
210 $sqspell_word=$tmparray[1];
213 * Check if the word is in user dictionary.
215 if (!in_array($sqspell_word,$words)){
216 $sqspell_symb=intval($tmparray[2])-1;
217 if (!isset($misses[$sqspell_word])) {
218 $misses[$sqspell_word] = '_NONE';
219 $missed_words[$errors] = $sqspell_word;
222 if (isset($locations[$sqspell_word])) {
223 $locations[$sqspell_word] .= ', ';
225 $locations[$sqspell_word] = '';
227 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
235 * Load the spelling errors into JavaScript arrays
238 $extrajs="<script type=\"text/javascript\">\n"
241 $sqspell_lines = explode("\n", $sqspell_text);
243 * The javascript array sqspell_lines[] contains all lines of
244 * the message we've been checking.
246 $extrajs.= "var sqspell_lines=new Array();\n";
247 for ($i=0; $i<sizeof($sqspell_lines); $i++){
248 $extrajs.= "sqspell_lines[$i] = \""
249 . chop(addslashes($sqspell_lines[$i])) . "\";\n";
254 * The javascript array misses[] contais all misspelled words.
256 $extrajs.= "var misses=new Array();\n";
257 for ($i=0; $i<sizeof($missed_words); $i++){
258 $extrajs.= "misses[$i] = \"" . $missed_words[$i] . "\";\n";
263 * Suggestions are (guess what!) suggestions for misspellings
265 $extrajs.= "var suggestions = new Array();\n";
267 while (list($word, $value) = each($misses)){
268 if ($value=='_NONE') $value='';
269 $extrajs.= "suggestions[$i] = \"$value\";\n";
275 * Locations are where those misspellings are located, line:symbol
277 $extrajs.= "var locations= new Array();\n";
279 while (list($word, $value) = each($locations)){
280 $extrajs.= "locations[$i] = \"$value\";\n";
285 * Add some strings so they can be i18n'd.
287 $extrajs.= "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
289 $extrajs.= "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
290 $extrajs.= "var ui_wait = \""
291 . _("Now saving your personal dictionary... Please wait.")
296 * Did I mention that I hate dots on the end of contcatenated lines?
297 * Dots at the beginning make so much more sense!
301 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n";
304 displayHtmlHeader(_("SquirrelSpell Results"),$extrajs);
306 echo "<body bgcolor=\"$color[4]\" text=\"$color[8]\" link=\"$color[7]\" "
307 . "alink=\"$color[7]\" vlink=\"$color[7]\" "
308 . "onload=\"populateSqspellForm()\">\n";
310 <table width="100%" border="0" cellpadding="2">
312 <td bgcolor="<?php echo $color[9] ?>" align="center">
314 <?php printf( ngettext(_("Found %d error"),_("Found %d errors"),$errors), $errors ) ?>
326 <input type="hidden" name="MOD" value="forget_me_not" />
327 <input type="hidden" name="words" value="" />
328 <input type="hidden" name="sqspell_use_app"
329 value="<?php echo $sqspell_use_app ?>" />
330 <table border="0" width="100%">
334 $sptag = "<span style=\"background-color: $color[9]\">";
335 echo $sptag . _("Line with an error:") . '</span>';
338 <textarea name="sqspell_line_area" cols="50" rows="3"
339 wrap="hard" onfocus="this.blur()"></textarea>
343 <td align="right" width="25%">
345 echo $sptag . _("Error:") . '</span>';
348 <td align="left" width="25%">
349 <input name="sqspell_error" size="10" value=""
350 onfocus="this.blur()" />
352 <td align="right" width="25%">
354 echo $sptag . _("Suggestions:") . '</span>';
357 <td align="left" width="25%">
358 <select name="sqspell_suggestion"
359 onchange="if (this.options[this.selectedIndex].value != '_NONE') document.forms[0].sqspell_oruse.value=this.options[this.selectedIndex].value">
361 echo '<option>' . _("Suggestions") . '</option>';
369 echo $sptag . _("Change to:") . '</span>';
373 <input name="sqspell_oruse" size="15" value=""
374 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value" />
378 echo $sptag . _("Occurs times:") . '</span>';
382 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()" />
385 <!-- hello? What is this? </td></tr> -->
387 <td colspan="4"><hr /></td>
391 <table border="0" cellpadding="0" cellspacing="3" width="100%">
392 <tr align="center" bgcolor="<?php echo $color[9] ?>">
394 SpellLink('sqspellChange()',
395 _("Change this word"),
397 SpellLink('sqspellChangeAll()',
398 _("Change ALL occurances of this word"),
400 SpellLink('sqspellIgnore()',
401 _("Ignore this word"),
403 SpellLink('sqspellIgnoreAll()',
404 _("Ignore ALL occurances this word"),
406 SpellLink('sqspellRemember()',
407 _("Add this word to your personal dictionary"),
415 <td colspan="4"><hr /></td>
418 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
420 echo '<input type="button" value=" '
421 . _("Close and Commit")
422 . ' " onclick="if (confirm(\''
423 . _("The spellcheck is not finished. Really close and commit changes?")
424 . '\')) sqspellCommitChanges()" />'
425 . ' <input type="button" value=" '
426 . _("Close and Cancel")
427 . ' " onclick="if (confirm(\''
428 . _("The spellcheck is not finished. Really close and discard changes?")
429 . '\')) self.close()" />';
442 * AREN'T YOU SUCH A KNOW-IT-ALL!
444 $msg='<form onsubmit="return false"><div align="center">'.
445 '<input type="submit" value=" ' . _("Close") .
446 ' " onclick="self.close()" /></div></form>';
447 sqspell_makeWindow(null, _("No errors found"), null, $msg);
455 * vim: syntax=php et ts=4