prefs.php 8.2 KB

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