adding address book block
[squirrelmail.git] / plugins / squirrelspell / modules / check_me.mod
CommitLineData
849bdf42 1<?php
d112ed5a 2/**
3 * check_me.mod
4 * -------------
5 * Squirrelspell module.
6 *
38c5802f 7 * Copyright (c) 1999-2004 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
85 * way, (minus the uneeded call to cat that messes up Wintel
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 );
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"));
44d661aa 102 fclose($pipes[$i]);
38c5802f 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}
849bdf42 115
d112ed5a 116/**
d88c744e 117 * Check if the execution was successful. Bail out if it wasn't.
118 */
119if ($sqspell_exitcode){
120 $msg= "<div align='center'>"
121 . sprintf(_("I tried to execute '%s', but it returned:"),
122 $sqspell_command) . "<pre>"
99f2ece3 123 . htmlspecialchars(join("\n", $sqspell_output)) . '</pre>'
04fa3c41 124 . '<form onsubmit="return false">'
125 . '<input type="submit" value=" ' . _("Close")
126 . ' " onclick="self.close()" /></form></div>';
d88c744e 127 sqspell_makeWindow(null, _("SquirrelSpell is misconfigured."), null, $msg);
128 exit;
129}
130
131/**
d112ed5a 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 */
150for ($i=0; $i<sizeof($sqspell_output); $i++){
849bdf42 151 switch (substr($sqspell_output[$i], 0, 1)){
d112ed5a 152 /**
153 * Line is empty.
154 * Ispell adds empty lines when an end of line is reached
155 */
158d478f 156 case '':
849bdf42 157 $current_line++;
d112ed5a 158 break;
159 /**
160 * Line begins with "&".
161 * This means there's a misspelled word and a few suggestions.
162 */
158d478f 163 case '&':
849bdf42 164 list($left, $right) = explode(": ", $sqspell_output[$i]);
165 $tmparray = explode(" ", $left);
166 $sqspell_word=$tmparray[1];
d112ed5a 167 /**
168 * Check if the word is in user dictionary.
169 */
158d478f 170 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
d112ed5a 171 $sqspell_symb=intval($tmparray[3])-1;
48a1015b 172 if (!isset($misses[$sqspell_word])) {
158d478f 173 $misses[$sqspell_word] = $right;
174 $missed_words[$errors] = $sqspell_word;
175 $errors++;
d112ed5a 176 }
48a1015b 177 if (isset($locations[$sqspell_word])){
158d478f 178 $locations[$sqspell_word] .= ', ';
48a1015b 179 } else {
180 $locations[$sqspell_word] = '';
d112ed5a 181 }
182 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
849bdf42 183 }
d112ed5a 184 break;
185 /**
186 * Line begins with "#".
187 * This means a misspelled word and no suggestions.
188 */
158d478f 189 case '#':
849bdf42 190 $tmparray = explode(" ", $sqspell_output[$i]);
191 $sqspell_word=$tmparray[1];
d112ed5a 192 /**
193 *
194 * Check if the word is in user dictionary.
195 */
849bdf42 196 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
d112ed5a 197 $sqspell_symb=intval($tmparray[2])-1;
6d68fb17 198 if (!isset($misses[$sqspell_word])) {
44d661aa 199 $misses[$sqspell_word] = '_NONE';
200 $missed_words[$errors] = $sqspell_word;
201 $errors++;
d112ed5a 202 }
6d68fb17 203 if (isset($locations[$sqspell_word])) {
44d661aa 204 $locations[$sqspell_word] .= ', ';
6d68fb17 205 } else {
44d661aa 206 $locations[$sqspell_word] = '';
207 }
d112ed5a 208 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
849bdf42 209 }
d112ed5a 210 break;
849bdf42 211 }
d112ed5a 212}
158d478f 213
d112ed5a 214if ($errors){
215 /**
d112ed5a 216 * Load the spelling errors into JavaScript arrays
217 * (More dark magic!)
218 */
6ed7bbcb 219 $extrajs="<script type=\"text/javascript\">\n"
d112ed5a 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 */
6ed7bbcb 227 $extrajs.= "var sqspell_lines=new Array();\n";
d112ed5a 228 for ($i=0; $i<sizeof($sqspell_lines); $i++){
6ed7bbcb 229 $extrajs.= "sqspell_lines[$i] = \""
d112ed5a 230 . chop(addslashes($sqspell_lines[$i])) . "\";\n";
231 }
6ed7bbcb 232 $extrajs.= "\n\n";
158d478f 233
d112ed5a 234 /**
235 * The javascript array misses[] contais all misspelled words.
236 */
6ed7bbcb 237 $extrajs.= "var misses=new Array();\n";
d112ed5a 238 for ($i=0; $i<sizeof($missed_words); $i++){
6ed7bbcb 239 $extrajs.= "misses[$i] = \"" . $missed_words[$i] . "\";\n";
d112ed5a 240 }
6ed7bbcb 241 $extrajs.= "\n\n";
d112ed5a 242
243 /**
244 * Suggestions are (guess what!) suggestions for misspellings
245 */
6ed7bbcb 246 $extrajs.= "var suggestions = new Array();\n";
d112ed5a 247 $i=0;
248 while (list($word, $value) = each($misses)){
249 if ($value=='_NONE') $value='';
6ed7bbcb 250 $extrajs.= "suggestions[$i] = \"$value\";\n";
d112ed5a 251 $i++;
252 }
6ed7bbcb 253 $extrajs.= "\n\n";
849bdf42 254
d112ed5a 255 /**
256 * Locations are where those misspellings are located, line:symbol
257 */
6ed7bbcb 258 $extrajs.= "var locations= new Array();\n";
d112ed5a 259 $i=0;
260 while (list($word, $value) = each($locations)){
6ed7bbcb 261 $extrajs.= "locations[$i] = \"$value\";\n";
d112ed5a 262 $i++;
263 }
849bdf42 264
d112ed5a 265 /**
266 * Add some strings so they can be i18n'd.
267 */
6ed7bbcb 268 $extrajs.= "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
d112ed5a 269 . "\";\n";
6ed7bbcb 270 $extrajs.= "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
271 $extrajs.= "var ui_wait = \""
d112ed5a 272 . _("Now saving your personal dictionary... Please wait.")
273 . "\";\n";
274
158d478f 275
d112ed5a 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 */
6ed7bbcb 280 $extrajs.= "//-->\n"
d112ed5a 281 . "</script>\n"
6ed7bbcb 282 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n";
d112ed5a 283
6ed7bbcb 284
285 displayHtmlHeader(_("SquirrelSpell Results"),$extrajs);
286
d112ed5a 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>
04fa3c41 301 <hr />
d112ed5a 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=""
04fa3c41 355 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value" />
d112ed5a 356 </td>
357 <td align="right">
358 <?php
359 echo $sptag . _("Occurs times:") . '</span>';
360 ?>
361 </td>
362 <td align="left">
04fa3c41 363 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()" />
d112ed5a 364 </td>
365 </tr>
366 <!-- hello? What is this? </td></tr> -->
367 <tr>
04fa3c41 368 <td colspan="4"><hr /></td>
d112ed5a 369 </tr>
370 <tr>
371 <td colspan="4">
372 <table border="0" cellpadding="0" cellspacing="3" width="100%">
44d661aa 373 <tr align="center" bgcolor="<?php echo $color[9] ?>">
d112ed5a 374 <?php
44d661aa 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"));
d112ed5a 390 ?>
391 </tr>
392 </table>
393 </td>
394 </tr>
395 <tr>
04fa3c41 396 <td colspan="4"><hr /></td>
d112ed5a 397 </tr>
398 <tr>
04fa3c41 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()" />';
d112ed5a 411 ?>
412 </td>
849bdf42 413 </tr>
414 </table>
d112ed5a 415 </form>
416 </td>
417 </tr>
849bdf42 418 </table>
d112ed5a 419 </body></html>
849bdf42 420 <?php
d112ed5a 421} else {
422 /**
423 * AREN'T YOU SUCH A KNOW-IT-ALL!
424 */
04fa3c41 425 $msg='<form onsubmit="return false"><div align="center">'.
426 '<input type="submit" value=" ' . _("Close") .
427 ' " onclick="self.close()" /></div></form>';
d112ed5a 428 sqspell_makeWindow(null, _("No errors found"), null, $msg);
429}
430
431/**
432 * For Emacs weenies:
433 * Local variables:
434 * mode: php
435 * End:
38c5802f 436 * vim: syntax=php et ts=4
d112ed5a 437 */
44d661aa 438?>