prefs.php 4.0 KB

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