fixed ngettext formating
[squirrelmail.git] / plugins / squirrelspell / modules / check_me.mod
1 <?php
2
3 /**
4 * check_me.mod
5 *
6 * Squirrelspell module.
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 *
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
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_DEFAULT, $SQSPELL_APP, $attachment_dir, $color;
40
41 if (! sqgetGlobalVar('sqspell_text',$sqspell_text,SQ_POST)) {
42 $sqspell_text = '';
43 }
44 if (! sqgetGlobalVar('sqspell_use_app',$sqspell_use_app,SQ_POST)) {
45 $sqspell_use_app = $SQSPELL_APP_DEFAULT;
46 }
47
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);
57 for ($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) != '>'){
71 $sqspell_new_lines[$i] = ' ' . $sqspell_raw_lines[$i];
72 } else {
73 $sqspell_new_lines[$i] = ' ';
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);
81
82 /**
83 * Define the command used to spellcheck the document.
84 */
85 $sqspell_command=$SQSPELL_APP[$sqspell_use_app];
86 /**
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
89 * way, (minus the uneeded call to cat that messes up Wintel
90 * boxen.)
91 * Thanks Ray Ferguson for providing this patch.
92 */
93 if( 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 );
99 $spell_proc = @proc_open($sqspell_command, $descriptorspec, $pipes);
100 if ( ! is_resource ( $spell_proc ) ) {
101 // TODO: replace error_box() with sqspell_makeWindow()
102 error_box ( sprintf(_("Could not run the spellchecker command (%s)."),
103 htmlspecialchars($sqspell_command) ) , $color );
104 // close html tags and abort script.
105 echo "</body></html>";
106 exit();
107 }
108 if ( ! @fwrite($pipes[0], $sqspell_new_text) ) {
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();
119 }
120 fclose($pipes[0]);
121 $sqspell_output = array();
122 for($i=1; $i<=2; $i++) {
123 while(!feof($pipes[$i])) {
124 array_push($sqspell_output, rtrim(fgetss($pipes[$i],999),"\r\n"));
125 }
126 fclose($pipes[$i]);
127 }
128 $sqspell_exitcode=proc_close($spell_proc);
129 } else {
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
135 do {
136 $floc = $attachment_dir . md5($sqspell_new_text . microtime());
137 } while (file_exists($floc));
138
139 $fp = @fopen($floc, 'w');
140 if ( ! is_resource ($fp) ) {
141 // TODO: replace error_box() with sqspell_makeWindow()
142 error_box ( sprintf(_("Could not open temporary file '%s'."),
143 htmlspecialchars($floc) ) , $color );
144 // failed to open temp file. abort script.
145 echo "</body></html>";
146 exit();
147 }
148 if ( ! @fwrite($fp, $sqspell_new_text) ) {
149 // TODO: replace error_box() with sqspell_makeWindow()
150 error_box ( sprintf(_("Error while writing to temporary file '%s'."),
151 htmlspecialchars($floc) ) , $color );
152 // close file descriptor
153 fclose($fp);
154 // failed writing to temp file. abort script.
155 echo "</body></html>";
156 exit();
157 }
158 fclose($fp);
159 exec("$sqspell_command < $floc 2>&1", $sqspell_output, $sqspell_exitcode);
160 unlink($floc);
161 }
162
163 /**
164 * Check if the execution was successful. Bail out if it wasn't.
165 */
166 if ($sqspell_exitcode){
167 $msg= "<div align='center'>"
168 . sprintf(_("I tried to execute '%s', but it returned:"),
169 $sqspell_command) . "<pre>"
170 . htmlspecialchars(join("\n", $sqspell_output)) . '</pre>'
171 . '<form onsubmit="return false">'
172 . '<input type="submit" value=" ' . _("Close")
173 . ' " onclick="self.close()" /></form></div>';
174 sqspell_makeWindow(null, _("SquirrelSpell is misconfigured."), null, $msg);
175 exit;
176 }
177
178 /**
179 * Load the user dictionary.
180 */
181 $words=sqspell_getLang($sqspell_use_app);
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 */
197 for ($i=0; $i<sizeof($sqspell_output); $i++){
198 switch (substr($sqspell_output[$i], 0, 1)){
199 /**
200 * Line is empty.
201 * Ispell adds empty lines when an end of line is reached
202 */
203 case '':
204 $current_line++;
205 break;
206 /**
207 * Line begins with "&".
208 * This means there's a misspelled word and a few suggestions.
209 */
210 case '&':
211 list($left, $right) = explode(": ", $sqspell_output[$i]);
212 $tmparray = explode(" ", $left);
213 $sqspell_word=$tmparray[1];
214 /**
215 * Check if the word is in user dictionary.
216 */
217 if (! in_array($sqspell_word,$words)){
218 $sqspell_symb=intval($tmparray[3])-1;
219 if (!isset($misses[$sqspell_word])) {
220 $misses[$sqspell_word] = $right;
221 $missed_words[$errors] = $sqspell_word;
222 $errors++;
223 }
224 if (isset($locations[$sqspell_word])){
225 $locations[$sqspell_word] .= ', ';
226 } else {
227 $locations[$sqspell_word] = '';
228 }
229 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
230 }
231 break;
232 /**
233 * Line begins with "#".
234 * This means a misspelled word and no suggestions.
235 */
236 case '#':
237 $tmparray = explode(" ", $sqspell_output[$i]);
238 $sqspell_word=$tmparray[1];
239 /**
240 *
241 * Check if the word is in user dictionary.
242 */
243 if (!in_array($sqspell_word,$words)){
244 $sqspell_symb=intval($tmparray[2])-1;
245 if (!isset($misses[$sqspell_word])) {
246 $misses[$sqspell_word] = '_NONE';
247 $missed_words[$errors] = $sqspell_word;
248 $errors++;
249 }
250 if (isset($locations[$sqspell_word])) {
251 $locations[$sqspell_word] .= ', ';
252 } else {
253 $locations[$sqspell_word] = '';
254 }
255 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
256 }
257 break;
258 }
259 }
260
261 if ($errors){
262 /**
263 * Load the spelling errors into JavaScript arrays
264 * (More dark magic!)
265 */
266 $extrajs="<script type=\"text/javascript\">\n"
267 . "<!--\n";
268
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 */
274 $extrajs.= "var sqspell_lines=new Array();\n";
275 for ($i=0; $i<sizeof($sqspell_lines); $i++){
276 $extrajs.= "sqspell_lines[$i] = \""
277 . chop(addslashes($sqspell_lines[$i])) . "\";\n";
278 }
279 $extrajs.= "\n\n";
280
281 /**
282 * The javascript array misses[] contais all misspelled words.
283 */
284 $extrajs.= "var misses=new Array();\n";
285 for ($i=0; $i<sizeof($missed_words); $i++){
286 $extrajs.= "misses[$i] = \"" . $missed_words[$i] . "\";\n";
287 }
288 $extrajs.= "\n\n";
289
290 /**
291 * Suggestions are (guess what!) suggestions for misspellings
292 */
293 $extrajs.= "var suggestions = new Array();\n";
294 $i=0;
295 while (list($word, $value) = each($misses)){
296 if ($value=='_NONE') $value='';
297 $extrajs.= "suggestions[$i] = \"$value\";\n";
298 $i++;
299 }
300 $extrajs.= "\n\n";
301
302 /**
303 * Locations are where those misspellings are located, line:symbol
304 */
305 $extrajs.= "var locations= new Array();\n";
306 $i=0;
307 while (list($word, $value) = each($locations)){
308 $extrajs.= "locations[$i] = \"$value\";\n";
309 $i++;
310 }
311
312 /**
313 * Add some strings so they can be i18n'd.
314 */
315 $extrajs.= "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
316 . "\";\n";
317 $extrajs.= "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
318 $extrajs.= "var ui_wait = \""
319 . _("Now saving your personal dictionary... Please wait.")
320 . "\";\n";
321
322
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 */
327 $extrajs.= "//-->\n"
328 . "</script>\n"
329 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n";
330
331
332 displayHtmlHeader(_("SquirrelSpell Results"),$extrajs);
333
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>
342 <?php printf( ngettext("Found %d error","Found %d errors",$errors), $errors ) ?>
343 </b>
344 </td>
345 </tr>
346 <tr>
347 <td>
348 <hr />
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="" />
356 <input type="hidden" name="sqspell_use_app"
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 />
366 <textarea name="sqspell_line_area" cols="50" rows="3"
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%">
377 <input name="sqspell_error" size="10" value=""
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%">
386 <select name="sqspell_suggestion"
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=""
402 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value" />
403 </td>
404 <td align="right">
405 <?php
406 echo $sptag . _("Occurs times:") . '</span>';
407 ?>
408 </td>
409 <td align="left">
410 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()" />
411 </td>
412 </tr>
413 <!-- hello? What is this? </td></tr> -->
414 <tr>
415 <td colspan="4"><hr /></td>
416 </tr>
417 <tr>
418 <td colspan="4">
419 <table border="0" cellpadding="0" cellspacing="3" width="100%">
420 <tr align="center" bgcolor="<?php echo $color[9] ?>">
421 <?php
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"));
437 ?>
438 </tr>
439 </table>
440 </td>
441 </tr>
442 <tr>
443 <td colspan="4"><hr /></td>
444 </tr>
445 <tr>
446 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
447 <?php
448 echo '<input type="button" value=" '
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()" />';
458 ?>
459 </td>
460 </tr>
461 </table>
462 </form>
463 </td>
464 </tr>
465 </table>
466 </body></html>
467 <?php
468 } else {
469 /**
470 * AREN'T YOU SUCH A KNOW-IT-ALL!
471 */
472 $msg='<form onsubmit="return false"><div align="center">' .
473 '<input type="submit" value=" ' . _("Close") .
474 ' " onclick="self.close()" /></div></form>';
475 sqspell_makeWindow(null, _("No errors found"), null, $msg);
476 }
477
478 /**
479 * For Emacs weenies:
480 * Local variables:
481 * mode: php
482 * End:
483 * vim: syntax=php et ts=4
484 */
485 ?>