file_prefs.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * file_prefs.php
  4. *
  5. * Copyright (c) 1999-2002 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This contains functions for manipulating user preferences in files
  9. *
  10. * $Id$
  11. */
  12. global $prefs_are_cached, $prefs_cache;
  13. /**
  14. * Check the preferences into the session cache.
  15. */
  16. function cachePrefValues($data_dir, $username) {
  17. global $prefs_are_cached, $prefs_cache;
  18. if ( isset($prefs_are_cached) && $prefs_are_cached) {
  19. return;
  20. }
  21. session_unregister('prefs_cache');
  22. session_unregister('prefs_are_cached');
  23. /* Calculate the filename for the user's preference file */
  24. $filename = getHashedFile($username, $data_dir, "$username.pref");
  25. /* A call to checkForPrefs here should take eliminate the need for */
  26. /* this to be called throughout the rest of the SquirrelMail code. */
  27. checkForPrefs($data_dir, $username, $filename);
  28. /* Make sure that the preference file now DOES exist. */
  29. if (!file_exists($filename)) {
  30. echo sprintf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) . "<br>\n";
  31. exit;
  32. }
  33. $file = fopen($filename, 'r');
  34. /* Read in the preferences. */
  35. $highlight_num = 0;
  36. while (! feof($file)) {
  37. $pref = trim(fgets($file, 1024));
  38. $equalsAt = strpos($pref, '=');
  39. if ($equalsAt > 0) {
  40. $key = substr($pref, 0, $equalsAt);
  41. $value = substr($pref, $equalsAt + 1);
  42. if (substr($key, 0, 9) == 'highlight') {
  43. $key = 'highlight' . $highlight_num;
  44. $highlight_num ++;
  45. }
  46. if ($value != '') {
  47. $prefs_cache[$key] = $value;
  48. }
  49. }
  50. }
  51. fclose($file);
  52. $prefs_are_cached = TRUE;
  53. session_register('prefs_cache');
  54. session_register('prefs_are_cached');
  55. }
  56. /**
  57. * Return the value for the prefernce given by $string.
  58. */
  59. function getPref($data_dir, $username, $string, $default = '') {
  60. global $prefs_cache;
  61. $result = '';
  62. cachePrefValues($data_dir, $username);
  63. if (isset($prefs_cache[$string])) {
  64. $result = $prefs_cache[$string];
  65. } else {
  66. $result = $default;
  67. }
  68. return ($result);
  69. }
  70. /**
  71. * Save the preferences for this user.
  72. */
  73. function savePrefValues($data_dir, $username) {
  74. global $prefs_cache;
  75. $filename = getHashedFile($username, $data_dir, "$username.pref");
  76. $file = fopen($filename, 'w');
  77. foreach ($prefs_cache as $Key => $Value) {
  78. if (isset($Value)) {
  79. fwrite($file, $Key . '=' . $Value . "\n");
  80. }
  81. }
  82. fclose($file);
  83. chmod($filename, 0600);
  84. }
  85. /**
  86. * Remove a preference for the current user.
  87. */
  88. function removePref($data_dir, $username, $string) {
  89. global $prefs_cache;
  90. cachePrefValues($data_dir, $username);
  91. if (isset($prefs_cache[$string])) {
  92. unset($prefs_cache[$string]);
  93. }
  94. savePrefValues($data_dir, $username);
  95. }
  96. /**
  97. * Set a there preference $string to $value.
  98. */
  99. function setPref($data_dir, $username, $string, $value) {
  100. global $prefs_cache;
  101. cachePrefValues($data_dir, $username);
  102. if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
  103. return;
  104. }
  105. if ($value === '') {
  106. removePref($data_dir, $username, $string);
  107. return;
  108. }
  109. $prefs_cache[$string] = $value;
  110. savePrefValues($data_dir, $username);
  111. }
  112. /**
  113. * Check for a preferences file. If one can not be found, create it.
  114. */
  115. function checkForPrefs($data_dir, $username, $filename = '') {
  116. /* First, make sure we have the filename. */
  117. if ($filename == '') {
  118. $filename = getHashedFile($username, $data_dir, "$username.pref");
  119. }
  120. /* Then, check if the file exists. */
  121. if (!@file_exists($filename) ) {
  122. /* First, check the $data_dir for the default preference file. */
  123. $default_pref = $data_dir . 'default_pref';
  124. /* If it is not there, check the internal data directory. */
  125. if (!@file_exists($default_pref)) {
  126. $default_pref = '../data/default_pref';
  127. }
  128. /* Otherwise, report an error. */
  129. if (!file_exists($default_pref)) {
  130. echo _("Error opening ") . $default_pref . "<br>\n";
  131. echo _("Default preference file not found!") . "<br>\n";
  132. echo _("Please contact your system administrator and report this error.") . "<br>\n";
  133. exit;
  134. } else if (!@copy($default_pref, $filename)) {
  135. echo _("Error opening ") . $default_pref . '<br>';
  136. echo _("Could not create initial preference file!") . "<br>\n";
  137. echo _("Please contact your system administrator and report this error.") . "<br>\n";
  138. exit;
  139. }
  140. }
  141. }
  142. /**
  143. * Write the User Signature.
  144. */
  145. function setSig($data_dir, $username, $number, $value) {
  146. $filename = getHashedFile($username, $data_dir, "$username.si$number");
  147. $file = fopen($filename, 'w');
  148. fwrite($file, $value);
  149. fclose($file);
  150. }
  151. /**
  152. * Get the signature.
  153. */
  154. function getSig($data_dir, $username, $number) {
  155. #$filename = $data_dir . $username . '.si$number';
  156. $filename = getHashedFile($username, $data_dir, "$username.si$number");
  157. $sig = '';
  158. if (file_exists($filename)) {
  159. $file = fopen($filename, 'r');
  160. while (!feof($file)) {
  161. $sig .= fgets($file, 1024);
  162. }
  163. fclose($file);
  164. }
  165. return $sig;
  166. }