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