file_prefs.php 7.5 KB

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