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