5a2a89d45059eab6ceb4f92632484ca36994ba72
[squirrelmail.git] / functions / prefs.php
1 <?
2 /**
3 ** prefs.php
4 **
5 ** This contains functions for manipulating user preferences
6 **/
7
8 /** returns the value for $string **/
9 function getPref($username, $string) {
10 $filename = "../data/$username.pref";
11 $file = fopen($filename, "r");
12
13 /** read in all the preferences **/
14 for ($i=0; !feof($file); $i++) {
15 $pref = fgets($file, 1024);
16 if (substr($pref, 0, strpos($pref, "=")) == $string) {
17 fclose($file);
18 return trim(substr($pref, strpos($pref, "=")+1));
19 }
20 }
21 fclose($file);
22 return "";
23 }
24
25 /** sets the pref, $string, to $set_to **/
26 function setPref($username, $string, $set_to) {
27 $filename = "../data/$username.pref";
28 $found = false;
29 if (!file_exists($filename)) {
30 echo "Preference file, $filename, does not exist. Log out, and log back in to create a default preference file.<BR>";
31 exit;
32 }
33 $file = fopen($filename, "r");
34
35 /** read in all the preferences **/
36 for ($i=0; !feof($file); $i++) {
37 $pref[$i] = fgets($file, 1024);
38 if (substr($pref[$i], 0, strpos($pref[$i], "=")) == $string) {
39 $found = true;
40 $pos = $i;
41 }
42 }
43 fclose($file);
44
45 $file = fopen($filename, "w");
46 if ($found == true) {
47 for ($i=0; $i < count($pref); $i++) {
48 if ($i == $pos) {
49 fwrite($file, "$string=$set_to\n", 1024);
50 } else {
51 fwrite($file, "$pref[$i]", 1024);
52 }
53 }
54 } else {
55 for ($i=0; $i < count($pref); $i++) {
56 fwrite($file, "$pref[$i]", 1024);
57 }
58 fwrite($file, "$string=$set_to\n", 1024);
59 }
60
61 fclose($file);
62 }
63
64 /** This checks if there is a pref file, if there isn't, it will create it. **/
65 function checkForPrefs($username) {
66 $filename = "../data/$username.pref";
67 if (!file_exists($filename)) {
68 if (!copy("../data/default_pref", $filename)) {
69 echo "Error opening $filename";
70 exit;
71 }
72 }
73 return;
74 }
75 ?>