prefs.php 4.0 KB

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