prefs.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?
  2. /**
  3. ** prefs.php
  4. **
  5. ** This contains functions for manipulating user preferences
  6. **/
  7. /** returns the value for $string **/
  8. function getPref($data_dir, $username, $string) {
  9. $filename = "$data_dir$username.pref";
  10. if (!file_exists($filename)) {
  11. echo "Preference file \"$filename\" not found. Exiting abnormally";
  12. exit;
  13. }
  14. $file = fopen($filename, "r");
  15. /** read in all the preferences **/
  16. for ($i=0; !feof($file); $i++) {
  17. $pref = fgets($file, 1024);
  18. if (substr($pref, 0, strpos($pref, "=")) == $string) {
  19. fclose($file);
  20. return trim(substr($pref, strpos($pref, "=")+1));
  21. }
  22. }
  23. fclose($file);
  24. return "";
  25. }
  26. /** sets the pref, $string, to $set_to **/
  27. function setPref($data_dir, $username, $string, $set_to) {
  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. /** read in all the preferences **/
  36. for ($i=0; !feof($file); $i++) {
  37. $pref[$i] = fgets($file, 1024);
  38. if (substr($pref[$i], 0, strpos($pref[$i], "=")) == $string) {
  39. $found = true;
  40. $pos = $i;
  41. }
  42. }
  43. fclose($file);
  44. $file = fopen($filename, "w");
  45. if ($found == true) {
  46. for ($i=0; $i < count($pref); $i++) {
  47. if ($i == $pos) {
  48. fwrite($file, "$string=$set_to\n", 1024);
  49. } else {
  50. fwrite($file, "$pref[$i]", 1024);
  51. }
  52. }
  53. } else {
  54. for ($i=0; $i < count($pref); $i++) {
  55. fwrite($file, "$pref[$i]", 1024);
  56. }
  57. fwrite($file, "$string=$set_to\n", 1024);
  58. }
  59. fclose($file);
  60. }
  61. /** This checks if there is a pref file, if there isn't, it will create it. **/
  62. function checkForPrefs($data_dir, $username) {
  63. $filename = "$data_dir$username.pref";
  64. if (!file_exists($filename)) {
  65. if (!copy("$data_dir" . "default_pref", $filename)) {
  66. echo "Error opening $filename";
  67. exit;
  68. }
  69. }
  70. return;
  71. }
  72. /** Writes the Signature **/
  73. function setSig($data_dir, $username, $string) {
  74. $filename = "$data_dir$username.sig";
  75. $file = fopen($filename, "w");
  76. fwrite($file, $string);
  77. fclose($file);
  78. }
  79. /** Gets the signature **/
  80. function getSig($data_dir, $username) {
  81. $filename = "$data_dir$username.sig";
  82. if (file_exists($filename)) {
  83. $file = fopen($filename, "r");
  84. while (!feof($file)) {
  85. $sig .= fgets($file, 1024);
  86. }
  87. fclose($file);
  88. } else {
  89. echo "Signature file not found.";
  90. exit;
  91. }
  92. return $sig;
  93. }
  94. ?>