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 | |
14 | /*****************************************************************/ |
15 | /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/ |
16 | /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/ |
17 | /*** + Base level indent should begin at left margin, as ***/ |
18 | /*** the $prefs_are_cached and $prefs_cache stuff below. ***/ |
19 | /*** + All identation should consist of four space blocks ***/ |
20 | /*** + Tab characters are evil. ***/ |
21 | /*** + all comments should use "slash-star ... star-slash" ***/ |
22 | /*** style -- no pound characters, no slash-slash style ***/ |
23 | /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/ |
24 | /*** ALWAYS USE { AND } CHARACTERS!!! ***/ |
25 | /*** + Please use ' instead of ", when possible. Note " ***/ |
26 | /*** should always be used in _( ) function calls. ***/ |
27 | /*** Thank you for your help making the SM code more readable. ***/ |
28 | /*****************************************************************/ |
29 | |
30 | global $prefs_are_cached, $prefs_cache; |
31 | if (!session_is_registered('prefs_are_cached')) { |
32 | $prefs_are_cached = false; |
33 | $prefs_cache = array(); |
34 | } |
31ac7db8 |
35 | |
374b7e37 |
36 | function cachePrefValues($data_dir, $username) { |
31ac7db8 |
37 | global $prefs_are_cached, $prefs_cache; |
38 | |
39 | if ($prefs_are_cached) |
40 | return; |
41 | |
42 | $filename = $data_dir . $username . '.pref'; |
43 | |
44 | if (!file_exists($filename)) { |
374b7e37 |
45 | printf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename); |
31ac7db8 |
46 | exit; |
47 | } |
48 | |
63123e7c |
49 | $file = fopen($filename, 'r'); |
31ac7db8 |
50 | |
51 | /** read in all the preferences **/ |
52 | $highlight_num = 0; |
53 | while (! feof($file)) { |
54 | $pref = trim(fgets($file, 1024)); |
374b7e37 |
55 | $equalsAt = strpos($pref, '='); |
56 | if ($equalsAt > 0) { |
57 | $Key = substr($pref, 0, $equalsAt); |
58 | $Value = substr($pref, $equalsAt + 1); |
59 | if (substr($Key, 0, 9) == 'highlight') { |
60 | $Key = 'highlight' . $highlight_num; |
61 | $highlight_num ++; |
31ac7db8 |
62 | } |
63 | |
374b7e37 |
64 | if ($Value != '') { |
65 | $prefs_cache[$Key] = $Value; |
66 | } |
67 | } |
31ac7db8 |
68 | } |
69 | fclose($file); |
374b7e37 |
70 | |
71 | session_unregister('prefs_cache'); |
72 | session_register('prefs_cache'); |
31ac7db8 |
73 | |
74 | $prefs_are_cached = true; |
374b7e37 |
75 | session_unregister('prefs_are_cached'); |
76 | session_register('prefs_are_cached'); |
31ac7db8 |
77 | } |
78 | |
79 | |
65b14f90 |
80 | /** returns the value for $string **/ |
f1e6f580 |
81 | function getPref($data_dir, $username, $string, $default = '') { |
31ac7db8 |
82 | global $prefs_cache; |
f1e6f580 |
83 | |
31ac7db8 |
84 | cachePrefValues($data_dir, $username); |
f1e6f580 |
85 | |
31ac7db8 |
86 | if (isset($prefs_cache[$string])) |
87 | return $prefs_cache[$string]; |
f1e6f580 |
88 | else |
71257f8b |
89 | return $default; |
65b14f90 |
90 | } |
91 | |
44f642f5 |
92 | |
374b7e37 |
93 | function savePrefValues($data_dir, $username) { |
31ac7db8 |
94 | global $prefs_cache; |
95 | |
63123e7c |
96 | $file = fopen($data_dir . $username . '.pref', 'w'); |
374b7e37 |
97 | foreach ($prefs_cache as $Key => $Value) { |
98 | if (isset($Value)) { |
31ac7db8 |
99 | fwrite($file, $Key . '=' . $Value . "\n"); |
374b7e37 |
100 | } |
44f642f5 |
101 | } |
102 | fclose($file); |
31ac7db8 |
103 | } |
44f642f5 |
104 | |
44f642f5 |
105 | |
31ac7db8 |
106 | function removePref($data_dir, $username, $string) { |
107 | global $prefs_cache; |
fb120846 |
108 | |
31ac7db8 |
109 | cachePrefValues($data_dir, $username); |
fb120846 |
110 | |
374b7e37 |
111 | if (isset($prefs_cache[$string])) { |
31ac7db8 |
112 | unset($prefs_cache[$string]); |
374b7e37 |
113 | } |
fb120846 |
114 | |
31ac7db8 |
115 | savePrefValues($data_dir, $username); |
44f642f5 |
116 | } |
fb120846 |
117 | |
65b14f90 |
118 | /** sets the pref, $string, to $set_to **/ |
d0747e26 |
119 | function setPref($data_dir, $username, $string, $set_to) { |
31ac7db8 |
120 | global $prefs_cache; |
fb120846 |
121 | |
31ac7db8 |
122 | cachePrefValues($data_dir, $username); |
bf1d3803 |
123 | if (isset($prefs_cache[$string]) && $prefs_cache[$string] == $set_to) |
fa42b484 |
124 | return; |
ed9f3a67 |
125 | if ($set_to === '') { |
fa42b484 |
126 | removePref($data_dir, $username, $string); |
fb120846 |
127 | return; |
fa42b484 |
128 | } |
31ac7db8 |
129 | $prefs_cache[$string] = $set_to; |
31ac7db8 |
130 | savePrefValues($data_dir, $username); |
65b14f90 |
131 | } |
132 | |
f804972b |
133 | |
d068c0ec |
134 | /** This checks if there is a pref file, if there isn't, it will |
135 | create it. **/ |
d0747e26 |
136 | function checkForPrefs($data_dir, $username) { |
63123e7c |
137 | $filename = $data_dir . $username . '.pref'; |
fb120846 |
138 | if (!file_exists($filename) ) { |
63123e7c |
139 | if (!copy($data_dir . 'default_pref', $filename)) { |
140 | echo _("Error opening ") . $filename; |
65b14f90 |
141 | exit; |
142 | } |
143 | } |
65b14f90 |
144 | } |
f804972b |
145 | |
146 | |
f804972b |
147 | /** Writes the Signature **/ |
148 | function setSig($data_dir, $username, $string) { |
63123e7c |
149 | $file = fopen($data_dir . $username . '.sig', 'w'); |
f804972b |
150 | fwrite($file, $string); |
151 | fclose($file); |
152 | } |
153 | |
154 | |
155 | |
156 | /** Gets the signature **/ |
157 | function getSig($data_dir, $username) { |
63123e7c |
158 | $filename = $data_dir . $username . '.sig'; |
159 | $sig = ''; |
f804972b |
160 | if (file_exists($filename)) { |
63123e7c |
161 | $file = fopen($filename, 'r'); |
f804972b |
162 | while (!feof($file)) { |
163 | $sig .= fgets($file, 1024); |
164 | } |
165 | fclose($file); |
f804972b |
166 | } |
167 | return $sig; |
168 | } |
fb120846 |
169 | |
35586184 |
170 | ?> |