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