prefs.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?
  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. /** sets the pref, $string, to $set_to **/
  28. function setPref($data_dir, $username, $string, $set_to) {
  29. $filename = "$data_dir$username.pref";
  30. $found = false;
  31. if (!file_exists($filename)) {
  32. echo _("Preference file, ") . "\"$filename\"" . _(", does not exist. Log out, and log back in to create a default preference file. ") ."<BR>";
  33. exit;
  34. }
  35. $file = fopen($filename, "r");
  36. /** read in all the preferences **/
  37. for ($i=0; !feof($file); $i++) {
  38. $pref[$i] = fgets($file, 1024);
  39. if (substr($pref[$i], 0, strpos($pref[$i], "=")) == $string) {
  40. $found = true;
  41. $pos = $i;
  42. }
  43. }
  44. fclose($file);
  45. $file = fopen($filename, "w");
  46. if ($found == true) {
  47. for ($i=0; $i < count($pref); $i++) {
  48. if ($i == $pos) {
  49. fwrite($file, "$string=$set_to\n", 1024);
  50. } else {
  51. fwrite($file, "$pref[$i]", 1024);
  52. }
  53. }
  54. } else {
  55. for ($i=0; $i < count($pref); $i++) {
  56. fwrite($file, "$pref[$i]", 1024);
  57. }
  58. fwrite($file, "$string=$set_to\n", 1024);
  59. }
  60. fclose($file);
  61. }
  62. /** This checks if there is a pref file, if there isn't, it will
  63. create it. **/
  64. function checkForPrefs($data_dir, $username) {
  65. $filename = "$data_dir$username.pref";
  66. if (!file_exists($filename)) {
  67. if (!copy("$data_dir" . "default_pref", $filename)) {
  68. echo _("Error opening ") ."$filename";
  69. exit;
  70. }
  71. }
  72. }
  73. /** Writes the Signature **/
  74. function setSig($data_dir, $username, $string) {
  75. $filename = "$data_dir$username.sig";
  76. $file = fopen($filename, "w");
  77. fwrite($file, $string);
  78. fclose($file);
  79. }
  80. /** Gets the signature **/
  81. function getSig($data_dir, $username) {
  82. $filename = "$data_dir$username.sig";
  83. if (file_exists($filename)) {
  84. $file = fopen($filename, "r");
  85. while (!feof($file)) {
  86. $sig .= fgets($file, 1024);
  87. }
  88. fclose($file);
  89. } else {
  90. echo _("Signature file not found.");
  91. exit;
  92. }
  93. return $sig;
  94. }
  95. ?>