file_prefs.php 8.6 KB

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