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 |
14 | global $prefs_are_cached, $prefs_cache; |
15 | if (!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 | */ |
23 | function cachePrefValues($data_dir, $username) { |
24 | global $prefs_are_cached, $prefs_cache; |
31ac7db8 |
25 | |
7b294953 |
26 | if ($prefs_are_cached) { |
27 | return; |
28 | } |
29 | |
30 | $filename = $data_dir . $username . '.pref'; |
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 | */ |
70 | function 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 | */ |
88 | function savePrefValues($data_dir, $username) { |
89 | global $prefs_cache; |
31ac7db8 |
90 | |
7b294953 |
91 | $file = fopen($data_dir . $username . '.pref', 'w'); |
92 | foreach ($prefs_cache as $Key => $Value) { |
93 | if (isset($Value)) { |
94 | fwrite($file, $Key . '=' . $Value . "\n"); |
95 | } |
96 | } |
97 | fclose($file); |
98 | } |
99 | |
100 | /** |
101 | * Remove a preference for the current user. |
102 | */ |
103 | function removePref($data_dir, $username, $string) { |
104 | global $prefs_cache; |
105 | |
106 | cachePrefValues($data_dir, $username); |
107 | |
108 | if (isset($prefs_cache[$string])) { |
109 | unset($prefs_cache[$string]); |
110 | } |
111 | |
112 | savePrefValues($data_dir, $username); |
113 | } |
f804972b |
114 | |
7b294953 |
115 | /** |
116 | * Set a there preference $string to $value. |
117 | */ |
118 | function setPref($data_dir, $username, $string, $value) { |
119 | global $prefs_cache; |
f804972b |
120 | |
7b294953 |
121 | cachePrefValues($data_dir, $username); |
122 | if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) { |
123 | return; |
124 | } |
f804972b |
125 | |
7b294953 |
126 | if ($value === '') { |
127 | removePref($data_dir, $username, $string); |
128 | return; |
129 | } |
f804972b |
130 | |
7b294953 |
131 | $prefs_cache[$string] = $value; |
132 | savePrefValues($data_dir, $username); |
133 | } |
f804972b |
134 | |
7b294953 |
135 | /** |
136 | * Check for a preferences file. If one can not be found, create it. |
137 | */ |
138 | function checkForPrefs($data_dir, $username) { |
139 | $filename = $data_dir . $username . '.pref'; |
140 | if (!file_exists($filename) ) { |
141 | if (!copy($data_dir . 'default_pref', $filename)) { |
142 | echo _("Error opening ") . $filename; |
143 | exit; |
144 | } |
145 | } |
146 | } |
147 | |
148 | /** |
149 | * Write the User Signature. |
150 | */ |
151 | function setSig($data_dir, $username, $value) { |
152 | $file = fopen($data_dir . $username . '.sig', 'w'); |
153 | fwrite($file, $value); |
154 | fclose($file); |
155 | } |
156 | |
157 | /** |
158 | * Get the signature. |
159 | */ |
160 | function getSig($data_dir, $username) { |
161 | $filename = $data_dir . $username . '.sig'; |
162 | $sig = ''; |
163 | if (file_exists($filename)) { |
164 | $file = fopen($filename, 'r'); |
165 | while (!feof($file)) { |
f804972b |
166 | $sig .= fgets($file, 1024); |
7b294953 |
167 | } |
168 | fclose($file); |
169 | } |
170 | return $sig; |
171 | } |
fb120846 |
172 | |
35586184 |
173 | ?> |