5 ** This contains functions for manipulating user preferences
10 global $prefs_are_cached, $prefs_cache;
11 if (!session_is_registered('prefs_are_cached')) {
12 $prefs_are_cached = false;
13 $prefs_cache = array();
16 function cachePrefValues($data_dir, $username) {
17 global $prefs_are_cached, $prefs_cache;
19 if ($prefs_are_cached)
22 $filename = $data_dir . $username . '.pref';
24 if (!file_exists($filename)) {
25 printf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename);
29 $file = fopen($filename, 'r');
31 /** read in all the preferences **/
33 while (! feof($file)) {
34 $pref = trim(fgets($file, 1024));
35 $equalsAt = strpos($pref, '=');
37 $Key = substr($pref, 0, $equalsAt);
38 $Value = substr($pref, $equalsAt +
1);
39 if (substr($Key, 0, 9) == 'highlight') {
40 $Key = 'highlight' . $highlight_num;
45 $prefs_cache[$Key] = $Value;
51 session_unregister('prefs_cache');
52 session_register('prefs_cache');
54 $prefs_are_cached = true;
55 session_unregister('prefs_are_cached');
56 session_register('prefs_are_cached');
60 /** returns the value for $string **/
61 function getPref($data_dir, $username, $string, $default = '') {
64 cachePrefValues($data_dir, $username);
66 if (isset($prefs_cache[$string]))
67 return $prefs_cache[$string];
73 function savePrefValues($data_dir, $username) {
76 $file = fopen($data_dir . $username . '.pref', 'w');
77 foreach ($prefs_cache as $Key => $Value) {
79 fwrite($file, $Key . '=' . $Value . "\n");
86 function removePref($data_dir, $username, $string) {
89 cachePrefValues($data_dir, $username);
91 if (isset($prefs_cache[$string])) {
92 unset($prefs_cache[$string]);
95 savePrefValues($data_dir, $username);
98 /** sets the pref, $string, to $set_to **/
99 function setPref($data_dir, $username, $string, $set_to) {
102 cachePrefValues($data_dir, $username);
103 if (isset($prefs_cache[$string]) && $prefs_cache[$string] == $set_to)
105 if ($set_to === '') {
106 removePref($data_dir, $username, $string);
109 $prefs_cache[$string] = $set_to;
110 savePrefValues($data_dir, $username);
114 /** This checks if there is a pref file, if there isn't, it will
116 function checkForPrefs($data_dir, $username) {
117 $filename = $data_dir . $username . '.pref';
118 if (!file_exists($filename)) {
119 if (!copy($data_dir . 'default_pref', $filename)) {
120 echo _("Error opening ") . $filename;
127 /** Writes the Signature **/
128 function setSig($data_dir, $username, $string) {
129 $file = fopen($data_dir . $username . '.sig', 'w');
130 fwrite($file, $string);
136 /** Gets the signature **/
137 function getSig($data_dir, $username) {
138 $filename = $data_dir . $username . '.sig';
140 if (file_exists($filename)) {
141 $file = fopen($filename, 'r');
142 while (!feof($file)) {
143 $sig .= fgets($file, 1024);