| 1 | <?php |
| 2 | /** |
| 3 | ** prefs.php |
| 4 | ** |
| 5 | ** This contains functions for manipulating user preferences |
| 6 | ** |
| 7 | ** $Id$ |
| 8 | **/ |
| 9 | |
| 10 | if (defined('prefs_php')) |
| 11 | return; |
| 12 | define('prefs_php', true); |
| 13 | |
| 14 | global $prefs_are_cached, $prefs_cache; |
| 15 | if (!session_is_registered('prefs_are_cached')) { |
| 16 | $prefs_are_cached = false; |
| 17 | $prefs_cache = array(); |
| 18 | } |
| 19 | |
| 20 | function cachePrefValues($data_dir, $username) { |
| 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)) { |
| 29 | printf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename); |
| 30 | exit; |
| 31 | } |
| 32 | |
| 33 | $file = fopen($filename, 'r'); |
| 34 | |
| 35 | /** read in all the preferences **/ |
| 36 | $highlight_num = 0; |
| 37 | while (! feof($file)) { |
| 38 | $pref = trim(fgets($file, 1024)); |
| 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 ++; |
| 46 | } |
| 47 | |
| 48 | if ($Value != '') { |
| 49 | $prefs_cache[$Key] = $Value; |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | fclose($file); |
| 54 | |
| 55 | session_unregister('prefs_cache'); |
| 56 | session_register('prefs_cache'); |
| 57 | |
| 58 | $prefs_are_cached = true; |
| 59 | session_unregister('prefs_are_cached'); |
| 60 | session_register('prefs_are_cached'); |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /** returns the value for $string **/ |
| 65 | function getPref($data_dir, $username, $string) { |
| 66 | global $prefs_cache; |
| 67 | |
| 68 | cachePrefValues($data_dir, $username); |
| 69 | |
| 70 | if (isset($prefs_cache[$string])) |
| 71 | return $prefs_cache[$string]; |
| 72 | return ''; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | function savePrefValues($data_dir, $username) { |
| 77 | global $prefs_cache; |
| 78 | |
| 79 | $file = fopen($data_dir . $username . '.pref', 'w'); |
| 80 | foreach ($prefs_cache as $Key => $Value) { |
| 81 | if (isset($Value)) { |
| 82 | fwrite($file, $Key . '=' . $Value . "\n"); |
| 83 | } |
| 84 | } |
| 85 | fclose($file); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | function removePref($data_dir, $username, $string) { |
| 90 | global $prefs_cache; |
| 91 | |
| 92 | cachePrefValues($data_dir, $username); |
| 93 | |
| 94 | if (isset($prefs_cache[$string])) { |
| 95 | unset($prefs_cache[$string]); |
| 96 | } |
| 97 | |
| 98 | savePrefValues($data_dir, $username); |
| 99 | } |
| 100 | |
| 101 | /** sets the pref, $string, to $set_to **/ |
| 102 | function setPref($data_dir, $username, $string, $set_to) { |
| 103 | global $prefs_cache; |
| 104 | |
| 105 | cachePrefValues($data_dir, $username); |
| 106 | if (isset($prefs_cache[$string]) && $prefs_cache[$string] == $set_to) |
| 107 | return; |
| 108 | if ($set_to === '') { |
| 109 | removePref($data_dir, $username, $string); |
| 110 | return; |
| 111 | } |
| 112 | $prefs_cache[$string] = $set_to; |
| 113 | savePrefValues($data_dir, $username); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /** This checks if there is a pref file, if there isn't, it will |
| 118 | create it. **/ |
| 119 | function checkForPrefs($data_dir, $username) { |
| 120 | $filename = $data_dir . $username . '.pref'; |
| 121 | if (!file_exists($filename)) { |
| 122 | if (!copy($data_dir . 'default_pref', $filename)) { |
| 123 | echo _("Error opening ") . $filename; |
| 124 | exit; |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /** Writes the Signature **/ |
| 131 | function setSig($data_dir, $username, $string) { |
| 132 | $file = fopen($data_dir . $username . '.sig', 'w'); |
| 133 | fwrite($file, $string); |
| 134 | fclose($file); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | /** Gets the signature **/ |
| 140 | function getSig($data_dir, $username) { |
| 141 | $filename = $data_dir . $username . '.sig'; |
| 142 | $sig = ''; |
| 143 | if (file_exists($filename)) { |
| 144 | $file = fopen($filename, 'r'); |
| 145 | while (!feof($file)) { |
| 146 | $sig .= fgets($file, 1024); |
| 147 | } |
| 148 | fclose($file); |
| 149 | } |
| 150 | return $sig; |
| 151 | } |
| 152 | ?> |