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 $prefs_are_cached = false;
21 $prefs_cache = array();
24 if (isset($prefs_dsn) && !empty($prefs_dsn)) {
25 require_once('../functions/db_prefs.php');
27 require_once('../functions/file_prefs.php');
30 /* Hashing functions */
32 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
33 global $dir_hash_level;
35 /* Remove trailing slash from $dir if found */
36 if (substr($dir, -1) == '/') {
37 $dir = substr($dir, 0, strlen($dir) - 1);
40 /* Compute the hash for this user and extract the hash directories. */
41 $hash_dirs = computeHashDirs($username);
43 /* First, get and make sure the full hash directory exists. */
44 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
46 /* Set the value of our real data file. */
47 $result = "$real_hash_dir/$datafile";
49 /* Check for this file in the real hash directory. */
50 if ($hash_search && !@file_exists
($result)) {
51 /* First check the base directory, the most common location. */
52 if (@file_exists
("$dir/$datafile")) {
53 rename("$dir/$datafile", $result);
55 /* Then check the full range of possible hash directories. */
57 $check_hash_dir = $dir;
58 for ($h = 0; $h < 4; ++
$h) {
59 $check_hash_dir .= '/' . $hash_dirs[$h];
60 if (@is_readable
("$check_hash_dir/$datafile")) {
61 rename("$check_hash_dir/$datafile", $result);
68 /* Return the full hashed datafile path. */
72 function getHashedDir($username, $dir, $hash_dirs = '') {
73 global $dir_hash_level;
75 /* Remove trailing slash from $dir if found */
76 if (substr($dir, -1) == '/') {
77 $dir = substr($dir, 0, strlen($dir) - 1);
80 /* If necessary, populate the hash dir variable. */
81 if ($hash_dirs == '') {
82 $hash_dirs = computeHashDirs($username);
85 /* Make sure the full hash directory exists. */
86 $real_hash_dir = $dir;
87 for ($h = 0; $h < $dir_hash_level; ++
$h) {
88 $real_hash_dir .= '/' . $hash_dirs[$h];
89 if (!@is_dir
($real_hash_dir)) {
90 if (!@mkdir
($real_hash_dir, 0770)) {
91 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
92 _("Could not create hashed directory structure!") . "<br>\n" .
93 _("Please contact your system administrator and report this error.") . "<br>\n";
99 /* And return that directory. */
100 return ($real_hash_dir);
103 function computeHashDirs($username) {
104 /* Compute the hash for this user and extract the hash directories. */
105 $hash = base_convert(crc32($username), 10, 16);
106 $hash_dirs = array();
107 for ($h = 0; $h < 4; ++
$h) {
108 $hash_dirs[] = substr($hash, $h, 1);
111 /* Return our array of hash directories. */