prefs.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. for ($i=0; $i < count($hlt); $i++) {
  58. fwrite($file, "highlight$i=$hlt[$i]");
  59. }
  60. fclose($file);
  61. }
  62. /** sets the pref, $string, to $set_to **/
  63. function setPref($data_dir, $username, $string, $set_to) {
  64. $filename = "$data_dir$username.pref";
  65. $found = false;
  66. if (!file_exists($filename)) {
  67. printf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename);
  68. echo "\n<br>\n";
  69. exit;
  70. }
  71. $file = fopen($filename, "r");
  72. /** read in all the preferences **/
  73. for ($i=0; !feof($file); $i++) {
  74. $pref[$i] = fgets($file, 1024);
  75. if (substr($pref[$i], 0, strpos($pref[$i], "=")) == $string) {
  76. $found = true;
  77. $pos = $i;
  78. }
  79. }
  80. fclose($file);
  81. $file = fopen($filename, "w");
  82. if ($found == true) {
  83. for ($i=0; $i < count($pref); $i++) {
  84. if ($i == $pos) {
  85. fwrite($file, "$string=$set_to\n", 1024);
  86. } else {
  87. fwrite($file, "$pref[$i]", 1024);
  88. }
  89. }
  90. } else {
  91. for ($i=0; $i < count($pref); $i++) {
  92. fwrite($file, "$pref[$i]", 1024);
  93. }
  94. fwrite($file, "$string=$set_to\n", 1024);
  95. }
  96. fclose($file);
  97. }
  98. /** This checks if there is a pref file, if there isn't, it will
  99. create it. **/
  100. function checkForPrefs($data_dir, $username) {
  101. $filename = "$data_dir$username.pref";
  102. if (!file_exists($filename)) {
  103. if (!copy("$data_dir" . "default_pref", $filename)) {
  104. echo _("Error opening ") ."$filename";
  105. exit;
  106. }
  107. }
  108. }
  109. /** Writes the Signature **/
  110. function setSig($data_dir, $username, $string) {
  111. $filename = "$data_dir$username.sig";
  112. $file = fopen($filename, "w");
  113. fwrite($file, $string);
  114. fclose($file);
  115. }
  116. /** Gets the signature **/
  117. function getSig($data_dir, $username) {
  118. $filename = "$data_dir$username.sig";
  119. if (file_exists($filename)) {
  120. $file = fopen($filename, "r");
  121. $sig = "";
  122. while (!feof($file)) {
  123. $sig .= fgets($file, 1024);
  124. }
  125. fclose($file);
  126. }
  127. return $sig;
  128. }
  129. ?>