b9db9f85252cb6cfb25618a4b9d89e2b11865227
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
12 * @package squirrelmail
15 /** Include global.php */
16 require_once(SM_PATH
. 'functions/global.php');
18 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION
);
19 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION
);
21 $rg = ini_get('register_globals');
23 /* if php version >= 4.1 OR (4.0 AND $rg = off) */
24 if ( !sqsession_is_registered('prefs_are_cached') ||
25 !isset( $prefs_cache) ||
26 !is_array( $prefs_cache) ||
27 check_php_version(4,1) ||
30 $prefs_are_cached = false;
31 $prefs_cache = array();
34 if (isset($prefs_backend) && file_exists(SM_PATH
. $prefs_backend)) {
35 require_once(SM_PATH
. $prefs_backend);
36 } elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
37 require_once(SM_PATH
. 'functions/db_prefs.php');
39 require_once(SM_PATH
. 'functions/file_prefs.php');
42 /* Hashing functions */
45 * Given a username and datafilename, this will return the path to the
46 * hashed location of that datafile.
48 * @param string username the username of the current user
49 * @param string dir the squirrelmail datadir
50 * @param string datafile the name of the file to open
51 * @param bool hash_seach default true
52 * @return string the hashed location of datafile
54 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
55 global $dir_hash_level;
57 /* Remove trailing slash from $dir if found */
58 if (substr($dir, -1) == '/') {
59 $dir = substr($dir, 0, strlen($dir) - 1);
62 /* Compute the hash for this user and extract the hash directories. */
63 $hash_dirs = computeHashDirs($username);
65 /* First, get and make sure the full hash directory exists. */
66 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
68 /* Set the value of our real data file, after we've removed unwanted characters. */
69 $datafile = str_replace('/', '_', $datafile);
70 $result = "$real_hash_dir/$datafile";
72 /* Check for this file in the real hash directory. */
73 if ($hash_search && !@file_exists
($result)) {
74 /* First check the base directory, the most common location. */
75 if (@file_exists
("$dir/$datafile")) {
76 rename("$dir/$datafile", $result);
78 /* Then check the full range of possible hash directories. */
80 $check_hash_dir = $dir;
81 for ($h = 0; $h < 4; ++
$h) {
82 $check_hash_dir .= '/' . $hash_dirs[$h];
83 if (@is_readable
("$check_hash_dir/$datafile")) {
84 rename("$check_hash_dir/$datafile", $result);
91 /* Return the full hashed datafile path. */
96 * Helper function for getHashedFile, given a username returns the hashed
97 * dir for that username.
99 * @param string username the username of the current user
100 * @param string dir the squirrelmail datadir
101 * @param string hash_dirs default ''
102 * @return the path to the hash dir for username
104 function getHashedDir($username, $dir, $hash_dirs = '') {
105 global $dir_hash_level;
107 /* Remove trailing slash from $dir if found */
108 if (substr($dir, -1) == '/') {
109 $dir = substr($dir, 0, strlen($dir) - 1);
112 /* If necessary, populate the hash dir variable. */
113 if ($hash_dirs == '') {
114 $hash_dirs = computeHashDirs($username);
117 /* Make sure the full hash directory exists. */
118 $real_hash_dir = $dir;
119 for ($h = 0; $h < $dir_hash_level; ++
$h) {
120 $real_hash_dir .= '/' . $hash_dirs[$h];
121 if (!@is_dir
($real_hash_dir)) {
122 if (!@mkdir
($real_hash_dir, 0770)) {
123 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
124 _("Could not create hashed directory structure!") . "<br>\n" .
125 _("Please contact your system administrator and report this error.") . "<br>\n";
131 /* And return that directory. */
132 return ($real_hash_dir);
136 * Helper function for getHashDir which does the actual hash calculation.
138 * @param string username the username to calculate the hash dir for
139 * @return array a list of hash dirs for this username
141 function computeHashDirs($username) {
142 /* Compute the hash for this user and extract the hash directories. */
143 $hash = base_convert(crc32($username), 10, 16);
144 $hash_dirs = array();
145 for ($h = 0; $h < 4; ++
$h) {
146 $hash_dirs[] = substr($hash, $h, 1);
149 /* Return our array of hash directories. */