file_prefs.php 8.6 KB

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