moving main color tags to css
[squirrelmail.git] / plugins / squirrelspell / modules / check_me.mod
CommitLineData
849bdf42 1<?php
4b4abf93 2
d112ed5a 3/**
4 * check_me.mod
d112ed5a 5 *
4b4abf93 6 * Squirrelspell module.
d112ed5a 7 *
8 * This module is the main workhorse of SquirrelSpell. It submits
9 * the message to the spell-checker, parses the output, and loads
10 * the interface window.
11 *
4b4abf93 12 * @author Konstantin Riabitsev <icon at duke.edu>
13 * @copyright &copy; 1999-2005 The SquirrelMail Project Team
14 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
44d661aa 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 ) ) {
3bb04c83 101 // TODO: replace error_box() with sqspell_makeWindow()
102 error_box ( sprintf(_("Could not run the spellchecker command (%s)."),
40c2e028 103 htmlspecialchars($sqspell_command) ) , $color );
3bb04c83 104 // close html tags and abort script.
105 echo "</body></html>";
106 exit();
40c2e028 107 }
2fd96e6b 108 if ( ! @fwrite($pipes[0], $sqspell_new_text) ) {
3bb04c83 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
114 @fclose($pipes[$i]);
115 }
116 // close html tags and abort script.
117 echo "</body></html>";
118 exit();
2fd96e6b 119 }
38c5802f 120 fclose($pipes[0]);
121 $sqspell_output = array();
40c2e028 122 for($i=1; $i<=2; $i++) {
123 while(!feof($pipes[$i])) {
6af7eb01 124 array_push($sqspell_output, rtrim(fgetss($pipes[$i],999),"\r\n"));
40c2e028 125 }
44d661aa 126 fclose($pipes[$i]);
38c5802f 127 }
128 $sqspell_exitcode=proc_close($spell_proc);
129} else {
3bb04c83 130 // add slash to attachment directory, if it does not end with slash.
131 if (substr($attachment_dir, -1) != '/')
132 $attachment_dir = $attachment_dir . '/';
133
134 // find unused file in attachment directory
38c5802f 135 do {
3bb04c83 136 $floc = $attachment_dir . md5($sqspell_new_text . microtime());
38c5802f 137 } while (file_exists($floc));
3bb04c83 138
40c2e028 139 $fp = @fopen($floc, 'w');
140 if ( ! is_resource ($fp) ) {
3bb04c83 141 // TODO: replace error_box() with sqspell_makeWindow()
142 error_box ( sprintf(_("Could not open temporary file '%s'."),
40c2e028 143 htmlspecialchars($floc) ) , $color );
3bb04c83 144 // failed to open temp file. abort script.
145 echo "</body></html>";
146 exit();
40c2e028 147 }
2fd96e6b 148 if ( ! @fwrite($fp, $sqspell_new_text) ) {
3bb04c83 149 // TODO: replace error_box() with sqspell_makeWindow()
150 error_box ( sprintf(_("Error while writing to temporary file '%s'."),
2fd96e6b 151 htmlspecialchars($floc) ) , $color );
3bb04c83 152 // close file descriptor
153 fclose($fp);
154 // failed writing to temp file. abort script.
155 echo "</body></html>";
156 exit();
2fd96e6b 157 }
38c5802f 158 fclose($fp);
159 exec("$sqspell_command < $floc 2>&1", $sqspell_output, $sqspell_exitcode);
160 unlink($floc);
161}
849bdf42 162
d88c744e 163/**
164 * Check if the execution was successful. Bail out if it wasn't.
165 */
166if ($sqspell_exitcode){
167 $msg= "<div align='center'>"
168 . sprintf(_("I tried to execute '%s', but it returned:"),
169 $sqspell_command) . "<pre>"
99f2ece3 170 . htmlspecialchars(join("\n", $sqspell_output)) . '</pre>'
04fa3c41 171 . '<form onsubmit="return false">'
172 . '<input type="submit" value=" ' . _("Close")
173 . ' " onclick="self.close()" /></form></div>';
d88c744e 174 sqspell_makeWindow(null, _("SquirrelSpell is misconfigured."), null, $msg);
175 exit;
176}
177
d112ed5a 178/**
179 * Load the user dictionary.
180 */
7996c920 181$words=sqspell_getLang($sqspell_use_app);
d112ed5a 182/**
183 * Define some variables to be used during the processing.
184 */
185$current_line=0;
186$missed_words=Array();
187$misses = Array();
188$locations = Array();
189$errors=0;
190/**
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
195 * "Dark Magic".
196 */
197for ($i=0; $i<sizeof($sqspell_output); $i++){
849bdf42 198 switch (substr($sqspell_output[$i], 0, 1)){
d112ed5a 199 /**
200 * Line is empty.
201 * Ispell adds empty lines when an end of line is reached
202 */
158d478f 203 case '':
849bdf42 204 $current_line++;
d112ed5a 205 break;
206 /**
207 * Line begins with "&".
208 * This means there's a misspelled word and a few suggestions.
209 */
158d478f 210 case '&':
849bdf42 211 list($left, $right) = explode(": ", $sqspell_output[$i]);
212 $tmparray = explode(" ", $left);
213 $sqspell_word=$tmparray[1];
d112ed5a 214 /**
215 * Check if the word is in user dictionary.
216 */
7996c920 217 if (! in_array($sqspell_word,$words)){
d112ed5a 218 $sqspell_symb=intval($tmparray[3])-1;
48a1015b 219 if (!isset($misses[$sqspell_word])) {
158d478f 220 $misses[$sqspell_word] = $right;
221 $missed_words[$errors] = $sqspell_word;
222 $errors++;
d112ed5a 223 }
48a1015b 224 if (isset($locations[$sqspell_word])){
158d478f 225 $locations[$sqspell_word] .= ', ';
91e0dccc 226 } else {
48a1015b 227 $locations[$sqspell_word] = '';
d112ed5a 228 }
229 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
849bdf42 230 }
d112ed5a 231 break;
232 /**
233 * Line begins with "#".
234 * This means a misspelled word and no suggestions.
235 */
158d478f 236 case '#':
849bdf42 237 $tmparray = explode(" ", $sqspell_output[$i]);
238 $sqspell_word=$tmparray[1];
d112ed5a 239 /**
91e0dccc 240 *
d112ed5a 241 * Check if the word is in user dictionary.
242 */
7996c920 243 if (!in_array($sqspell_word,$words)){
d112ed5a 244 $sqspell_symb=intval($tmparray[2])-1;
6d68fb17 245 if (!isset($misses[$sqspell_word])) {
44d661aa 246 $misses[$sqspell_word] = '_NONE';
247 $missed_words[$errors] = $sqspell_word;
248 $errors++;
d112ed5a 249 }
6d68fb17 250 if (isset($locations[$sqspell_word])) {
44d661aa 251 $locations[$sqspell_word] .= ', ';
6d68fb17 252 } else {
44d661aa 253 $locations[$sqspell_word] = '';
254 }
d112ed5a 255 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
849bdf42 256 }
d112ed5a 257 break;
849bdf42 258 }
d112ed5a 259}
158d478f 260
d112ed5a 261if ($errors){
d112ed5a 262 /**
263 * Load the spelling errors into JavaScript arrays
264 * (More dark magic!)
265 */
6ed7bbcb 266 $extrajs="<script type=\"text/javascript\">\n"
d112ed5a 267 . "<!--\n";
91e0dccc 268
d112ed5a 269 $sqspell_lines = explode("\n", $sqspell_text);
270 /**
271 * The javascript array sqspell_lines[] contains all lines of
272 * the message we've been checking.
273 */
6ed7bbcb 274 $extrajs.= "var sqspell_lines=new Array();\n";
d112ed5a 275 for ($i=0; $i<sizeof($sqspell_lines); $i++){
91e0dccc 276 $extrajs.= "sqspell_lines[$i] = \""
d112ed5a 277 . chop(addslashes($sqspell_lines[$i])) . "\";\n";
91e0dccc 278 }
6ed7bbcb 279 $extrajs.= "\n\n";
158d478f 280
d112ed5a 281 /**
282 * The javascript array misses[] contais all misspelled words.
283 */
6ed7bbcb 284 $extrajs.= "var misses=new Array();\n";
d112ed5a 285 for ($i=0; $i<sizeof($missed_words); $i++){
6ed7bbcb 286 $extrajs.= "misses[$i] = \"" . $missed_words[$i] . "\";\n";
d112ed5a 287 }
6ed7bbcb 288 $extrajs.= "\n\n";
91e0dccc 289
d112ed5a 290 /**
291 * Suggestions are (guess what!) suggestions for misspellings
292 */
6ed7bbcb 293 $extrajs.= "var suggestions = new Array();\n";
d112ed5a 294 $i=0;
295 while (list($word, $value) = each($misses)){
296 if ($value=='_NONE') $value='';
6ed7bbcb 297 $extrajs.= "suggestions[$i] = \"$value\";\n";
d112ed5a 298 $i++;
299 }
6ed7bbcb 300 $extrajs.= "\n\n";
849bdf42 301
d112ed5a 302 /**
303 * Locations are where those misspellings are located, line:symbol
304 */
6ed7bbcb 305 $extrajs.= "var locations= new Array();\n";
d112ed5a 306 $i=0;
307 while (list($word, $value) = each($locations)){
6ed7bbcb 308 $extrajs.= "locations[$i] = \"$value\";\n";
d112ed5a 309 $i++;
310 }
849bdf42 311
91e0dccc 312 /**
d112ed5a 313 * Add some strings so they can be i18n'd.
314 */
6ed7bbcb 315 $extrajs.= "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
d112ed5a 316 . "\";\n";
6ed7bbcb 317 $extrajs.= "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
91e0dccc 318 $extrajs.= "var ui_wait = \""
d112ed5a 319 . _("Now saving your personal dictionary... Please wait.")
320 . "\";\n";
91e0dccc 321
158d478f 322
d112ed5a 323 /**
324 * Did I mention that I hate dots on the end of contcatenated lines?
325 * Dots at the beginning make so much more sense!
326 */
6ed7bbcb 327 $extrajs.= "//-->\n"
d112ed5a 328 . "</script>\n"
6ed7bbcb 329 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n";
91e0dccc 330
6ed7bbcb 331
332 displayHtmlHeader(_("SquirrelSpell Results"),$extrajs);
333
d112ed5a 334 echo "<body bgcolor=\"$color[4]\" text=\"$color[8]\" link=\"$color[7]\" "
335 . "alink=\"$color[7]\" vlink=\"$color[7]\" "
336 . "onload=\"populateSqspellForm()\">\n";
337 ?>
338 <table width="100%" border="0" cellpadding="2">
339 <tr>
340 <td bgcolor="<?php echo $color[9] ?>" align="center">
341 <b>
a0bce09f 342 <?php printf( ngettext("Found %d error","Found %d errors",$errors), $errors ) ?>
d112ed5a 343 </b>
344 </td>
345 </tr>
346 <tr>
347 <td>
04fa3c41 348 <hr />
d112ed5a 349 </td>
350 </tr>
351 <tr>
352 <td>
353 <form method="post">
354 <input type="hidden" name="MOD" value="forget_me_not" />
355 <input type="hidden" name="words" value="" />
91e0dccc 356 <input type="hidden" name="sqspell_use_app"
d112ed5a 357 value="<?php echo $sqspell_use_app ?>" />
358 <table border="0" width="100%">
359 <tr align="center">
360 <td colspan="4">
361 <?php
362 $sptag = "<span style=\"background-color: $color[9]\">";
363 echo $sptag . _("Line with an error:") . '</span>';
364 ?>
365 <br />
91e0dccc 366 <textarea name="sqspell_line_area" cols="50" rows="3"
d112ed5a 367 wrap="hard" onfocus="this.blur()"></textarea>
368 </td>
369 </tr>
370 <tr valign="middle">
371 <td align="right" width="25%">
372 <?php
373 echo $sptag . _("Error:") . '</span>';
374 ?>
375 </td>
376 <td align="left" width="25%">
91e0dccc 377 <input name="sqspell_error" size="10" value=""
d112ed5a 378 onfocus="this.blur()" />
379 </td>
380 <td align="right" width="25%">
381 <?php
382 echo $sptag . _("Suggestions:") . '</span>';
383 ?>
384 </td>
385 <td align="left" width="25%">
91e0dccc 386 <select name="sqspell_suggestion"
d112ed5a 387 onchange="if (this.options[this.selectedIndex].value != '_NONE') document.forms[0].sqspell_oruse.value=this.options[this.selectedIndex].value">
388 <?php
389 echo '<option>' . _("Suggestions") . '</option>';
390 ?>
391 </select>
392 </td>
393 </tr>
394 <tr>
395 <td align="right">
396 <?php
397 echo $sptag . _("Change to:") . '</span>';
398 ?>
399 </td>
400 <td align="left">
401 <input name="sqspell_oruse" size="15" value=""
04fa3c41 402 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value" />
d112ed5a 403 </td>
404 <td align="right">
405 <?php
406 echo $sptag . _("Occurs times:") . '</span>';
407 ?>
408 </td>
409 <td align="left">
04fa3c41 410 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()" />
d112ed5a 411 </td>
412 </tr>
413 <!-- hello? What is this? </td></tr> -->
414 <tr>
04fa3c41 415 <td colspan="4"><hr /></td>
d112ed5a 416 </tr>
417 <tr>
418 <td colspan="4">
419 <table border="0" cellpadding="0" cellspacing="3" width="100%">
90ad8cd1 420 <tr align="center" bgcolor="<?php echo $color[9] ?>">
d112ed5a 421 <?php
90ad8cd1 422 SpellLink('sqspellChange()',
423 _("Change this word"),
424 _("Change"));
425 SpellLink('sqspellChangeAll()',
426 _("Change ALL occurances of this word"),
427 _("Change All"));
428 SpellLink('sqspellIgnore()',
429 _("Ignore this word"),
430 _("Ignore"));
431 SpellLink('sqspellIgnoreAll()',
432 _("Ignore ALL occurances this word"),
433 _("Ignore All"));
434 SpellLink('sqspellRemember()',
435 _("Add this word to your personal dictionary"),
436 _("Add to Dic"));
d112ed5a 437 ?>
438 </tr>
439 </table>
440 </td>
441 </tr>
442 <tr>
04fa3c41 443 <td colspan="4"><hr /></td>
d112ed5a 444 </tr>
445 <tr>
04fa3c41 446 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
447 <?php
448 echo '<input type="button" value=" '
90ad8cd1 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()" />';
d112ed5a 458 ?>
459 </td>
849bdf42 460 </tr>
461 </table>
d112ed5a 462 </form>
463 </td>
464 </tr>
849bdf42 465 </table>
d112ed5a 466 </body></html>
849bdf42 467 <?php
d112ed5a 468} else {
469 /**
470 * AREN'T YOU SUCH A KNOW-IT-ALL!
471 */
90ad8cd1 472 $msg='<form onsubmit="return false"><div align="center">' .
04fa3c41 473 '<input type="submit" value=" ' . _("Close") .
474 ' " onclick="self.close()" /></div></form>';
d112ed5a 475 sqspell_makeWindow(null, _("No errors found"), null, $msg);
476}
477
478/**
479 * For Emacs weenies:
480 * Local variables:
481 * mode: php
482 * End:
38c5802f 483 * vim: syntax=php et ts=4
d112ed5a 484 */
90ad8cd1 485?>