59177427 |
1 | <?php |
65b14f90 |
2 | /** |
3 | ** prefs.php |
4 | ** |
5 | ** This contains functions for manipulating user preferences |
6 | **/ |
7 | |
d068c0ec |
8 | $prefs_php = true; |
9 | |
65b14f90 |
10 | /** returns the value for $string **/ |
d0747e26 |
11 | function getPref($data_dir, $username, $string) { |
12 | $filename = "$data_dir$username.pref"; |
3021b626 |
13 | if (!file_exists($filename)) { |
2ad42ea3 |
14 | printf (_("Preference file %s not found. Exiting abnormally"), $filename); |
3021b626 |
15 | exit; |
16 | } |
17 | |
65b14f90 |
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); |
d3cdb279 |
25 | return trim(substr($pref, strpos($pref, "=")+1)); |
65b14f90 |
26 | } |
27 | } |
28 | fclose($file); |
29 | return ""; |
30 | } |
31 | |
44f642f5 |
32 | function removePref($data_dir, $username, $string) { |
33 | $filename = "$data_dir$username.pref"; |
34 | $found = false; |
35 | if (!file_exists($filename)) { |
2ad42ea3 |
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"; |
44f642f5 |
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 | |
65b14f90 |
70 | /** sets the pref, $string, to $set_to **/ |
d0747e26 |
71 | function setPref($data_dir, $username, $string, $set_to) { |
72 | $filename = "$data_dir$username.pref"; |
65b14f90 |
73 | $found = false; |
74 | if (!file_exists($filename)) { |
2ad42ea3 |
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"; |
65b14f90 |
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 | |
f804972b |
110 | |
111 | |
112 | |
d068c0ec |
113 | /** This checks if there is a pref file, if there isn't, it will |
114 | create it. **/ |
d0747e26 |
115 | function checkForPrefs($data_dir, $username) { |
116 | $filename = "$data_dir$username.pref"; |
65b14f90 |
117 | if (!file_exists($filename)) { |
8105d00f |
118 | if (!copy("$data_dir" . "default_pref", $filename)) { |
5f29dd3b |
119 | echo _("Error opening ") ."$filename"; |
65b14f90 |
120 | exit; |
121 | } |
122 | } |
65b14f90 |
123 | } |
f804972b |
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"); |
7aaa81fc |
142 | $sig = ""; |
f804972b |
143 | while (!feof($file)) { |
144 | $sig .= fgets($file, 1024); |
145 | } |
146 | fclose($file); |
f804972b |
147 | } |
148 | return $sig; |
149 | } |
5f29dd3b |
150 | ?> |