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