6 * Copyright (c) 1999-2003 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 require_once(SM_PATH
. 'functions/global.php');
16 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION
);
17 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION
);
19 $rg = ini_get('register_globals');
21 if ( !sqsession_is_registered('prefs_are_cached') ||
22 !isset( $prefs_cache) ||
23 !is_array( $prefs_cache) ||
24 substr( phpversion(), 0, 3 ) == '4.1' ||
25 substr( phpversion(), 0, 3 ) == '4.2' ||
26 (substr( phpversion(), 0, 3 ) == '4.0' && empty($rg))) {
27 $prefs_are_cached = false;
28 $prefs_cache = array();
31 if (isset($prefs_dsn) && !empty($prefs_dsn)) {
32 require_once(SM_PATH
. 'functions/db_prefs.php');
34 require_once(SM_PATH
. 'functions/file_prefs.php');
37 /* Hashing functions */
39 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
40 global $dir_hash_level;
42 /* Remove trailing slash from $dir if found */
43 if (substr($dir, -1) == '/') {
44 $dir = substr($dir, 0, strlen($dir) - 1);
47 /* Compute the hash for this user and extract the hash directories. */
48 $hash_dirs = computeHashDirs($username);
50 /* First, get and make sure the full hash directory exists. */
51 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
53 /* Set the value of our real data file. */
54 $result = "$real_hash_dir/$datafile";
56 /* Check for this file in the real hash directory. */
57 if ($hash_search && !@file_exists
($result)) {
58 /* First check the base directory, the most common location. */
59 if (@file_exists
("$dir/$datafile")) {
60 rename("$dir/$datafile", $result);
62 /* Then check the full range of possible hash directories. */
64 $check_hash_dir = $dir;
65 for ($h = 0; $h < 4; ++
$h) {
66 $check_hash_dir .= '/' . $hash_dirs[$h];
67 if (@is_readable
("$check_hash_dir/$datafile")) {
68 rename("$check_hash_dir/$datafile", $result);
75 /* Return the full hashed datafile path. */
79 function getHashedDir($username, $dir, $hash_dirs = '') {
80 global $dir_hash_level;
82 /* Remove trailing slash from $dir if found */
83 if (substr($dir, -1) == '/') {
84 $dir = substr($dir, 0, strlen($dir) - 1);
87 /* If necessary, populate the hash dir variable. */
88 if ($hash_dirs == '') {
89 $hash_dirs = computeHashDirs($username);
92 /* Make sure the full hash directory exists. */
93 $real_hash_dir = $dir;
94 for ($h = 0; $h < $dir_hash_level; ++
$h) {
95 $real_hash_dir .= '/' . $hash_dirs[$h];
96 if (!@is_dir
($real_hash_dir)) {
97 if (!@mkdir
($real_hash_dir, 0770)) {
98 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
99 _("Could not create hashed directory structure!") . "<br>\n" .
100 _("Please contact your system administrator and report this error.") . "<br>\n";
106 /* And return that directory. */
107 return ($real_hash_dir);
110 function computeHashDirs($username) {
111 /* Compute the hash for this user and extract the hash directories. */
112 $hash = base_convert(crc32($username), 10, 16);
113 $hash_dirs = array();
114 for ($h = 0; $h < 4; ++
$h) {
115 $hash_dirs[] = substr($hash, $h, 1);
118 /* Return our array of hash directories. */