| 1 | <? |
| 2 | /** |
| 3 | ** prefs.php |
| 4 | ** |
| 5 | ** This contains functions for manipulating user preferences |
| 6 | **/ |
| 7 | |
| 8 | $prefs_php = true; |
| 9 | |
| 10 | /** returns the value for $string **/ |
| 11 | function getPref($data_dir, $username, $string) { |
| 12 | $filename = "$data_dir$username.pref"; |
| 13 | if (!file_exists($filename)) { |
| 14 | echo _("Preference file ") . "\"$filename\"" . _(" not found. Exiting abnormally"); |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | $file = fopen($filename, "r"); |
| 19 | |
| 20 | /** read in all the preferences **/ |
| 21 | for ($i=0; !feof($file); $i++) { |
| 22 | $pref = fgets($file, 1024); |
| 23 | if (substr($pref, 0, strpos($pref, "=")) == $string) { |
| 24 | fclose($file); |
| 25 | return trim(substr($pref, strpos($pref, "=")+1)); |
| 26 | } |
| 27 | } |
| 28 | fclose($file); |
| 29 | return ""; |
| 30 | } |
| 31 | |
| 32 | /** sets the pref, $string, to $set_to **/ |
| 33 | function setPref($data_dir, $username, $string, $set_to) { |
| 34 | $filename = "$data_dir$username.pref"; |
| 35 | $found = false; |
| 36 | if (!file_exists($filename)) { |
| 37 | echo _("Preference file, ") . "\"$filename\"" . _(", does not exist. Log out, and log back in to create a default preference file. ") ."<BR>"; |
| 38 | exit; |
| 39 | } |
| 40 | $file = fopen($filename, "r"); |
| 41 | |
| 42 | /** read in all the preferences **/ |
| 43 | for ($i=0; !feof($file); $i++) { |
| 44 | $pref[$i] = fgets($file, 1024); |
| 45 | if (substr($pref[$i], 0, strpos($pref[$i], "=")) == $string) { |
| 46 | $found = true; |
| 47 | $pos = $i; |
| 48 | } |
| 49 | } |
| 50 | fclose($file); |
| 51 | |
| 52 | $file = fopen($filename, "w"); |
| 53 | if ($found == true) { |
| 54 | for ($i=0; $i < count($pref); $i++) { |
| 55 | if ($i == $pos) { |
| 56 | fwrite($file, "$string=$set_to\n", 1024); |
| 57 | } else { |
| 58 | fwrite($file, "$pref[$i]", 1024); |
| 59 | } |
| 60 | } |
| 61 | } else { |
| 62 | for ($i=0; $i < count($pref); $i++) { |
| 63 | fwrite($file, "$pref[$i]", 1024); |
| 64 | } |
| 65 | fwrite($file, "$string=$set_to\n", 1024); |
| 66 | } |
| 67 | |
| 68 | fclose($file); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | /** This checks if there is a pref file, if there isn't, it will |
| 75 | create it. **/ |
| 76 | function checkForPrefs($data_dir, $username) { |
| 77 | $filename = "$data_dir$username.pref"; |
| 78 | if (!file_exists($filename)) { |
| 79 | if (!copy("$data_dir" . "default_pref", $filename)) { |
| 80 | echo _("Error opening ") ."$filename"; |
| 81 | exit; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | /** Writes the Signature **/ |
| 89 | function setSig($data_dir, $username, $string) { |
| 90 | $filename = "$data_dir$username.sig"; |
| 91 | $file = fopen($filename, "w"); |
| 92 | fwrite($file, $string); |
| 93 | fclose($file); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | |
| 98 | /** Gets the signature **/ |
| 99 | function getSig($data_dir, $username) { |
| 100 | $filename = "$data_dir$username.sig"; |
| 101 | if (file_exists($filename)) { |
| 102 | $file = fopen($filename, "r"); |
| 103 | while (!feof($file)) { |
| 104 | $sig .= fgets($file, 1024); |
| 105 | } |
| 106 | fclose($file); |
| 107 | } else { |
| 108 | echo _("Signature file not found."); |
| 109 | exit; |
| 110 | } |
| 111 | return $sig; |
| 112 | } |
| 113 | ?> |