check_me.mod 13 KB

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