bug 490451, image number 2 contains a lone = at the end of a line
[squirrelmail.git] / functions / prefs.php
CommitLineData
59177427 1<?php
2ba13803 2
35586184 3/**
4 * prefs.php
5 *
6 * Copyright (c) 1999-2001 The SquirrelMail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains functions for manipulating user preferences
10 *
11 * $Id$
12 */
13
35586184 14global $prefs_are_cached, $prefs_cache;
15if (!session_is_registered('prefs_are_cached')) {
16 $prefs_are_cached = false;
17 $prefs_cache = array();
18}
31ac7db8 19
7b294953 20/**
21 * Check the preferences into the session cache.
22 */
23function cachePrefValues($data_dir, $username) {
24 global $prefs_are_cached, $prefs_cache;
31ac7db8 25
7b294953 26 if ($prefs_are_cached) {
27 return;
28 }
29
3392dc86 30 $filename = getHashedFile($username, $data_dir, "$username.pref");
7b294953 31
32 if (!file_exists($filename)) {
33 printf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename);
34 exit;
35 }
36
37 $file = fopen($filename, 'r');
38
39 /* Read in the preferences. */
40 $highlight_num = 0;
41 while (! feof($file)) {
42 $pref = trim(fgets($file, 1024));
43 $equalsAt = strpos($pref, '=');
44 if ($equalsAt > 0) {
45 $key = substr($pref, 0, $equalsAt);
46 $value = substr($pref, $equalsAt + 1);
47 if (substr($key, 0, 9) == 'highlight') {
48 $key = 'highlight' . $highlight_num;
49 $highlight_num ++;
50 }
51
52 if ($value != '') {
53 $prefs_cache[$key] = $value;
54 }
55 }
56 }
57 fclose($file);
58
59 session_unregister('prefs_cache');
60 session_register('prefs_cache');
31ac7db8 61
7b294953 62 $prefs_are_cached = true;
63 session_unregister('prefs_are_cached');
64 session_register('prefs_are_cached');
65}
31ac7db8 66
7b294953 67/**
68 * Return the value for the prefernce given by $string.
69 */
70function getPref($data_dir, $username, $string, $default = '') {
71 global $prefs_cache;
72 $result = '';
73
74 cachePrefValues($data_dir, $username);
75
76 if (isset($prefs_cache[$string])) {
77 $result = $prefs_cache[$string];
78 } else {
79 $result = $default;
80 }
81
82 return ($result);
83}
84
85/**
86 * Save the preferences for this user.
87 */
88function savePrefValues($data_dir, $username) {
89 global $prefs_cache;
31ac7db8 90
3392dc86 91 $filename = getHashedFile($username, $data_dir, "$username.pref");
92
93 $file = fopen($filename, 'w');
7b294953 94 foreach ($prefs_cache as $Key => $Value) {
95 if (isset($Value)) {
96 fwrite($file, $Key . '=' . $Value . "\n");
97 }
98 }
99 fclose($file);
100}
101
102/**
103 * Remove a preference for the current user.
104 */
105function removePref($data_dir, $username, $string) {
106 global $prefs_cache;
107
108 cachePrefValues($data_dir, $username);
109
110 if (isset($prefs_cache[$string])) {
111 unset($prefs_cache[$string]);
112 }
113
114 savePrefValues($data_dir, $username);
115}
f804972b 116
7b294953 117/**
118 * Set a there preference $string to $value.
119 */
120function setPref($data_dir, $username, $string, $value) {
121 global $prefs_cache;
f804972b 122
7b294953 123 cachePrefValues($data_dir, $username);
124 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
125 return;
126 }
f804972b 127
7b294953 128 if ($value === '') {
129 removePref($data_dir, $username, $string);
130 return;
131 }
f804972b 132
7b294953 133 $prefs_cache[$string] = $value;
134 savePrefValues($data_dir, $username);
135}
f804972b 136
7b294953 137/**
138 * Check for a preferences file. If one can not be found, create it.
139 */
140function checkForPrefs($data_dir, $username) {
3392dc86 141 $filename = getHashedFile($username, $data_dir, "$username.pref");
7b294953 142 if (!file_exists($filename) ) {
143 if (!copy($data_dir . 'default_pref', $filename)) {
144 echo _("Error opening ") . $filename;
145 exit;
146 }
147 }
148}
149
150/**
151 * Write the User Signature.
152 */
153function setSig($data_dir, $username, $value) {
3392dc86 154 $filename = getHashedFile($username, $data_dir, "$username.sig");
155 $file = fopen($filename, 'w');
7b294953 156 fwrite($file, $value);
157 fclose($file);
158}
159
160/**
161 * Get the signature.
162 */
163function getSig($data_dir, $username) {
3392dc86 164 #$filename = $data_dir . $username . '.sig';
165 $filename = getHashedFile($username, $data_dir, "$username.sig");
7b294953 166 $sig = '';
167 if (file_exists($filename)) {
168 $file = fopen($filename, 'r');
169 while (!feof($file)) {
f804972b 170 $sig .= fgets($file, 1024);
7b294953 171 }
172 fclose($file);
173 }
174 return $sig;
175}
fb120846 176
3392dc86 177function getHashedFile($username, $dir, $datafile, $hash_search = true) {
178 global $dir_hash_level;
179
180 /* Compute the hash for this user and extract the hash directories. */
181 $hash_dirs = computeHashDirs($username);
182
183 /* First, get and make sure the full hash directory exists. */
184 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
185
186 /* Set the value of our real data file. */
187 $result = "$real_hash_dir/$datafile";
188
189 /* Check for this file in the real hash directory. */
190 if ($hash_search && !file_exists($result)) {
191 /* First check the base directory, the most common location. */
192 if (file_exists("$dir/$datafile")) {
193 rename("$dir/$datafile", $result);
194
195 /* Then check the full range of possible hash directories. */
196 } else {
197 $check_hash_dir = $dir;
198 for ($h = 0; $h < 4; ++$h) {
199 $check_hash_dir .= '/' . $hash_dirs[$h];
200 if (is_readable("$check_hash_dir/$datafile")) {
201 rename("$check_hash_dir/$datafile", $result);
202 break;
203 }
204 }
205 }
206 }
207
208 /* Return the full hashed datafile path. */
209 return ($result);
210}
211
212function getHashedDir($username, $dir, $hash_dirs = '') {
213 global $dir_hash_level;
214
215 /* If necessary, populate the hash dir variable. */
216 if ($hash_dirs == '') {
217 $hash_dirs = computeHashDirs($username);
218 }
219
220 /* Make sure the full hash directory exists. */
221 $real_hash_dir = $dir;
222 for ($h = 0; $h < $dir_hash_level; ++$h) {
223 $real_hash_dir .= '/' . $hash_dirs[$h];
224 if (!is_dir($real_hash_dir)) {
225 mkdir($real_hash_dir, 0770);
226 }
227 }
228
229 /* And return that directory. */
230 return ($real_hash_dir);
231}
232
233function computeHashDirs($username) {
234 /* Compute the hash for this user and extract the hash directories. */
235 $hash = base_convert(crc32($username), 10, 16);
236 $hash_dirs = array();
237 for ($h = 0; $h < 4; ++ $h) {
238 $hash_dirs[] = substr($hash, $h, 1);
239 }
240
241 /* Return our array of hash directories. */
242 return ($hash_dirs);
243}
244
35586184 245?>