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 php version >= 4.1 OR (4.0 AND $rg = off) */
22 if ( !sqsession_is_registered('prefs_are_cached') ||
23 !isset( $prefs_cache) ||
24 !is_array( $prefs_cache) ||
25 check_php_version(4,1) ||
28 $prefs_are_cached = false;
29 $prefs_cache = array();
32 if (isset($prefs_dsn) && !empty($prefs_dsn)) {
33 require_once(SM_PATH
. 'functions/db_prefs.php');
35 require_once(SM_PATH
. 'functions/file_prefs.php');
38 /* Hashing functions */
40 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
41 global $dir_hash_level;
43 /* Remove trailing slash from $dir if found */
44 if (substr($dir, -1) == '/') {
45 $dir = substr($dir, 0, strlen($dir) - 1);
48 /* Compute the hash for this user and extract the hash directories. */
49 $hash_dirs = computeHashDirs($username);
51 /* First, get and make sure the full hash directory exists. */
52 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
54 /* Set the value of our real data file, after we've removed unwanted characters. */
55 $datafile = str_replace('/', '_', $datafile);
56 $result = "$real_hash_dir/$datafile";
58 /* Check for this file in the real hash directory. */
59 if ($hash_search && !@file_exists
($result)) {
60 /* First check the base directory, the most common location. */
61 if (@file_exists
("$dir/$datafile")) {
62 rename("$dir/$datafile", $result);
64 /* Then check the full range of possible hash directories. */
66 $check_hash_dir = $dir;
67 for ($h = 0; $h < 4; ++
$h) {
68 $check_hash_dir .= '/' . $hash_dirs[$h];
69 if (@is_readable
("$check_hash_dir/$datafile")) {
70 rename("$check_hash_dir/$datafile", $result);
77 /* Return the full hashed datafile path. */
81 function getHashedDir($username, $dir, $hash_dirs = '') {
82 global $dir_hash_level;
84 /* Remove trailing slash from $dir if found */
85 if (substr($dir, -1) == '/') {
86 $dir = substr($dir, 0, strlen($dir) - 1);
89 /* If necessary, populate the hash dir variable. */
90 if ($hash_dirs == '') {
91 $hash_dirs = computeHashDirs($username);
94 /* Make sure the full hash directory exists. */
95 $real_hash_dir = $dir;
96 for ($h = 0; $h < $dir_hash_level; ++
$h) {
97 $real_hash_dir .= '/' . $hash_dirs[$h];
98 if (!@is_dir
($real_hash_dir)) {
99 if (!@mkdir
($real_hash_dir, 0770)) {
100 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
101 _("Could not create hashed directory structure!") . "<br>\n" .
102 _("Please contact your system administrator and report this error.") . "<br>\n";
108 /* And return that directory. */
109 return ($real_hash_dir);
112 function computeHashDirs($username) {
113 /* Compute the hash for this user and extract the hash directories. */
114 $hash = base_convert(crc32($username), 10, 16);
115 $hash_dirs = array();
116 for ($h = 0; $h < 4; ++
$h) {
117 $hash_dirs[] = substr($hash, $h, 1);
120 /* Return our array of hash directories. */