prefs.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * prefs.php
  4. *
  5. * Copyright (c) 1999-2001 The SquirrelMail Development Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This contains functions for manipulating user preferences
  9. *
  10. * $Id$
  11. */
  12. global $prefs_are_cached, $prefs_cache;
  13. if (!session_is_registered('prefs_are_cached')) {
  14. $prefs_are_cached = false;
  15. $prefs_cache = array();
  16. }
  17. /**
  18. * Check the preferences into the session cache.
  19. */
  20. function cachePrefValues($data_dir, $username) {
  21. global $prefs_are_cached, $prefs_cache;
  22. if ($prefs_are_cached) {
  23. return;
  24. }
  25. $filename = getHashedFile($username, $data_dir, "$username.pref");
  26. if (!file_exists($filename)) {
  27. printf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename);
  28. exit;
  29. }
  30. $file = fopen($filename, 'r');
  31. /* Read in the preferences. */
  32. $highlight_num = 0;
  33. while (! feof($file)) {
  34. $pref = trim(fgets($file, 1024));
  35. $equalsAt = strpos($pref, '=');
  36. if ($equalsAt > 0) {
  37. $key = substr($pref, 0, $equalsAt);
  38. $value = substr($pref, $equalsAt + 1);
  39. if (substr($key, 0, 9) == 'highlight') {
  40. $key = 'highlight' . $highlight_num;
  41. $highlight_num ++;
  42. }
  43. if ($value != '') {
  44. $prefs_cache[$key] = $value;
  45. }
  46. }
  47. }
  48. fclose($file);
  49. session_unregister('prefs_cache');
  50. session_register('prefs_cache');
  51. $prefs_are_cached = true;
  52. session_unregister('prefs_are_cached');
  53. session_register('prefs_are_cached');
  54. }
  55. /**
  56. * Return the value for the prefernce given by $string.
  57. */
  58. function getPref($data_dir, $username, $string, $default = '') {
  59. global $prefs_cache;
  60. $result = '';
  61. cachePrefValues($data_dir, $username);
  62. if (isset($prefs_cache[$string])) {
  63. $result = $prefs_cache[$string];
  64. } else {
  65. $result = $default;
  66. }
  67. return ($result);
  68. }
  69. /**
  70. * Save the preferences for this user.
  71. */
  72. function savePrefValues($data_dir, $username) {
  73. global $prefs_cache;
  74. $filename = getHashedFile($username, $data_dir, "$username.pref");
  75. $file = fopen($filename, 'w');
  76. foreach ($prefs_cache as $Key => $Value) {
  77. if (isset($Value)) {
  78. fwrite($file, $Key . '=' . $Value . "\n");
  79. }
  80. }
  81. fclose($file);
  82. }
  83. /**
  84. * Remove a preference for the current user.
  85. */
  86. function removePref($data_dir, $username, $string) {
  87. global $prefs_cache;
  88. cachePrefValues($data_dir, $username);
  89. if (isset($prefs_cache[$string])) {
  90. unset($prefs_cache[$string]);
  91. }
  92. savePrefValues($data_dir, $username);
  93. }
  94. /**
  95. * Set a there preference $string to $value.
  96. */
  97. function setPref($data_dir, $username, $string, $value) {
  98. global $prefs_cache;
  99. cachePrefValues($data_dir, $username);
  100. if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
  101. return;
  102. }
  103. if ($value === '') {
  104. removePref($data_dir, $username, $string);
  105. return;
  106. }
  107. $prefs_cache[$string] = $value;
  108. savePrefValues($data_dir, $username);
  109. }
  110. /**
  111. * Check for a preferences file. If one can not be found, create it.
  112. */
  113. function checkForPrefs($data_dir, $username) {
  114. $filename = getHashedFile($username, $data_dir, "$username.pref");
  115. if (!file_exists($filename) ) {
  116. if (!copy($data_dir . 'default_pref', $filename)) {
  117. echo _("Error opening ") . $filename;
  118. exit;
  119. }
  120. }
  121. }
  122. /**
  123. * Write the User Signature.
  124. */
  125. function setSig($data_dir, $username, $value) {
  126. $filename = getHashedFile($username, $data_dir, "$username.sig");
  127. $file = fopen($filename, 'w');
  128. fwrite($file, $value);
  129. fclose($file);
  130. }
  131. /**
  132. * Get the signature.
  133. */
  134. function getSig($data_dir, $username) {
  135. #$filename = $data_dir . $username . '.sig';
  136. $filename = getHashedFile($username, $data_dir, "$username.sig");
  137. $sig = '';
  138. if (file_exists($filename)) {
  139. $file = fopen($filename, 'r');
  140. while (!feof($file)) {
  141. $sig .= fgets($file, 1024);
  142. }
  143. fclose($file);
  144. }
  145. return $sig;
  146. }
  147. function getHashedFile($username, $dir, $datafile, $hash_search = true) {
  148. global $dir_hash_level;
  149. /* Remove trailing slash from $dir if found */
  150. if (substr($dir, -1) == '/') {
  151. $dir = substr($dir, 0, strlen($dir) - 1);
  152. }
  153. /* Compute the hash for this user and extract the hash directories. */
  154. $hash_dirs = computeHashDirs($username);
  155. /* First, get and make sure the full hash directory exists. */
  156. $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
  157. /* Set the value of our real data file. */
  158. $result = "$real_hash_dir/$datafile";
  159. /* Check for this file in the real hash directory. */
  160. if ($hash_search && !file_exists($result)) {
  161. /* First check the base directory, the most common location. */
  162. if (file_exists("$dir/$datafile")) {
  163. rename("$dir/$datafile", $result);
  164. /* Then check the full range of possible hash directories. */
  165. } else {
  166. $check_hash_dir = $dir;
  167. for ($h = 0; $h < 4; ++$h) {
  168. $check_hash_dir .= '/' . $hash_dirs[$h];
  169. if (@is_readable("$check_hash_dir/$datafile")) {
  170. rename("$check_hash_dir/$datafile", $result);
  171. break;
  172. }
  173. }
  174. }
  175. }
  176. /* Return the full hashed datafile path. */
  177. return ($result);
  178. }
  179. function getHashedDir($username, $dir, $hash_dirs = '') {
  180. global $dir_hash_level;
  181. /* If necessary, populate the hash dir variable. */
  182. if ($hash_dirs == '') {
  183. $hash_dirs = computeHashDirs($username);
  184. }
  185. /* Make sure the full hash directory exists. */
  186. $real_hash_dir = $dir;
  187. for ($h = 0; $h < $dir_hash_level; ++$h) {
  188. $real_hash_dir .= '/' . $hash_dirs[$h];
  189. if (!is_dir($real_hash_dir)) {
  190. mkdir($real_hash_dir, 0770);
  191. }
  192. }
  193. /* And return that directory. */
  194. return ($real_hash_dir);
  195. }
  196. function computeHashDirs($username) {
  197. /* Compute the hash for this user and extract the hash directories. */
  198. $hash = base_convert(crc32($username), 10, 16);
  199. $hash_dirs = array();
  200. for ($h = 0; $h < 4; ++ $h) {
  201. $hash_dirs[] = substr($hash, $h, 1);
  202. }
  203. /* Return our array of hash directories. */
  204. return ($hash_dirs);
  205. }
  206. ?>