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