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