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