4430a191cd6e8912d5f8684184994831d0cfbccc
[squirrelmail.git] / functions / prefs.php
1 <?php
2
3 /**
4 * prefs.php
5 *
6 * Copyright (c) 1999-2001 The SquirrelMail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains functions for manipulating user preferences
10 *
11 * $Id$
12 */
13
14 /*****************************************************************/
15 /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
16 /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
17 /*** + Base level indent should begin at left margin, as ***/
18 /*** the $prefs_are_cached and $prefs_cache stuff below. ***/
19 /*** + All identation should consist of four space blocks ***/
20 /*** + Tab characters are evil. ***/
21 /*** + all comments should use "slash-star ... star-slash" ***/
22 /*** style -- no pound characters, no slash-slash style ***/
23 /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
24 /*** ALWAYS USE { AND } CHARACTERS!!! ***/
25 /*** + Please use ' instead of ", when possible. Note " ***/
26 /*** should always be used in _( ) function calls. ***/
27 /*** Thank you for your help making the SM code more readable. ***/
28 /*****************************************************************/
29
30 global $prefs_are_cached, $prefs_cache;
31 if (!session_is_registered('prefs_are_cached')) {
32 $prefs_are_cached = false;
33 $prefs_cache = array();
34 }
35
36 function cachePrefValues($data_dir, $username) {
37 global $prefs_are_cached, $prefs_cache;
38
39 if ($prefs_are_cached)
40 return;
41
42 $filename = $data_dir . $username . '.pref';
43
44 if (!file_exists($filename)) {
45 printf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename);
46 exit;
47 }
48
49 $file = fopen($filename, 'r');
50
51 /** read in all the preferences **/
52 $highlight_num = 0;
53 while (! feof($file)) {
54 $pref = trim(fgets($file, 1024));
55 $equalsAt = strpos($pref, '=');
56 if ($equalsAt > 0) {
57 $Key = substr($pref, 0, $equalsAt);
58 $Value = substr($pref, $equalsAt + 1);
59 if (substr($Key, 0, 9) == 'highlight') {
60 $Key = 'highlight' . $highlight_num;
61 $highlight_num ++;
62 }
63
64 if ($Value != '') {
65 $prefs_cache[$Key] = $Value;
66 }
67 }
68 }
69 fclose($file);
70
71 session_unregister('prefs_cache');
72 session_register('prefs_cache');
73
74 $prefs_are_cached = true;
75 session_unregister('prefs_are_cached');
76 session_register('prefs_are_cached');
77 }
78
79
80 /** returns the value for $string **/
81 function getPref($data_dir, $username, $string, $default = '') {
82 global $prefs_cache;
83
84 cachePrefValues($data_dir, $username);
85
86 if (isset($prefs_cache[$string]))
87 return $prefs_cache[$string];
88 else
89 return $default;
90 }
91
92
93 function savePrefValues($data_dir, $username) {
94 global $prefs_cache;
95
96 $file = fopen($data_dir . $username . '.pref', 'w');
97 foreach ($prefs_cache as $Key => $Value) {
98 if (isset($Value)) {
99 fwrite($file, $Key . '=' . $Value . "\n");
100 }
101 }
102 fclose($file);
103 }
104
105
106 function removePref($data_dir, $username, $string) {
107 global $prefs_cache;
108
109 cachePrefValues($data_dir, $username);
110
111 if (isset($prefs_cache[$string])) {
112 unset($prefs_cache[$string]);
113 }
114
115 savePrefValues($data_dir, $username);
116 }
117
118 /** sets the pref, $string, to $set_to **/
119 function setPref($data_dir, $username, $string, $set_to) {
120 global $prefs_cache;
121
122 cachePrefValues($data_dir, $username);
123 if (isset($prefs_cache[$string]) && $prefs_cache[$string] == $set_to)
124 return;
125 if ($set_to === '') {
126 removePref($data_dir, $username, $string);
127 return;
128 }
129 $prefs_cache[$string] = $set_to;
130 savePrefValues($data_dir, $username);
131 }
132
133
134 /** This checks if there is a pref file, if there isn't, it will
135 create it. **/
136 function checkForPrefs($data_dir, $username) {
137 $filename = $data_dir . $username . '.pref';
138 if (!file_exists($filename) ) {
139 if (!copy($data_dir . 'default_pref', $filename)) {
140 echo _("Error opening ") . $filename;
141 exit;
142 }
143 }
144 }
145
146
147 /** Writes the Signature **/
148 function setSig($data_dir, $username, $string) {
149 $file = fopen($data_dir . $username . '.sig', 'w');
150 fwrite($file, $string);
151 fclose($file);
152 }
153
154
155
156 /** Gets the signature **/
157 function getSig($data_dir, $username) {
158 $filename = $data_dir . $username . '.sig';
159 $sig = '';
160 if (file_exists($filename)) {
161 $file = fopen($filename, 'r');
162 while (!feof($file)) {
163 $sig .= fgets($file, 1024);
164 }
165 fclose($file);
166 }
167 return $sig;
168 }
169
170 ?>