file_prefs.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /**
  3. * file_prefs.php
  4. *
  5. * Copyright (c) 1999-2005 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. /* If it does not exist, check for default_prefs */
  160. /* First, check legacy locations: data dir */
  161. if(substr($data_dir,-1) != '/') {
  162. $data_dir .= '/';
  163. }
  164. $default_pref = $data_dir . 'default_pref';
  165. /* or legacy location: internal data dir */
  166. if (!@file_exists($default_pref)) {
  167. $default_pref = SM_PATH . 'data/default_pref';
  168. }
  169. /* If no legacies, check where we'd expect it to be located:
  170. * under config/ */
  171. if (!@file_exists($default_pref)) {
  172. $default_pref = SM_PATH . 'config/default_pref';
  173. }
  174. /* If a default_pref file found, try to copy it, if none found,
  175. * try to create an empty one. If that fails, report an error.
  176. */
  177. if (
  178. ( is_readable($default_pref) && !@copy($default_pref, $filename) ) ||
  179. !@touch($filename)
  180. ) {
  181. $uid = 'httpd';
  182. if (function_exists('posix_getuid')){
  183. $user_data = posix_getpwuid(posix_getuid());
  184. $uid = $user_data['name'];
  185. }
  186. $errTitle = _("Could not create initial preference file!");
  187. $errString = $errTitle . "<br />\n" .
  188. sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) . "<br />\n" .
  189. _("Please contact your system administrator and report this error.") . "<br />\n";
  190. logout_error( $errString, $errTitle );
  191. exit;
  192. }
  193. }
  194. }
  195. /**
  196. * Write the User Signature.
  197. */
  198. function setSig($data_dir, $username, $number, $value) {
  199. // Limit signature size to 64KB (database BLOB limit)
  200. if (strlen($value)>65536) {
  201. error_option_save(_("Signature is too big."));
  202. return;
  203. }
  204. $filename = getHashedFile($username, $data_dir, "$username.si$number");
  205. /* Open the file for writing, or else display an error to the user. */
  206. if(!$file = @fopen("$filename.tmp", 'w')) {
  207. logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
  208. exit;
  209. }
  210. if ( sq_fwrite($file, $value) === FALSE ) {
  211. logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
  212. exit;
  213. }
  214. fclose($file);
  215. if (! @copy($filename . '.tmp',$filename) ) {
  216. 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') );
  217. exit;
  218. }
  219. @unlink($filename . '.tmp');
  220. @chmod($filename, 0600);
  221. }
  222. /**
  223. * Get the signature.
  224. */
  225. function getSig($data_dir, $username, $number) {
  226. $filename = getHashedFile($username, $data_dir, "$username.si$number");
  227. $sig = '';
  228. if (file_exists($filename)) {
  229. /* Open the file, or else display an error to the user. */
  230. if(!$file = @fopen($filename, 'r'))
  231. {
  232. logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
  233. exit;
  234. }
  235. while (!feof($file)) {
  236. $sig .= fgets($file, 1024);
  237. }
  238. fclose($file);
  239. }
  240. return $sig;
  241. }
  242. // vim: et ts=4
  243. ?>