sqspell_functions.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <?php
  2. /**
  3. * sqspell_functions.php
  4. * ----------------------
  5. * All SquirrelSpell-wide functions are in this file.
  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. * $Id$
  11. *
  12. * @author Konstantin Riabitsev <icon@duke.edu> ($Author$)
  13. * @version $Date$
  14. */
  15. /**
  16. * This function is the GUI wrapper for the options page. SquirrelSpell
  17. * uses it for creating all Options pages.
  18. *
  19. * @param $title The title of the page to display
  20. * @param $scriptsrc This is used to link a file.js into the
  21. * <script src="file.js"></script> format. This
  22. * allows to separate javascript from the rest of the
  23. * plugin and place it into the js/ directory.
  24. * @param $body The body of the message to display.
  25. * @return void
  26. */
  27. function sqspell_makePage($title, $scriptsrc, $body){
  28. global $color, $SQSPELL_VERSION, $MOD;
  29. displayPageHeader($color, 'None');
  30. echo "&nbsp;<br>\n";
  31. /**
  32. * Check if we need to link in a script.
  33. */
  34. if($scriptsrc) {
  35. echo "<script type=\"text/javascript\" src=\"js/$scriptsrc\"></script>\n";
  36. }
  37. echo html_tag( 'table', '', 'center', '', 'width="95%" border="0" cellpadding="2" cellspacing="0"' ) . "\n"
  38. . html_tag( 'tr', "\n" .
  39. html_tag( 'td', '<strong>' . $title .'</strong>', 'center', $color[9] )
  40. ) . "\n"
  41. . html_tag( 'tr', "\n" .
  42. html_tag( 'td', '<hr>', 'left' )
  43. ) . "\n"
  44. . html_tag( 'tr', "\n" .
  45. html_tag( 'td', $body, 'left' )
  46. ) . "\n";
  47. /**
  48. * Generate a nice "Return to Options" link, unless this is the
  49. * starting page.
  50. */
  51. if ($MOD != "options_main"){
  52. echo html_tag( 'tr', "\n" .
  53. html_tag( 'td', '<hr>', 'left' )
  54. ) . "\n"
  55. . html_tag( 'tr', "\n" .
  56. html_tag( 'td', '<a href="sqspell_options.php">'
  57. . _("Back to &quot;SpellChecker Options&quot; page")
  58. . '</a>',
  59. 'center' )
  60. ) . "\n";
  61. }
  62. /**
  63. * Close the table and display the version.
  64. */
  65. echo html_tag( 'tr', "\n" .
  66. html_tag( 'td', '<hr>', 'left' )
  67. ) . "\n"
  68. . html_tag( 'tr',
  69. html_tag( 'td', 'SquirrelSpell ' . $SQSPELL_VERSION, 'center', $color[9] )
  70. ) . "\n";
  71. }
  72. /**
  73. * Function similar to the one above. This one is a general wrapper
  74. * for the Squirrelspell pop-up window. It's called form nearly
  75. * everywhere, except the check_me module, since that one is highly
  76. * customized.
  77. *
  78. * @param $onload Used to indicate and pass the name of a js function
  79. * to call in a <body onload="function()" for automatic
  80. * onload script execution.
  81. * @param $title Title of the page.
  82. * @param $scriptsrc If defined, link this javascript source page into
  83. * the document using <script src="file.js"> format.
  84. * @param $body The content to include.
  85. * @return void
  86. */
  87. function sqspell_makeWindow($onload, $title, $scriptsrc, $body){
  88. global $color, $SQSPELL_VERSION, $theme_css;
  89. echo "<html>\n"
  90. . "<head>\n"
  91. . "<title>$title</title>\n";
  92. /**
  93. * Check if we have a defined css theme to use.
  94. */
  95. if ($theme_css != "") {
  96. echo "<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"$theme_css\">\n";
  97. }
  98. /**
  99. * Link in the .js file if needed
  100. */
  101. if ($scriptsrc){
  102. echo "<script type=\"text/javascript\" src=\"js/$scriptsrc\"></script>\n";
  103. }
  104. echo "</head>\n"
  105. . "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" "
  106. . "vlink=\"$color[7]\" alink=\"$color[7]\"";
  107. /**
  108. * Provide an onload="jsfunction()" if asked to.
  109. */
  110. if ($onload) {
  111. echo " onload=\"$onload\"";
  112. }
  113. /**
  114. * Draw the rest of the page.
  115. */
  116. echo '>'
  117. . html_tag( 'table', "\n" .
  118. html_tag( 'tr', "\n" .
  119. html_tag( 'td', '<strong>' . $title . '</strong>', 'center', $color[9] )
  120. ) . "\n" .
  121. html_tag( 'tr', "\n" .
  122. html_tag( 'td', '<hr>', 'left' )
  123. ) . "\n" .
  124. html_tag( 'tr', "\n" .
  125. html_tag( 'td', $body, 'left' )
  126. ) . "\n" .
  127. html_tag( 'tr', "\n" .
  128. html_tag( 'td', '<hr>', 'left' )
  129. ) . "\n" .
  130. html_tag( 'tr', "\n" .
  131. html_tag( 'td', 'SquirrelSpell ' . $SQSPELL_VERSION, 'center', $color[9] )
  132. ) ,
  133. '', '', 'width="100%" border="0" cellpadding="2"' )
  134. . "</body>\n</html>\n";
  135. }
  136. /**
  137. * This function does the encryption and decryption of the user
  138. * dictionary. It is only available when PHP is compiled with
  139. * mcrypt support (--with-mcrypt). See doc/CRYPTO for more
  140. * information.
  141. *
  142. * @param $mode A string with either of the two recognized values:
  143. * "encrypt" or "decrypt".
  144. * @param $ckey The key to use for processing (the user's password
  145. * in our case.
  146. * @param $input Content to decrypt or encrypt, according to $mode.
  147. * @return encrypted/decrypted content, or "PANIC" if the
  148. * process bails out.
  149. */
  150. function sqspell_crypto($mode, $ckey, $input){
  151. /**
  152. * Double-check if we have the mcrypt_generic function. Bail out if
  153. * not so.
  154. */
  155. if (!function_exists(mcrypt_generic)) {
  156. return 'PANIC';
  157. }
  158. /**
  159. * Setup mcrypt routines.
  160. */
  161. $td = mcrypt_module_open(MCRYPT_Blowfish, "", MCRYPT_MODE_ECB, "");
  162. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
  163. mcrypt_generic_init($td, $ckey, $iv);
  164. /**
  165. * See what we have to do depending on $mode.
  166. * 'encrypt' -- Encrypt the content.
  167. * 'decrypt' -- Decrypt the content.
  168. */
  169. switch ($mode){
  170. case 'encrypt':
  171. $crypto = mcrypt_generic($td, $input);
  172. break;
  173. case 'decrypt':
  174. $crypto = mdecrypt_generic($td, $input);
  175. /**
  176. * See if it decrypted successfully. If so, it should contain
  177. * the string "# SquirrelSpell". If not, then bail out.
  178. */
  179. if (!strstr($crypto, "# SquirrelSpell")){
  180. $crypto='PANIC';
  181. }
  182. break;
  183. }
  184. /**
  185. * Finish up the mcrypt routines and return the processed content.
  186. */
  187. mcrypt_generic_end ($td);
  188. return $crypto;
  189. }
  190. /**
  191. * This function transparently upgrades the 0.2 dictionary format to the
  192. * 0.3 format, since user-defined languages have been added in 0.3 and
  193. * the new format keeps user dictionaries selection in the file.
  194. *
  195. * This function will be retired soon, as it's been a while since anyone
  196. * has been using SquirrelSpell-0.2.
  197. *
  198. * @param $words_string Contents of the 0.2-style user dictionary.
  199. * @return Contents of the 0.3-style user dictionary.
  200. */
  201. function sqspell_upgradeWordsFile($words_string){
  202. global $SQSPELL_APP_DEFAULT, $SQSPELL_VERSION;
  203. /**
  204. * Define just one dictionary for this user -- the default.
  205. * If the user wants more, s/he can set them up in personal
  206. * preferences. See doc/UPGRADING for more info.
  207. */
  208. $new_words_string =
  209. substr_replace($words_string,
  210. "# SquirrelSpell User Dictionary $SQSPELL_VERSION\n# "
  211. . "Last Revision: " . date("Y-m-d")
  212. . "\n# LANG: $SQSPELL_APP_DEFAULT\n# $SQSPELL_APP_DEFAULT",
  213. 0, strpos($words_string, "\n")) . "# End\n";
  214. sqspell_writeWords($new_words_string);
  215. return $new_words_string;
  216. }
  217. /**
  218. * Right now it just returns an array with the dictionaries
  219. * available to the user for spell-checking. It will probably
  220. * do more in the future, as features are added.
  221. *
  222. * @param $words The contents of the user's ".words" file.
  223. * @return a strings array with dictionaries available
  224. * to this user, e.g. {"English", "Spanish"}, etc.
  225. */
  226. function sqspell_getSettings($words){
  227. global $SQSPELL_APP, $SQSPELL_APP_DEFAULT;
  228. /**
  229. * Check if there is more than one dictionary configured in the
  230. * system config.
  231. */
  232. if (sizeof($SQSPELL_APP) > 1){
  233. /**
  234. * Now load the user prefs. Check if $words was empty -- a bit of
  235. * a dirty fall-back. TODO: make it so this is not required.
  236. */
  237. if(!$words){
  238. $words=sqspell_getWords();
  239. }
  240. if ($words){
  241. /**
  242. * This user has a ".words" file.
  243. * Find which dictionaries s/he wants to use and load them into
  244. * the $langs array.
  245. */
  246. preg_match("/# LANG: (.*)/i", $words, $matches);
  247. $langs=explode(", ", $matches[1]);
  248. } else {
  249. /**
  250. * User doesn't have a personal dictionary. Grab the default
  251. * system setting.
  252. */
  253. $langs[0]=$SQSPELL_APP_DEFAULT;
  254. }
  255. } else {
  256. /**
  257. * There is no need to read the ".words" file as there is only one
  258. * dictionary defined system-wide.
  259. */
  260. $langs[0]=$SQSPELL_APP_DEFAULT;
  261. }
  262. return $langs;
  263. }
  264. /**
  265. * This function returns only user-defined dictionary words that correspond
  266. * to the requested language.
  267. *
  268. * @param $words The contents of the user's ".words" file.
  269. * @param $lang Which language words to return, e.g. requesting
  270. * "English" will return ONLY the words from user's
  271. * English dictionary, disregarding any others.
  272. * @return The list of words corresponding to the language
  273. * requested.
  274. */
  275. function sqspell_getLang($words, $lang){
  276. $start=strpos($words, "# $lang\n");
  277. /**
  278. * strpos() will return -1 if no # $lang\n string was found.
  279. * Use this to return a zero-length value and indicate that no
  280. * words are present in the requested dictionary.
  281. */
  282. if (!$start) return '';
  283. /**
  284. * The words list will end with a new directive, which will start
  285. * with "#". Locate the next "#" and thus find out where the
  286. * words end.
  287. */
  288. $end=strpos($words, "#", $start+1);
  289. $lang_words = substr($words, $start, $end-$start);
  290. return $lang_words;
  291. }
  292. /**
  293. * This function operates the user dictionary. If the format is
  294. * clear-text, then it just reads the file and returns it. However, if
  295. * the file is encrypted (well, "garbled"), then it tries to decrypt
  296. * it, checks whether the decryption was successful, troubleshoots if
  297. * not, then returns the clear-text dictionary to the app.
  298. *
  299. * @return the contents of the user's ".words" file, decrypted if
  300. * necessary.
  301. */
  302. function sqspell_getWords(){
  303. global $SQSPELL_WORDS_FILE, $SQSPELL_CRYPTO;
  304. $words="";
  305. if (file_exists($SQSPELL_WORDS_FILE)){
  306. /**
  307. * Gobble it up.
  308. */
  309. $fp=fopen($SQSPELL_WORDS_FILE, 'r');
  310. $words=fread($fp, filesize($SQSPELL_WORDS_FILE));
  311. fclose($fp);
  312. }
  313. /**
  314. * Check if this is an encrypted file by looking for
  315. * the string "# SquirrelSpell" in it (the crypto
  316. * function does that).
  317. */
  318. if ($words && !strstr($words, "# SquirrelSpell")){
  319. /**
  320. * This file is encrypted or mangled. Try to decrypt it.
  321. * If fails, complain loudly.
  322. *
  323. * $old_key would be a value submitted by one of the modules with
  324. * the user's old mailbox password. I admin, this is rather dirty,
  325. * but efficient. ;)
  326. */
  327. global $key, $onetimepad, $old_key;
  328. if ($old_key) {
  329. $clear_key=$old_key;
  330. } else {
  331. /**
  332. * Get user's password (the key).
  333. */
  334. $clear_key = OneTimePadDecrypt($key, $onetimepad);
  335. }
  336. /**
  337. * Invoke the decryption routines.
  338. */
  339. $words=sqspell_crypto("decrypt", $clear_key, $words);
  340. /**
  341. * See if decryption failed.
  342. */
  343. if ($words=="PANIC"){
  344. /**
  345. * AAAAAAAAAAAH!!!!! OK, ok, breathe!
  346. * Let's hope the decryption failed because the user changed his
  347. * password. Bring up the option to key in the old password
  348. * or wipe the file and start over if everything else fails.
  349. *
  350. * The _("SquirrelSpell...) line has to be on one line, otherwise
  351. * gettext will bork. ;(
  352. */
  353. $msg = html_tag( 'p', "\n" .
  354. '<strong>' . _("ATTENTION:") . '</strong><br>'
  355. . _("SquirrelSpell was unable to decrypt your personal dictionary. This is most likely due to the fact that you have changed your mailbox password. In order to proceed, you will have to supply your old password so that SquirrelSpell can decrypt your personal dictionary. It will be re-encrypted with your new password after this.<br>If you haven't encrypted your dictionary, then it got mangled and is no longer valid. You will have to delete it and start anew. This is also true if you don't remember your old password -- without it, the encrypted data is no longer accessible.") ,
  356. 'left' ) . "\n"
  357. . '<blockquote>' . "\n"
  358. . '<form method="post" onsubmit="return AYS()">' . "\n"
  359. . '<input type="hidden" name="MOD" value="crypto_badkey">' . "\n"
  360. . html_tag( 'p', "\n" .
  361. '<input type="checkbox" name="delete_words" value="ON">'
  362. . _("Delete my dictionary and start a new one") . '<br>'
  363. . _("Decrypt my dictionary with my old password:")
  364. . '<input name="old_key" size=\"10\">' ,
  365. 'left' ) . "\n"
  366. . '</blockquote>' . "\n"
  367. . html_tag( 'p', "\n" .
  368. '<input type="submit" value="'
  369. . _("Proceed") . ' &gt;&gt;">' ,
  370. 'center' ) . "\n"
  371. . '</form>' . "\n";
  372. /**
  373. * Add some string vars so they can be i18n'd.
  374. */
  375. $msg .= "<script type='text/javascript'><!--\n"
  376. . "var ui_choice = \"" . _("You must make a choice") ."\";\n"
  377. . "var ui_candel = \"" . _("You can either delete your dictionary or type in the old password. Not both.") . "\";\n"
  378. . "var ui_willdel = \"" . _("This will delete your personal dictionary file. Proceed?") . "\";\n"
  379. . "//--></script>\n";
  380. /**
  381. * See if this happened in the pop-up window or when accessing
  382. * the SpellChecker options page.
  383. * This is a dirty solution, I agree. TODO: make this prettier.
  384. */
  385. global $SCRIPT_NAME;
  386. if (strstr($SCRIPT_NAME, "sqspell_options")){
  387. sqspell_makePage(_("Error Decrypting Dictionary"),
  388. "decrypt_error.js", $msg);
  389. } else {
  390. sqspell_makeWindow(null, _("Error Decrypting Dictionary"),
  391. "decrypt_error.js", $msg);
  392. }
  393. exit;
  394. } else {
  395. /**
  396. * OK! Phew. Set the encryption flag to true so we can later on
  397. * encrypt it again before saving to HDD.
  398. */
  399. $SQSPELL_CRYPTO=true;
  400. }
  401. } else {
  402. /**
  403. * No encryption is/was used. Set $SQSPELL_CRYPTO to false,
  404. * in case we have to save the dictionary later.
  405. */
  406. $SQSPELL_CRYPTO=false;
  407. }
  408. /**
  409. * Check if we need to upgrade the dictionary from version 0.2.x
  410. * This is going away soon.
  411. */
  412. if (strstr($words, "Dictionary v0.2")){
  413. $words=sqspell_upgradeWordsFile($words);
  414. }
  415. return $words;
  416. }
  417. /**
  418. * Writes user dictionary into the $username.words file, then changes mask
  419. * to 0600. If encryption is needed -- does that, too.
  420. *
  421. * @param $words The contents of the ".words" file to write.
  422. * @return void
  423. */
  424. function sqspell_writeWords($words){
  425. global $SQSPELL_WORDS_FILE, $SQSPELL_CRYPTO;
  426. /**
  427. * if $words is empty, create a template entry by calling the
  428. * sqspell_makeDummy() function.
  429. */
  430. if (!$words){
  431. $words=sqspell_makeDummy();
  432. }
  433. if ($SQSPELL_CRYPTO){
  434. /**
  435. * User wants to encrypt the file. So be it.
  436. * Get the user's password to use as a key.
  437. */
  438. global $key, $onetimepad;
  439. $clear_key=OneTimePadDecrypt($key, $onetimepad);
  440. /**
  441. * Try encrypting it. If fails, scream bloody hell.
  442. */
  443. $save_words = sqspell_crypto("encrypt", $clear_key, $words);
  444. if ($save_words == 'PANIC'){
  445. /**
  446. * AAAAAAAAH! I'm not handling this yet, since obviously
  447. * the admin of the site forgot to compile the MCRYPT support in
  448. * when upgrading an existing PHP installation.
  449. * I will add a handler for this case later, when I can come up
  450. * with some work-around... Right now, do nothing. Let the Admin's
  451. * head hurt.. ;)))
  452. */
  453. }
  454. } else {
  455. $save_words = $words;
  456. }
  457. /**
  458. * Do the actual writing.
  459. */
  460. $fp=fopen($SQSPELL_WORDS_FILE, "w");
  461. fwrite($fp, $save_words);
  462. fclose($fp);
  463. chmod($SQSPELL_WORDS_FILE, 0600);
  464. }
  465. function sqspell_deleteWords(){
  466. /**
  467. * So I open the door to my enemies,
  468. * and I ask can we wipe the slate clean,
  469. * but they tell me to please go...
  470. * uhm... Well, this just erases the user dictionary file.
  471. */
  472. global $SQSPELL_WORDS_FILE;
  473. if (file_exists($SQSPELL_WORDS_FILE)){
  474. unlink($SQSPELL_WORDS_FILE);
  475. }
  476. }
  477. /**
  478. * Creates an empty user dictionary for the sake of saving prefs or
  479. * whatever.
  480. *
  481. * @return The template to use when storing the user dictionary.
  482. */
  483. function sqspell_makeDummy(){
  484. global $SQSPELL_VERSION, $SQSPELL_APP_DEFAULT;
  485. $words = "# SquirrelSpell User Dictionary $SQSPELL_VERSION\n"
  486. . "# Last Revision: " . date('Y-m-d')
  487. . "\n# LANG: $SQSPELL_APP_DEFAULT\n# End\n";
  488. return $words;
  489. }
  490. /**
  491. * This function checks for security attacks. A $MOD variable is
  492. * provided in the QUERY_STRING and includes one of the files from the
  493. * modules directory ($MOD.mod). See if someone is trying to get out
  494. * of the modules directory by providing dots, unicode strings, or
  495. * slashes.
  496. *
  497. * @param $rMOD the name of the module requested to include.
  498. * @return void, since it bails out with an access error if needed.
  499. */
  500. function sqspell_ckMOD($rMOD){
  501. if (strstr($rMOD, '.')
  502. || strstr($rMOD, '/')
  503. || strstr($rMOD, '%')
  504. || strstr($rMOD, "\\")){
  505. echo _("Cute.");
  506. exit;
  507. }
  508. }
  509. /**
  510. * SquirrelSpell version. Don't modify, since it identifies the format
  511. * of the user dictionary files and messing with this can do ugly
  512. * stuff. :)
  513. */
  514. $SQSPELL_VERSION="v0.3.8";
  515. ?>