Upping the version number and logging the change in ChangeLog
[squirrelmail.git] / plugins / squirrelspell / sqspell_functions.php
CommitLineData
849bdf42 1<?php
849bdf42 2
15e6162e 3/**
4 * sqspell_functions.php -- All SquirrelSpell-wide functions are in this file.
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail development team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * $Id$
10 */
9804bcde 11
12 function sqspell_makePage($title, $scriptsrc, $body){
13 /*
14 ** GUI wrap-around for the OPTIONS page.
15 */
16 global $color, $SQSPELL_VERSION, $MOD;
17 displayPageHeader($color, 'None');
18
19 echo "&nbsp;<br>\n";
20 if($scriptsrc) {
21 echo "<script type=\"text/javascript\" src=\"js/$scriptsrc\"></script>\n";
22 }
23 echo '<table width="95%" align="center" border="0" cellpadding="2" cellspacing="0">'.
24 '<tr>'.
25 "<td bgcolor=\"$color[9]\" align=center>".
26 "<strong>$title</strong>".
27 '</td>'.
28 '</tr>'.
29 '<tr><td><hr></td></tr>'.
30 "<tr><td>$body</td></tr>";
31 if ($MOD!="options_main"){
32 // Generate a nice return-to-main link.
33 echo '<tr><td><hr></td></tr>'.
34 '<tr><td align="center"><a href="sqspell_options.php">' .
35 _("Back to &quot;SpellChecker Options&quot; page") . '</a></td></tr>';
36 }
37 echo '<tr><td><hr></td></tr>'.
38 '<tr>'.
39 "<td bgcolor=\"$color[9]\" align=center>".
40 "SquirrelSpell $SQSPELL_VERSION".
41 '</td>'.
42 '</tr>'.
43 '</table>';
44 }
45
46 function sqspell_makeWindow($onload, $title, $scriptsrc, $body){
47
48 /*
49 ** GUI wrap-around for the pop-up window interface.
50 */
51 global $color, $SQSPELL_VERSION, $theme_css;
52
53 echo "<html>\n".
54 "<head>\n".
55 "<title>$title</title>\n";
56 if ($theme_css != "") {
57 echo "<LINK REL=\"stylesheet\" TYPE=\"text/css\" HREF=\"$theme_css\">\n";
58 }
59 if ($scriptsrc){
60 echo "<script type=\"text/javascript\" src=\"js/$scriptsrc\"></script>\n";
61 }
62 echo "</head>\n".
63 "<body text=\"$color[8]\" bgcolor=\"$color[4]\" link=\"$color[7]\" vlink=\"$color[7]\" alink=\"$color[7]\"";
64 if ($onload) {
65 echo " onload=\"$onload\"";
66 }
67 echo '>'.
68 '<table width="100%" border="0" cellpadding="2">'.
69 '<tr>'.
70 "<td bgcolor=\"$color[9]\" align=center>".
71 "<strong>$title</strong>".
72 '</td>'.
73 '</tr>'.
74 '<tr><td><hr></td></tr>'.
75 '<tr>'.
76 "<td>$body</td>".
77 '</tr>'.
78 '<tr><td><hr></td></tr>'.
79 '<tr>'.
80 "<td bgcolor=\"$color[9]\" align=center>".
81 "SquirrelSpell $SQSPELL_VERSION".
82 '</td>'.
83 '</tr>'.
84 '</table>'.
85 "</body>\n</html>\n";
86 }
87
88 function sqspell_crypto($mode, $ckey, $input){
89 //
90 // This function does the encryption and decryption of the user
91 // dictionary. It is only available when PHP is compiled
92 // --with-mcrypt. See doc/CRYPTO for more information.
93 //
94 if (!function_exists(mcrypt_generic)) {
95 return 'PANIC';
96 }
97 $td = mcrypt_module_open(MCRYPT_Blowfish, "", MCRYPT_MODE_ECB, "");
98 $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size ($td), MCRYPT_RAND);
99 mcrypt_generic_init($td, $ckey, $iv);
100 switch ($mode){
101 case 'encrypt':
102 $crypto = mcrypt_generic($td, $input);
103 break;
104 case 'decrypt':
105 $crypto = mdecrypt_generic($td, $input);
106 // See if it decrypted successfully. If so, it should contain
107 // the string "# SquirrelSpell".
108 if (!strstr($crypto, "# SquirrelSpell"))
109 $crypto='PANIC';
110 break;
111 }
112 mcrypt_generic_end ($td);
113 return $crypto;
114 }
115
116 function sqspell_upgradeWordsFile($words_string){
117 /*
118 ** This function transparently upgrades the 0.2 dictionary format to
119 ** 0.3, since user-defined languages have been added in 0.3 and
120 ** the new format keeps user dictionaries selection in the file.
121 */
122 global $SQSPELL_APP_DEFAULT, $SQSPELL_VERSION;
123
124 /* Define just one dictionary for this user -- the default.
125 ** If the user wants more, s/he can set them up in personal
126 ** preferences. See doc/UPGRADING for more info.
127 */
128 $new_words_string=substr_replace($words_string, "# SquirrelSpell User Dictionary $SQSPELL_VERSION\n# Last Revision: " . date("Y-m-d") . "\n# LANG: $SQSPELL_APP_DEFAULT\n# $SQSPELL_APP_DEFAULT", 0, strpos($words_string, "\n")) . "# End\n";
129 sqspell_writeWords($new_words_string);
130 return $new_words_string;
131 }
132
133 function sqspell_getSettings($words){
134 /*
135 ** Right now it just returns an array with the dictionaries
136 ** available to the user for spell-checking. It will probably
137 ** do more in the future, as features are added.
138 */
139 global $SQSPELL_APP, $SQSPELL_APP_DEFAULT;
140 if (sizeof($SQSPELL_APP) > 1){
141 // OK, so there are more than one dictionary option.
142 // Now load the user prefs.
143 if(!$words)
144 $words=sqspell_getWords();
145 if ($words){
146 // find which dictionaries user wants to use
147 preg_match("/# LANG: (.*)/i", $words, $matches);
148 $langs=explode(", ", $matches[1]);
149 } else {
150 // User doesn't have a personal dictionary. Set him up with
151 // a default setting.
152 $langs[0]=$SQSPELL_APP_DEFAULT;
153 }
154 } else {
155 // There is only one dictionary defined system-wide.
156 $langs[0]=$SQSPELL_APP_DEFAULT;
157 }
158 return $langs;
159 }
160
161 function sqspell_getLang($words, $lang){
162 //
163 // Returns words of a specific user dictionary.
164 //
165 $start=strpos($words, "# $lang\n");
166 if (!$start) return '';
167 $end=strpos($words, "#", $start+1);
168 $lang_words = substr($words, $start, $end-$start);
169 return $lang_words;
170 }
171
172 function sqspell_getWords(){
173 //
174 // This baby operates the user dictionary. If the format is clear-text,
175 // then it just reads the file and returns it. However, if the file is
176 // encrypted, then it decrypts it, checks whether the decryption was
177 // successful, troubleshoots if not, then returns the clear-text dictionary
178 // to the app.
179 //
180 global $SQSPELL_WORDS_FILE, $SQSPELL_CRYPTO;
181 $words="";
182 if (file_exists($SQSPELL_WORDS_FILE)){
183 // Gobble it up.
184 $fp=fopen($SQSPELL_WORDS_FILE, 'r');
185 $words=fread($fp, filesize($SQSPELL_WORDS_FILE));
186 fclose($fp);
187 }
188 // Check if this is an encrypted file by looking for
189 // the string "# SquirrelSpell" in it.
190 if ($words && !strstr($words, "# SquirrelSpell")){
191 // This file is encrypted or mangled. Try to decrypt it.
192 // If fails, raise hell.
193 global $key, $onetimepad, $old_key;
194 if ($old_key) {
195 // an override in case user is trying to decrypt a dictionary
196 // with his old password
197 $clear_key=$old_key;
198 } else {
199 // get user's password (the key).
200 $clear_key = OneTimePadDecrypt($key, $onetimepad);
201 }
202 // decrypt
203 $words=sqspell_crypto("decrypt", $clear_key, $words);
204 if ($words=="PANIC"){
205 // AAAAAAAAAAAH!!!!! OK, ok, breathe!
206 // Let's hope the decryption failed because the user changed his
207 // password. Bring up the option to key in the old password
208 // or wipe the file and start over if everything else fails.
209 $msg='<p>'.
210 '<strong>' . _("ATTENTION:") . '</strong><br>' .
211 _("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.").
2b5a7157 212 '</p>' .
213 '<blockquote>'.
214 '<form method="post" onsubmit="return AYS()">'.
215 '<input type="hidden" name="MOD" value="crypto_badkey">'.
216 '<p><input type="checkbox" name="delete_words" value="ON">' .
217 _("Delete my dictionary and start a new one") . '<br>'.
218 _("Decrypt my dictionary with my old password:") .
219 '<input name="old_key" size=\"10\"></p>'.
220 '</blockquote>'.
221 '<p align="center"><input type="submit" value="' . _("Proceed") . ' &gt;&gt;"></p>'.
222 '</form>';
9804bcde 223 // See if this happened in the pop-up window or when accessing
224 // the SpellChecker options page.
225 global $SCRIPT_NAME;
226 if (strstr($SCRIPT_NAME, "sqspell_options"))
227 sqspell_makePage( _("Error Decrypting Dictionary"), "decrypt_error.js", $msg);
228 else
229 sqspell_makeWindow(null, _("Error Decrypting Dictionary"), "decrypt_error.js", $msg);
230 exit;
231 } else {
232 // OK! Phew. Set the encryption flag to true so we can later on
233 // encrypt it again before saving to HDD.
234 $SQSPELL_CRYPTO=true;
235 }
236 } else {
237 // No encryption is used. Set $SQSPELL_CRYPTO to false, in case we have to
238 // save the dictionary later.
239 $SQSPELL_CRYPTO=false;
240 }
241 // Check if we need to upgrade the dictionary from version 0.2.x
242 if (strstr($words, "Dictionary v0.2")) $words=sqspell_upgradeWordsFile($words);
243 return $words;
244 }
245
246 function sqspell_writeWords($words){
247 //
248 // Writes user dictionary into the $username.words file, then changes mask
249 // to 0600. If encryption is needed -- does that, too.
250 //
251 global $SQSPELL_WORDS_FILE, $SQSPELL_CRYPTO;
252 // if $words is empty, create a template entry.
253 if (!$words) $words=sqspell_makeDummy();
254 if ($SQSPELL_CRYPTO){
255 // User wants to encrypt the file. So be it.
256 // get his password to use as a key.
257 global $key, $onetimepad;
258 $clear_key=OneTimePadDecrypt($key, $onetimepad);
259 // Try encrypting it. If fails, scream bloody hell.
260 $save_words = sqspell_crypto("encrypt", $clear_key, $words);
261 if ($save_words == 'PANIC'){
262 /*
263 ** AAAAAAAAH! I'm not handling this yet, since obviously
264 ** the admin of the site forgot to compile the MCRYPT support in.
265 ** I will add a handler for this case later, when I can come up
266 ** with some work-around... Right now, do nothing. Let the Admin's
267 ** head hurt.. ;)))
268 */
269 }
270 } else {
271 $save_words = $words;
272 }
273 $fp=fopen($SQSPELL_WORDS_FILE, "w");
274 fwrite($fp, $save_words);
275 fclose($fp);
276 chmod($SQSPELL_WORDS_FILE, 0600);
277 }
278
279 function sqspell_deleteWords(){
280 /*
281 ** so I open the door to my enemies,
282 ** and I ask can we wipe the slate clean,
283 ** but they tell me to please go...
284 ** uhm... Well, this just erases the user dictionary file.
285 */
286 global $SQSPELL_WORDS_FILE;
287 if (file_exists($SQSPELL_WORDS_FILE)) unlink($SQSPELL_WORDS_FILE);
288 }
289
290 function sqspell_makeDummy(){
291 //
292 // Creates an empty user dictionary for the sake of saving prefs or
293 // whatever.
294 //
295 global $SQSPELL_VERSION, $SQSPELL_APP_DEFAULT;
296 $words="# SquirrelSpell User Dictionary $SQSPELL_VERSION\n# Last Revision: " . date('Y-m-d') . "\n# LANG: $SQSPELL_APP_DEFAULT\n# End\n";
297 return $words;
298 }
299
300 /**
301 VERSION:
302 ---------
303 SquirrelSpell version. Don't modify, since it identifies the format
304 of the user dictionary files and messing with this can do ugly
305 stuff. :)
306 **/
9b00b026 307 $SQSPELL_VERSION="v0.3.6";
9804bcde 308
15e6162e 309?>