6 * Copyright (c) 1999-2001 The SquirrelMail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains functions for manipulating user preferences
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 /*****************************************************************/
30 global $prefs_are_cached, $prefs_cache;
31 if (!session_is_registered('prefs_are_cached')) {
32 $prefs_are_cached = false;
33 $prefs_cache = array();
36 function cachePrefValues($data_dir, $username) {
37 global $prefs_are_cached, $prefs_cache;
39 if ($prefs_are_cached)
42 $filename = $data_dir . $username . '.pref';
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);
49 $file = fopen($filename, 'r');
51 /** read in all the preferences **/
53 while (! feof($file)) {
54 $pref = trim(fgets($file, 1024));
55 $equalsAt = strpos($pref, '=');
57 $Key = substr($pref, 0, $equalsAt);
58 $Value = substr($pref, $equalsAt +
1);
59 if (substr($Key, 0, 9) == 'highlight') {
60 $Key = 'highlight' . $highlight_num;
65 $prefs_cache[$Key] = $Value;
71 session_unregister('prefs_cache');
72 session_register('prefs_cache');
74 $prefs_are_cached = true;
75 session_unregister('prefs_are_cached');
76 session_register('prefs_are_cached');
80 /** returns the value for $string **/
81 function getPref($data_dir, $username, $string, $default = '') {
84 cachePrefValues($data_dir, $username);
86 if (isset($prefs_cache[$string]))
87 return $prefs_cache[$string];
93 function savePrefValues($data_dir, $username) {
96 $file = fopen($data_dir . $username . '.pref', 'w');
97 foreach ($prefs_cache as $Key => $Value) {
99 fwrite($file, $Key . '=' . $Value . "\n");
106 function removePref($data_dir, $username, $string) {
109 cachePrefValues($data_dir, $username);
111 if (isset($prefs_cache[$string])) {
112 unset($prefs_cache[$string]);
115 savePrefValues($data_dir, $username);
118 /** sets the pref, $string, to $set_to **/
119 function setPref($data_dir, $username, $string, $set_to) {
122 cachePrefValues($data_dir, $username);
123 if (isset($prefs_cache[$string]) && $prefs_cache[$string] == $set_to)
125 if ($set_to === '') {
126 removePref($data_dir, $username, $string);
129 $prefs_cache[$string] = $set_to;
130 savePrefValues($data_dir, $username);
134 /** This checks if there is a pref file, if there isn't, it will
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;
147 /** Writes the Signature **/
148 function setSig($data_dir, $username, $string) {
149 $file = fopen($data_dir . $username . '.sig', 'w');
150 fwrite($file, $string);
156 /** Gets the signature **/
157 function getSig($data_dir, $username) {
158 $filename = $data_dir . $username . '.sig';
160 if (file_exists($filename)) {
161 $file = fopen($filename, 'r');
162 while (!feof($file)) {
163 $sig .= fgets($file, 1024);