check_me.mod 13 KB

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