59177427 |
1 | <?php |
2ba13803 |
2 | |
35586184 |
3 | /** |
4 | * prefs.php |
5 | * |
76911253 |
6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
35586184 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This contains functions for manipulating user preferences |
10 | * |
11 | * $Id$ |
12 | */ |
13 | |
0b97a708 |
14 | require_once(SM_PATH . 'functions/global.php'); |
15 | |
0365891c |
16 | sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION ); |
17 | sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION ); |
0b97a708 |
18 | |
19 | $rg = ini_get('register_globals'); |
93b2364f |
20 | |
d7c82551 |
21 | if ( !sqsession_is_registered('prefs_are_cached') || |
93b2364f |
22 | !isset( $prefs_cache) || |
3499f99f |
23 | !is_array( $prefs_cache) || |
121fc115 |
24 | substr( phpversion(), 0, 3 ) == '4.1' || |
0b97a708 |
25 | substr( phpversion(), 0, 3 ) == '4.2' || |
26 | (substr( phpversion(), 0, 3 ) == '4.0' && empty($rg))) { |
35586184 |
27 | $prefs_are_cached = false; |
28 | $prefs_cache = array(); |
29 | } |
31ac7db8 |
30 | |
3499f99f |
31 | if (isset($prefs_dsn) && !empty($prefs_dsn)) { |
b68edc75 |
32 | require_once(SM_PATH . 'functions/db_prefs.php'); |
3499f99f |
33 | } else { |
b68edc75 |
34 | require_once(SM_PATH . 'functions/file_prefs.php'); |
7b294953 |
35 | } |
36 | |
3499f99f |
37 | /* Hashing functions */ |
fb120846 |
38 | |
3392dc86 |
39 | function getHashedFile($username, $dir, $datafile, $hash_search = true) { |
40 | global $dir_hash_level; |
41 | |
311339a0 |
42 | /* Remove trailing slash from $dir if found */ |
43 | if (substr($dir, -1) == '/') { |
44 | $dir = substr($dir, 0, strlen($dir) - 1); |
45 | } |
46 | |
3392dc86 |
47 | /* Compute the hash for this user and extract the hash directories. */ |
48 | $hash_dirs = computeHashDirs($username); |
49 | |
50 | /* First, get and make sure the full hash directory exists. */ |
51 | $real_hash_dir = getHashedDir($username, $dir, $hash_dirs); |
52 | |
53 | /* Set the value of our real data file. */ |
4fc152a6 |
54 | $result = "$real_hash_dir/$datafile"; |
3392dc86 |
55 | |
56 | /* Check for this file in the real hash directory. */ |
3a94810f |
57 | if ($hash_search && !@file_exists($result)) { |
3392dc86 |
58 | /* First check the base directory, the most common location. */ |
3a94810f |
59 | if (@file_exists("$dir/$datafile")) { |
3392dc86 |
60 | rename("$dir/$datafile", $result); |
61 | |
62 | /* Then check the full range of possible hash directories. */ |
63 | } else { |
64 | $check_hash_dir = $dir; |
65 | for ($h = 0; $h < 4; ++$h) { |
66 | $check_hash_dir .= '/' . $hash_dirs[$h]; |
311339a0 |
67 | if (@is_readable("$check_hash_dir/$datafile")) { |
3392dc86 |
68 | rename("$check_hash_dir/$datafile", $result); |
69 | break; |
70 | } |
71 | } |
72 | } |
73 | } |
74 | |
75 | /* Return the full hashed datafile path. */ |
76 | return ($result); |
77 | } |
78 | |
79 | function getHashedDir($username, $dir, $hash_dirs = '') { |
80 | global $dir_hash_level; |
81 | |
3a94810f |
82 | /* Remove trailing slash from $dir if found */ |
83 | if (substr($dir, -1) == '/') { |
84 | $dir = substr($dir, 0, strlen($dir) - 1); |
85 | } |
86 | |
3392dc86 |
87 | /* If necessary, populate the hash dir variable. */ |
88 | if ($hash_dirs == '') { |
89 | $hash_dirs = computeHashDirs($username); |
90 | } |
91 | |
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]; |
3a94810f |
96 | if (!@is_dir($real_hash_dir)) { |
97 | if (!@mkdir($real_hash_dir, 0770)) { |
cab99c3a |
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"; |
3a94810f |
101 | exit; |
102 | } |
3392dc86 |
103 | } |
104 | } |
105 | |
106 | /* And return that directory. */ |
107 | return ($real_hash_dir); |
108 | } |
109 | |
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); |
116 | } |
117 | |
118 | /* Return our array of hash directories. */ |
119 | return ($hash_dirs); |
120 | } |
121 | |
35586184 |
122 | ?> |