file_prefs.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. include_once( '../functions/display_messages.php' );
  31. logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
  32. exit;
  33. }
  34. $file = fopen($filename, 'r');
  35. /* Read in the preferences. */
  36. $highlight_num = 0;
  37. while (! feof($file)) {
  38. $pref = trim(fgets($file, 1024));
  39. $equalsAt = strpos($pref, '=');
  40. if ($equalsAt > 0) {
  41. $key = substr($pref, 0, $equalsAt);
  42. $value = substr($pref, $equalsAt + 1);
  43. if (substr($key, 0, 9) == 'highlight') {
  44. $key = 'highlight' . $highlight_num;
  45. $highlight_num ++;
  46. }
  47. if ($value != '') {
  48. $prefs_cache[$key] = $value;
  49. }
  50. }
  51. }
  52. fclose($file);
  53. $prefs_are_cached = TRUE;
  54. session_register('prefs_cache');
  55. session_register('prefs_are_cached');
  56. }
  57. /**
  58. * Return the value for the prefernce given by $string.
  59. */
  60. function getPref($data_dir, $username, $string, $default = '') {
  61. global $prefs_cache;
  62. $result = '';
  63. cachePrefValues($data_dir, $username);
  64. if (isset($prefs_cache[$string])) {
  65. $result = $prefs_cache[$string];
  66. } else {
  67. $result = $default;
  68. }
  69. return ($result);
  70. }
  71. /**
  72. * Save the preferences for this user.
  73. */
  74. function savePrefValues($data_dir, $username) {
  75. global $prefs_cache;
  76. $filename = getHashedFile($username, $data_dir, "$username.pref");
  77. $file = fopen($filename, 'w');
  78. foreach ($prefs_cache as $Key => $Value) {
  79. if (isset($Value)) {
  80. fwrite($file, $Key . '=' . $Value . "\n");
  81. }
  82. }
  83. fclose($file);
  84. chmod($filename, 0600);
  85. }
  86. /**
  87. * Remove a preference for the current user.
  88. */
  89. function removePref($data_dir, $username, $string) {
  90. global $prefs_cache;
  91. cachePrefValues($data_dir, $username);
  92. if (isset($prefs_cache[$string])) {
  93. unset($prefs_cache[$string]);
  94. }
  95. savePrefValues($data_dir, $username);
  96. }
  97. /**
  98. * Set a there preference $string to $value.
  99. */
  100. function setPref($data_dir, $username, $string, $value) {
  101. global $prefs_cache;
  102. cachePrefValues($data_dir, $username);
  103. if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
  104. return;
  105. }
  106. if ($value === '') {
  107. removePref($data_dir, $username, $string);
  108. return;
  109. }
  110. $prefs_cache[$string] = $value;
  111. savePrefValues($data_dir, $username);
  112. }
  113. /**
  114. * Check for a preferences file. If one can not be found, create it.
  115. */
  116. function checkForPrefs($data_dir, $username, $filename = '') {
  117. /* First, make sure we have the filename. */
  118. if ($filename == '') {
  119. $filename = getHashedFile($username, $data_dir, "$username.pref");
  120. }
  121. /* Then, check if the file exists. */
  122. if (!@file_exists($filename) ) {
  123. /* First, check the $data_dir for the default preference file. */
  124. $default_pref = $data_dir . 'default_pref';
  125. /* If it is not there, check the internal data directory. */
  126. if (!@file_exists($default_pref)) {
  127. $default_pref = '../data/default_pref';
  128. }
  129. /* Otherwise, report an error. */
  130. $errTitle = sprintf( _("Error opening %s"), $default_pref );
  131. if (!file_exists($default_pref)) {
  132. $errString = $errTitle . "<br>\n" .
  133. _("Default preference file not found!") . "<br>\n" .
  134. _("Please contact your system administrator and report this error.") . "<br>\n";
  135. include_once( '../functions/display_messages.php' );
  136. logout_error( $errString, $errTitle );
  137. exit;
  138. } else if (!@copy($default_pref, $filename)) {
  139. $user_data = posix_getpwuid(posix_getuid());
  140. $uid = $user_data['name'];
  141. $errString = $errTitle . '<br>' .
  142. _("Could not create initial preference file!") . "<br>\n" .
  143. sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) .
  144. "<br>\n" . _("Please contact your system administrator and report this error.") . "<br>\n";
  145. include_once( '../functions/display_messages.php' );
  146. logout_error( $errString, $errTitle );
  147. exit;
  148. }
  149. }
  150. }
  151. /**
  152. * Write the User Signature.
  153. */
  154. function setSig($data_dir, $username, $number, $value) {
  155. $filename = getHashedFile($username, $data_dir, "$username.si$number");
  156. $file = fopen($filename, 'w');
  157. fwrite($file, $value);
  158. fclose($file);
  159. }
  160. /**
  161. * Get the signature.
  162. */
  163. function getSig($data_dir, $username, $number) {
  164. #$filename = $data_dir . $username . '.si$number';
  165. $filename = getHashedFile($username, $data_dir, "$username.si$number");
  166. $sig = '';
  167. if (file_exists($filename)) {
  168. $file = fopen($filename, 'r');
  169. while (!feof($file)) {
  170. $sig .= fgets($file, 1024);
  171. }
  172. fclose($file);
  173. }
  174. return $sig;
  175. }