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