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