file_prefs.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. * @subpackage prefs
  13. * @since 1.2.5
  14. */
  15. /** @ignore */
  16. if (! defined('SM_PATH')) define('SM_PATH','../');
  17. /** include this for error messages */
  18. include_once(SM_PATH . 'functions/display_messages.php');
  19. /**
  20. * Check the preferences into the session cache.
  21. * @param string $data_dir
  22. * @param string $username
  23. * @since 1.1.3
  24. */
  25. function cachePrefValues($data_dir, $username) {
  26. global $prefs_are_cached, $prefs_cache;
  27. sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
  28. if ( isset($prefs_are_cached) && $prefs_are_cached) {
  29. sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
  30. return;
  31. }
  32. sqsession_unregister('prefs_cache');
  33. sqsession_unregister('prefs_are_cached');
  34. /* Calculate the filename for the user's preference file */
  35. $filename = getHashedFile($username, $data_dir, "$username.pref");
  36. /* A call to checkForPrefs here should take eliminate the need for */
  37. /* this to be called throughout the rest of the SquirrelMail code. */
  38. checkForPrefs($data_dir, $username, $filename);
  39. /* Make sure that the preference file now DOES exist. */
  40. if (!file_exists($filename)) {
  41. logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
  42. exit;
  43. }
  44. /* Open the file, or else display an error to the user. */
  45. if(!$file = @fopen($filename, 'r'))
  46. {
  47. logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
  48. exit;
  49. }
  50. /* Read in the preferences. */
  51. $highlight_num = 0;
  52. while (! feof($file)) {
  53. $pref = '';
  54. /* keep reading a pref until we reach an eol (\n (or \r for macs)) */
  55. while($read = fgets($file, 1024))
  56. {
  57. $pref .= $read;
  58. if(strpos($read,"\n") || strpos($read,"\r"))
  59. break;
  60. }
  61. $pref = trim($pref);
  62. $equalsAt = strpos($pref, '=');
  63. if ($equalsAt > 0) {
  64. $key = substr($pref, 0, $equalsAt);
  65. $value = substr($pref, $equalsAt + 1);
  66. /* this is to 'rescue' old-style highlighting rules. */
  67. if (substr($key, 0, 9) == 'highlight') {
  68. $key = 'highlight' . $highlight_num;
  69. $highlight_num ++;
  70. }
  71. if ($value != '') {
  72. $prefs_cache[$key] = $value;
  73. }
  74. }
  75. }
  76. fclose($file);
  77. $prefs_are_cached = TRUE;
  78. sqsession_register($prefs_cache, 'prefs_cache');
  79. sqsession_register($prefs_are_cached, 'prefs_are_cached');
  80. }
  81. /**
  82. * Return the value for the preference given by $string.
  83. * @param string $data_dir data directory
  84. * @param string $username user name
  85. * @param string $string preference name
  86. * @param string $default (since 1.2.0) default preference value
  87. * @return mixed
  88. */
  89. function getPref($data_dir, $username, $string, $default = '') {
  90. global $prefs_cache;
  91. $result = do_hook_function('get_pref_override',array($username,$string));
  92. if (!$result) {
  93. cachePrefValues($data_dir, $username);
  94. if (isset($prefs_cache[$string])) {
  95. $result = $prefs_cache[$string];
  96. } else {
  97. $result = do_hook_function('get_pref', array($username,$string));
  98. if (!$result) {
  99. $result = $default;
  100. }
  101. }
  102. }
  103. return ($result);
  104. }
  105. /**
  106. * Save the preferences for this user.
  107. * @param string $data_dir data directory
  108. * @param string $username user name
  109. * @since 1.1.3
  110. */
  111. function savePrefValues($data_dir, $username) {
  112. global $prefs_cache;
  113. $filename = getHashedFile($username, $data_dir, "$username.pref");
  114. /* Open the file for writing, or else display an error to the user. */
  115. if(!$file = @fopen($filename.'.tmp', 'w'))
  116. {
  117. logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
  118. exit;
  119. }
  120. foreach ($prefs_cache as $Key => $Value) {
  121. if (isset($Value)) {
  122. if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) {
  123. logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
  124. exit;
  125. }
  126. }
  127. }
  128. fclose($file);
  129. if (! @copy($filename . '.tmp',$filename) ) {
  130. 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') );
  131. exit;
  132. }
  133. @unlink($filename . '.tmp');
  134. @chmod($filename, 0600);
  135. sqsession_register($prefs_cache , 'prefs_cache');
  136. }
  137. /**
  138. * Remove a preference for the current user.
  139. * @param string $data_dir data directory
  140. * @param string $username user name
  141. * @param string $string preference name
  142. */
  143. function removePref($data_dir, $username, $string) {
  144. global $prefs_cache;
  145. cachePrefValues($data_dir, $username);
  146. if (isset($prefs_cache[$string])) {
  147. unset($prefs_cache[$string]);
  148. }
  149. savePrefValues($data_dir, $username);
  150. }
  151. /**
  152. * Set a there preference $string to $value.
  153. * @param string $data_dir data directory
  154. * @param string $username user name
  155. * @param string $string preference name
  156. * @param mixed $value preference value
  157. */
  158. function setPref($data_dir, $username, $string, $value) {
  159. global $prefs_cache;
  160. cachePrefValues($data_dir, $username);
  161. if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
  162. return;
  163. }
  164. if ($value === '') {
  165. removePref($data_dir, $username, $string);
  166. return;
  167. }
  168. $prefs_cache[$string] = $value;
  169. savePrefValues($data_dir, $username);
  170. }
  171. /**
  172. * Check for a preferences file. If one can not be found, create it.
  173. * @param string $data_dir data directory
  174. * @param string $username user name
  175. * @param string $filename (since 1.2.0) preference file name.
  176. * detects file name, if set to empty string.
  177. */
  178. function checkForPrefs($data_dir, $username, $filename = '') {
  179. /* First, make sure we have the filename. */
  180. if ($filename == '') {
  181. $filename = getHashedFile($username, $data_dir, "$username.pref");
  182. }
  183. /* Then, check if the file exists. */
  184. if (!@file_exists($filename) ) {
  185. /* If it does not exist, check for default_prefs */
  186. /* First, check legacy locations: data dir */
  187. if(substr($data_dir,-1) != '/') {
  188. $data_dir .= '/';
  189. }
  190. $default_pref = $data_dir . 'default_pref';
  191. /* or legacy location: internal data dir */
  192. if (!@file_exists($default_pref)) {
  193. $default_pref = SM_PATH . 'data/default_pref';
  194. }
  195. /* If no legacies, check where we'd expect it to be located:
  196. * under config/ */
  197. if (!@file_exists($default_pref)) {
  198. $default_pref = SM_PATH . 'config/default_pref';
  199. }
  200. /* If a default_pref file found, try to copy it, if none found,
  201. * try to create an empty one. If that fails, report an error.
  202. */
  203. if (
  204. ( is_readable($default_pref) && !@copy($default_pref, $filename) ) ||
  205. !@touch($filename)
  206. ) {
  207. $uid = 'httpd';
  208. if (function_exists('posix_getuid')){
  209. $user_data = posix_getpwuid(posix_getuid());
  210. $uid = $user_data['name'];
  211. }
  212. $errTitle = _("Could not create initial preference file!");
  213. $errString = $errTitle . "<br />\n" .
  214. sprintf( _("%s should be writable by user %s"), $data_dir, $uid ) . "<br />\n" .
  215. _("Please contact your system administrator and report this error.") . "<br />\n";
  216. logout_error( $errString, $errTitle );
  217. exit;
  218. }
  219. }
  220. }
  221. /**
  222. * Write the User Signature.
  223. * @param string $data_dir data directory
  224. * @param string $username user name
  225. * @param integer $number (since 1.2.5) identity number.
  226. * parameter was used for signature text before 1.2.5.
  227. * @param string $value (since 1.2.5) signature text
  228. */
  229. function setSig($data_dir, $username, $number, $value) {
  230. // Limit signature size to 64KB (database BLOB limit)
  231. if (strlen($value)>65536) {
  232. error_option_save(_("Signature is too big."));
  233. return;
  234. }
  235. $filename = getHashedFile($username, $data_dir, "$username.si$number");
  236. /* Open the file for writing, or else display an error to the user. */
  237. if(!$file = @fopen("$filename.tmp", 'w')) {
  238. logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
  239. exit;
  240. }
  241. if ( sq_fwrite($file, $value) === FALSE ) {
  242. logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
  243. exit;
  244. }
  245. fclose($file);
  246. if (! @copy($filename . '.tmp',$filename) ) {
  247. 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') );
  248. exit;
  249. }
  250. @unlink($filename . '.tmp');
  251. @chmod($filename, 0600);
  252. }
  253. /**
  254. * Get the signature.
  255. * @param string $data_dir data directory
  256. * @param string $username user name
  257. * @param integer $number (since 1.2.5) identity number
  258. * @return string signature
  259. */
  260. function getSig($data_dir, $username, $number) {
  261. $filename = getHashedFile($username, $data_dir, "$username.si$number");
  262. $sig = '';
  263. if (file_exists($filename)) {
  264. /* Open the file, or else display an error to the user. */
  265. if(!$file = @fopen($filename, 'r'))
  266. {
  267. logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
  268. exit;
  269. }
  270. while (!feof($file)) {
  271. $sig .= fgets($file, 1024);
  272. }
  273. fclose($file);
  274. }
  275. return $sig;
  276. }
  277. // vim: et ts=4
  278. ?>