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