prefs.php 8.1 KB

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