file_prefs.php 7.3 KB

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