fixed warnings
[squirrelmail.git] / plugins / squirrelspell / modules / check_me.mod
CommitLineData
849bdf42 1<?php
d112ed5a 2/**
3 * check_me.mod
4 * -------------
5 * Squirrelspell module.
6 *
7 * Copyright (c) 1999-2002 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 */
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 */
849bdf42 39global $sqspell_text, $SQSPELL_APP, $sqspell_use_app, $attachment_dir,
d112ed5a 40 $username, $SQSPELL_EREG, $color;
849bdf42 41
d112ed5a 42/**
43 * Now we explode the lines for three reasons:
44 * 1) So we can ignore lines starting with ">" (reply's)
45 * 2) So we can stop processing when we get to "--" on a single line,
46 * which means that the signature is starting
47 * 3) So we can add an extra space at the beginning of each line. This way
48 * ispell/aspell don't treat these as command characters.
49 */
50$sqspell_raw_lines = explode("\n", $sqspell_text);
51for ($i=0; $i<sizeof($sqspell_raw_lines); $i++){
52 /**
53 * See if the signature is starting, which will be a "--" on the
54 * single line (after trimming).
55 */
56 if (trim($sqspell_raw_lines[$i]) == '--'){
57 break;
58 }
59 /**
60 * See if this is quoted text. Don't check the quoted text, since
61 * it's no business of ours how badly our correspondents misspell
62 * stuff.
63 */
64 if(substr($sqspell_raw_lines[$i], 0, 1) != '>'){
158d478f 65 $sqspell_new_lines[$i] = ' ' . $sqspell_raw_lines[$i];
d112ed5a 66 } else {
67 $sqspell_new_lines[$i] = '';
68 }
69}
70/**
71 * $sqspell_new_lines array now contains the lines to submit to the
72 * spellchecker.
73 */
74$sqspell_new_text=implode("\n", $sqspell_new_lines);
849bdf42 75
d112ed5a 76/**
77 * Define the command used to spellcheck the document.
78 */
79$sqspell_command=$SQSPELL_APP[$sqspell_use_app];
80/**
81 * For the simplicity's sake we'll put all text into a file in
82 * attachment_dir directory, then cat it and pipe it to
83 * sqspell_command. There are other ways to do it, including popen(),
84 * but it's unidirectional and no fun at all.
85 *
86 * The name of the file is an md5 hash of the message itself plus
87 * microtime. This prevents symlink attacks. The loop is here to
88 * further enhance this feature, and make sure we don't overwrite
89 * someone else's data, although the possibility of this happening is
90 * QUITE remote.
91 */
92do {
5c325683 93 $floc = "$attachment_dir/" . md5($sqspell_new_text . microtime());
d112ed5a 94} while (file_exists($floc));
95/**
96 * Write the contents to the file.
97 */
98$fp=fopen($floc, 'w');
99fwrite($fp, $sqspell_new_text);
100fclose($fp);
101/**
102 * Execute ispell/aspell and catch the output.
103 */
d88c744e 104exec("cat $floc | $sqspell_command 2>&1", $sqspell_output, $sqspell_exitcode);
d112ed5a 105/**
106 * Remove the temp file.
107 */
108unlink($floc);
849bdf42 109
d88c744e 110/**
111 * Check if the execution was successful. Bail out if it wasn't.
112 */
113if ($sqspell_exitcode){
114 $msg= "<div align='center'>"
115 . sprintf(_("I tried to execute '%s', but it returned:"),
116 $sqspell_command) . "<pre>"
117 . nl2br(join("\n", $sqspell_output)) . "</pre>"
118 . "<form onsubmit=\"return false\">"
119 . "<input type=\"submit\" value=\" " . _("Close")
120 . " \" onclick=\"self.close()\"></form></div>";
121 sqspell_makeWindow(null, _("SquirrelSpell is misconfigured."), null, $msg);
122 exit;
123}
124
d112ed5a 125/**
126 * Load the user dictionary.
127 */
128$words=sqspell_getLang(sqspell_getWords(), $sqspell_use_app);
129/**
130 * Define some variables to be used during the processing.
131 */
132$current_line=0;
133$missed_words=Array();
134$misses = Array();
135$locations = Array();
136$errors=0;
137/**
138 * Now we process the output of sqspell_command (ispell or aspell in
139 * ispell compatibility mode, whichever). I'm going to be scarce on
140 * comments here, since you can just look at the ispell/aspell output
141 * and figure out what's going on. ;) The best way to describe this is
142 * "Dark Magic".
143 */
144for ($i=0; $i<sizeof($sqspell_output); $i++){
849bdf42 145 switch (substr($sqspell_output[$i], 0, 1)){
d112ed5a 146 /**
147 * Line is empty.
148 * Ispell adds empty lines when an end of line is reached
149 */
158d478f 150 case '':
849bdf42 151 $current_line++;
d112ed5a 152 break;
153 /**
154 * Line begins with "&".
155 * This means there's a misspelled word and a few suggestions.
156 */
158d478f 157 case '&':
849bdf42 158 list($left, $right) = explode(": ", $sqspell_output[$i]);
159 $tmparray = explode(" ", $left);
160 $sqspell_word=$tmparray[1];
d112ed5a 161 /**
162 * Check if the word is in user dictionary.
163 */
158d478f 164 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
d112ed5a 165 $sqspell_symb=intval($tmparray[3])-1;
48a1015b 166 if (!isset($misses[$sqspell_word])) {
158d478f 167 $misses[$sqspell_word] = $right;
168 $missed_words[$errors] = $sqspell_word;
169 $errors++;
d112ed5a 170 }
48a1015b 171 if (isset($locations[$sqspell_word])){
158d478f 172 $locations[$sqspell_word] .= ', ';
48a1015b 173 } else {
174 $locations[$sqspell_word] = '';
d112ed5a 175 }
176 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
849bdf42 177 }
d112ed5a 178 break;
179 /**
180 * Line begins with "#".
181 * This means a misspelled word and no suggestions.
182 */
158d478f 183 case '#':
849bdf42 184 $tmparray = explode(" ", $sqspell_output[$i]);
185 $sqspell_word=$tmparray[1];
d112ed5a 186 /**
187 *
188 * Check if the word is in user dictionary.
189 */
849bdf42 190 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
d112ed5a 191 $sqspell_symb=intval($tmparray[2])-1;
192 if (!$misses[$sqspell_word]) {
193 $misses[$sqspell_word] = '_NONE';
194 $missed_words[$errors] = $sqspell_word;
195 $errors++;
196 }
197 if ($locations[$sqspell_word]) $locations[$sqspell_word] .= ', ';
198 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
849bdf42 199 }
d112ed5a 200 break;
849bdf42 201 }
d112ed5a 202}
158d478f 203
d112ed5a 204if ($errors){
205 /**
206 * So, there are errors
207 * This is the only place where the generic GUI-wrapper is not
208 * called, but generated right here. This is due to the complexity
209 * of the output.
210 */
211 echo "<html>\n"
212 . "<head>\n"
213 . '<title>' . _("SquirrelSpell Results") . '</title>';
214 /**
215 * Check if there are user-defined stylesheets.
216 */
217 if ($theme_css != '') {
218 echo "<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"$theme_css\">\n";
219 }
220 /**
221 * Load the spelling errors into JavaScript arrays
222 * (More dark magic!)
223 */
224 echo "<script type=\"text/javascript\">\n"
225 . "<!--\n";
226
227 $sqspell_lines = explode("\n", $sqspell_text);
228 /**
229 * The javascript array sqspell_lines[] contains all lines of
230 * the message we've been checking.
231 */
232 echo "var sqspell_lines=new Array();\n";
233 for ($i=0; $i<sizeof($sqspell_lines); $i++){
234 echo "sqspell_lines[$i] = \""
235 . chop(addslashes($sqspell_lines[$i])) . "\";\n";
236 }
237 echo "\n\n";
158d478f 238
d112ed5a 239 /**
240 * The javascript array misses[] contais all misspelled words.
241 */
242 echo "var misses=new Array();\n";
243 for ($i=0; $i<sizeof($missed_words); $i++){
244 echo "misses[$i] = \"" . $missed_words[$i] . "\";\n";
245 }
246 echo "\n\n";
247
248 /**
249 * Suggestions are (guess what!) suggestions for misspellings
250 */
251 echo "var suggestions = new Array();\n";
252 $i=0;
253 while (list($word, $value) = each($misses)){
254 if ($value=='_NONE') $value='';
255 echo "suggestions[$i] = \"$value\";\n";
256 $i++;
257 }
258 echo "\n\n";
849bdf42 259
d112ed5a 260 /**
261 * Locations are where those misspellings are located, line:symbol
262 */
263 echo "var locations= new Array();\n";
264 $i=0;
265 while (list($word, $value) = each($locations)){
266 echo "locations[$i] = \"$value\";\n";
267 $i++;
268 }
849bdf42 269
d112ed5a 270 /**
271 * Add some strings so they can be i18n'd.
272 */
273 echo "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
274 . "\";\n";
275 echo "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
276 echo "var ui_wait = \""
277 . _("Now saving your personal dictionary... Please wait.")
278 . "\";\n";
279
158d478f 280
d112ed5a 281 /**
282 * Did I mention that I hate dots on the end of contcatenated lines?
283 * Dots at the beginning make so much more sense!
284 */
285 echo "//-->\n"
286 . "</script>\n"
287 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n"
288 . "</head>\n";
289
290 echo "<body bgcolor=\"$color[4]\" text=\"$color[8]\" link=\"$color[7]\" "
291 . "alink=\"$color[7]\" vlink=\"$color[7]\" "
292 . "onload=\"populateSqspellForm()\">\n";
293 ?>
294 <table width="100%" border="0" cellpadding="2">
295 <tr>
296 <td bgcolor="<?php echo $color[9] ?>" align="center">
297 <b>
298 <?php printf( _("Found %s errors"), $errors ) ?>
299 </b>
300 </td>
301 </tr>
302 <tr>
303 <td>
304 <hr>
305 </td>
306 </tr>
307 <tr>
308 <td>
309 <form method="post">
310 <input type="hidden" name="MOD" value="forget_me_not" />
311 <input type="hidden" name="words" value="" />
312 <input type="hidden" name="sqspell_use_app"
313 value="<?php echo $sqspell_use_app ?>" />
314 <table border="0" width="100%">
315 <tr align="center">
316 <td colspan="4">
317 <?php
318 $sptag = "<span style=\"background-color: $color[9]\">";
319 echo $sptag . _("Line with an error:") . '</span>';
320 ?>
321 <br />
322 <textarea name="sqspell_line_area" cols="50" rows="3"
323 wrap="hard" onfocus="this.blur()"></textarea>
324 </td>
325 </tr>
326 <tr valign="middle">
327 <td align="right" width="25%">
328 <?php
329 echo $sptag . _("Error:") . '</span>';
330 ?>
331 </td>
332 <td align="left" width="25%">
333 <input name="sqspell_error" size="10" value=""
334 onfocus="this.blur()" />
335 </td>
336 <td align="right" width="25%">
337 <?php
338 echo $sptag . _("Suggestions:") . '</span>';
339 ?>
340 </td>
341 <td align="left" width="25%">
342 <select name="sqspell_suggestion"
343 onchange="if (this.options[this.selectedIndex].value != '_NONE') document.forms[0].sqspell_oruse.value=this.options[this.selectedIndex].value">
344 <?php
345 echo '<option>' . _("Suggestions") . '</option>';
346 ?>
347 </select>
348 </td>
349 </tr>
350 <tr>
351 <td align="right">
352 <?php
353 echo $sptag . _("Change to:") . '</span>';
354 ?>
355 </td>
356 <td align="left">
357 <input name="sqspell_oruse" size="15" value=""
358 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value">
359 </td>
360 <td align="right">
361 <?php
362 echo $sptag . _("Occurs times:") . '</span>';
363 ?>
364 </td>
365 <td align="left">
366 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()">
367 </td>
368 </tr>
369 <!-- hello? What is this? </td></tr> -->
370 <tr>
371 <td colspan="4"><hr></td>
372 </tr>
373 <tr>
374 <td colspan="4">
375 <table border="0" cellpadding="0" cellspacing="3" width="100%">
376 <tr align="center" bgcolor="<?php echo $color[9] ?>">
377 <?php
378 SpellLink('sqspellChange()',
379 _("Change this word"),
380 _("Change"));
381 SpellLink('sqspellChangeAll()',
382 _("Change ALL occurances of this word"),
383 _("Change All"));
384 SpellLink('sqspellIgnore()',
385 _("Ignore this word"),
386 _("Ignore"));
387 SpellLink('sqspellIgnoreAll()',
388 _("Ignore ALL occurances this word"),
389 _("Ignore All"));
390 SpellLink('sqspellRemember()',
391 _("Add this word to your personal dictionary"),
392 _("Add to Dic"));
393 ?>
394 </tr>
395 </table>
396 </td>
397 </tr>
398 <tr>
399 <td colspan="4"><hr></td>
400 </tr>
401 <tr>
402 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
403 <?php
404 echo '<input type="button" value=" '
405 . _("Close and Commit")
406 . ' " onclick="if (confirm(\''
407 . _("The spellcheck is not finished. Really close and commit changes?")
408 . '\')) sqspellCommitChanges()">'
409 . ' <input type="button" value=" '
410 . _("Close and Cancel")
411 . ' " onclick="if (confirm(\''
412 . _("The spellcheck is not finished. Really close and discard changes?")
413 . '\')) self.close()">';
414 ?>
415 </td>
849bdf42 416 </tr>
417 </table>
d112ed5a 418 </form>
419 </td>
420 </tr>
849bdf42 421 </table>
d112ed5a 422 </body></html>
849bdf42 423 <?php
d112ed5a 424} else {
425 /**
426 * AREN'T YOU SUCH A KNOW-IT-ALL!
427 */
428 $msg="<form onsubmit=\"return false\"><div align=\"center\">"
429 . "<input type=\"submit\" value=\" " . _("Close")
430 . " \" onclick=\"self.close()\"></div></form>";
431 sqspell_makeWindow(null, _("No errors found"), null, $msg);
432}
433
434/**
435 * For Emacs weenies:
436 * Local variables:
437 * mode: php
438 * End:
439 */
849bdf42 440?>