* Removed another warning that shouldn't ever pop up, but it did with a
[squirrelmail.git] / functions / prefs.php
CommitLineData
59177427 1<?php
65b14f90 2 /**
3 ** prefs.php
4 **
5 ** This contains functions for manipulating user preferences
245a6892 6 **
7 ** $Id$
65b14f90 8 **/
9
d068c0ec 10 $prefs_php = true;
31ac7db8 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
65b14f90 56 /** returns the value for $string **/
d0747e26 57 function getPref($data_dir, $username, $string) {
31ac7db8 58 global $prefs_cache;
59
60 cachePrefValues($data_dir, $username);
61
62 if (isset($prefs_cache[$string]))
63 return $prefs_cache[$string];
64 return '';
65b14f90 65 }
66
44f642f5 67
31ac7db8 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");
44f642f5 77 }
78 fclose($file);
31ac7db8 79 }
44f642f5 80
44f642f5 81
31ac7db8 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);
44f642f5 91 }
92
65b14f90 93 /** sets the pref, $string, to $set_to **/
d0747e26 94 function setPref($data_dir, $username, $string, $set_to) {
31ac7db8 95 global $prefs_cache;
96
97 cachePrefValues($data_dir, $username);
98
99 $prefs_cache[$string] = $set_to;
65b14f90 100
31ac7db8 101 savePrefValues($data_dir, $username);
65b14f90 102 }
103
f804972b 104
d068c0ec 105 /** This checks if there is a pref file, if there isn't, it will
106 create it. **/
d0747e26 107 function checkForPrefs($data_dir, $username) {
108 $filename = "$data_dir$username.pref";
65b14f90 109 if (!file_exists($filename)) {
8105d00f 110 if (!copy("$data_dir" . "default_pref", $filename)) {
5f29dd3b 111 echo _("Error opening ") ."$filename";
65b14f90 112 exit;
113 }
114 }
65b14f90 115 }
f804972b 116
117
f804972b 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";
a7ea7540 131 $sig = "";
f804972b 132 if (file_exists($filename)) {
133 $file = fopen($filename, "r");
134 while (!feof($file)) {
135 $sig .= fgets($file, 1024);
136 }
137 fclose($file);
f804972b 138 }
139 return $sig;
140 }
5f29dd3b 141?>