prefs.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. /**
  3. * prefs.php
  4. *
  5. * Copyright (c) 1999-2002 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
  9. *
  10. * $Id$
  11. */
  12. /*
  13. global $prefs_are_cached, $prefs_cache;
  14. if ( !session_is_registered('prefs_are_cached') ||
  15. !isset( $prefs_cache) ||
  16. !is_array( $prefs_cache) ||
  17. substr( phpversion(), 0, 3 ) == '4.1' ) {
  18. $prefs_are_cached = false;
  19. $prefs_cache = array();
  20. }
  21. */
  22. /**
  23. * Check the preferences into the session cache.
  24. */
  25. function cachePrefValues($data_dir, $username) {
  26. global $prefs_are_cached, $prefs_cache;
  27. if ( isset($prefs_are_cached) && $prefs_are_cached) {
  28. return;
  29. }
  30. session_unregister('prefs_cache');
  31. session_unregister('prefs_are_cached');
  32. /* Calculate the filename for the user's preference file */
  33. $filename = getHashedFile($username, $data_dir, "$username.pref");
  34. /* A call to checkForPrefs here should take eliminate the need for */
  35. /* this to be called throughout the rest of the SquirrelMail code. */
  36. checkForPrefs($data_dir, $username, $filename);
  37. /* Make sure that the preference file now DOES exist. */
  38. if (!file_exists($filename)) {
  39. echo sprintf (_("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) . "<br>\n";
  40. exit;
  41. }
  42. $file = fopen($filename, 'r');
  43. /* Read in the preferences. */
  44. $highlight_num = 0;
  45. while (! feof($file)) {
  46. $pref = trim(fgets($file, 1024));
  47. $equalsAt = strpos($pref, '=');
  48. if ($equalsAt > 0) {
  49. $key = substr($pref, 0, $equalsAt);
  50. $value = substr($pref, $equalsAt + 1);
  51. if (substr($key, 0, 9) == 'highlight') {
  52. $key = 'highlight' . $highlight_num;
  53. $highlight_num ++;
  54. }
  55. if ($value != '') {
  56. $prefs_cache[$key] = $value;
  57. }
  58. }
  59. }
  60. fclose($file);
  61. $prefs_are_cached = TRUE;
  62. session_register('prefs_cache');
  63. session_register('prefs_are_cached');
  64. }
  65. /**
  66. * Return the value for the prefernce given by $string.
  67. */
  68. function getPref($data_dir, $username, $string, $default = '') {
  69. global $prefs_cache;
  70. $result = '';
  71. cachePrefValues($data_dir, $username);
  72. if (isset($prefs_cache[$string])) {
  73. $result = $prefs_cache[$string];
  74. } else {
  75. $result = $default;
  76. }
  77. return ($result);
  78. }
  79. /**
  80. * Save the preferences for this user.
  81. */
  82. function savePrefValues($data_dir, $username) {
  83. global $prefs_cache;
  84. $filename = getHashedFile($username, $data_dir, "$username.pref");
  85. $file = fopen($filename, 'w');
  86. foreach ($prefs_cache as $Key => $Value) {
  87. if (isset($Value)) {
  88. fwrite($file, $Key . '=' . $Value . "\n");
  89. }
  90. }
  91. fclose($file);
  92. chmod($filename, 0600);
  93. }
  94. /**
  95. * Remove a preference for the current user.
  96. */
  97. function removePref($data_dir, $username, $string) {
  98. global $prefs_cache;
  99. cachePrefValues($data_dir, $username);
  100. if (isset($prefs_cache[$string])) {
  101. unset($prefs_cache[$string]);
  102. }
  103. savePrefValues($data_dir, $username);
  104. }
  105. /**
  106. * Set a there preference $string to $value.
  107. */
  108. function setPref($data_dir, $username, $string, $value) {
  109. global $prefs_cache;
  110. cachePrefValues($data_dir, $username);
  111. if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
  112. return;
  113. }
  114. if ($value === '') {
  115. removePref($data_dir, $username, $string);
  116. return;
  117. }
  118. $prefs_cache[$string] = $value;
  119. savePrefValues($data_dir, $username);
  120. }
  121. /**
  122. * Check for a preferences file. If one can not be found, create it.
  123. */
  124. function checkForPrefs($data_dir, $username, $filename = '') {
  125. /* First, make sure we have the filename. */
  126. if ($filename == '') {
  127. $filename = getHashedFile($username, $data_dir, '$username.pref');
  128. }
  129. /* Then, check if the file exists. */
  130. if (!@file_exists($filename) ) {
  131. /* First, check the $data_dir for the default preference file. */
  132. $default_pref = $data_dir . 'default_pref';
  133. /* If it is not there, check the internal data directory. */
  134. if (!@file_exists($default_pref)) {
  135. $default_pref = '../data/default_pref';
  136. }
  137. /* Otherwise, report an error. */
  138. if (!file_exists($default_pref)) {
  139. echo _("Error opening ") . $default_pref . "<br>\n";
  140. echo _("Default preference file not found!") . "<br>\n";
  141. echo _("Please contact your system administrator and report this error.") . "<br>\n";
  142. exit;
  143. } else if (!@copy($default_pref, $filename)) {
  144. echo _("Error opening ") . $default_pref . '<br>';
  145. echo _("Could not create initial preference file!") . "<br>\n";
  146. echo _("Please contact your system administrator and report this error.") . "<br>\n";
  147. exit;
  148. }
  149. }
  150. }
  151. /**
  152. * Write the User Signature.
  153. */
  154. function setSig($data_dir, $username, $value) {
  155. $filename = getHashedFile($username, $data_dir, "$username.sig");
  156. $file = fopen($filename, 'w');
  157. fwrite($file, $value);
  158. fclose($file);
  159. }
  160. /**
  161. * Get the signature.
  162. */
  163. function getSig($data_dir, $username) {
  164. #$filename = $data_dir . $username . '.sig';
  165. $filename = getHashedFile($username, $data_dir, "$username.sig");
  166. $sig = '';
  167. if (file_exists($filename)) {
  168. $file = fopen($filename, 'r');
  169. while (!feof($file)) {
  170. $sig .= fgets($file, 1024);
  171. }
  172. fclose($file);
  173. }
  174. return $sig;
  175. }
  176. function getHashedFile($username, $dir, $datafile, $hash_search = true) {
  177. global $dir_hash_level;
  178. /* Remove trailing slash from $dir if found */
  179. if (substr($dir, -1) == '/') {
  180. $dir = substr($dir, 0, strlen($dir) - 1);
  181. }
  182. /* Compute the hash for this user and extract the hash directories. */
  183. $hash_dirs = computeHashDirs($username);
  184. /* First, get and make sure the full hash directory exists. */
  185. $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
  186. /* Set the value of our real data file. */
  187. $result = "$real_hash_dir/$datafile";
  188. /* Check for this file in the real hash directory. */
  189. if ($hash_search && !@file_exists($result)) {
  190. /* First check the base directory, the most common location. */
  191. if (@file_exists("$dir/$datafile")) {
  192. rename("$dir/$datafile", $result);
  193. /* Then check the full range of possible hash directories. */
  194. } else {
  195. $check_hash_dir = $dir;
  196. for ($h = 0; $h < 4; ++$h) {
  197. $check_hash_dir .= '/' . $hash_dirs[$h];
  198. if (@is_readable("$check_hash_dir/$datafile")) {
  199. rename("$check_hash_dir/$datafile", $result);
  200. break;
  201. }
  202. }
  203. }
  204. }
  205. /* Return the full hashed datafile path. */
  206. return ($result);
  207. }
  208. function getHashedDir($username, $dir, $hash_dirs = '') {
  209. global $dir_hash_level;
  210. /* Remove trailing slash from $dir if found */
  211. if (substr($dir, -1) == '/') {
  212. $dir = substr($dir, 0, strlen($dir) - 1);
  213. }
  214. /* If necessary, populate the hash dir variable. */
  215. if ($hash_dirs == '') {
  216. $hash_dirs = computeHashDirs($username);
  217. }
  218. /* Make sure the full hash directory exists. */
  219. $real_hash_dir = $dir;
  220. for ($h = 0; $h < $dir_hash_level; ++$h) {
  221. $real_hash_dir .= '/' . $hash_dirs[$h];
  222. if (!@is_dir($real_hash_dir)) {
  223. if (!@mkdir($real_hash_dir, 0770)) {
  224. echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
  225. _("Could not create hashed directory structure!") . "<br>\n" .
  226. _("Please contact your system administrator and report this error.") . "<br>\n";
  227. exit;
  228. }
  229. }
  230. }
  231. /* And return that directory. */
  232. return ($real_hash_dir);
  233. }
  234. function computeHashDirs($username) {
  235. /* Compute the hash for this user and extract the hash directories. */
  236. $hash = base_convert(crc32($username), 10, 16);
  237. $hash_dirs = array();
  238. for ($h = 0; $h < 4; ++ $h) {
  239. $hash_dirs[] = substr($hash, $h, 1);
  240. }
  241. /* Return our array of hash directories. */
  242. return ($hash_dirs);
  243. }
  244. ?>