59177427 |
1 | <?php |
65b14f90 |
2 | /** |
3 | ** prefs.php |
4 | ** |
5 | ** This contains functions for manipulating user preferences |
245a6892 |
6 | ** |
7 | ** $Id$ |
65b14f90 |
8 | **/ |
9 | |
f435778e |
10 | global $prefs_are_cached, $prefs_cache; |
374b7e37 |
11 | if (!session_is_registered('prefs_are_cached')) { |
12 | $prefs_are_cached = false; |
13 | $prefs_cache = array(); |
14 | } |
31ac7db8 |
15 | |
374b7e37 |
16 | function cachePrefValues($data_dir, $username) { |
31ac7db8 |
17 | global $prefs_are_cached, $prefs_cache; |
18 | |
19 | if ($prefs_are_cached) |
20 | return; |
21 | |
22 | $filename = $data_dir . $username . '.pref'; |
23 | |
24 | if (!file_exists($filename)) { |
374b7e37 |
25 | printf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename); |
31ac7db8 |
26 | exit; |
27 | } |
28 | |
63123e7c |
29 | $file = fopen($filename, 'r'); |
31ac7db8 |
30 | |
31 | /** read in all the preferences **/ |
32 | $highlight_num = 0; |
33 | while (! feof($file)) { |
34 | $pref = trim(fgets($file, 1024)); |
374b7e37 |
35 | $equalsAt = strpos($pref, '='); |
36 | if ($equalsAt > 0) { |
37 | $Key = substr($pref, 0, $equalsAt); |
38 | $Value = substr($pref, $equalsAt + 1); |
39 | if (substr($Key, 0, 9) == 'highlight') { |
40 | $Key = 'highlight' . $highlight_num; |
41 | $highlight_num ++; |
31ac7db8 |
42 | } |
43 | |
374b7e37 |
44 | if ($Value != '') { |
45 | $prefs_cache[$Key] = $Value; |
46 | } |
47 | } |
31ac7db8 |
48 | } |
49 | fclose($file); |
374b7e37 |
50 | |
51 | session_unregister('prefs_cache'); |
52 | session_register('prefs_cache'); |
31ac7db8 |
53 | |
54 | $prefs_are_cached = true; |
374b7e37 |
55 | session_unregister('prefs_are_cached'); |
56 | session_register('prefs_are_cached'); |
31ac7db8 |
57 | } |
58 | |
59 | |
65b14f90 |
60 | /** returns the value for $string **/ |
f1e6f580 |
61 | function getPref($data_dir, $username, $string, $default = '') { |
31ac7db8 |
62 | global $prefs_cache; |
f1e6f580 |
63 | |
31ac7db8 |
64 | cachePrefValues($data_dir, $username); |
f1e6f580 |
65 | |
31ac7db8 |
66 | if (isset($prefs_cache[$string])) |
67 | return $prefs_cache[$string]; |
f1e6f580 |
68 | else |
69 | return $default; |
65b14f90 |
70 | } |
71 | |
44f642f5 |
72 | |
374b7e37 |
73 | function savePrefValues($data_dir, $username) { |
31ac7db8 |
74 | global $prefs_cache; |
75 | |
63123e7c |
76 | $file = fopen($data_dir . $username . '.pref', 'w'); |
374b7e37 |
77 | foreach ($prefs_cache as $Key => $Value) { |
78 | if (isset($Value)) { |
31ac7db8 |
79 | fwrite($file, $Key . '=' . $Value . "\n"); |
374b7e37 |
80 | } |
44f642f5 |
81 | } |
82 | fclose($file); |
31ac7db8 |
83 | } |
44f642f5 |
84 | |
44f642f5 |
85 | |
31ac7db8 |
86 | function removePref($data_dir, $username, $string) { |
87 | global $prefs_cache; |
88 | |
89 | cachePrefValues($data_dir, $username); |
90 | |
374b7e37 |
91 | if (isset($prefs_cache[$string])) { |
31ac7db8 |
92 | unset($prefs_cache[$string]); |
374b7e37 |
93 | } |
94 | |
31ac7db8 |
95 | savePrefValues($data_dir, $username); |
44f642f5 |
96 | } |
97 | |
65b14f90 |
98 | /** sets the pref, $string, to $set_to **/ |
d0747e26 |
99 | function setPref($data_dir, $username, $string, $set_to) { |
31ac7db8 |
100 | global $prefs_cache; |
101 | |
102 | cachePrefValues($data_dir, $username); |
bf1d3803 |
103 | if (isset($prefs_cache[$string]) && $prefs_cache[$string] == $set_to) |
fa42b484 |
104 | return; |
ed9f3a67 |
105 | if ($set_to === '') { |
fa42b484 |
106 | removePref($data_dir, $username, $string); |
107 | return; |
108 | } |
31ac7db8 |
109 | $prefs_cache[$string] = $set_to; |
31ac7db8 |
110 | savePrefValues($data_dir, $username); |
65b14f90 |
111 | } |
112 | |
f804972b |
113 | |
d068c0ec |
114 | /** This checks if there is a pref file, if there isn't, it will |
115 | create it. **/ |
d0747e26 |
116 | function checkForPrefs($data_dir, $username) { |
63123e7c |
117 | $filename = $data_dir . $username . '.pref'; |
65b14f90 |
118 | if (!file_exists($filename)) { |
63123e7c |
119 | if (!copy($data_dir . 'default_pref', $filename)) { |
120 | echo _("Error opening ") . $filename; |
65b14f90 |
121 | exit; |
122 | } |
123 | } |
65b14f90 |
124 | } |
f804972b |
125 | |
126 | |
f804972b |
127 | /** Writes the Signature **/ |
128 | function setSig($data_dir, $username, $string) { |
63123e7c |
129 | $file = fopen($data_dir . $username . '.sig', 'w'); |
f804972b |
130 | fwrite($file, $string); |
131 | fclose($file); |
132 | } |
133 | |
134 | |
135 | |
136 | /** Gets the signature **/ |
137 | function getSig($data_dir, $username) { |
63123e7c |
138 | $filename = $data_dir . $username . '.sig'; |
139 | $sig = ''; |
f804972b |
140 | if (file_exists($filename)) { |
63123e7c |
141 | $file = fopen($filename, 'r'); |
f804972b |
142 | while (!feof($file)) { |
143 | $sig .= fgets($file, 1024); |
144 | } |
145 | fclose($file); |
f804972b |
146 | } |
147 | return $sig; |
148 | } |
cbe5423b |
149 | ?> |