More SM_PATH changes
[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 {
bd9c880b 93 $floc = SM_PATH . "$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;
166 if (!$misses[$sqspell_word]) {
158d478f 167 $misses[$sqspell_word] = $right;
168 $missed_words[$errors] = $sqspell_word;
169 $errors++;
d112ed5a 170 }
171 if ($locations[$sqspell_word]){
158d478f 172 $locations[$sqspell_word] .= ', ';
d112ed5a 173 }
174 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
849bdf42 175 }
d112ed5a 176 break;
177 /**
178 * Line begins with "#".
179 * This means a misspelled word and no suggestions.
180 */
158d478f 181 case '#':
849bdf42 182 $tmparray = explode(" ", $sqspell_output[$i]);
183 $sqspell_word=$tmparray[1];
d112ed5a 184 /**
185 *
186 * Check if the word is in user dictionary.
187 */
849bdf42 188 if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){
d112ed5a 189 $sqspell_symb=intval($tmparray[2])-1;
190 if (!$misses[$sqspell_word]) {
191 $misses[$sqspell_word] = '_NONE';
192 $missed_words[$errors] = $sqspell_word;
193 $errors++;
194 }
195 if ($locations[$sqspell_word]) $locations[$sqspell_word] .= ', ';
196 $locations[$sqspell_word] .= "$current_line:$sqspell_symb";
849bdf42 197 }
d112ed5a 198 break;
849bdf42 199 }
d112ed5a 200}
158d478f 201
d112ed5a 202if ($errors){
203 /**
204 * So, there are errors
205 * This is the only place where the generic GUI-wrapper is not
206 * called, but generated right here. This is due to the complexity
207 * of the output.
208 */
209 echo "<html>\n"
210 . "<head>\n"
211 . '<title>' . _("SquirrelSpell Results") . '</title>';
212 /**
213 * Check if there are user-defined stylesheets.
214 */
215 if ($theme_css != '') {
216 echo "<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"$theme_css\">\n";
217 }
218 /**
219 * Load the spelling errors into JavaScript arrays
220 * (More dark magic!)
221 */
222 echo "<script type=\"text/javascript\">\n"
223 . "<!--\n";
224
225 $sqspell_lines = explode("\n", $sqspell_text);
226 /**
227 * The javascript array sqspell_lines[] contains all lines of
228 * the message we've been checking.
229 */
230 echo "var sqspell_lines=new Array();\n";
231 for ($i=0; $i<sizeof($sqspell_lines); $i++){
232 echo "sqspell_lines[$i] = \""
233 . chop(addslashes($sqspell_lines[$i])) . "\";\n";
234 }
235 echo "\n\n";
158d478f 236
d112ed5a 237 /**
238 * The javascript array misses[] contais all misspelled words.
239 */
240 echo "var misses=new Array();\n";
241 for ($i=0; $i<sizeof($missed_words); $i++){
242 echo "misses[$i] = \"" . $missed_words[$i] . "\";\n";
243 }
244 echo "\n\n";
245
246 /**
247 * Suggestions are (guess what!) suggestions for misspellings
248 */
249 echo "var suggestions = new Array();\n";
250 $i=0;
251 while (list($word, $value) = each($misses)){
252 if ($value=='_NONE') $value='';
253 echo "suggestions[$i] = \"$value\";\n";
254 $i++;
255 }
256 echo "\n\n";
849bdf42 257
d112ed5a 258 /**
259 * Locations are where those misspellings are located, line:symbol
260 */
261 echo "var locations= new Array();\n";
262 $i=0;
263 while (list($word, $value) = each($locations)){
264 echo "locations[$i] = \"$value\";\n";
265 $i++;
266 }
849bdf42 267
d112ed5a 268 /**
269 * Add some strings so they can be i18n'd.
270 */
271 echo "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
272 . "\";\n";
273 echo "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
274 echo "var ui_wait = \""
275 . _("Now saving your personal dictionary... Please wait.")
276 . "\";\n";
277
158d478f 278
d112ed5a 279 /**
280 * Did I mention that I hate dots on the end of contcatenated lines?
281 * Dots at the beginning make so much more sense!
282 */
283 echo "//-->\n"
284 . "</script>\n"
285 . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n"
286 . "</head>\n";
287
288 echo "<body bgcolor=\"$color[4]\" text=\"$color[8]\" link=\"$color[7]\" "
289 . "alink=\"$color[7]\" vlink=\"$color[7]\" "
290 . "onload=\"populateSqspellForm()\">\n";
291 ?>
292 <table width="100%" border="0" cellpadding="2">
293 <tr>
294 <td bgcolor="<?php echo $color[9] ?>" align="center">
295 <b>
296 <?php printf( _("Found %s errors"), $errors ) ?>
297 </b>
298 </td>
299 </tr>
300 <tr>
301 <td>
302 <hr>
303 </td>
304 </tr>
305 <tr>
306 <td>
307 <form method="post">
308 <input type="hidden" name="MOD" value="forget_me_not" />
309 <input type="hidden" name="words" value="" />
310 <input type="hidden" name="sqspell_use_app"
311 value="<?php echo $sqspell_use_app ?>" />
312 <table border="0" width="100%">
313 <tr align="center">
314 <td colspan="4">
315 <?php
316 $sptag = "<span style=\"background-color: $color[9]\">";
317 echo $sptag . _("Line with an error:") . '</span>';
318 ?>
319 <br />
320 <textarea name="sqspell_line_area" cols="50" rows="3"
321 wrap="hard" onfocus="this.blur()"></textarea>
322 </td>
323 </tr>
324 <tr valign="middle">
325 <td align="right" width="25%">
326 <?php
327 echo $sptag . _("Error:") . '</span>';
328 ?>
329 </td>
330 <td align="left" width="25%">
331 <input name="sqspell_error" size="10" value=""
332 onfocus="this.blur()" />
333 </td>
334 <td align="right" width="25%">
335 <?php
336 echo $sptag . _("Suggestions:") . '</span>';
337 ?>
338 </td>
339 <td align="left" width="25%">
340 <select name="sqspell_suggestion"
341 onchange="if (this.options[this.selectedIndex].value != '_NONE') document.forms[0].sqspell_oruse.value=this.options[this.selectedIndex].value">
342 <?php
343 echo '<option>' . _("Suggestions") . '</option>';
344 ?>
345 </select>
346 </td>
347 </tr>
348 <tr>
349 <td align="right">
350 <?php
351 echo $sptag . _("Change to:") . '</span>';
352 ?>
353 </td>
354 <td align="left">
355 <input name="sqspell_oruse" size="15" value=""
356 onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value">
357 </td>
358 <td align="right">
359 <?php
360 echo $sptag . _("Occurs times:") . '</span>';
361 ?>
362 </td>
363 <td align="left">
364 <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()">
365 </td>
366 </tr>
367 <!-- hello? What is this? </td></tr> -->
368 <tr>
369 <td colspan="4"><hr></td>
370 </tr>
371 <tr>
372 <td colspan="4">
373 <table border="0" cellpadding="0" cellspacing="3" width="100%">
374 <tr align="center" bgcolor="<?php echo $color[9] ?>">
375 <?php
376 SpellLink('sqspellChange()',
377 _("Change this word"),
378 _("Change"));
379 SpellLink('sqspellChangeAll()',
380 _("Change ALL occurances of this word"),
381 _("Change All"));
382 SpellLink('sqspellIgnore()',
383 _("Ignore this word"),
384 _("Ignore"));
385 SpellLink('sqspellIgnoreAll()',
386 _("Ignore ALL occurances this word"),
387 _("Ignore All"));
388 SpellLink('sqspellRemember()',
389 _("Add this word to your personal dictionary"),
390 _("Add to Dic"));
391 ?>
392 </tr>
393 </table>
394 </td>
395 </tr>
396 <tr>
397 <td colspan="4"><hr></td>
398 </tr>
399 <tr>
400 <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
401 <?php
402 echo '<input type="button" value=" '
403 . _("Close and Commit")
404 . ' " onclick="if (confirm(\''
405 . _("The spellcheck is not finished. Really close and commit changes?")
406 . '\')) sqspellCommitChanges()">'
407 . ' <input type="button" value=" '
408 . _("Close and Cancel")
409 . ' " onclick="if (confirm(\''
410 . _("The spellcheck is not finished. Really close and discard changes?")
411 . '\')) self.close()">';
412 ?>
413 </td>
849bdf42 414 </tr>
415 </table>
d112ed5a 416 </form>
417 </td>
418 </tr>
849bdf42 419 </table>
d112ed5a 420 </body></html>
849bdf42 421 <?php
d112ed5a 422} else {
423 /**
424 * AREN'T YOU SUCH A KNOW-IT-ALL!
425 */
426 $msg="<form onsubmit=\"return false\"><div align=\"center\">"
427 . "<input type=\"submit\" value=\" " . _("Close")
428 . " \" onclick=\"self.close()\"></div></form>";
429 sqspell_makeWindow(null, _("No errors found"), null, $msg);
430}
431
432/**
433 * For Emacs weenies:
434 * Local variables:
435 * mode: php
436 * End:
437 */
849bdf42 438?>