check_me.mod 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. <?php
  2. /**
  3. * check_me.mod
  4. *
  5. * Squirrelspell module.
  6. *
  7. * This module is the main workhorse of SquirrelSpell. It submits
  8. * the message to the spell-checker, parses the output, and loads
  9. * the interface window.
  10. *
  11. * @author Konstantin Riabitsev <icon at duke.edu>
  12. * @copyright &copy; 1999-2006 The SquirrelMail Project Team
  13. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  14. * @version $Id$
  15. * @package plugins
  16. * @subpackage squirrelspell
  17. */
  18. /**
  19. * This function makes a javascript-powered link. Not sure why
  20. * Philippe decided to move it outside the main code, but hey. ;)
  21. * I bet for the i18n reasons.
  22. *
  23. * @param $jscode Javascript code to include in the link.
  24. * @param $title A little pop-up title to provide for the links.
  25. * @param $link The content of the link.
  26. * @return void, since this just draws the content.
  27. */
  28. function SpellLink($jscode, $title, $link) {
  29. echo "<td><a href=\"javascript:$jscode\" "
  30. . "title=\"$title\">$link</a>"
  31. . '</td>';
  32. }
  33. /**
  34. * Declaring globals for users with E_ALL set.
  35. */
  36. global $SQSPELL_APP_DEFAULT, $SQSPELL_APP, $SQSPELL_SPELLCHECKER,
  37. $SQSPELL_FORCE_POPEN, $attachment_dir, $color;
  38. if (! sqgetGlobalVar('sqspell_text',$sqspell_text,SQ_POST)) {
  39. $sqspell_text = '';
  40. }
  41. if (! sqgetGlobalVar('sqspell_use_app',$sqspell_use_app,SQ_POST)) {
  42. $sqspell_use_app = $SQSPELL_APP_DEFAULT;
  43. }
  44. /**
  45. * Now we explode the lines for two 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. */
  50. $sqspell_raw_lines = explode("\n", $sqspell_text);
  51. for ($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) != '>'){
  65. $sqspell_new_lines[$i] = $sqspell_raw_lines[$i];
  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);
  75. include_once(SM_PATH . 'plugins/squirrelspell/class/common.php');
  76. $aParams = array();
  77. $aParams['words'] = sqspell_getLang($sqspell_use_app);
  78. if ($SQSPELL_SPELLCHECKER===1) {
  79. include_once(SM_PATH . 'plugins/squirrelspell/class/php_pspell.php');
  80. $aParams['dictionary'] = $SQSPELL_APP[$sqspell_use_app];
  81. $aParams['charset'] = $default_charset;
  82. $check = new php_pspell($aParams);
  83. } else {
  84. include_once(SM_PATH . 'plugins/squirrelspell/class/cmd_spell.php');
  85. $aParams['spell_command'] = $SQSPELL_APP[$sqspell_use_app];
  86. if ($SQSPELL_FORCE_POPEN) {
  87. $aParams['use_proc_open'] = false;
  88. } else {
  89. $aParams['use_proc_open'] = check_php_version(4,3);
  90. }
  91. $aParams['temp_dir'] = $attachment_dir;
  92. $aParams['debug'] = false;
  93. $check = new cmd_spell($aParams);
  94. }
  95. /**
  96. * Check for class constructor function errors
  97. */
  98. if (!empty($check->error)) {
  99. $msg= '<div style="text-align: center;">'
  100. . nl2br(htmlspecialchars($check->error))
  101. . '<form onsubmit="return false">'
  102. . '<input type="submit" value=" ' . _("Close")
  103. . ' " onclick="self.close()" /></form></div>';
  104. sqspell_makeWindow(null, _("SquirrelSpell is misconfigured."), null, $msg);
  105. exit;
  106. }
  107. $missed_words=Array();
  108. $misses = Array();
  109. $locations = Array();
  110. $errors=0;
  111. $results = $check->check_text($sqspell_new_text);
  112. /**
  113. * Check for execution errors
  114. */
  115. if (!empty($check->error)) {
  116. $msg= '<div style="text-align: center;">'
  117. . nl2br(htmlspecialchars($check->error))
  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. if (is_array($results)) {
  125. // convert variables to old style squirrelspell results
  126. if (!empty($results)) {
  127. foreach(array_keys($results) as $word) {
  128. if (isset($results[$word]['locations'])) {
  129. $missed_words[] = $word;
  130. $locations[$word] = implode(', ',$results[$word]['locations']);
  131. if (isset($results[$word]['suggestions'])) {
  132. $misses[$word] = implode(', ',$results[$word]['suggestions']);
  133. } else {
  134. $misses[$word] = '_NONE';
  135. }
  136. } else {
  137. // $word without 'locations'. ignore it
  138. }
  139. }
  140. $errors = count($missed_words);
  141. }
  142. } else {
  143. if (!empty($check->error)) {
  144. $error_msg = nl2br(htmlspecialchars($check->error));
  145. } else {
  146. $error_msg = _("Unknown error");
  147. }
  148. $msg= '<div style="text-align: center;">'
  149. . $error_msg
  150. . '<form onsubmit="return false">'
  151. . '<input type="submit" value=" ' . _("Close")
  152. . ' " onclick="self.close()" /></form></div>';
  153. sqspell_makeWindow(null, _("SquirrelSpell error."), null, $msg);
  154. exit;
  155. }
  156. if ($errors){
  157. /**
  158. * Load the spelling errors into JavaScript arrays
  159. * (More dark magic!)
  160. */
  161. $extrajs="<script type=\"text/javascript\">\n"
  162. . "<!--\n";
  163. $sqspell_lines = explode("\n", $sqspell_text);
  164. /**
  165. * The javascript array sqspell_lines[] contains all lines of
  166. * the message we've been checking.
  167. */
  168. $extrajs.= "var sqspell_lines=new Array();\n";
  169. for ($i=0; $i<sizeof($sqspell_lines); $i++){
  170. // use addcslashes for compatibility with magic_quotes_sybase
  171. $extrajs.= "sqspell_lines[$i] = \""
  172. . chop(addcslashes($sqspell_lines[$i], "'\"\\\x0")) . "\";\n";
  173. }
  174. $extrajs.= "\n\n";
  175. /**
  176. * The javascript array misses[] contais all misspelled words.
  177. */
  178. $extrajs.= "var misses=new Array();\n";
  179. for ($i=0; $i<sizeof($missed_words); $i++){
  180. $extrajs.= "misses[$i] = \"" . $missed_words[$i] . "\";\n";
  181. }
  182. $extrajs.= "\n\n";
  183. /**
  184. * Suggestions are (guess what!) suggestions for misspellings
  185. */
  186. $extrajs.= "var suggestions = new Array();\n";
  187. $i=0;
  188. while (list($word, $value) = each($misses)){
  189. if ($value=='_NONE') $value='';
  190. $extrajs.= "suggestions[$i] = \"$value\";\n";
  191. $i++;
  192. }
  193. $extrajs.= "\n\n";
  194. /**
  195. * Locations are where those misspellings are located, line:symbol
  196. */
  197. $extrajs.= "var locations= new Array();\n";
  198. $i=0;
  199. while (list($word, $value) = each($locations)){
  200. $extrajs.= "locations[$i] = \"$value\";\n";
  201. $i++;
  202. }
  203. /**
  204. * Add some strings so they can be i18n'd.
  205. */
  206. $extrajs.= "var ui_completed = \"" . _("Spellcheck completed. Commit changes?")
  207. . "\";\n";
  208. $extrajs.= "var ui_nochange = \"" . _("No changes were made.") . "\";\n";
  209. $extrajs.= "var ui_wait = \""
  210. . _("Now saving your personal dictionary... Please wait.")
  211. . "\";\n";
  212. /**
  213. * Did I mention that I hate dots on the end of concatenated lines?
  214. * Dots at the beginning make so much more sense!
  215. */
  216. $extrajs.= "//-->\n"
  217. . "</script>\n"
  218. . "<script src=\"js/check_me.js\" type=\"text/javascript\"></script>\n";
  219. displayHtmlHeader(_("SquirrelSpell Results"),$extrajs);
  220. echo "<body bgcolor=\"$color[4]\" text=\"$color[8]\" link=\"$color[7]\" "
  221. . "alink=\"$color[7]\" vlink=\"$color[7]\" "
  222. . "onload=\"populateSqspellForm()\">\n";
  223. ?>
  224. <table width="100%" border="0" cellpadding="2">
  225. <tr>
  226. <td bgcolor="<?php echo $color[9] ?>" align="center">
  227. <b>
  228. <?php printf( ngettext("Found %d error","Found %d errors",$errors), $errors ) ?>
  229. </b>
  230. </td>
  231. </tr>
  232. <tr>
  233. <td>
  234. <hr />
  235. </td>
  236. </tr>
  237. <tr>
  238. <td>
  239. <form method="post">
  240. <input type="hidden" name="MOD" value="forget_me_not" />
  241. <input type="hidden" name="words" value="" />
  242. <input type="hidden" name="sqspell_use_app"
  243. value="<?php echo $sqspell_use_app ?>" />
  244. <table border="0" width="100%">
  245. <tr align="center">
  246. <td colspan="4">
  247. <?php
  248. $sptag = "<span style=\"background-color: $color[9]\">";
  249. echo $sptag . _("Line with an error:") . '</span>';
  250. ?>
  251. <br />
  252. <textarea name="sqspell_line_area" cols="50" rows="3"
  253. onfocus="this.blur()"></textarea>
  254. </td>
  255. </tr>
  256. <tr valign="middle">
  257. <td align="right" width="25%">
  258. <?php
  259. echo $sptag . _("Error:") . '</span>';
  260. ?>
  261. </td>
  262. <td align="left" width="25%">
  263. <input name="sqspell_error" size="10" value=""
  264. onfocus="this.blur()" />
  265. </td>
  266. <td align="right" width="25%">
  267. <?php
  268. echo $sptag . _("Suggestions:") . '</span>';
  269. ?>
  270. </td>
  271. <td align="left" width="25%">
  272. <select name="sqspell_suggestion"
  273. onchange="if (this.options[this.selectedIndex].value != '_NONE') document.forms[0].sqspell_oruse.value=this.options[this.selectedIndex].value">
  274. <?php
  275. echo '<option>' . _("Suggestions") . '</option>';
  276. ?>
  277. </select>
  278. </td>
  279. </tr>
  280. <tr>
  281. <td align="right">
  282. <?php
  283. echo $sptag . _("Change to:") . '</span>';
  284. ?>
  285. </td>
  286. <td align="left">
  287. <input name="sqspell_oruse" size="15" value=""
  288. onfocus="if(!this.value) this.value=document.forms[0].sqspell_error.value" />
  289. </td>
  290. <td align="right">
  291. <?php
  292. echo $sptag . _("Occurs times:") . '</span>';
  293. ?>
  294. </td>
  295. <td align="left">
  296. <input name="sqspell_likethis" size=3 value="" onfocus="this.blur()" />
  297. </td>
  298. </tr>
  299. <!-- hello? What is this? </td></tr> -->
  300. <tr>
  301. <td colspan="4"><hr /></td>
  302. </tr>
  303. <tr>
  304. <td colspan="4">
  305. <table border="0" cellpadding="0" cellspacing="3" width="100%">
  306. <tr align="center" bgcolor="<?php echo $color[9] ?>">
  307. <?php
  308. SpellLink('sqspellChange()',
  309. _("Change this word"),
  310. _("Change"));
  311. SpellLink('sqspellChangeAll()',
  312. _("Change ALL occurances of this word"),
  313. _("Change All"));
  314. SpellLink('sqspellIgnore()',
  315. _("Ignore this word"),
  316. _("Ignore"));
  317. SpellLink('sqspellIgnoreAll()',
  318. _("Ignore ALL occurances this word"),
  319. _("Ignore All"));
  320. SpellLink('sqspellRemember()',
  321. _("Add this word to your personal dictionary"),
  322. _("Add to Dic"));
  323. ?>
  324. </tr>
  325. </table>
  326. </td>
  327. </tr>
  328. <tr>
  329. <td colspan="4"><hr /></td>
  330. </tr>
  331. <tr>
  332. <td colspan="4" align="center" bgcolor="<?php echo $color[9] ?>">
  333. <?php
  334. echo '<input type="button" value=" '
  335. . _("Close and Commit")
  336. . ' " onclick="if (confirm(\''
  337. . _("The spellcheck is not finished. Really close and commit changes?")
  338. . '\')) sqspellCommitChanges()" />'
  339. . ' <input type="button" value=" '
  340. . _("Close and Cancel")
  341. . ' " onclick="if (confirm(\''
  342. . _("The spellcheck is not finished. Really close and discard changes?")
  343. . '\')) self.close()" />';
  344. ?>
  345. </td>
  346. </tr>
  347. </table>
  348. </form>
  349. </td>
  350. </tr>
  351. </table>
  352. </body></html>
  353. <?php
  354. } else {
  355. /**
  356. * AREN'T YOU SUCH A KNOW-IT-ALL!
  357. */
  358. $msg='<form onsubmit="return false"><div style="text-align: center;">' .
  359. '<input type="submit" value=" ' . _("Close") .
  360. ' " onclick="self.close()" /></div></form>';
  361. sqspell_makeWindow(null, _("No errors found"), null, $msg);
  362. }
  363. /**
  364. * For Emacs weenies:
  365. * Local variables:
  366. * mode: php
  367. * End:
  368. * vim: syntax=php et ts=4
  369. */