HTML fix
[squirrelmail.git] / plugins / squirrelspell / modules / check_me.mod
CommitLineData
849bdf42 1<?php
d112ed5a 2/**
3 * check_me.mod
4 * -------------
5 * Squirrelspell module.
6 *
9eb3fcb3 7 * Copyright (c) 1999-2005 The SquirrelMail Project Team
d112ed5a 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 *
44d661aa 14 * @author Konstantin Riabitsev <icon@duke.edu>
15 * @version $Id$
16 * @package plugins
17 * @subpackage squirrelspell
d112ed5a 18 */
158d478f 19
d112ed5a 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 */
30function SpellLink($jscode, $title, $link) {
31 echo "<td><a href=\"javascript:$jscode\" "
4e37bc18 32 . "title=\"$title\">$link</a>"
d112ed5a 33 . '</td>';
158d478f 34}
35
d112ed5a 36/**
37 * Declaring globals for users with E_ALL set.
38 */
7996c920 39global $SQSPELL_APP_DEFAULT, $SQSPELL_APP, $attachment_dir, $color;
d8aa9efe 40
7996c920 41if (! sqgetGlobalVar('sqspell_text',$sqspell_text,SQ_POST)) {
42 $sqspell_text = '';
43}
44if (! sqgetGlobalVar('sqspell_use_app',$sqspell_use_app,SQ_POST)) {
45 $sqspell_use_app = $SQSPELL_APP_DEFAULT;
46}
849bdf42 47
d112ed5a 48/**
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.
55 */
56$sqspell_raw_lines = explode("\n", $sqspell_text);
57for ($i=0; $i<sizeof($sqspell_raw_lines); $i++){
58 /**
59 * See if the signature is starting, which will be a "--" on the
60 * single line (after trimming).
61 */
62 if (trim($sqspell_raw_lines[$i]) == '--'){
63 break;
64 }
65 /**
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
68 * stuff.
69 */
70 if(substr($sqspell_raw_lines[$i], 0, 1) != '>'){
158d478f 71 $sqspell_new_lines[$i] = ' ' . $sqspell_raw_lines[$i];
d112ed5a 72 } else {
3ad591f5 73 $sqspell_new_lines[$i] = ' ';
d112ed5a 74 }
75}
76/**
77 * $sqspell_new_lines array now contains the lines to submit to the
78 * spellchecker.
79 */
80$sqspell_new_text=implode("\n", $sqspell_new_lines);
849bdf42 81
d112ed5a 82/**
83 * Define the command used to spellcheck the document.
84 */
85$sqspell_command=$SQSPELL_APP[$sqspell_use_app];
86/**
38c5802f 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
91e0dccc 89 * way, (minus the uneeded call to cat that messes up Wintel
38c5802f 90 * boxen.)
91 * Thanks Ray Ferguson for providing this patch.
d112ed5a 92 */
38c5802f 93if( 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
98 );
40c2e028 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 );
103 }
2fd96e6b 104 if ( ! @fwrite($pipes[0], $sqspell_new_text) ) {
105 error_box ( _("Error while writing to pipe.",
106 htmlspecialchars($floc) ) , $color );
107 }
38c5802f 108 fclose($pipes[0]);
109 $sqspell_output = array();
40c2e028 110 for($i=1; $i<=2; $i++) {
111 while(!feof($pipes[$i])) {
91e0dccc 112 array_push($sqspell_output, rtrim(fgetss($pipes[$i],999),"\n"));
40c2e028 113 }
44d661aa 114 fclose($pipes[$i]);
38c5802f 115 }
116 $sqspell_exitcode=proc_close($spell_proc);
117} else {
118 do {
119 $floc = "$attachment_dir/" . md5($sqspell_new_text . microtime());
120 } while (file_exists($floc));
40c2e028 121 $fp = @fopen($floc, 'w');
122 if ( ! is_resource ($fp) ) {
123 error_box ( _("Could not open temporary file '%s'.",
124 htmlspecialchars($floc) ) , $color );
125 }
2fd96e6b 126 if ( ! @fwrite($fp, $sqspell_new_text) ) {
127 error_box ( _("Error while writing to temporary file '%s'.",
128 htmlspecialchars($floc) ) , $color );
129 }
38c5802f 130 fclose($fp);
131 exec("$sqspell_command < $floc 2>&1", $sqspell_output, $sqspell_exitcode);
132 unlink($floc);
133}
849bdf42 134
d112ed5a 135/**
d88c744e 136 * Check if the execution was successful. Bail out if it wasn't.
137 */
138if ($sqspell_exitcode){
139 $msg= "<div align='center'>"
140 . sprintf(_("I tried to execute '%s', but it returned:"),
141 $sqspell_command) . "<pre>"
99f2ece3 142 . htmlspecialchars(join("\n", $sqspell_output)) . '</pre>'
04fa3c41 143 . '<form onsubmit="return false">'
144 . '<input type="submit" value=" ' . _("Close")
145 . ' " onclick="self.close()" /></form></div>';
d88c744e 146 sqspell_makeWindow(null, _("SquirrelSpell is misconfigured."), null, $msg);
147 exit;
148}
149
150/**
d112ed5a 151 * Load the user dictionary.
152 */
7996c920 153$words=sqspell_getLang($sqspell_use_app);
d112ed5a 154/**
155 * Define some variables to be used during the processing.
156 */
157$current_line=0;
158$missed_words=Array();
159$misses = Array();
160$locations = Array();
161$errors=0;
162/**
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
167 * "Dark Magic".
168 */
169for ($i=0; $i<sizeof($sqspell_output); $i++){
849bdf42 170 switch (substr($sqspell_output[$i], 0, 1)){
d112ed5a 171 /**
172 * Line is empty.
173 * Ispell adds empty lines when an end of line is reached
174 */
158d478f 175 case '':
849bdf42 176 $current_line++;
d112ed5a 177 break;
178 /**
179 * Line begins with "&".
180 * This means there's a misspelled word and a few suggestions.
181 */
158d478f 182 case '&':
849bdf42 183 list($left, $right) = explode(": ", $sqspell_output[$i]);
184 $tmparray = explode(" ", $left);
185 $sqspell_word=$tmparray[1];
d112ed5a 186 /**
187 * Check if the word is in user dictionary.
188 */
7996c920 189 if (! in_array($sqspell_word,$words)){
d112ed5a 190 $sqspell_symb=intval($tmparray[3])-1;
48a1015b 191 if (!isset($misses[$sqspell_word])) {
158d478f 192 $misses[$sqspell_word] = $right;
193 $missed_words[$errors] = $sqspell_word;
194 $errors++;
d112ed5a 195 }
48a1015b 196 if (isset($locations[$sqspell_word])){
158d478f 197 $locations[$sqspell_word] .= ', ';
91e0dccc 198 } else {
48a1015b 199 $locations[$sqspell_word] = '';
d112ed5a 200 }
201 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
849bdf42 202 }
d112ed5a 203 break;
204 /**
205 * Line begins with "#".
206 * This means a misspelled word and no suggestions.
207 */
158d478f 208 case '#':
849bdf42 209 $tmparray = explode(" ", $sqspell_output[$i]);
210 $sqspell_word=$tmparray[1];
d112ed5a 211 /**
91e0dccc 212 *
d112ed5a 213 * Check if the word is in user dictionary.
214 */
7996c920 215 if (!in_array($sqspell_word,$words)){
d112ed5a 216 $sqspell_symb=intval($tmparray[2])-1;
6d68fb17 217 if (!isset($misses[$sqspell_word])) {
44d661aa 218 $misses[$sqspell_word] = '_NONE';
219 $missed_words[$errors] = $sqspell_word;
220 $errors++;
d112ed5a 221 }
6d68fb17 222 if (isset($locations[$sqspell_word])) {
44d661aa 223 $locations[$sqspell_word] .= ', ';
6d68fb17 224 } else {
44d661aa 225 $locations[$sqspell_word] = '';
226 }
d112ed5a 227 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
849bdf42 228 }
d112ed5a 229 break;
849bdf42 230 }
d112ed5a 231}
158d478f 232
d112ed5a 233if ($errors){
234 /**
d112ed5a 235 * Load the spelling errors into JavaScript arrays
236 * (More dark magic!)
237 */
6ed7bbcb 238 $extrajs="<script type=\"text/javascript\">\n"
d112ed5a 239 . "<!--\n";
91e0dccc 240
d112ed5a 241 $sqspell_lines = explode("\n", $sqspell_text);
242 /**
243 * The javascript array sqspell_lines[] contains all lines of
244 * the message we've been checking.
245 */
6ed7bbcb 246 $extrajs.= "var sqspell_lines=new Array();\n";
d112ed5a 247 for ($i=0; $i<sizeof($sqspell_lines); $i++){
91e0dccc 248 $extrajs.= "sqspell_lines[$i] = \""
d112ed5a 249 . chop(addslashes($sqspell_lines[$i])) . "\";\n";
91e0dccc 250 }
6ed7bbcb 251 $extrajs.= "\n\n";
158d478f 252
d112ed5a 253 /**
254 * The javascript array misses[] contais all misspelled words.
255 */
6ed7bbcb 256 $extrajs.= "var misses=new Array();\n";
d112ed5a 257 for ($i=0; $i<sizeof($missed_words); $i++){
6ed7bbcb 258 $extrajs.= "misses[$i] = \"" . $missed_words[$i] . "\";\n";
d112ed5a 259 }
6ed7bbcb 260 $extrajs.= "\n\n";
91e0dccc 261
d112ed5a 262 /**
263 * Suggestions are (guess what!) suggestions for misspellings
264 */
6ed7bbcb 265 $extrajs.= "var suggestions = new Array();\n";
d112ed5a 266 $i=0;
267 while (list($word, $value) = each($misses)){
268 if ($value=='_NONE') $value='';
6ed7bbcb 269 $extrajs.= "suggestions[$i] = \"$value\";\n";
d112ed5a 270 $i++;
271 }
6ed7bbcb 272 $extrajs.= "\n\n";
849bdf42 273
d112ed5a 274 /**
275 * Locations are where those misspellings are located, line:symbol
276 */
6ed7bbcb 277 $extrajs.= "var locations= new Array();\n";
d112ed5a 278 $i=0;
279 while (list($word, $value) = each($locations)){
6ed7bbcb 280 $extrajs.= "locations[$i] = \"$value\";\n";
d112ed5a 281 $i++;
282 }
849bdf42 283
91e0dccc 284 /**
d112ed5a 285 * Add some strings so they can be i18n'd.
286 */
6ed7bbcb 287 $extrajs.= "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
d112ed5a 288 . "\";\n";
6ed7bbcb 289 $extrajs.= "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
91e0dccc 290 $extrajs.= "var ui_wait = \""
d112ed5a 291 . _("Now saving your personal dictionary... Please wait.")
292 . "\";\n";
91e0dccc 293
158d478f 294
d112ed5a 295 /**
296 * Did I mention that I hate dots on the end of contcatenated lines?
297 * Dots at the beginning make so much more sense!
298 */
6ed7bbcb 299 $extrajs.= "//-->\n"
d112ed5a 300 . "</script>\n"
6ed7bbcb 301 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n";
91e0dccc 302
6ed7bbcb 303
304 displayHtmlHeader(_("SquirrelSpell Results"),$extrajs);
305
d112ed5a 306 echo "<body bgcolor=\"$color[4]\" text=\"$color[8]\" link=\"$color[7]\" "
307 . "alink=\"$color[7]\" vlink=\"$color[7]\" "
308 . "onload=\"populateSqspellForm()\">\n";
309 ?>
310 <table width="100%" border="0" cellpadding="2">
311 <tr>
312 <td bgcolor="<?php echo $color[9] ?>" align="center">
313 <b>
7996c920 314 <?php printf( ngettext(_("Found %d error"),_("Found %d errors"),$errors), $errors ) ?>
d112ed5a 315 </b>
316 </td>
317 </tr>
318 <tr>
319 <td>
04fa3c41 320 <hr />
d112ed5a 321 </td>
322 </tr>
323 <tr>
324 <td>
325 <form method="post">
326 <input type="hidden" name="MOD" value="forget_me_not" />
327 <input type="hidden" name="words" value="" />
91e0dccc 328 <input type="hidden" name="sqspell_use_app"
d112ed5a 329 value="<?php echo $sqspell_use_app ?>" />
330 <table border="0" width="100%">
331 <tr align="center">
332 <td colspan="4">
333 <?php
334 $sptag = "<span style=\"background-color: $color[9]\">";
335 echo $sptag . _("Line with an error:") . '</span>';
336 ?>
337 <br />
91e0dccc 338 <textarea name="sqspell_line_area" cols="50" rows="3"
d112ed5a 339 wrap="hard" onfocus="this.blur()"></textarea>
340 </td>
341 </tr>
342 <tr valign="middle">
343 <td align="right" width="25%">
344 <?php
345 echo $sptag . _("Error:") . '</span>';
346 ?>
347 </td>
348 <td align="left" width="25%">
91e0dccc 349 <input name="sqspell_error" size="10" value=""
d112ed5a 350 onfocus="this.blur()" />
351 </td>
352 <td align="right" width="25%">
353 <?php
354 echo $sptag . _("Suggestions:") . '</span>';
355 ?>
356 </td>
357 <td align="left" width="25%">
91e0dccc 358 <select name="sqspell_suggestion"
d112ed5a 359 onchange="if (this.options[this.selectedIndex].value != '_NONE') document.forms[0].sqspell_oruse.value=this.options[this.selectedIndex].value">
360 <?php
361 echo '<option>' . _("Suggestions") . '</option>';
362 ?>
363 </select>
364 </td>
365 </tr>
366 <tr>
367 <td align="right">
368 <?php
369 echo $sptag . _("Change to:") . '</span>';
370 ?>
371 </td>
372 <td align="left">
373 <input name="sqspell_oruse" size="15" value=""
04fa3c41 374 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value" />
d112ed5a 375 </td>
376 <td align="right">
377 <?php
378 echo $sptag . _("Occurs times:") . '</span>';
379 ?>
380 </td>
381 <td align="left">
04fa3c41 382 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()" />
d112ed5a 383 </td>
384 </tr>
385 <!-- hello? What is this? </td></tr> -->
386 <tr>
04fa3c41 387 <td colspan="4"><hr /></td>
d112ed5a 388 </tr>
389 <tr>
390 <td colspan="4">
391 <table border="0" cellpadding="0" cellspacing="3" width="100%">
90ad8cd1 392 <tr align="center" bgcolor="<?php echo $color[9] ?>">
d112ed5a 393 <?php
90ad8cd1 394 SpellLink('sqspellChange()',
395 _("Change this word"),
396 _("Change"));
397 SpellLink('sqspellChangeAll()',
398 _("Change ALL occurances of this word"),
399 _("Change All"));
400 SpellLink('sqspellIgnore()',
401 _("Ignore this word"),
402 _("Ignore"));
403 SpellLink('sqspellIgnoreAll()',
404 _("Ignore ALL occurances this word"),
405 _("Ignore All"));
406 SpellLink('sqspellRemember()',
407 _("Add this word to your personal dictionary"),
408 _("Add to Dic"));
d112ed5a 409 ?>
410 </tr>
411 </table>
412 </td>
413 </tr>
414 <tr>
04fa3c41 415 <td colspan="4"><hr /></td>
d112ed5a 416 </tr>
417 <tr>
04fa3c41 418 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
419 <?php
420 echo '<input type="button" value=" '
90ad8cd1 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()" />';
d112ed5a 430 ?>
431 </td>
849bdf42 432 </tr>
433 </table>
d112ed5a 434 </form>
435 </td>
436 </tr>
849bdf42 437 </table>
d112ed5a 438 </body></html>
849bdf42 439 <?php
d112ed5a 440} else {
441 /**
442 * AREN'T YOU SUCH A KNOW-IT-ALL!
443 */
90ad8cd1 444 $msg='<form onsubmit="return false"><div align="center">' .
04fa3c41 445 '<input type="submit" value=" ' . _("Close") .
446 ' " onclick="self.close()" /></div></form>';
d112ed5a 447 sqspell_makeWindow(null, _("No errors found"), null, $msg);
448}
449
450/**
451 * For Emacs weenies:
452 * Local variables:
453 * mode: php
454 * End:
38c5802f 455 * vim: syntax=php et ts=4
d112ed5a 456 */
90ad8cd1 457?>