Fix check_php_version for versions that contain a patchlevel.
[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
3499f99f 14/**
15 * Check the preferences into the session cache.
16 */
17function cachePrefValues($data_dir, $username) {
18 global $prefs_are_cached, $prefs_cache;
19
20 if ( isset($prefs_are_cached) && $prefs_are_cached) {
21 return;
22 }
23
9eb0fbd4 24 sqsession_unregister('prefs_cache');
25 sqsession_unregister('prefs_are_cached');
3499f99f 26
27 /* Calculate the filename for the user's preference file */
28 $filename = getHashedFile($username, $data_dir, "$username.pref");
29
30 /* A call to checkForPrefs here should take eliminate the need for */
31 /* this to be called throughout the rest of the SquirrelMail code. */
32 checkForPrefs($data_dir, $username, $filename);
33
34 /* Make sure that the preference file now DOES exist. */
35 if (!file_exists($filename)) {
bd9c880b 36 include_once(SM_PATH . 'functions/display_messages.php');
9be8198d 37 logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
3499f99f 38 exit;
39 }
40
35b5fa13 41 /* Open the file, or else display an error to the user. */
42 if(!$file = @fopen($filename, 'r'))
43 {
bd9c880b 44 include_once(SM_PATH . 'functions/display_messages.php');
35b5fa13 45 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
46 exit;
47 }
3499f99f 48
49 /* Read in the preferences. */
50 $highlight_num = 0;
51 while (! feof($file)) {
52 $pref = trim(fgets($file, 1024));
53 $equalsAt = strpos($pref, '=');
54 if ($equalsAt > 0) {
55 $key = substr($pref, 0, $equalsAt);
56 $value = substr($pref, $equalsAt + 1);
1c159927 57 /* this is to 'rescue' old-style highlighting rules. */
3499f99f 58 if (substr($key, 0, 9) == 'highlight') {
59 $key = 'highlight' . $highlight_num;
60 $highlight_num ++;
61 }
62
63 if ($value != '') {
64 $prefs_cache[$key] = $value;
65 }
66 }
67 }
68 fclose($file);
69
70 $prefs_are_cached = TRUE;
71
9eb0fbd4 72 sqsession_register($prefs_cache, 'prefs_cache');
73 sqsession_register($prefs_are_cached, 'prefs_are_cached');
3499f99f 74}
75
76/**
77 * Return the value for the prefernce given by $string.
78 */
79function getPref($data_dir, $username, $string, $default = '') {
80 global $prefs_cache;
397a9d7b 81
be6f5c46 82 $result = do_hook_function('get_pref_override',array($username,$string));
83 if (!$result) {
84 cachePrefValues($data_dir, $username);
85 if (isset($prefs_cache[$string])) {
86 $result = $prefs_cache[$string];
87 } else {
88 $result = do_hook_function('get_pref', array($username,$string));
89 if (!$result) {
90 $result = $default;
91 }
92 }
3499f99f 93 }
3499f99f 94 return ($result);
95}
96
97/**
98 * Save the preferences for this user.
99 */
100function savePrefValues($data_dir, $username) {
101 global $prefs_cache;
102
103 $filename = getHashedFile($username, $data_dir, "$username.pref");
104
35b5fa13 105 /* Open the file for writing, or else display an error to the user. */
be6f5c46 106 if(!$file = @fopen($filename.'.tmp', 'w'))
35b5fa13 107 {
bd9c880b 108 include_once(SM_PATH . 'functions/display_messages.php');
be6f5c46 109 logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
35b5fa13 110 exit;
111 }
3499f99f 112 foreach ($prefs_cache as $Key => $Value) {
113 if (isset($Value)) {
dabef6fd 114 $tmpwrite = @fwrite($file, $Key . '=' . $Value . "\n");
115 if ($tmpwrite == -1) {
116 logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
117 exit;
118 }
3499f99f 119 }
120 }
121 fclose($file);
dabef6fd 122 @copy($filename . '.tmp',$filename);
123 @unlink($filename . '.tmp');
3499f99f 124 chmod($filename, 0600);
125}
126
127/**
128 * Remove a preference for the current user.
129 */
130function removePref($data_dir, $username, $string) {
131 global $prefs_cache;
132
133 cachePrefValues($data_dir, $username);
134
135 if (isset($prefs_cache[$string])) {
136 unset($prefs_cache[$string]);
137 }
138
139 savePrefValues($data_dir, $username);
140}
141
142/**
143 * Set a there preference $string to $value.
144 */
145function setPref($data_dir, $username, $string, $value) {
146 global $prefs_cache;
147
148 cachePrefValues($data_dir, $username);
149 if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
150 return;
151 }
152
153 if ($value === '') {
154 removePref($data_dir, $username, $string);
155 return;
156 }
157
158 $prefs_cache[$string] = $value;
159 savePrefValues($data_dir, $username);
160}
161
162/**
163 * Check for a preferences file. If one can not be found, create it.
164 */
165function checkForPrefs($data_dir, $username, $filename = '') {
166 /* First, make sure we have the filename. */
167 if ($filename == '') {
bd1180c1 168 $filename = getHashedFile($username, $data_dir, "$username.pref");
3499f99f 169 }
170
171 /* Then, check if the file exists. */
172 if (!@file_exists($filename) ) {
173 /* First, check the $data_dir for the default preference file. */
174 $default_pref = $data_dir . 'default_pref';
175
176 /* If it is not there, check the internal data directory. */
177 if (!@file_exists($default_pref)) {
bd9c880b 178 $default_pref = SM_PATH . 'data/default_pref';
3499f99f 179 }
180
181 /* Otherwise, report an error. */
9be8198d 182 $errTitle = sprintf( _("Error opening %s"), $default_pref );
921bea1a 183 if (!is_readable($default_pref)) {
9be8198d 184 $errString = $errTitle . "<br>\n" .
921bea1a 185 _("Default preference file not found or not readable!") . "<br>\n" .
9be8198d 186 _("Please contact your system administrator and report this error.") . "<br>\n";
bd9c880b 187 include_once(SM_PATH . 'functions/display_messages.php' );
9be8198d 188 logout_error( $errString, $errTitle );
3499f99f 189 exit;
190 } else if (!@copy($default_pref, $filename)) {
305a1012 191 $uid = 'httpd';
192 if (function_exists('posix_getuid')){
193 $user_data = posix_getpwuid(posix_getuid());
194 $uid = $user_data['name'];
195 }
9be8198d 196 $errString = $errTitle . '<br>' .
197 _("Could not create initial preference file!") . "<br>\n" .
198 sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) .
199 "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n";
bd9c880b 200 include_once(SM_PATH . 'functions/display_messages.php' );
9be8198d 201 logout_error( $errString, $errTitle );
3499f99f 202 exit;
203 }
204 }
205}
206
207/**
208 * Write the User Signature.
209 */
01265fba 210function setSig($data_dir, $username, $number, $value) {
211 $filename = getHashedFile($username, $data_dir, "$username.si$number");
35b5fa13 212 /* Open the file for writing, or else display an error to the user. */
dabef6fd 213 if(!$file = @fopen("$filename.tmp", 'w')) {
214 include_once( '../functions/display_messages.php' );
215 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
35b5fa13 216 exit;
217 }
dabef6fd 218 $tmpwrite = @fwrite($file, $value);
219 if ($tmpwrite == -1) {
220 include_once( '../functions/display_messages.php' );
221 logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
222 exit;
223 }
3499f99f 224 fclose($file);
dabef6fd 225 @copy($filename . '.tmp',$filename);
226 @unlink($filename . '.tmp');
227 chmod($filename, 0600);
228
3499f99f 229}
230
231/**
232 * Get the signature.
233 */
01265fba 234function getSig($data_dir, $username, $number) {
01265fba 235 $filename = getHashedFile($username, $data_dir, "$username.si$number");
3499f99f 236 $sig = '';
237 if (file_exists($filename)) {
35b5fa13 238 /* Open the file, or else display an error to the user. */
239 if(!$file = @fopen($filename, 'r'))
240 {
bd9c880b 241 include_once(SM_PATH . 'functions/display_messages.php');
35b5fa13 242 logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
243 exit;
244 }
3499f99f 245 while (!feof($file)) {
246 $sig .= fgets($file, 1024);
247 }
248 fclose($file);
249 }
250 return $sig;
251}