posix_* functions don't exist on !UNIX platforms. Adding workarounds.
[squirrelmail.git] / functions / file_prefs.php
CommitLineData
3499f99f 1<?php
2
3/**
4 * file_prefs.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains functions for manipulating user preferences in files
10 *
11 * $Id$
12 */
13
14global $prefs_are_cached, $prefs_cache;
15
16/**
17 * Check the preferences into the session cache.
18 */
19function cachePrefValues($data_dir, $username) {
20 global $prefs_are_cached, $prefs_cache;
21
22 if ( isset($prefs_are_cached) && $prefs_are_cached) {
23 return;
24 }
25
26 session_unregister('prefs_cache');
27 session_unregister('prefs_are_cached');
28
29 /* Calculate the filename for the user's preference file */
30 $filename = getHashedFile($username, $data_dir, "$username.pref");
31
32 /* A call to checkForPrefs here should take eliminate the need for */
33 /* this to be called throughout the rest of the SquirrelMail code. */
34 checkForPrefs($data_dir, $username, $filename);
35
36 /* Make sure that the preference file now DOES exist. */
37 if (!file_exists($filename)) {
9be8198d 38 include_once( '../functions/display_messages.php' );
39 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
3499f99f 40 exit;
41 }
42
43 $file = fopen($filename, 'r');
44
45 /* Read in the preferences. */
46 $highlight_num = 0;
47 while (! feof($file)) {
48 $pref = trim(fgets($file, 1024));
49 $equalsAt = strpos($pref, '=');
50 if ($equalsAt > 0) {
51 $key = substr($pref, 0, $equalsAt);
52 $value = substr($pref, $equalsAt + 1);
53 if (substr($key, 0, 9) == 'highlight') {
54 $key = 'highlight' . $highlight_num;
55 $highlight_num ++;
56 }
57
58 if ($value != '') {
59 $prefs_cache[$key] = $value;
60 }
61 }
62 }
63 fclose($file);
64
65 $prefs_are_cached = TRUE;
66
67 session_register('prefs_cache');
68 session_register('prefs_are_cached');
69}
70
71/**
72 * Return the value for the prefernce given by $string.
73 */
74function getPref($data_dir, $username, $string, $default = '') {
75 global $prefs_cache;
76 $result = '';
77
78 cachePrefValues($data_dir, $username);
79
80 if (isset($prefs_cache[$string])) {
81 $result = $prefs_cache[$string];
82 } else {
83 $result = $default;
84 }
85
86 return ($result);
87}
88
89/**
90 * Save the preferences for this user.
91 */
92function savePrefValues($data_dir, $username) {
93 global $prefs_cache;
94
95 $filename = getHashedFile($username, $data_dir, "$username.pref");
96
97 $file = fopen($filename, 'w');
98 foreach ($prefs_cache as $Key => $Value) {
99 if (isset($Value)) {
100 fwrite($file, $Key . '=' . $Value . "\n");
101 }
102 }
103 fclose($file);
104 chmod($filename, 0600);
105}
106
107/**
108 * Remove a preference for the current user.
109 */
110function removePref($data_dir, $username, $string) {
111 global $prefs_cache;
112
113 cachePrefValues($data_dir, $username);
114
115 if (isset($prefs_cache[$string])) {
116 unset($prefs_cache[$string]);
117 }
118
119 savePrefValues($data_dir, $username);
120}
121
122/**
123 * Set a there preference $string to $value.
124 */
125function setPref($data_dir, $username, $string, $value) {
126 global $prefs_cache;
127
128 cachePrefValues($data_dir, $username);
129 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
130 return;
131 }
132
133 if ($value === '') {
134 removePref($data_dir, $username, $string);
135 return;
136 }
137
138 $prefs_cache[$string] = $value;
139 savePrefValues($data_dir, $username);
140}
141
142/**
143 * Check for a preferences file. If one can not be found, create it.
144 */
145function checkForPrefs($data_dir, $username, $filename = '') {
146 /* First, make sure we have the filename. */
147 if ($filename == '') {
bd1180c1 148 $filename = getHashedFile($username, $data_dir, "$username.pref");
3499f99f 149 }
150
151 /* Then, check if the file exists. */
152 if (!@file_exists($filename) ) {
153 /* First, check the $data_dir for the default preference file. */
154 $default_pref = $data_dir . 'default_pref';
155
156 /* If it is not there, check the internal data directory. */
157 if (!@file_exists($default_pref)) {
158 $default_pref = '../data/default_pref';
159 }
160
161 /* Otherwise, report an error. */
9be8198d 162 $errTitle = sprintf( _("Error opening %s"), $default_pref );
3499f99f 163 if (!file_exists($default_pref)) {
9be8198d 164 $errString = $errTitle . "<br>\n" .
165 _("Default preference file not found!") . "<br>\n" .
166 _("Please contact your system administrator and report this error.") . "<br>\n";
167 include_once( '../functions/display_messages.php' );
168 logout_error( $errString, $errTitle );
3499f99f 169 exit;
170 } else if (!@copy($default_pref, $filename)) {
305a1012 171 $uid = 'httpd';
172 if (function_exists('posix_getuid')){
173 $user_data = posix_getpwuid(posix_getuid());
174 $uid = $user_data['name'];
175 }
9be8198d 176 $errString = $errTitle . '<br>' .
177 _("Could not create initial preference file!") . "<br>\n" .
178 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) .
179 "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n";
180 include_once( '../functions/display_messages.php' );
181 logout_error( $errString, $errTitle );
3499f99f 182 exit;
183 }
184 }
185}
186
187/**
188 * Write the User Signature.
189 */
01265fba 190function setSig($data_dir, $username, $number, $value) {
191 $filename = getHashedFile($username, $data_dir, "$username.si$number");
3499f99f 192 $file = fopen($filename, 'w');
193 fwrite($file, $value);
194 fclose($file);
195}
196
197/**
198 * Get the signature.
199 */
01265fba 200function getSig($data_dir, $username, $number) {
201 #$filename = $data_dir . $username . '.si$number';
202 $filename = getHashedFile($username, $data_dir, "$username.si$number");
3499f99f 203 $sig = '';
204 if (file_exists($filename)) {
205 $file = fopen($filename, 'r');
206 while (!feof($file)) {
207 $sig .= fgets($file, 1024);
208 }
209 fclose($file);
210 }
211 return $sig;
212}