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; |
11 | |
65b14f90 |
12 | /** returns the value for $string **/ |
d0747e26 |
13 | function getPref($data_dir, $username, $string) { |
14 | $filename = "$data_dir$username.pref"; |
3021b626 |
15 | if (!file_exists($filename)) { |
2ad42ea3 |
16 | printf (_("Preference file %s not found. Exiting abnormally"), $filename); |
3021b626 |
17 | exit; |
18 | } |
19 | |
65b14f90 |
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); |
d3cdb279 |
27 | return trim(substr($pref, strpos($pref, "=")+1)); |
65b14f90 |
28 | } |
29 | } |
30 | fclose($file); |
31 | return ""; |
32 | } |
33 | |
44f642f5 |
34 | function removePref($data_dir, $username, $string) { |
35 | $filename = "$data_dir$username.pref"; |
36 | $found = false; |
37 | if (!file_exists($filename)) { |
2ad42ea3 |
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"; |
44f642f5 |
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 | } |
cd928157 |
65 | if (isset($htl)) { |
66 | for ($i=0; $i < count($hlt); $i++) { |
67 | fwrite($file, "highlight$i=$hlt[$i]"); |
68 | } |
44f642f5 |
69 | } |
44f642f5 |
70 | fclose($file); |
71 | } |
72 | |
65b14f90 |
73 | /** sets the pref, $string, to $set_to **/ |
d0747e26 |
74 | function setPref($data_dir, $username, $string, $set_to) { |
75 | $filename = "$data_dir$username.pref"; |
65b14f90 |
76 | $found = false; |
77 | if (!file_exists($filename)) { |
2ad42ea3 |
78 | printf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename); |
79 | echo "\n<br>\n"; |
65b14f90 |
80 | exit; |
81 | } |
82 | $file = fopen($filename, "r"); |
83 | |
84 | /** read in all the preferences **/ |
85 | for ($i=0; !feof($file); $i++) { |
86 | $pref[$i] = fgets($file, 1024); |
87 | if (substr($pref[$i], 0, strpos($pref[$i], "=")) == $string) { |
88 | $found = true; |
89 | $pos = $i; |
90 | } |
91 | } |
92 | fclose($file); |
93 | |
94 | $file = fopen($filename, "w"); |
95 | if ($found == true) { |
96 | for ($i=0; $i < count($pref); $i++) { |
97 | if ($i == $pos) { |
98 | fwrite($file, "$string=$set_to\n", 1024); |
99 | } else { |
100 | fwrite($file, "$pref[$i]", 1024); |
101 | } |
102 | } |
103 | } else { |
104 | for ($i=0; $i < count($pref); $i++) { |
105 | fwrite($file, "$pref[$i]", 1024); |
106 | } |
107 | fwrite($file, "$string=$set_to\n", 1024); |
108 | } |
109 | |
110 | fclose($file); |
111 | } |
112 | |
f804972b |
113 | |
114 | |
115 | |
d068c0ec |
116 | /** This checks if there is a pref file, if there isn't, it will |
117 | create it. **/ |
d0747e26 |
118 | function checkForPrefs($data_dir, $username) { |
119 | $filename = "$data_dir$username.pref"; |
65b14f90 |
120 | if (!file_exists($filename)) { |
8105d00f |
121 | if (!copy("$data_dir" . "default_pref", $filename)) { |
5f29dd3b |
122 | echo _("Error opening ") ."$filename"; |
65b14f90 |
123 | exit; |
124 | } |
125 | } |
65b14f90 |
126 | } |
f804972b |
127 | |
128 | |
129 | |
130 | /** Writes the Signature **/ |
131 | function setSig($data_dir, $username, $string) { |
132 | $filename = "$data_dir$username.sig"; |
133 | $file = fopen($filename, "w"); |
134 | fwrite($file, $string); |
135 | fclose($file); |
136 | } |
137 | |
138 | |
139 | |
140 | /** Gets the signature **/ |
141 | function getSig($data_dir, $username) { |
142 | $filename = "$data_dir$username.sig"; |
a7ea7540 |
143 | $sig = ""; |
f804972b |
144 | if (file_exists($filename)) { |
145 | $file = fopen($filename, "r"); |
146 | while (!feof($file)) { |
147 | $sig .= fgets($file, 1024); |
148 | } |
149 | fclose($file); |
f804972b |
150 | } |
151 | return $sig; |
152 | } |
5f29dd3b |
153 | ?> |