file_prefs.php 8.8 KB

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