file_prefs.php 7.3 KB

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