using internal SM functions to create page header.
[squirrelmail.git] / plugins / squirrelspell / sqspell_functions.php
1 <?php
2 /**
3 * sqspell_functions.php
4 * ----------------------
5 * All SquirrelSpell-wide functions are in this file.
6 *
7 * Copyright (c) 1999-2004 The SquirrelMail development team
8 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 *
10 * $Id$
11 *
12 * @author Konstantin Riabitsev <icon@duke.edu> ($Author$)
13 * @version $Date$
14 * @package plugins
15 * @subpackage squirrelspell
16 */
17
18 /**
19 * This function is the GUI wrapper for the options page. SquirrelSpell
20 * uses it for creating all Options pages.
21 *
22 * @param $title The title of the page to display
23 * @param $scriptsrc This is used to link a file.js into the
24 * <script src="file.js"></script> format. This
25 * allows to separate javascript from the rest of the
26 * plugin and place it into the js/ directory.
27 * @param $body The body of the message to display.
28 * @return void
29 */
30 function sqspell_makePage($title, $scriptsrc, $body){
31 global $color, $SQSPELL_VERSION;
32
33 if (! sqgetGlobalVar('MOD', $MOD, SQ_GET) ) {
34 $MOD = 'options_main';
35 }
36
37 displayPageHeader($color, 'None');
38 echo "&nbsp;<br />\n";
39 /**
40 * Check if we need to link in a script.
41 */
42 if($scriptsrc) {
43 echo "<script type=\"text/javascript\" src=\"js/$scriptsrc\"></script>\n";
44 }
45 echo html_tag( 'table', '', 'center', '', 'width="95%" border="0" cellpadding="2" cellspacing="0"' ) . "\n"
46 . html_tag( 'tr', "\n" .
47 html_tag( 'td', '<strong>' . $title .'</strong>', 'center', $color[9] )
48 ) . "\n"
49 . html_tag( 'tr', "\n" .
50 html_tag( 'td', '<hr />', 'left' )
51 ) . "\n"
52 . html_tag( 'tr', "\n" .
53 html_tag( 'td', $body, 'left' )
54 ) . "\n";
55 /**
56 * Generate a nice "Return to Options" link, unless this is the
57 * starting page.
58 */
59 if ($MOD != "options_main"){
60 echo html_tag( 'tr', "\n" .
61 html_tag( 'td', '<hr />', 'left' )
62 ) . "\n"
63 . html_tag( 'tr', "\n" .
64 html_tag( 'td', '<a href="sqspell_options.php">'
65 . _("Back to &quot;SpellChecker Options&quot; page")
66 . '</a>',
67 'center' )
68 ) . "\n";
69 }
70 /**
71 * Close the table and display the version.
72 */
73 echo html_tag( 'tr', "\n" .
74 html_tag( 'td', '<hr />', 'left' )
75 ) . "\n"
76 . html_tag( 'tr',
77 html_tag( 'td', 'SquirrelSpell ' . $SQSPELL_VERSION, 'center', $color[9] )
78 ) . "\n</table>\n";
79 echo '</body></html>';
80 }
81
82 /**
83 * Function similar to the one above. This one is a general wrapper
84 * for the Squirrelspell pop-up window. It's called form nearly
85 * everywhere, except the check_me module, since that one is highly
86 * customized.
87 *
88 * @param $onload Used to indicate and pass the name of a js function
89 * to call in a <body onload="function()" for automatic
90 * onload script execution.
91 * @param $title Title of the page.
92 * @param $scriptsrc If defined, link this javascript source page into
93 * the document using <script src="file.js"> format.
94 * @param $body The content to include.
95 * @return void
96 */
97 function sqspell_makeWindow($onload, $title, $scriptsrc, $body){
98 global $color, $SQSPELL_VERSION;
99
100 displayHtmlHeader($title,
101 ($scriptsrc ? "\n<script type=\"text/javascript\" src=\"js/$scriptsrc\"></script>\n" : ''));
102
103 echo "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" "
104 . "vlink=\"$color[7]\" alink=\"$color[7]\"";
105 /**
106 * Provide an onload="jsfunction()" if asked to.
107 */
108 if ($onload) {
109 echo " onload=\"$onload\"";
110 }
111 /**
112 * Draw the rest of the page.
113 */
114 echo ">\n"
115 . html_tag( 'table', "\n" .
116 html_tag( 'tr', "\n" .
117 html_tag( 'td', '<strong>' . $title . '</strong>', 'center', $color[9] )
118 ) . "\n" .
119 html_tag( 'tr', "\n" .
120 html_tag( 'td', '<hr />', 'left' )
121 ) . "\n" .
122 html_tag( 'tr', "\n" .
123 html_tag( 'td', $body, 'left' )
124 ) . "\n" .
125 html_tag( 'tr', "\n" .
126 html_tag( 'td', '<hr />', 'left' )
127 ) . "\n" .
128 html_tag( 'tr', "\n" .
129 html_tag( 'td', 'SquirrelSpell ' . $SQSPELL_VERSION, 'center', $color[9] )
130 ) ,
131 '', '', 'width="100%" border="0" cellpadding="2"' )
132 . "</body>\n</html>\n";
133 }
134
135 /**
136 * This function does the encryption and decryption of the user
137 * dictionary. It is only available when PHP is compiled with
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.
148 */
149 function sqspell_crypto($mode, $ckey, $input){
150 /**
151 * Double-check if we have the mcrypt_generic function. Bail out if
152 * not so.
153 */
154 if (!function_exists(mcrypt_generic)) {
155 return 'PANIC';
156 }
157 /**
158 * Setup mcrypt routines.
159 */
160 $td = mcrypt_module_open(MCRYPT_Blowfish, "", MCRYPT_MODE_ECB, "");
161 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
162 mcrypt_generic_init($td, $ckey, $iv);
163 /**
164 * See what we have to do depending on $mode.
165 * 'encrypt' -- Encrypt the content.
166 * 'decrypt' -- Decrypt the content.
167 */
168 switch ($mode){
169 case 'encrypt':
170 $crypto = mcrypt_generic($td, $input);
171 break;
172 case 'decrypt':
173 $crypto = mdecrypt_generic($td, $input);
174 /**
175 * See if it decrypted successfully. If so, it should contain
176 * the string "# SquirrelSpell". If not, then bail out.
177 */
178 if (!strstr($crypto, "# SquirrelSpell")){
179 $crypto='PANIC';
180 }
181 break;
182 }
183 /**
184 * Finish up the mcrypt routines and return the processed content.
185 */
186 mcrypt_generic_end ($td);
187 return $crypto;
188 }
189
190 /**
191 * This function transparently upgrades the 0.2 dictionary format to the
192 * 0.3 format, since user-defined languages have been added in 0.3 and
193 * the new format keeps user dictionaries selection in the file.
194 *
195 * This function will be retired soon, as it's been a while since anyone
196 * has been using SquirrelSpell-0.2.
197 *
198 * @param $words_string Contents of the 0.2-style user dictionary.
199 * @return Contents of the 0.3-style user dictionary.
200 */
201 function sqspell_upgradeWordsFile($words_string){
202 global $SQSPELL_APP_DEFAULT, $SQSPELL_VERSION;
203 /**
204 * Define just one dictionary for this user -- the default.
205 * If the user wants more, s/he can set them up in personal
206 * preferences. See doc/UPGRADING for more info.
207 */
208 $new_words_string =
209 substr_replace($words_string,
210 "# SquirrelSpell User Dictionary $SQSPELL_VERSION\n# "
211 . "Last Revision: " . date("Y-m-d")
212 . "\n# LANG: $SQSPELL_APP_DEFAULT\n# $SQSPELL_APP_DEFAULT",
213 0, strpos($words_string, "\n")) . "# End\n";
214 sqspell_writeWords($new_words_string);
215 return $new_words_string;
216 }
217
218 /**
219 * Right now it just returns an array with the dictionaries
220 * available to the user for spell-checking. It will probably
221 * do more in the future, as features are added.
222 *
223 * @param $words The contents of the user's ".words" file.
224 * @return a strings array with dictionaries available
225 * to this user, e.g. {"English", "Spanish"}, etc.
226 */
227 function sqspell_getSettings($words){
228 global $SQSPELL_APP, $SQSPELL_APP_DEFAULT;
229 /**
230 * Check if there is more than one dictionary configured in the
231 * system config.
232 */
233 if (sizeof($SQSPELL_APP) > 1){
234 /**
235 * Now load the user prefs. Check if $words was empty -- a bit of
236 * a dirty fall-back. TODO: make it so this is not required.
237 */
238 if(!$words){
239 $words=sqspell_getWords();
240 }
241 if ($words){
242 /**
243 * This user has a ".words" file.
244 * Find which dictionaries s/he wants to use and load them into
245 * the $langs array.
246 */
247 preg_match("/# LANG: (.*)/i", $words, $matches);
248 $langs=explode(", ", $matches[1]);
249 } else {
250 /**
251 * User doesn't have a personal dictionary. Grab the default
252 * system setting.
253 */
254 $langs[0]=$SQSPELL_APP_DEFAULT;
255 }
256 } else {
257 /**
258 * There is no need to read the ".words" file as there is only one
259 * dictionary defined system-wide.
260 */
261 $langs[0]=$SQSPELL_APP_DEFAULT;
262 }
263 return $langs;
264 }
265
266 /**
267 * This function returns only user-defined dictionary words that correspond
268 * to the requested language.
269 *
270 * @param $words The contents of the user's ".words" file.
271 * @param $lang Which language words to return, e.g. requesting
272 * "English" will return ONLY the words from user's
273 * English dictionary, disregarding any others.
274 * @return The list of words corresponding to the language
275 * requested.
276 */
277 function sqspell_getLang($words, $lang){
278 $start=strpos($words, "# $lang\n");
279 /**
280 * strpos() will return -1 if no # $lang\n string was found.
281 * Use this to return a zero-length value and indicate that no
282 * words are present in the requested dictionary.
283 */
284 if (!$start) return '';
285 /**
286 * The words list will end with a new directive, which will start
287 * with "#". Locate the next "#" and thus find out where the
288 * words end.
289 */
290 $end=strpos($words, "#", $start+1);
291 $lang_words = substr($words, $start, $end-$start);
292 return $lang_words;
293 }
294
295 /**
296 * This function operates the user dictionary. If the format is
297 * clear-text, then it just reads the file and returns it. However, if
298 * the file is encrypted (well, "garbled"), then it tries to decrypt
299 * it, checks whether the decryption was successful, troubleshoots if
300 * not, then returns the clear-text dictionary to the app.
301 *
302 * @return the contents of the user's ".words" file, decrypted if
303 * necessary.
304 */
305 function sqspell_getWords(){
306 global $SQSPELL_WORDS_FILE, $SQSPELL_CRYPTO;
307 $words="";
308 if (file_exists($SQSPELL_WORDS_FILE)){
309 /**
310 * Gobble it up.
311 */
312 $fp=fopen($SQSPELL_WORDS_FILE, 'r');
313 $words=fread($fp, filesize($SQSPELL_WORDS_FILE));
314 fclose($fp);
315 }
316 /**
317 * Check if this is an encrypted file by looking for
318 * the string "# SquirrelSpell" in it (the crypto
319 * function does that).
320 */
321 if ($words && !strstr($words, "# SquirrelSpell")){
322 /**
323 * This file is encrypted or mangled. Try to decrypt it.
324 * If fails, complain loudly.
325 *
326 * $old_key would be a value submitted by one of the modules with
327 * the user's old mailbox password. I admin, this is rather dirty,
328 * but efficient. ;)
329 */
330 sqgetGlobalVar('key', $key, SQ_COOKIE);
331 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
332
333 sqgetGlobalVar('old_key', $old_key, SQ_POST);
334
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);
342 }
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 */
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. 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";
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";
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")){
394 sqspell_makePage(_("Error Decrypting Dictionary"),
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;
407 }
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 */
432 function 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 sqgetGlobalVar('key', $key, SQ_COOKIE);
447 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
448
449 $clear_key=OneTimePadDecrypt($key, $onetimepad);
450 /**
451 * Try encrypting it. If fails, scream bloody hell.
452 */
453 $save_words = sqspell_crypto("encrypt", $clear_key, $words);
454 if ($save_words == 'PANIC'){
455 /**
456 * AAAAAAAAH! I'm not handling this yet, since obviously
457 * the admin of the site forgot to compile the MCRYPT support in
458 * when upgrading an existing PHP installation.
459 * I will add a handler for this case later, when I can come up
460 * with some work-around... Right now, do nothing. Let the Admin's
461 * head hurt.. ;)))
462 */
463 }
464 } else {
465 $save_words = $words;
466 }
467 /**
468 * Do the actual writing.
469 */
470 $fp=fopen($SQSPELL_WORDS_FILE, "w");
471 fwrite($fp, $save_words);
472 fclose($fp);
473 chmod($SQSPELL_WORDS_FILE, 0600);
474 }
475
476 function sqspell_deleteWords(){
477 /**
478 * So I open the door to my enemies,
479 * and I ask can we wipe the slate clean,
480 * but they tell me to please go...
481 * uhm... Well, this just erases the user dictionary file.
482 */
483 global $SQSPELL_WORDS_FILE;
484 if (file_exists($SQSPELL_WORDS_FILE)){
485 unlink($SQSPELL_WORDS_FILE);
486 }
487 }
488 /**
489 * Creates an empty user dictionary for the sake of saving prefs or
490 * whatever.
491 *
492 * @return The template to use when storing the user dictionary.
493 */
494 function sqspell_makeDummy(){
495 global $SQSPELL_VERSION, $SQSPELL_APP_DEFAULT;
496 $words = "# SquirrelSpell User Dictionary $SQSPELL_VERSION\n"
497 . "# Last Revision: " . date('Y-m-d')
498 . "\n# LANG: $SQSPELL_APP_DEFAULT\n# End\n";
499 return $words;
500 }
501
502 /**
503 * This function checks for security attacks. A $MOD variable is
504 * provided in the QUERY_STRING and includes one of the files from the
505 * modules directory ($MOD.mod). See if someone is trying to get out
506 * of the modules directory by providing dots, unicode strings, or
507 * slashes.
508 *
509 * @param $rMOD the name of the module requested to include.
510 * @return void, since it bails out with an access error if needed.
511 */
512 function sqspell_ckMOD($rMOD){
513 if (strstr($rMOD, '.')
514 || strstr($rMOD, '/')
515 || strstr($rMOD, '%')
516 || strstr($rMOD, "\\")){
517 echo _("Cute.");
518 exit;
519 }
520 }
521
522 /**
523 * SquirrelSpell version. Don't modify, since it identifies the format
524 * of the user dictionary files and messing with this can do ugly
525 * stuff. :)
526 */
527 $SQSPELL_VERSION="v0.3.8";
528 ?>