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