* Changed preferences to cache them for reading (Yeay!)
[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 $prefs_php = true;
11
12 $prefs_are_cached = false;
13 $prefs_cache = array();
14
15 function cachePrefValues($data_dir, $username)
16 {
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 {
38 $Key = substr($pref, 0, $equalsAt);
39 $Value = substr($pref, $equalsAt + 1);
40 if (substr($Key, 0, 9) == 'highlight')
41 {
42 $Key = 'highlight' . $highlight_num;
43 $highlight_num ++;
44 }
45
46 if ($Value != '')
47 $prefs_cache[$Key] = $Value;
48 }
49 }
50 fclose($file);
51
52 $prefs_are_cached = true;
53 }
54
55
56 /** returns the value for $string **/
57 function getPref($data_dir, $username, $string) {
58 global $prefs_cache;
59
60 cachePrefValues($data_dir, $username);
61
62 if (isset($prefs_cache[$string]))
63 return $prefs_cache[$string];
64 return '';
65 }
66
67
68 function savePrefValues($data_dir, $username)
69 {
70 global $prefs_cache;
71
72 $file = fopen($data_dir . $username . '.pref', "w");
73 foreach ($prefs_cache as $Key => $Value)
74 {
75 if (isset($Value))
76 fwrite($file, $Key . '=' . $Value . "\n");
77 }
78 fclose($file);
79 }
80
81
82 function removePref($data_dir, $username, $string) {
83 global $prefs_cache;
84
85 cachePrefValues($data_dir, $username);
86
87 if (isset($prefs_cache[$string]))
88 unset($prefs_cache[$string]);
89
90 savePrefValues($data_dir, $username);
91 }
92
93 /** sets the pref, $string, to $set_to **/
94 function setPref($data_dir, $username, $string, $set_to) {
95 global $prefs_cache;
96
97 cachePrefValues($data_dir, $username);
98
99 $prefs_cache[$string] = $set_to;
100
101 savePrefValues($data_dir, $username);
102 }
103
104
105 /** This checks if there is a pref file, if there isn't, it will
106 create it. **/
107 function checkForPrefs($data_dir, $username) {
108 $filename = "$data_dir$username.pref";
109 if (!file_exists($filename)) {
110 if (!copy("$data_dir" . "default_pref", $filename)) {
111 echo _("Error opening ") ."$filename";
112 exit;
113 }
114 }
115 }
116
117
118 /** Writes the Signature **/
119 function setSig($data_dir, $username, $string) {
120 $filename = "$data_dir$username.sig";
121 $file = fopen($filename, "w");
122 fwrite($file, $string);
123 fclose($file);
124 }
125
126
127
128 /** Gets the signature **/
129 function getSig($data_dir, $username) {
130 $filename = "$data_dir$username.sig";
131 $sig = "";
132 if (file_exists($filename)) {
133 $file = fopen($filename, "r");
134 while (!feof($file)) {
135 $sig .= fgets($file, 1024);
136 }
137 fclose($file);
138 }
139 return $sig;
140 }
141 ?>