Fix for 906217. If replying to an email inline, and a spell check is done,
[squirrelmail.git] / plugins / squirrelspell / modules / check_me.mod
1 <?php
2 /**
3 * check_me.mod
4 * -------------
5 * Squirrelspell module.
6 *
7 * Copyright (c) 1999-2004 The SquirrelMail development team
8 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 *
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.
13 *
14 * $Id$
15 *
16 * @author Konstantin Riabitsev <icon@duke.edu> ($Author$)
17 * @version $Date$
18 */
19
20 /**
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.
24 *
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.
29 */
30 function SpellLink($jscode, $title, $link) {
31 echo "<td><a href=\"javascript:$jscode\" "
32 . "title=\"$title\">$link</a>"
33 . '</td>';
34 }
35
36 /**
37 * Declaring globals for users with E_ALL set.
38 */
39 global $SQSPELL_APP, $attachment_dir, $SQSPELL_EREG, $color;
40
41 $sqspell_text = $_POST['sqspell_text'];
42 $sqspell_use_app = $_POST['sqspell_use_app'];
43
44 /**
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.
51 */
52 $sqspell_raw_lines = explode("\n", $sqspell_text);
53 for ($i=0; $i<sizeof($sqspell_raw_lines); $i++){
54 /**
55 * See if the signature is starting, which will be a "--" on the
56 * single line (after trimming).
57 */
58 if (trim($sqspell_raw_lines[$i]) == '--'){
59 break;
60 }
61 /**
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
64 * stuff.
65 */
66 if(substr($sqspell_raw_lines[$i], 0, 1) != '>'){
67 $sqspell_new_lines[$i] = ' ' . $sqspell_raw_lines[$i];
68 } else {
69 $sqspell_new_lines[$i] = ' ';
70 }
71 }
72 /**
73 * $sqspell_new_lines array now contains the lines to submit to the
74 * spellchecker.
75 */
76 $sqspell_new_text=implode("\n", $sqspell_new_lines);
77
78 /**
79 * Define the command used to spellcheck the document.
80 */
81 $sqspell_command=$SQSPELL_APP[$sqspell_use_app];
82 /**
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
86 * boxen.)
87 * Thanks Ray Ferguson for providing this patch.
88 */
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
94 );
95 $spell_proc=proc_open($sqspell_command, $descriptorspec, $pipes);
96 fwrite($pipes[0], $sqspell_new_text);
97 fclose($pipes[0]);
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"));
102 fclose($pipes[$i]);
103 }
104 $sqspell_exitcode=proc_close($spell_proc);
105 } else {
106 do {
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);
111 fclose($fp);
112 exec("$sqspell_command < $floc 2>&1", $sqspell_output, $sqspell_exitcode);
113 unlink($floc);
114 }
115
116 /**
117 * Check if the execution was successful. Bail out if it wasn't.
118 */
119 if ($sqspell_exitcode){
120 $msg= "<div align='center'>"
121 . sprintf(_("I tried to execute '%s', but it returned:"),
122 $sqspell_command) . "<pre>"
123 . join("\n", htmlspecialchars($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);
128 exit;
129 }
130
131 /**
132 * Load the user dictionary.
133 */
134 $words=sqspell_getLang(sqspell_getWords(), $sqspell_use_app);
135 /**
136 * Define some variables to be used during the processing.
137 */
138 $current_line=0;
139 $missed_words=Array();
140 $misses = Array();
141 $locations = Array();
142 $errors=0;
143 /**
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
148 * "Dark Magic".
149 */
150 for ($i=0; $i<sizeof($sqspell_output); $i++){
151 switch (substr($sqspell_output[$i], 0, 1)){
152 /**
153 * Line is empty.
154 * Ispell adds empty lines when an end of line is reached
155 */
156 case '':
157 $current_line++;
158 break;
159 /**
160 * Line begins with "&".
161 * This means there's a misspelled word and a few suggestions.
162 */
163 case '&':
164 list($left, $right) = explode(": ", $sqspell_output[$i]);
165 $tmparray = explode(" ", $left);
166 $sqspell_word=$tmparray[1];
167 /**
168 * Check if the word is in user dictionary.
169 */
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;
175 $errors++;
176 }
177 if (isset($locations[$sqspell_word])){
178 $locations[$sqspell_word] .= ', ';
179 } else {
180 $locations[$sqspell_word] = '';
181 }
182 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
183 }
184 break;
185 /**
186 * Line begins with "#".
187 * This means a misspelled word and no suggestions.
188 */
189 case '#':
190 $tmparray = explode(" ", $sqspell_output[$i]);
191 $sqspell_word=$tmparray[1];
192 /**
193 *
194 * Check if the word is in user dictionary.
195 */
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;
201 $errors++;
202 }
203 if (isset($locations[$sqspell_word])) {
204 $locations[$sqspell_word] .= ', ';
205 } else {
206 $locations[$sqspell_word] = '';
207 }
208 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
209 }
210 break;
211 }
212 }
213
214 if ($errors){
215 /**
216 * Load the spelling errors into JavaScript arrays
217 * (More dark magic!)
218 */
219 $extrajs="<script type=\"text/javascript\">\n"
220 . "<!--\n";
221
222 $sqspell_lines = explode("\n", $sqspell_text);
223 /**
224 * The javascript array sqspell_lines[] contains all lines of
225 * the message we've been checking.
226 */
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";
231 }
232 $extrajs.= "\n\n";
233
234 /**
235 * The javascript array misses[] contais all misspelled words.
236 */
237 $extrajs.= "var misses=new Array();\n";
238 for ($i=0; $i<sizeof($missed_words); $i++){
239 $extrajs.= "misses[$i] = \"" . $missed_words[$i] . "\";\n";
240 }
241 $extrajs.= "\n\n";
242
243 /**
244 * Suggestions are (guess what!) suggestions for misspellings
245 */
246 $extrajs.= "var suggestions = new Array();\n";
247 $i=0;
248 while (list($word, $value) = each($misses)){
249 if ($value=='_NONE') $value='';
250 $extrajs.= "suggestions[$i] = \"$value\";\n";
251 $i++;
252 }
253 $extrajs.= "\n\n";
254
255 /**
256 * Locations are where those misspellings are located, line:symbol
257 */
258 $extrajs.= "var locations= new Array();\n";
259 $i=0;
260 while (list($word, $value) = each($locations)){
261 $extrajs.= "locations[$i] = \"$value\";\n";
262 $i++;
263 }
264
265 /**
266 * Add some strings so they can be i18n'd.
267 */
268 $extrajs.= "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
269 . "\";\n";
270 $extrajs.= "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
271 $extrajs.= "var ui_wait = \""
272 . _("Now saving your personal dictionary... Please wait.")
273 . "\";\n";
274
275
276 /**
277 * Did I mention that I hate dots on the end of contcatenated lines?
278 * Dots at the beginning make so much more sense!
279 */
280 $extrajs.= "//-->\n"
281 . "</script>\n"
282 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n";
283
284
285 displayHtmlHeader(_("SquirrelSpell Results"),$extrajs);
286
287 echo "<body bgcolor=\"$color[4]\" text=\"$color[8]\" link=\"$color[7]\" "
288 . "alink=\"$color[7]\" vlink=\"$color[7]\" "
289 . "onload=\"populateSqspellForm()\">\n";
290 ?>
291 <table width="100%" border="0" cellpadding="2">
292 <tr>
293 <td bgcolor="<?php echo $color[9] ?>" align="center">
294 <b>
295 <?php printf( _("Found %s errors"), $errors ) ?>
296 </b>
297 </td>
298 </tr>
299 <tr>
300 <td>
301 <hr />
302 </td>
303 </tr>
304 <tr>
305 <td>
306 <form method="post">
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%">
312 <tr align="center">
313 <td colspan="4">
314 <?php
315 $sptag = "<span style=\"background-color: $color[9]\">";
316 echo $sptag . _("Line with an error:") . '</span>';
317 ?>
318 <br />
319 <textarea name="sqspell_line_area" cols="50" rows="3"
320 wrap="hard" onfocus="this.blur()"></textarea>
321 </td>
322 </tr>
323 <tr valign="middle">
324 <td align="right" width="25%">
325 <?php
326 echo $sptag . _("Error:") . '</span>';
327 ?>
328 </td>
329 <td align="left" width="25%">
330 <input name="sqspell_error" size="10" value=""
331 onfocus="this.blur()" />
332 </td>
333 <td align="right" width="25%">
334 <?php
335 echo $sptag . _("Suggestions:") . '</span>';
336 ?>
337 </td>
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">
341 <?php
342 echo '<option>' . _("Suggestions") . '</option>';
343 ?>
344 </select>
345 </td>
346 </tr>
347 <tr>
348 <td align="right">
349 <?php
350 echo $sptag . _("Change to:") . '</span>';
351 ?>
352 </td>
353 <td align="left">
354 <input name="sqspell_oruse" size="15" value=""
355 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value" />
356 </td>
357 <td align="right">
358 <?php
359 echo $sptag . _("Occurs times:") . '</span>';
360 ?>
361 </td>
362 <td align="left">
363 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()" />
364 </td>
365 </tr>
366 <!-- hello? What is this? </td></tr> -->
367 <tr>
368 <td colspan="4"><hr /></td>
369 </tr>
370 <tr>
371 <td colspan="4">
372 <table border="0" cellpadding="0" cellspacing="3" width="100%">
373 <tr align="center" bgcolor="<?php echo $color[9] ?>">
374 <?php
375 SpellLink('sqspellChange()',
376 _("Change this word"),
377 _("Change"));
378 SpellLink('sqspellChangeAll()',
379 _("Change ALL occurances of this word"),
380 _("Change All"));
381 SpellLink('sqspellIgnore()',
382 _("Ignore this word"),
383 _("Ignore"));
384 SpellLink('sqspellIgnoreAll()',
385 _("Ignore ALL occurances this word"),
386 _("Ignore All"));
387 SpellLink('sqspellRemember()',
388 _("Add this word to your personal dictionary"),
389 _("Add to Dic"));
390 ?>
391 </tr>
392 </table>
393 </td>
394 </tr>
395 <tr>
396 <td colspan="4"><hr /></td>
397 </tr>
398 <tr>
399 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
400 <?php
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()" />';
411 ?>
412 </td>
413 </tr>
414 </table>
415 </form>
416 </td>
417 </tr>
418 </table>
419 </body></html>
420 <?php
421 } else {
422 /**
423 * AREN'T YOU SUCH A KNOW-IT-ALL!
424 */
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);
429 }
430
431 /**
432 * For Emacs weenies:
433 * Local variables:
434 * mode: php
435 * End:
436 * vim: syntax=php et ts=4
437 */
438 ?>