file_prefs.php 11 KB

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