Tyler: This is not very original but should do the trick
[squirrelmail.git] / functions / prefs.php
CommitLineData
59177427 1<?php
2ba13803 2
65b14f90 3 /**
7350889b 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 */
65b14f90 13
f435778e 14 global $prefs_are_cached, $prefs_cache;
374b7e37 15 if (!session_is_registered('prefs_are_cached')) {
16 $prefs_are_cached = false;
17 $prefs_cache = array();
18 }
31ac7db8 19
374b7e37 20 function cachePrefValues($data_dir, $username) {
31ac7db8 21 global $prefs_are_cached, $prefs_cache;
22
23 if ($prefs_are_cached)
24 return;
25
26 $filename = $data_dir . $username . '.pref';
27
28 if (!file_exists($filename)) {
374b7e37 29 printf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename);
31ac7db8 30 exit;
31 }
32
63123e7c 33 $file = fopen($filename, 'r');
31ac7db8 34
35 /** read in all the preferences **/
36 $highlight_num = 0;
37 while (! feof($file)) {
38 $pref = trim(fgets($file, 1024));
374b7e37 39 $equalsAt = strpos($pref, '=');
40 if ($equalsAt > 0) {
41 $Key = substr($pref, 0, $equalsAt);
42 $Value = substr($pref, $equalsAt + 1);
43 if (substr($Key, 0, 9) == 'highlight') {
44 $Key = 'highlight' . $highlight_num;
45 $highlight_num ++;
31ac7db8 46 }
47
374b7e37 48 if ($Value != '') {
49 $prefs_cache[$Key] = $Value;
50 }
51 }
31ac7db8 52 }
53 fclose($file);
374b7e37 54
55 session_unregister('prefs_cache');
56 session_register('prefs_cache');
31ac7db8 57
58 $prefs_are_cached = true;
374b7e37 59 session_unregister('prefs_are_cached');
60 session_register('prefs_are_cached');
31ac7db8 61 }
62
63
65b14f90 64 /** returns the value for $string **/
f1e6f580 65 function getPref($data_dir, $username, $string, $default = '') {
31ac7db8 66 global $prefs_cache;
f1e6f580 67
31ac7db8 68 cachePrefValues($data_dir, $username);
f1e6f580 69
31ac7db8 70 if (isset($prefs_cache[$string]))
71 return $prefs_cache[$string];
f1e6f580 72 else
71257f8b 73 return $default;
65b14f90 74 }
75
44f642f5 76
374b7e37 77 function savePrefValues($data_dir, $username) {
31ac7db8 78 global $prefs_cache;
79
63123e7c 80 $file = fopen($data_dir . $username . '.pref', 'w');
374b7e37 81 foreach ($prefs_cache as $Key => $Value) {
82 if (isset($Value)) {
31ac7db8 83 fwrite($file, $Key . '=' . $Value . "\n");
374b7e37 84 }
44f642f5 85 }
86 fclose($file);
31ac7db8 87 }
44f642f5 88
44f642f5 89
31ac7db8 90 function removePref($data_dir, $username, $string) {
91 global $prefs_cache;
92
93 cachePrefValues($data_dir, $username);
94
374b7e37 95 if (isset($prefs_cache[$string])) {
31ac7db8 96 unset($prefs_cache[$string]);
374b7e37 97 }
98
31ac7db8 99 savePrefValues($data_dir, $username);
44f642f5 100 }
101
65b14f90 102 /** sets the pref, $string, to $set_to **/
d0747e26 103 function setPref($data_dir, $username, $string, $set_to) {
31ac7db8 104 global $prefs_cache;
105
106 cachePrefValues($data_dir, $username);
bf1d3803 107 if (isset($prefs_cache[$string]) && $prefs_cache[$string] == $set_to)
fa42b484 108 return;
ed9f3a67 109 if ($set_to === '') {
fa42b484 110 removePref($data_dir, $username, $string);
111 return;
112 }
31ac7db8 113 $prefs_cache[$string] = $set_to;
31ac7db8 114 savePrefValues($data_dir, $username);
65b14f90 115 }
116
f804972b 117
d068c0ec 118 /** This checks if there is a pref file, if there isn't, it will
119 create it. **/
d0747e26 120 function checkForPrefs($data_dir, $username) {
63123e7c 121 $filename = $data_dir . $username . '.pref';
65b14f90 122 if (!file_exists($filename)) {
63123e7c 123 if (!copy($data_dir . 'default_pref', $filename)) {
124 echo _("Error opening ") . $filename;
65b14f90 125 exit;
126 }
127 }
65b14f90 128 }
f804972b 129
130
f804972b 131 /** Writes the Signature **/
132 function setSig($data_dir, $username, $string) {
63123e7c 133 $file = fopen($data_dir . $username . '.sig', 'w');
f804972b 134 fwrite($file, $string);
135 fclose($file);
136 }
137
138
139
140 /** Gets the signature **/
141 function getSig($data_dir, $username) {
63123e7c 142 $filename = $data_dir . $username . '.sig';
143 $sig = '';
f804972b 144 if (file_exists($filename)) {
63123e7c 145 $file = fopen($filename, 'r');
f804972b 146 while (!feof($file)) {
147 $sig .= fgets($file, 1024);
148 }
149 fclose($file);
f804972b 150 }
151 return $sig;
152 }
cbe5423b 153?>