6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This contains functions for manipulating user preferences
14 global $prefs_are_cached, $prefs_cache;
16 if ( !session_is_registered('prefs_are_cached') ||
17 !isset( $prefs_cache) ||
18 !is_array( $prefs_cache) ||
19 substr( phpversion(), 0, 3 ) == '4.1' ||
20 substr( phpversion(), 0, 3 ) == '4.2' ) {
21 $prefs_are_cached = false;
22 $prefs_cache = array();
25 if (isset($prefs_dsn) && !empty($prefs_dsn)) {
26 require_once(SM_PATH
. 'functions/db_prefs.php');
28 require_once(SM_PATH
. 'functions/file_prefs.php');
31 /* Hashing functions */
33 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
34 global $dir_hash_level;
36 /* Remove trailing slash from $dir if found */
37 if (substr($dir, -1) == '/') {
38 $dir = substr($dir, 0, strlen($dir) - 1);
41 /* Compute the hash for this user and extract the hash directories. */
42 $hash_dirs = computeHashDirs($username);
44 /* First, get and make sure the full hash directory exists. */
45 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
47 /* Set the value of our real data file. */
48 $result = SM_PATH
. "$real_hash_dir/$datafile";
50 /* Check for this file in the real hash directory. */
51 if ($hash_search && !@file_exists
($result)) {
52 /* First check the base directory, the most common location. */
53 if (@file_exists
("$dir/$datafile")) {
54 rename("$dir/$datafile", $result);
56 /* Then check the full range of possible hash directories. */
58 $check_hash_dir = $dir;
59 for ($h = 0; $h < 4; ++
$h) {
60 $check_hash_dir .= '/' . $hash_dirs[$h];
61 if (@is_readable
("$check_hash_dir/$datafile")) {
62 rename("$check_hash_dir/$datafile", $result);
69 /* Return the full hashed datafile path. */
73 function getHashedDir($username, $dir, $hash_dirs = '') {
74 global $dir_hash_level;
76 /* Remove trailing slash from $dir if found */
77 if (substr($dir, -1) == '/') {
78 $dir = substr($dir, 0, strlen($dir) - 1);
81 /* If necessary, populate the hash dir variable. */
82 if ($hash_dirs == '') {
83 $hash_dirs = computeHashDirs($username);
86 /* Make sure the full hash directory exists. */
87 $real_hash_dir = $dir;
88 for ($h = 0; $h < $dir_hash_level; ++
$h) {
89 $real_hash_dir .= '/' . $hash_dirs[$h];
90 if (!@is_dir
($real_hash_dir)) {
91 if (!@mkdir
($real_hash_dir, 0770)) {
92 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
93 _("Could not create hashed directory structure!") . "<br>\n" .
94 _("Please contact your system administrator and report this error.") . "<br>\n";
100 /* And return that directory. */
101 return ($real_hash_dir);
104 function computeHashDirs($username) {
105 /* Compute the hash for this user and extract the hash directories. */
106 $hash = base_convert(crc32($username), 10, 16);
107 $hash_dirs = array();
108 for ($h = 0; $h < 4; ++
$h) {
109 $hash_dirs[] = substr($hash, $h, 1);
112 /* Return our array of hash directories. */