prefs.php 4.0 KB

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