prefs.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. chmod($filename, 0600);
  91. }
  92. /**
  93. * Remove a preference for the current user.
  94. */
  95. function removePref($data_dir, $username, $string) {
  96. global $prefs_cache;
  97. cachePrefValues($data_dir, $username);
  98. if (isset($prefs_cache[$string])) {
  99. unset($prefs_cache[$string]);
  100. }
  101. savePrefValues($data_dir, $username);
  102. }
  103. /**
  104. * Set a there preference $string to $value.
  105. */
  106. function setPref($data_dir, $username, $string, $value) {
  107. global $prefs_cache;
  108. cachePrefValues($data_dir, $username);
  109. if (isset($prefs_cache[$string]) && ($prefs_cache[$string] == $value)) {
  110. return;
  111. }
  112. if ($value === '') {
  113. removePref($data_dir, $username, $string);
  114. return;
  115. }
  116. $prefs_cache[$string] = $value;
  117. savePrefValues($data_dir, $username);
  118. }
  119. /**
  120. * Check for a preferences file. If one can not be found, create it.
  121. */
  122. function checkForPrefs($data_dir, $username, $filename = '') {
  123. /* First, make sure we have the filename. */
  124. if ($filename == '') {
  125. $filename = getHashedFile($username, $data_dir, '$username.pref');
  126. }
  127. /* Then, check if the file exists. */
  128. if (!@file_exists($filename) ) {
  129. /* First, check the $data_dir for the default preference file. */
  130. $default_pref = $data_dir . 'default_pref';
  131. /* If it is not there, check the internal data directory. */
  132. if (!@file_exists($default_pref)) {
  133. $default_pref = '../data/default_pref';
  134. }
  135. /* Otherwise, report an error. */
  136. if (!file_exists($default_pref)) {
  137. echo _("Error opening ") . $default_pref . "<br>\n";
  138. echo _("Default preference file not found!") . "<br>\n";
  139. echo _("Please contact your system administrator and report this error.") . "<br>\n";
  140. exit;
  141. } else if (!@copy($default_pref, $filename)) {
  142. echo _("Error opening ") . $default_pref . '<br>';
  143. echo _("Could not create initial preference file!") . "<br>\n";
  144. echo _("Please contact your system administrator and report this error.") . "<br>\n";
  145. exit;
  146. }
  147. }
  148. }
  149. /**
  150. * Write the User Signature.
  151. */
  152. function setSig($data_dir, $username, $value) {
  153. $filename = getHashedFile($username, $data_dir, "$username.sig");
  154. $file = fopen($filename, 'w');
  155. fwrite($file, $value);
  156. fclose($file);
  157. }
  158. /**
  159. * Get the signature.
  160. */
  161. function getSig($data_dir, $username) {
  162. #$filename = $data_dir . $username . '.sig';
  163. $filename = getHashedFile($username, $data_dir, "$username.sig");
  164. $sig = '';
  165. if (file_exists($filename)) {
  166. $file = fopen($filename, 'r');
  167. while (!feof($file)) {
  168. $sig .= fgets($file, 1024);
  169. }
  170. fclose($file);
  171. }
  172. return $sig;
  173. }
  174. function getHashedFile($username, $dir, $datafile, $hash_search = true) {
  175. global $dir_hash_level;
  176. /* Remove trailing slash from $dir if found */
  177. if (substr($dir, -1) == '/') {
  178. $dir = substr($dir, 0, strlen($dir) - 1);
  179. }
  180. /* Compute the hash for this user and extract the hash directories. */
  181. $hash_dirs = computeHashDirs($username);
  182. /* First, get and make sure the full hash directory exists. */
  183. $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
  184. /* Set the value of our real data file. */
  185. $result = "$real_hash_dir/$datafile";
  186. /* Check for this file in the real hash directory. */
  187. if ($hash_search && !@file_exists($result)) {
  188. /* First check the base directory, the most common location. */
  189. if (@file_exists("$dir/$datafile")) {
  190. rename("$dir/$datafile", $result);
  191. /* Then check the full range of possible hash directories. */
  192. } else {
  193. $check_hash_dir = $dir;
  194. for ($h = 0; $h < 4; ++$h) {
  195. $check_hash_dir .= '/' . $hash_dirs[$h];
  196. if (@is_readable("$check_hash_dir/$datafile")) {
  197. rename("$check_hash_dir/$datafile", $result);
  198. break;
  199. }
  200. }
  201. }
  202. }
  203. /* Return the full hashed datafile path. */
  204. return ($result);
  205. }
  206. function getHashedDir($username, $dir, $hash_dirs = '') {
  207. global $dir_hash_level;
  208. /* Remove trailing slash from $dir if found */
  209. if (substr($dir, -1) == '/') {
  210. $dir = substr($dir, 0, strlen($dir) - 1);
  211. }
  212. /* If necessary, populate the hash dir variable. */
  213. if ($hash_dirs == '') {
  214. $hash_dirs = computeHashDirs($username);
  215. }
  216. /* Make sure the full hash directory exists. */
  217. $real_hash_dir = $dir;
  218. for ($h = 0; $h < $dir_hash_level; ++$h) {
  219. $real_hash_dir .= '/' . $hash_dirs[$h];
  220. if (!@is_dir($real_hash_dir)) {
  221. if (!@mkdir($real_hash_dir, 0770)) {
  222. echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
  223. _("Could not create hashed directory structure!") . "<br>\n" .
  224. _("Please contact your system administrator and report this error.") . "<br>\n";
  225. exit;
  226. }
  227. }
  228. }
  229. /* And return that directory. */
  230. return ($real_hash_dir);
  231. }
  232. function computeHashDirs($username) {
  233. /* Compute the hash for this user and extract the hash directories. */
  234. $hash = base_convert(crc32($username), 10, 16);
  235. $hash_dirs = array();
  236. for ($h = 0; $h < 4; ++ $h) {
  237. $hash_dirs[] = substr($hash, $h, 1);
  238. }
  239. /* Return our array of hash directories. */
  240. return ($hash_dirs);
  241. }
  242. ?>