| 1 | <?php |
| 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 | function removePref($data_dir, $username, $string) { |
| 33 | $filename = "$data_dir$username.pref"; |
| 34 | $found = false; |
| 35 | if (!file_exists($filename)) { |
| 36 | echo _("Preference file, ") . "\"$filename\"" . _(", does not exist. Log out, and log back in to create a default preference file. ") ."<BR>"; |
| 37 | exit; |
| 38 | } |
| 39 | $file = fopen($filename, "r"); |
| 40 | |
| 41 | for ($i=0; !feof($file); $i++) { |
| 42 | $pref[$i] = fgets($file, 1024); |
| 43 | if (substr($pref[$i], 0, strpos($pref[$i], "=")) == $string) { |
| 44 | $i--; |
| 45 | } |
| 46 | } |
| 47 | fclose($file); |
| 48 | |
| 49 | for ($i=0,$j=0; $i < count($pref); $i++) { |
| 50 | if (substr($pref[$i], 0, 9) == "highlight") { |
| 51 | $hlt[$j] = substr($pref[$i], strpos($pref[$i], "=")+1); |
| 52 | $j++; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | $file = fopen($filename, "w"); |
| 57 | for ($i=0; $i < count($pref); $i++) { |
| 58 | if (substr($pref[$i], 0, 9) != "highlight") { |
| 59 | fwrite($file, "$pref[$i]", 1024); |
| 60 | } |
| 61 | } |
| 62 | for ($i=0; $i < count($hlt); $i++) { |
| 63 | fwrite($file, "highlight$i=$hlt[$i]"); |
| 64 | } |
| 65 | |
| 66 | fclose($file); |
| 67 | } |
| 68 | |
| 69 | /** sets the pref, $string, to $set_to **/ |
| 70 | function setPref($data_dir, $username, $string, $set_to) { |
| 71 | $filename = "$data_dir$username.pref"; |
| 72 | $found = false; |
| 73 | if (!file_exists($filename)) { |
| 74 | echo _("Preference file, ") . "\"$filename\"" . _(", does not exist. Log out, and log back in to create a default preference file. ") ."<BR>"; |
| 75 | exit; |
| 76 | } |
| 77 | $file = fopen($filename, "r"); |
| 78 | |
| 79 | /** read in all the preferences **/ |
| 80 | for ($i=0; !feof($file); $i++) { |
| 81 | $pref[$i] = fgets($file, 1024); |
| 82 | if (substr($pref[$i], 0, strpos($pref[$i], "=")) == $string) { |
| 83 | $found = true; |
| 84 | $pos = $i; |
| 85 | } |
| 86 | } |
| 87 | fclose($file); |
| 88 | |
| 89 | $file = fopen($filename, "w"); |
| 90 | if ($found == true) { |
| 91 | for ($i=0; $i < count($pref); $i++) { |
| 92 | if ($i == $pos) { |
| 93 | fwrite($file, "$string=$set_to\n", 1024); |
| 94 | } else { |
| 95 | fwrite($file, "$pref[$i]", 1024); |
| 96 | } |
| 97 | } |
| 98 | } else { |
| 99 | for ($i=0; $i < count($pref); $i++) { |
| 100 | fwrite($file, "$pref[$i]", 1024); |
| 101 | } |
| 102 | fwrite($file, "$string=$set_to\n", 1024); |
| 103 | } |
| 104 | |
| 105 | fclose($file); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | /** This checks if there is a pref file, if there isn't, it will |
| 112 | create it. **/ |
| 113 | function checkForPrefs($data_dir, $username) { |
| 114 | $filename = "$data_dir$username.pref"; |
| 115 | if (!file_exists($filename)) { |
| 116 | if (!copy("$data_dir" . "default_pref", $filename)) { |
| 117 | echo _("Error opening ") ."$filename"; |
| 118 | exit; |
| 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | |
| 125 | /** Writes the Signature **/ |
| 126 | function setSig($data_dir, $username, $string) { |
| 127 | $filename = "$data_dir$username.sig"; |
| 128 | $file = fopen($filename, "w"); |
| 129 | fwrite($file, $string); |
| 130 | fclose($file); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | |
| 135 | /** Gets the signature **/ |
| 136 | function getSig($data_dir, $username) { |
| 137 | $filename = "$data_dir$username.sig"; |
| 138 | if (file_exists($filename)) { |
| 139 | $file = fopen($filename, "r"); |
| 140 | while (!feof($file)) { |
| 141 | $sig .= fgets($file, 1024); |
| 142 | } |
| 143 | fclose($file); |
| 144 | } |
| 145 | return $sig; |
| 146 | } |
| 147 | ?> |