5 ** This contains functions for manipulating user preferences
10 if (defined('prefs_php'))
12 define('prefs_php', true);
14 global $prefs_are_cached, $prefs_cache;
15 if (!session_is_registered('prefs_are_cached')) {
16 $prefs_are_cached = false;
17 $prefs_cache = array();
20 function cachePrefValues($data_dir, $username) {
21 global $prefs_are_cached, $prefs_cache;
23 if ($prefs_are_cached)
26 $filename = $data_dir . $username . '.pref';
28 if (!file_exists($filename)) {
29 printf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename);
33 $file = fopen($filename, 'r');
35 /** read in all the preferences **/
37 while (! feof($file)) {
38 $pref = trim(fgets($file, 1024));
39 $equalsAt = strpos($pref, '=');
41 $Key = substr($pref, 0, $equalsAt);
42 $Value = substr($pref, $equalsAt +
1);
43 if (substr($Key, 0, 9) == 'highlight') {
44 $Key = 'highlight' . $highlight_num;
49 $prefs_cache[$Key] = $Value;
55 session_unregister('prefs_cache');
56 session_register('prefs_cache');
58 $prefs_are_cached = true;
59 session_unregister('prefs_are_cached');
60 session_register('prefs_are_cached');
64 /** returns the value for $string **/
65 function getPref($data_dir, $username, $string, $default = '') {
68 cachePrefValues($data_dir, $username);
70 if (isset($prefs_cache[$string]))
71 return $prefs_cache[$string];
77 function savePrefValues($data_dir, $username) {
80 $file = fopen($data_dir . $username . '.pref', 'w');
81 foreach ($prefs_cache as $Key => $Value) {
83 fwrite($file, $Key . '=' . $Value . "\n");
90 function removePref($data_dir, $username, $string) {
93 cachePrefValues($data_dir, $username);
95 if (isset($prefs_cache[$string])) {
96 unset($prefs_cache[$string]);
99 savePrefValues($data_dir, $username);
102 /** sets the pref, $string, to $set_to **/
103 function setPref($data_dir, $username, $string, $set_to) {
106 cachePrefValues($data_dir, $username);
107 if (isset($prefs_cache[$string]) && $prefs_cache[$string] == $set_to)
109 if ($set_to === '') {
110 removePref($data_dir, $username, $string);
113 $prefs_cache[$string] = $set_to;
114 savePrefValues($data_dir, $username);
118 /** This checks if there is a pref file, if there isn't, it will
120 function checkForPrefs($data_dir, $username) {
121 $filename = $data_dir . $username . '.pref';
122 if (!file_exists($filename)) {
123 if (!copy($data_dir . 'default_pref', $filename)) {
124 echo _("Error opening ") . $filename;
131 /** Writes the Signature **/
132 function setSig($data_dir, $username, $string) {
133 $file = fopen($data_dir . $username . '.sig', 'w');
134 fwrite($file, $string);
140 /** Gets the signature **/
141 function getSig($data_dir, $username) {
142 $filename = $data_dir . $username . '.sig';
144 if (file_exists($filename)) {
145 $file = fopen($filename, 'r');
146 while (!feof($file)) {
147 $sig .= fgets($file, 1024);