| 1 | <?php |
| 2 | /** |
| 3 | * check_me.mod |
| 4 | * ------------- |
| 5 | * Squirrelspell module. |
| 6 | * |
| 7 | * Copyright (c) 1999-2003 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 | * For the simplicity's sake we'll put all text into a file in |
| 84 | * attachment_dir directory, then cat it and pipe it to |
| 85 | * sqspell_command. There are other ways to do it, including popen(), |
| 86 | * but it's unidirectional and no fun at all. |
| 87 | * |
| 88 | * The name of the file is an md5 hash of the message itself plus |
| 89 | * microtime. This prevents symlink attacks. The loop is here to |
| 90 | * further enhance this feature, and make sure we don't overwrite |
| 91 | * someone else's data, although the possibility of this happening is |
| 92 | * QUITE remote. |
| 93 | */ |
| 94 | do { |
| 95 | $floc = "$attachment_dir/" . md5($sqspell_new_text . microtime()); |
| 96 | } while (file_exists($floc)); |
| 97 | /** |
| 98 | * Write the contents to the file. |
| 99 | */ |
| 100 | $fp=fopen($floc, 'w'); |
| 101 | fwrite($fp, $sqspell_new_text); |
| 102 | fclose($fp); |
| 103 | /** |
| 104 | * Execute ispell/aspell and catch the output. |
| 105 | */ |
| 106 | exec("cat $floc | $sqspell_command 2>&1", $sqspell_output, $sqspell_exitcode); |
| 107 | /** |
| 108 | * Remove the temp file. |
| 109 | */ |
| 110 | unlink($floc); |
| 111 | |
| 112 | /** |
| 113 | * Check if the execution was successful. Bail out if it wasn't. |
| 114 | */ |
| 115 | if ($sqspell_exitcode){ |
| 116 | $msg= "<div align='center'>" |
| 117 | . sprintf(_("I tried to execute '%s', but it returned:"), |
| 118 | $sqspell_command) . "<pre>" |
| 119 | . nl2br(join("\n", $sqspell_output)) . "</pre>" |
| 120 | . "<form onsubmit=\"return false\">" |
| 121 | . "<input type=\"submit\" value=\" " . _("Close") |
| 122 | . " \" onclick=\"self.close()\"></form></div>"; |
| 123 | sqspell_makeWindow(null, _("SquirrelSpell is misconfigured."), null, $msg); |
| 124 | exit; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Load the user dictionary. |
| 129 | */ |
| 130 | $words=sqspell_getLang(sqspell_getWords(), $sqspell_use_app); |
| 131 | /** |
| 132 | * Define some variables to be used during the processing. |
| 133 | */ |
| 134 | $current_line=0; |
| 135 | $missed_words=Array(); |
| 136 | $misses = Array(); |
| 137 | $locations = Array(); |
| 138 | $errors=0; |
| 139 | /** |
| 140 | * Now we process the output of sqspell_command (ispell or aspell in |
| 141 | * ispell compatibility mode, whichever). I'm going to be scarce on |
| 142 | * comments here, since you can just look at the ispell/aspell output |
| 143 | * and figure out what's going on. ;) The best way to describe this is |
| 144 | * "Dark Magic". |
| 145 | */ |
| 146 | for ($i=0; $i<sizeof($sqspell_output); $i++){ |
| 147 | switch (substr($sqspell_output[$i], 0, 1)){ |
| 148 | /** |
| 149 | * Line is empty. |
| 150 | * Ispell adds empty lines when an end of line is reached |
| 151 | */ |
| 152 | case '': |
| 153 | $current_line++; |
| 154 | break; |
| 155 | /** |
| 156 | * Line begins with "&". |
| 157 | * This means there's a misspelled word and a few suggestions. |
| 158 | */ |
| 159 | case '&': |
| 160 | list($left, $right) = explode(": ", $sqspell_output[$i]); |
| 161 | $tmparray = explode(" ", $left); |
| 162 | $sqspell_word=$tmparray[1]; |
| 163 | /** |
| 164 | * Check if the word is in user dictionary. |
| 165 | */ |
| 166 | if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){ |
| 167 | $sqspell_symb=intval($tmparray[3])-1; |
| 168 | if (!isset($misses[$sqspell_word])) { |
| 169 | $misses[$sqspell_word] = $right; |
| 170 | $missed_words[$errors] = $sqspell_word; |
| 171 | $errors++; |
| 172 | } |
| 173 | if (isset($locations[$sqspell_word])){ |
| 174 | $locations[$sqspell_word] .= ', '; |
| 175 | } else { |
| 176 | $locations[$sqspell_word] = ''; |
| 177 | } |
| 178 | $locations[$sqspell_word] .= "$current_line:$sqspell_symb"; |
| 179 | } |
| 180 | break; |
| 181 | /** |
| 182 | * Line begins with "#". |
| 183 | * This means a misspelled word and no suggestions. |
| 184 | */ |
| 185 | case '#': |
| 186 | $tmparray = explode(" ", $sqspell_output[$i]); |
| 187 | $sqspell_word=$tmparray[1]; |
| 188 | /** |
| 189 | * |
| 190 | * Check if the word is in user dictionary. |
| 191 | */ |
| 192 | if (!$SQSPELL_EREG("\n$sqspell_word\n", $words)){ |
| 193 | $sqspell_symb=intval($tmparray[2])-1; |
| 194 | if (!isset($misses[$sqspell_word])) { |
| 195 | $misses[$sqspell_word] = '_NONE'; |
| 196 | $missed_words[$errors] = $sqspell_word; |
| 197 | $errors++; |
| 198 | } |
| 199 | if (isset($locations[$sqspell_word])) { |
| 200 | $locations[$sqspell_word] .= ', '; |
| 201 | } else { |
| 202 | $locations[$sqspell_word] = ''; |
| 203 | } |
| 204 | $locations[$sqspell_word] .= "$current_line:$sqspell_symb"; |
| 205 | } |
| 206 | break; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | if ($errors){ |
| 211 | /** |
| 212 | * So, there are errors |
| 213 | * This is the only place where the generic GUI-wrapper is not |
| 214 | * called, but generated right here. This is due to the complexity |
| 215 | * of the output. |
| 216 | */ |
| 217 | echo "<html>\n" |
| 218 | . "<head>\n" |
| 219 | . '<title>' . _("SquirrelSpell Results") . '</title>'; |
| 220 | /** |
| 221 | * Check if there are user-defined stylesheets. |
| 222 | */ |
| 223 | if ($theme_css != '') { |
| 224 | echo "<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"$theme_css\">\n"; |
| 225 | } |
| 226 | /** |
| 227 | * Load the spelling errors into JavaScript arrays |
| 228 | * (More dark magic!) |
| 229 | */ |
| 230 | echo "<script type=\"text/javascript\">\n" |
| 231 | . "<!--\n"; |
| 232 | |
| 233 | $sqspell_lines = explode("\n", $sqspell_text); |
| 234 | /** |
| 235 | * The javascript array sqspell_lines[] contains all lines of |
| 236 | * the message we've been checking. |
| 237 | */ |
| 238 | echo "var sqspell_lines=new Array();\n"; |
| 239 | for ($i=0; $i<sizeof($sqspell_lines); $i++){ |
| 240 | echo "sqspell_lines[$i] = \"" |
| 241 | . chop(addslashes($sqspell_lines[$i])) . "\";\n"; |
| 242 | } |
| 243 | echo "\n\n"; |
| 244 | |
| 245 | /** |
| 246 | * The javascript array misses[] contais all misspelled words. |
| 247 | */ |
| 248 | echo "var misses=new Array();\n"; |
| 249 | for ($i=0; $i<sizeof($missed_words); $i++){ |
| 250 | echo "misses[$i] = \"" . $missed_words[$i] . "\";\n"; |
| 251 | } |
| 252 | echo "\n\n"; |
| 253 | |
| 254 | /** |
| 255 | * Suggestions are (guess what!) suggestions for misspellings |
| 256 | */ |
| 257 | echo "var suggestions = new Array();\n"; |
| 258 | $i=0; |
| 259 | while (list($word, $value) = each($misses)){ |
| 260 | if ($value=='_NONE') $value=''; |
| 261 | echo "suggestions[$i] = \"$value\";\n"; |
| 262 | $i++; |
| 263 | } |
| 264 | echo "\n\n"; |
| 265 | |
| 266 | /** |
| 267 | * Locations are where those misspellings are located, line:symbol |
| 268 | */ |
| 269 | echo "var locations= new Array();\n"; |
| 270 | $i=0; |
| 271 | while (list($word, $value) = each($locations)){ |
| 272 | echo "locations[$i] = \"$value\";\n"; |
| 273 | $i++; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Add some strings so they can be i18n'd. |
| 278 | */ |
| 279 | echo "var ui_completed = \"" . _("Spellcheck completed. Commit changes?") |
| 280 | . "\";\n"; |
| 281 | echo "var ui_nochange = \"" . _("No changes were made.") . "\";\n"; |
| 282 | echo "var ui_wait = \"" |
| 283 | . _("Now saving your personal dictionary... Please wait.") |
| 284 | . "\";\n"; |
| 285 | |
| 286 | |
| 287 | /** |
| 288 | * Did I mention that I hate dots on the end of contcatenated lines? |
| 289 | * Dots at the beginning make so much more sense! |
| 290 | */ |
| 291 | echo "//-->\n" |
| 292 | . "</script>\n" |
| 293 | . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n" |
| 294 | . "</head>\n"; |
| 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 | */ |
| 446 | ?> |