sqspell_functions.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  1. <?php
  2. /**
  3. * sqspell_functions.php
  4. *
  5. * All SquirrelSpell-wide functions are in this file.
  6. *
  7. * @author Konstantin Riabitsev <icon at duke.edu>
  8. * @copyright &copy; 1999-2007 The SquirrelMail Project Team
  9. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10. * @version $Id$
  11. * @package plugins
  12. * @subpackage squirrelspell
  13. */
  14. /** globalize configuration vars **/
  15. global $SQSPELL_APP, $SQSPELL_APP_DEFAULT, $SQSPELL_WORDS_FILE, $SQSPELL_CRYPTO;
  16. /**
  17. * load plugin configuration
  18. * @todo allow storing configuration file in config/ directory
  19. */
  20. include_once(SM_PATH . 'plugins/squirrelspell/sqspell_config.php');
  21. /**
  22. * Workaround for including function squirrelspell_version() in SM 1.5 CVS,
  23. * where plugins' setup.php is not included by default.
  24. */
  25. include_once(SM_PATH . 'plugins/squirrelspell/setup.php');
  26. /** Hooked functions **/
  27. /**
  28. * Register option page block (internal function)
  29. * @since 1.5.1 (sqspell 0.5)
  30. * @return void
  31. */
  32. function squirrelspell_optpage_block_function() {
  33. global $optpage_blocks;
  34. /**
  35. * Dependency on JavaScript is checked by SquirrelMail scripts
  36. * Register Squirrelspell with the $optpage_blocks array.
  37. */
  38. $optpage_blocks[] =
  39. array(
  40. 'name' => _("SpellChecker Options"),
  41. 'url' => '../plugins/squirrelspell/sqspell_options.php',
  42. 'desc' => _("Here you may set up how your personal dictionary is stored, edit it, or choose which languages should be available to you when spell-checking."),
  43. 'js' => TRUE);
  44. }
  45. /**
  46. * This function adds a "Check Spelling" link to the "Compose" row
  47. * during message composition (internal function).
  48. * @since 1.5.1 (sqspell 0.5)
  49. * @return void
  50. */
  51. function squirrelspell_setup_function() {
  52. /**
  53. * Check if this browser is capable of displaying SquirrelSpell
  54. * correctly.
  55. */
  56. if (checkForJavascript()) {
  57. global $oTemplate, $base_uri, $nbsp;
  58. $output = addButton(_("Check Spelling"),
  59. 'check_spelling',
  60. array('onclick' => 'window.open(\'' . $base_uri
  61. . 'plugins/squirrelspell/sqspell_interface.php\', \'sqspell\', \'status=yes,width=550,height=370,resizable=yes\')')) . $nbsp;
  62. return array('compose_button_row' => $output);
  63. }
  64. }
  65. /**
  66. * Upgrade dictionaries (internal function)
  67. *
  68. * Transparently upgrades user's dictionaries when message listing is loaded
  69. * @since 1.5.1 (sqspell 0.5)
  70. */
  71. function squirrelspell_upgrade_function() {
  72. global $data_dir, $username;
  73. if (! sqspell_check_version(0,5)) {
  74. $langs=sqspell_getSettings_old(null);
  75. $words=sqspell_getWords_old();
  76. sqspell_saveSettings($langs);
  77. foreach ($langs as $lang) {
  78. $lang_words=sqspell_getLang_old($words,$lang);
  79. $aLang_words=explode("\n",$lang_words);
  80. $new_words=array();
  81. foreach($aLang_words as $word) {
  82. if (! preg_match("/^#/",$word) && trim($word)!='') {
  83. $new_words[]=$word;
  84. }
  85. }
  86. sqspell_writeWords($new_words,$lang);
  87. }
  88. // bump up version number
  89. setPref($data_dir,$username,'sqspell_version','0.5');
  90. }
  91. }
  92. /** Internal functions **/
  93. /**
  94. * This function is the GUI wrapper for the options page. SquirrelSpell
  95. * uses it for creating all Options pages.
  96. *
  97. * @param string $title The title of the page to display
  98. * @param string $scriptsrc This is used to link a file.js into the
  99. * <script src="file.js"></script> format. This
  100. * allows to separate javascript from the rest of the
  101. * plugin and place it into the js/ directory.
  102. * @param string $body The body of the message to display.
  103. * @return void
  104. */
  105. function sqspell_makePage($title, $scriptsrc, $body){
  106. global $color;
  107. if (! sqgetGlobalVar('MOD', $MOD, SQ_GET) ) {
  108. $MOD = 'options_main';
  109. }
  110. displayPageHeader($color);
  111. echo "&nbsp;<br />\n";
  112. /**
  113. * Check if we need to link in a script.
  114. */
  115. if($scriptsrc) {
  116. echo "<script type=\"text/javascript\" src=\"js/$scriptsrc\"></script>\n";
  117. }
  118. echo html_tag( 'table', '', 'center', '', 'width="95%" border="0" cellpadding="2" cellspacing="0"' ) . "\n"
  119. . html_tag( 'tr', "\n" .
  120. html_tag( 'td', '<strong>' . $title .'</strong>', 'center', $color[9] )
  121. ) . "\n"
  122. . html_tag( 'tr', "\n" .
  123. html_tag( 'td', '<hr />', 'left' )
  124. ) . "\n"
  125. . html_tag( 'tr', "\n" .
  126. html_tag( 'td', $body, 'left' )
  127. ) . "\n";
  128. /**
  129. * Generate a nice "Return to Options" link, unless this is the
  130. * starting page.
  131. */
  132. if ($MOD != "options_main"){
  133. echo html_tag( 'tr', "\n" .
  134. html_tag( 'td', '<hr />', 'left' )
  135. ) . "\n"
  136. . html_tag( 'tr', "\n" .
  137. html_tag( 'td', '<a href="sqspell_options.php">'
  138. . _("Back to &quot;SpellChecker Options&quot; page")
  139. . '</a>',
  140. 'center' )
  141. ) . "\n";
  142. }
  143. /**
  144. * Close the table and display the version.
  145. */
  146. echo html_tag( 'tr', "\n" .
  147. html_tag( 'td', '<hr />', 'left' )
  148. ) . "\n"
  149. . html_tag( 'tr',
  150. html_tag( 'td', 'SquirrelSpell ' . squirrelspell_version(), 'center', $color[9] )
  151. ) . "\n</table>\n";
  152. echo '</body></html>';
  153. }
  154. /**
  155. * Function similar to the one above. This one is a general wrapper
  156. * for the Squirrelspell pop-up window. It's called form nearly
  157. * everywhere, except the check_me module, since that one is highly
  158. * customized.
  159. *
  160. * @param string $onload Used to indicate and pass the name of a js function
  161. * to call in a <body onload="function()" for automatic
  162. * onload script execution.
  163. * @param string $title Title of the page.
  164. * @param string $scriptsrc If defined, link this javascript source page into
  165. * the document using <script src="file.js"> format.
  166. * @param string $body The content to include.
  167. * @return void
  168. */
  169. function sqspell_makeWindow($onload, $title, $scriptsrc, $body){
  170. global $color;
  171. displayHtmlHeader($title,
  172. ($scriptsrc ? "\n<script type=\"text/javascript\" src=\"js/$scriptsrc\"></script>\n" : ''));
  173. echo "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" "
  174. . "vlink=\"$color[7]\" alink=\"$color[7]\"";
  175. /**
  176. * Provide an onload="jsfunction()" if asked to.
  177. */
  178. if ($onload) {
  179. echo " onload=\"$onload\"";
  180. }
  181. /**
  182. * Draw the rest of the page.
  183. */
  184. echo ">\n"
  185. . html_tag( 'table', "\n" .
  186. html_tag( 'tr', "\n" .
  187. html_tag( 'td', '<strong>' . $title . '</strong>', 'center', $color[9] )
  188. ) . "\n" .
  189. html_tag( 'tr', "\n" .
  190. html_tag( 'td', '<hr />', 'left' )
  191. ) . "\n" .
  192. html_tag( 'tr', "\n" .
  193. html_tag( 'td', $body, 'left' )
  194. ) . "\n" .
  195. html_tag( 'tr', "\n" .
  196. html_tag( 'td', '<hr />', 'left' )
  197. ) . "\n" .
  198. html_tag( 'tr', "\n" .
  199. html_tag( 'td', 'SquirrelSpell ' . squirrelspell_version(), 'center', $color[9] )
  200. ) ,
  201. '', '', 'width="100%" border="0" cellpadding="2"' );
  202. global $oTemplate;
  203. $oTemplate->display('footer.tpl');
  204. }
  205. /**
  206. * Encryption function used by plugin (old format)
  207. *
  208. * This function does the encryption and decryption of the user
  209. * dictionary. It is only available when PHP is compiled with
  210. * mcrypt support (--with-mcrypt). See doc/CRYPTO for more
  211. * information.
  212. *
  213. * @param $mode A string with either of the two recognized values:
  214. * "encrypt" or "decrypt".
  215. * @param $ckey The key to use for processing (the user's password
  216. * in our case.
  217. * @param $input Content to decrypt or encrypt, according to $mode.
  218. * @return encrypted/decrypted content, or "PANIC" if the
  219. * process bails out.
  220. * @since 1.5.1 (sqspell 0.5)
  221. * @deprecated
  222. */
  223. function sqspell_crypto_old($mode, $ckey, $input){
  224. /**
  225. * Double-check if we have the mcrypt_generic function. Bail out if
  226. * not so.
  227. */
  228. if (!function_exists('mcrypt_generic')) {
  229. return 'PANIC';
  230. }
  231. /**
  232. * Setup mcrypt routines.
  233. */
  234. $td = mcrypt_module_open(MCRYPT_Blowfish, "", MCRYPT_MODE_ECB, "");
  235. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
  236. mcrypt_generic_init($td, $ckey, $iv);
  237. /**
  238. * See what we have to do depending on $mode.
  239. * 'encrypt' -- Encrypt the content.
  240. * 'decrypt' -- Decrypt the content.
  241. */
  242. switch ($mode){
  243. case 'encrypt':
  244. $crypto = mcrypt_generic($td, $input);
  245. break;
  246. case 'decrypt':
  247. $crypto = mdecrypt_generic($td, $input);
  248. /**
  249. * See if it decrypted successfully. If so, it should contain
  250. * the string "# SquirrelSpell". If not, then bail out.
  251. */
  252. if (!strstr($crypto, "# SquirrelSpell")){
  253. $crypto='PANIC';
  254. }
  255. break;
  256. }
  257. /**
  258. * Finish up the mcrypt routines and return the processed content.
  259. */
  260. if (function_exists('mcrypt_generic_deinit')) {
  261. // php 4.1.1+ syntax
  262. mcrypt_generic_deinit ($td);
  263. mcrypt_module_close ($td);
  264. } else {
  265. // older deprecated function
  266. mcrypt_generic_end ($td);
  267. }
  268. return $crypto;
  269. }
  270. /**
  271. * Encryption function used by plugin
  272. *
  273. * This function does the encryption and decryption of the user
  274. * dictionary. It is only available when PHP is compiled with
  275. * mcrypt support (--with-mcrypt). See doc/CRYPTO for more
  276. * information.
  277. *
  278. * @param $mode A string with either of the two recognized values:
  279. * "encrypt" or "decrypt".
  280. * @param $ckey The key to use for processing (the user's password
  281. * in our case.
  282. * @param $input Content to decrypt or encrypt, according to $mode.
  283. * @return encrypted/decrypted content, or "PANIC" if the
  284. * process bails out.
  285. */
  286. function sqspell_crypto($mode, $ckey, $input){
  287. /**
  288. * Double-check if we have the mcrypt_generic function. Bail out if
  289. * not so.
  290. */
  291. if (!function_exists('mcrypt_generic')) {
  292. return 'PANIC';
  293. }
  294. /**
  295. * Setup mcrypt routines.
  296. */
  297. $td = mcrypt_module_open(MCRYPT_Blowfish, "", MCRYPT_MODE_ECB, "");
  298. $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
  299. mcrypt_generic_init($td, $ckey, $iv);
  300. /**
  301. * See what we have to do depending on $mode.
  302. * 'encrypt' -- Encrypt the content.
  303. * 'decrypt' -- Decrypt the content.
  304. */
  305. switch ($mode){
  306. case 'encrypt':
  307. $crypto = mcrypt_generic($td, '{sqspell}'.$input);
  308. break;
  309. case 'decrypt':
  310. $crypto = mdecrypt_generic($td, $input);
  311. if (preg_match("/^\{sqspell\}(.*)/",$crypto,$match)){
  312. $crypto = trim($match[1]);
  313. } else {
  314. $crypto='PANIC';
  315. }
  316. break;
  317. }
  318. /**
  319. * Finish up the mcrypt routines and return the processed content.
  320. */
  321. if (function_exists('mcrypt_generic_deinit')) {
  322. // php 4.1.1+ syntax
  323. mcrypt_generic_deinit ($td);
  324. mcrypt_module_close ($td);
  325. } else {
  326. // older deprecated function
  327. mcrypt_generic_end ($td);
  328. }
  329. return $crypto;
  330. }
  331. /**
  332. * This function transparently upgrades the 0.2 dictionary format to the
  333. * 0.3 format, since user-defined languages have been added in 0.3 and
  334. * the new format keeps user dictionaries selection in the file.
  335. *
  336. * This function will be retired soon, as it's been a while since anyone
  337. * has been using SquirrelSpell-0.2.
  338. *
  339. * @param $words_string Contents of the 0.2-style user dictionary.
  340. * @return Contents of the 0.3-style user dictionary.
  341. * @deprecated
  342. */
  343. function sqspell_upgradeWordsFile($words_string){
  344. global $SQSPELL_APP_DEFAULT, $SQSPELL_VERSION;
  345. /**
  346. * Define just one dictionary for this user -- the default.
  347. * If the user wants more, s/he can set them up in personal
  348. * preferences. See doc/UPGRADING for more info.
  349. */
  350. $new_words_string =
  351. substr_replace($words_string,
  352. "# SquirrelSpell User Dictionary $SQSPELL_VERSION\n# "
  353. . "Last Revision: " . date("Y-m-d")
  354. . "\n# LANG: $SQSPELL_APP_DEFAULT\n# $SQSPELL_APP_DEFAULT",
  355. 0, strpos($words_string, "\n")) . "# End\n";
  356. sqspell_writeWords($new_words_string);
  357. return $new_words_string;
  358. }
  359. /**
  360. * gets list of available dictionaries from user's prefs.
  361. * Function was modified in 1.5.1 (sqspell 0.5).
  362. * Older function is suffixed with '_old'
  363. * @return array list of dictionaries used by end user.
  364. */
  365. function sqspell_getSettings(){
  366. global $data_dir, $username, $SQSPELL_APP_DEFAULT, $SQSPELL_APP;
  367. $ret=array();
  368. $sLangs=getPref($data_dir,$username,'sqspell_langs','');
  369. if ($sLangs=='') {
  370. $ret[0]=$SQSPELL_APP_DEFAULT;
  371. } else {
  372. $aLangs = explode(',',$sLangs);
  373. foreach ($aLangs as $lang) {
  374. if (array_key_exists($lang,$SQSPELL_APP)) {
  375. $ret[]=$lang;
  376. }
  377. }
  378. }
  379. return $ret;
  380. }
  381. /**
  382. * Saves user's language preferences
  383. * @param array $langs languages array (first key is default language)
  384. * @since 1.5.1 (sqspell 0.5)
  385. */
  386. function sqspell_saveSettings($langs) {
  387. global $data_dir, $username;
  388. setPref($data_dir,$username,'sqspell_langs',implode(',',$langs));
  389. }
  390. /**
  391. * Get list of enabled languages.
  392. *
  393. * Right now it just returns an array with the dictionaries
  394. * available to the user for spell-checking. It will probably
  395. * do more in the future, as features are added.
  396. *
  397. * @param string $words The contents of the user's ".words" file.
  398. * @return array a strings array with dictionaries available
  399. * to this user, e.g. {"English", "Spanish"}, etc.
  400. * @since 1.5.1 (sqspell 0.5)
  401. * @deprecated
  402. */
  403. function sqspell_getSettings_old($words){
  404. global $SQSPELL_APP, $SQSPELL_APP_DEFAULT;
  405. /**
  406. * Check if there is more than one dictionary configured in the
  407. * system config.
  408. */
  409. if (sizeof($SQSPELL_APP) > 1){
  410. /**
  411. * Now load the user prefs. Check if $words was empty -- a bit of
  412. * a dirty fall-back. TODO: make it so this is not required.
  413. */
  414. if(!$words){
  415. $words=sqspell_getWords_old();
  416. }
  417. if ($words){
  418. /**
  419. * This user has a ".words" file.
  420. * Find which dictionaries s/he wants to use and load them into
  421. * the $langs array.
  422. */
  423. preg_match("/# LANG: (.*)/i", $words, $matches);
  424. $langs=explode(", ", $matches[1]);
  425. } else {
  426. /**
  427. * User doesn't have a personal dictionary. Grab the default
  428. * system setting.
  429. */
  430. $langs[0]=$SQSPELL_APP_DEFAULT;
  431. }
  432. } else {
  433. /**
  434. * There is no need to read the ".words" file as there is only one
  435. * dictionary defined system-wide.
  436. */
  437. $langs[0]=$SQSPELL_APP_DEFAULT;
  438. }
  439. return $langs;
  440. }
  441. /**
  442. * Get user dictionary for selected language
  443. * Function was modified in 1.5.1 (sqspell 0.5).
  444. * Older function is suffixed with '_old'
  445. * @param string $lang language
  446. * @param array words stored in selected language dictionary
  447. */
  448. function sqspell_getLang($lang) {
  449. global $data_dir, $username,$SQSPELL_CRYPTO;
  450. $sWords=getPref($data_dir,$username,'sqspell_dict_' . $lang,'');
  451. if (preg_match("/^\{crypt\}(.*)/i",$sWords,$match)) {
  452. /**
  453. * Dictionary is encrypted or mangled. Try to decrypt it.
  454. * If fails, complain loudly.
  455. *
  456. * $old_key would be a value submitted by one of the modules with
  457. * the user's old mailbox password. I admin, this is rather dirty,
  458. * but efficient. ;)
  459. */
  460. if (sqgetGlobalVar('old_key', $old_key, SQ_POST)) {
  461. $clear_key=$old_key;
  462. } else {
  463. sqgetGlobalVar('key', $key, SQ_COOKIE);
  464. sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
  465. /**
  466. * Get user's password (the key).
  467. */
  468. $clear_key = OneTimePadDecrypt($key, $onetimepad);
  469. }
  470. /**
  471. * Invoke the decryption routines.
  472. */
  473. $sWords=sqspell_crypto("decrypt", $clear_key, $match[1]);
  474. /**
  475. * See if decryption failed.
  476. */
  477. if ($sWords=="PANIC"){
  478. sqspell_handle_crypt_panic($lang);
  479. // script execution stops here
  480. } else {
  481. /**
  482. * OK! Phew. Set the encryption flag to true so we can later on
  483. * encrypt it again before saving to HDD.
  484. */
  485. $SQSPELL_CRYPTO=true;
  486. }
  487. } else {
  488. /**
  489. * No encryption is/was used. Set $SQSPELL_CRYPTO to false,
  490. * in case we have to save the dictionary later.
  491. */
  492. $SQSPELL_CRYPTO=false;
  493. }
  494. // rebuild word list and remove empty entries
  495. $aWords=array();
  496. foreach (explode(',',$sWords) as $word) {
  497. if (trim($word) !='') {
  498. $aWords[]=trim($word);
  499. }
  500. }
  501. return $aWords;
  502. }
  503. /**
  504. * Get user's dictionary (old format)
  505. *
  506. * This function returns only user-defined dictionary words that correspond
  507. * to the requested language.
  508. *
  509. * @param $words The contents of the user's ".words" file.
  510. * @param $lang Which language words to return, e.g. requesting
  511. * "English" will return ONLY the words from user's
  512. * English dictionary, disregarding any others.
  513. * @return The list of words corresponding to the language
  514. * requested.
  515. * @since 1.5.1 (sqspell 0.5)
  516. * @deprecated
  517. */
  518. function sqspell_getLang_old($words, $lang){
  519. $start=strpos($words, "# $lang\n");
  520. /**
  521. * strpos() will return -1 if no # $lang\n string was found.
  522. * Use this to return a zero-length value and indicate that no
  523. * words are present in the requested dictionary.
  524. */
  525. if (!$start) return '';
  526. /**
  527. * The words list will end with a new directive, which will start
  528. * with "#". Locate the next "#" and thus find out where the
  529. * words end.
  530. */
  531. $end=strpos($words, "#", $start+1);
  532. $lang_words = substr($words, $start, $end-$start);
  533. return $lang_words;
  534. }
  535. /**
  536. * Saves user's dictionary (old format)
  537. *
  538. * This function operates the user dictionary. If the format is
  539. * clear-text, then it just reads the file and returns it. However, if
  540. * the file is encrypted (well, "garbled"), then it tries to decrypt
  541. * it, checks whether the decryption was successful, troubleshoots if
  542. * not, then returns the clear-text dictionary to the app.
  543. *
  544. * @return the contents of the user's ".words" file, decrypted if
  545. * necessary.
  546. * @since 1.5.1 (sqspell 0.5)
  547. * @deprecated
  548. */
  549. function sqspell_getWords_old(){
  550. global $SQSPELL_WORDS_FILE, $SQSPELL_CRYPTO;
  551. $words="";
  552. if (file_exists($SQSPELL_WORDS_FILE)){
  553. /**
  554. * Gobble it up.
  555. */
  556. $fp=fopen($SQSPELL_WORDS_FILE, 'r');
  557. $words=fread($fp, filesize($SQSPELL_WORDS_FILE));
  558. fclose($fp);
  559. }
  560. /**
  561. * Check if this is an encrypted file by looking for
  562. * the string "# SquirrelSpell" in it (the crypto
  563. * function does that).
  564. */
  565. if ($words && !strstr($words, "# SquirrelSpell")){
  566. /**
  567. * This file is encrypted or mangled. Try to decrypt it.
  568. * If fails, complain loudly.
  569. *
  570. * $old_key would be a value submitted by one of the modules with
  571. * the user's old mailbox password. I admin, this is rather dirty,
  572. * but efficient. ;)
  573. */
  574. sqgetGlobalVar('key', $key, SQ_COOKIE);
  575. sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
  576. sqgetGlobalVar('old_key', $old_key, SQ_POST);
  577. if ($old_key != '') {
  578. $clear_key=$old_key;
  579. } else {
  580. /**
  581. * Get user's password (the key).
  582. */
  583. $clear_key = OneTimePadDecrypt($key, $onetimepad);
  584. }
  585. /**
  586. * Invoke the decryption routines.
  587. */
  588. $words=sqspell_crypto_old("decrypt", $clear_key, $words);
  589. /**
  590. * See if decryption failed.
  591. */
  592. if ($words=="PANIC"){
  593. sqspell_handle_crypt_panic();
  594. // script execution stops here.
  595. } else {
  596. /**
  597. * OK! Phew. Set the encryption flag to true so we can later on
  598. * encrypt it again before saving to HDD.
  599. */
  600. $SQSPELL_CRYPTO=true;
  601. }
  602. } else {
  603. /**
  604. * No encryption is/was used. Set $SQSPELL_CRYPTO to false,
  605. * in case we have to save the dictionary later.
  606. */
  607. $SQSPELL_CRYPTO=false;
  608. }
  609. /**
  610. * Check if we need to upgrade the dictionary from version 0.2.x
  611. * This is going away soon.
  612. */
  613. if (strstr($words, "Dictionary v0.2")){
  614. $words=sqspell_upgradeWordsFile($words);
  615. }
  616. return $words;
  617. }
  618. /**
  619. * Saves user's dictionary
  620. * Function was replaced in 1.5.1 (sqspell 0.5).
  621. * Older function is suffixed with '_old'
  622. * @param array $words words that should be stored in dictionary
  623. * @param string $lang language
  624. */
  625. function sqspell_writeWords($words,$lang){
  626. global $SQSPELL_CRYPTO,$username,$data_dir;
  627. $sWords = implode(',',$words);
  628. if ($SQSPELL_CRYPTO){
  629. /**
  630. * User wants to encrypt the file. So be it.
  631. * Get the user's password to use as a key.
  632. */
  633. sqgetGlobalVar('key', $key, SQ_COOKIE);
  634. sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
  635. $clear_key=OneTimePadDecrypt($key, $onetimepad);
  636. /**
  637. * Try encrypting it. If fails, scream bloody hell.
  638. */
  639. $save_words = sqspell_crypto("encrypt", $clear_key, $sWords);
  640. if ($save_words == 'PANIC'){
  641. // FIXME: handle errors here
  642. }
  643. $save_words='{crypt}'.$save_words;
  644. } else {
  645. $save_words=$sWords;
  646. }
  647. setPref($data_dir,$username,'sqspell_dict_'.$lang,$save_words);
  648. }
  649. /**
  650. * Writes user dictionary into the $username.words file, then changes mask
  651. * to 0600. If encryption is needed -- does that, too.
  652. *
  653. * @param $words The contents of the ".words" file to write.
  654. * @return void
  655. * @since 1.5.1 (sqspell 0.5)
  656. * @deprecated
  657. */
  658. function sqspell_writeWords_old($words){
  659. global $SQSPELL_WORDS_FILE, $SQSPELL_CRYPTO;
  660. /**
  661. * if $words is empty, create a template entry by calling the
  662. * sqspell_makeDummy() function.
  663. */
  664. if (!$words){
  665. $words=sqspell_makeDummy();
  666. }
  667. if ($SQSPELL_CRYPTO){
  668. /**
  669. * User wants to encrypt the file. So be it.
  670. * Get the user's password to use as a key.
  671. */
  672. sqgetGlobalVar('key', $key, SQ_COOKIE);
  673. sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
  674. $clear_key=OneTimePadDecrypt($key, $onetimepad);
  675. /**
  676. * Try encrypting it. If fails, scream bloody hell.
  677. */
  678. $save_words = sqspell_crypto("encrypt", $clear_key, $words);
  679. if ($save_words == 'PANIC'){
  680. /**
  681. * AAAAAAAAH! I'm not handling this yet, since obviously
  682. * the admin of the site forgot to compile the MCRYPT support in
  683. * when upgrading an existing PHP installation.
  684. * I will add a handler for this case later, when I can come up
  685. * with some work-around... Right now, do nothing. Let the Admin's
  686. * head hurt.. ;)))
  687. */
  688. /** save some hairs on admin's head and store error message in logs */
  689. error_log('SquirrelSpell: php does not have mcrypt support');
  690. }
  691. } else {
  692. $save_words = $words;
  693. }
  694. /**
  695. * Do the actual writing.
  696. */
  697. $fp=fopen($SQSPELL_WORDS_FILE, "w");
  698. fwrite($fp, $save_words);
  699. fclose($fp);
  700. chmod($SQSPELL_WORDS_FILE, 0600);
  701. }
  702. /**
  703. * Deletes user's dictionary
  704. * Function was modified in 1.5.1 (sqspell 0.5). Older function is suffixed
  705. * with '_old'
  706. * @param string $lang dictionary
  707. */
  708. function sqspell_deleteWords($lang) {
  709. global $data_dir, $username;
  710. removePref($data_dir,$username,'sqspell_dict_'.$lang);
  711. }
  712. /**
  713. * Deletes user's dictionary when it is corrupted.
  714. * @since 1.5.1 (sqspell 0.5)
  715. * @deprecated
  716. */
  717. function sqspell_deleteWords_old(){
  718. /**
  719. * So I open the door to my enemies,
  720. * and I ask can we wipe the slate clean,
  721. * but they tell me to please go...
  722. * uhm... Well, this just erases the user dictionary file.
  723. */
  724. global $SQSPELL_WORDS_FILE;
  725. if (file_exists($SQSPELL_WORDS_FILE)){
  726. unlink($SQSPELL_WORDS_FILE);
  727. }
  728. }
  729. /**
  730. * Creates an empty user dictionary for the sake of saving prefs or
  731. * whatever.
  732. *
  733. * @return The template to use when storing the user dictionary.
  734. * @deprecated
  735. */
  736. function sqspell_makeDummy(){
  737. global $SQSPELL_VERSION, $SQSPELL_APP_DEFAULT;
  738. $words = "# SquirrelSpell User Dictionary $SQSPELL_VERSION\n"
  739. . "# Last Revision: " . date('Y-m-d')
  740. . "\n# LANG: $SQSPELL_APP_DEFAULT\n# End\n";
  741. return $words;
  742. }
  743. /**
  744. * This function checks for security attacks. A $MOD variable is
  745. * provided in the QUERY_STRING and includes one of the files from the
  746. * modules directory ($MOD.mod). See if someone is trying to get out
  747. * of the modules directory by providing dots, unicode strings, or
  748. * slashes.
  749. *
  750. * @param string $rMOD the name of the module requested to include.
  751. * @return void, since it bails out with an access error if needed.
  752. */
  753. function sqspell_ckMOD($rMOD){
  754. if (strstr($rMOD, '.')
  755. || strstr($rMOD, '/')
  756. || strstr($rMOD, '%')
  757. || strstr($rMOD, "\\")){
  758. echo _("Invalid URL");
  759. exit;
  760. }
  761. }
  762. /**
  763. * Used to check internal version of SquirrelSpell dictionary
  764. * @param integer $major main version number
  765. * @param integer $minor second version number
  766. * @return boolean true if stored dictionary version is $major.$minor or newer
  767. * @since 1.5.1 (sqspell 0.5)
  768. */
  769. function sqspell_check_version($major,$minor) {
  770. global $data_dir, $username;
  771. // 0.4 version is internal version number that is used to indicate upgrade from
  772. // separate files to generic SquirrelMail prefs storage.
  773. $sqspell_version=getPref($data_dir,$username,'sqspell_version','0.4');
  774. $aVersion=explode('.',$sqspell_version);
  775. if ($aVersion[0] < $major ||
  776. ( $aVersion[0] == $major && $aVersion[1] < $minor)) {
  777. return false;
  778. }
  779. return true;
  780. }
  781. /**
  782. * Displays form that allows to enter different password for dictionary decryption.
  783. * If language is not set, function provides form to handle older dictionary files.
  784. * @param string $lang language
  785. * @since 1.5.1 (sqspell 0.5)
  786. */
  787. function sqspell_handle_crypt_panic($lang=false) {
  788. if (! sqgetGlobalVar('SCRIPT_NAME',$SCRIPT_NAME,SQ_SERVER))
  789. $SCRIPT_NAME='';
  790. /**
  791. * AAAAAAAAAAAH!!!!! OK, ok, breathe!
  792. * Let's hope the decryption failed because the user changed his
  793. * password. Bring up the option to key in the old password
  794. * or wipe the file and start over if everything else fails.
  795. *
  796. * The _("SquirrelSpell...) line has to be on one line, otherwise
  797. * gettext will bork. ;(
  798. */
  799. $msg = html_tag( 'p', "\n" .
  800. '<strong>' . _("ATTENTION:") . '</strong><br />'
  801. . _("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. 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.") ,
  802. 'left' ) . "\n"
  803. . (($lang) ? html_tag('p',sprintf(_("Your %s dictionary is encrypted with password that differs from your current password."),
  804. htmlspecialchars($lang)),'left') : '')
  805. . '<blockquote>' . "\n"
  806. . '<form method="post" onsubmit="return AYS()">' . "\n"
  807. . '<input type="hidden" name="MOD" value="crypto_badkey" />' . "\n"
  808. . (($lang) ?
  809. '<input type="hidden" name="dict_lang" value="'.htmlspecialchars($lang).'" />' :
  810. '<input type="hidden" name="old_setup" value="yes" />')
  811. . html_tag( 'p', "\n" .
  812. '<input type="checkbox" name="delete_words" value="ON" id="delete_words" />'
  813. . '<label for="delete_words">'
  814. . _("Delete my dictionary and start a new one")
  815. . '</label><br /><label for="old_key">'
  816. . _("Decrypt my dictionary with my old password:")
  817. . '</label><input type="text" name="old_key" id="old_key" size="10" />' ,
  818. 'left' ) . "\n"
  819. . '</blockquote>' . "\n"
  820. . html_tag( 'p', "\n"
  821. . '<input type="submit" value="'
  822. . _("Proceed") . ' &gt;&gt;" />' ,
  823. 'center' ) . "\n"
  824. . '</form>' . "\n";
  825. /**
  826. * Add some string vars so they can be i18n'd.
  827. */
  828. $msg .= "<script type=\"text/javascript\"><!--\n"
  829. . "var ui_choice = \"" . _("You must make a choice") ."\";\n"
  830. . "var ui_candel = \"" . _("You can either delete your dictionary or type in the old password. Not both.") . "\";\n"
  831. . "var ui_willdel = \"" . _("This will delete your personal dictionary file. Proceed?") . "\";\n"
  832. . "//--></script>\n";
  833. /**
  834. * See if this happened in the pop-up window or when accessing
  835. * the SpellChecker options page.
  836. * This is a dirty solution, I agree.
  837. * TODO: make this prettier.
  838. */
  839. if (strstr($SCRIPT_NAME, "sqspell_options")){
  840. sqspell_makePage(_("Error Decrypting Dictionary"),
  841. "decrypt_error.js", $msg);
  842. } else {
  843. sqspell_makeWindow(null, _("Error Decrypting Dictionary"),
  844. "decrypt_error.js", $msg);
  845. }
  846. exit;
  847. }
  848. /**
  849. * SquirrelSpell version. Don't modify, since it identifies the format
  850. * of the user dictionary files and messing with this can do ugly
  851. * stuff. :)
  852. * @global string $SQSPELL_VERSION
  853. * @deprecated
  854. */
  855. $SQSPELL_VERSION="v0.3.8";