prefs.php 4.0 KB

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