59177427 |
1 | <?php |
2ba13803 |
2 | |
35586184 |
3 | /** |
4 | * prefs.php |
5 | * |
15e6162e |
6 | * Copyright (c) 1999-2002 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 | |
16 | if (isset($_SESSION['prefs_cache'])) { |
17 | $prefs_cache = $_SESSION['prefs_cache']; |
18 | } |
19 | if (isset($_SESSION['prefs_are_cached'])) { |
20 | $prefs_are_cached = $_SESSION['prefs_are_cached']; |
21 | } |
22 | |
23 | $rg = ini_get('register_globals'); |
93b2364f |
24 | |
25 | if ( !session_is_registered('prefs_are_cached') || |
26 | !isset( $prefs_cache) || |
3499f99f |
27 | !is_array( $prefs_cache) || |
121fc115 |
28 | substr( phpversion(), 0, 3 ) == '4.1' || |
0b97a708 |
29 | substr( phpversion(), 0, 3 ) == '4.2' || |
30 | (substr( phpversion(), 0, 3 ) == '4.0' && empty($rg))) { |
35586184 |
31 | $prefs_are_cached = false; |
32 | $prefs_cache = array(); |
33 | } |
31ac7db8 |
34 | |
3499f99f |
35 | if (isset($prefs_dsn) && !empty($prefs_dsn)) { |
b68edc75 |
36 | require_once(SM_PATH . 'functions/db_prefs.php'); |
3499f99f |
37 | } else { |
b68edc75 |
38 | require_once(SM_PATH . 'functions/file_prefs.php'); |
7b294953 |
39 | } |
40 | |
3499f99f |
41 | /* Hashing functions */ |
fb120846 |
42 | |
3392dc86 |
43 | function getHashedFile($username, $dir, $datafile, $hash_search = true) { |
44 | global $dir_hash_level; |
45 | |
311339a0 |
46 | /* Remove trailing slash from $dir if found */ |
47 | if (substr($dir, -1) == '/') { |
48 | $dir = substr($dir, 0, strlen($dir) - 1); |
49 | } |
50 | |
3392dc86 |
51 | /* Compute the hash for this user and extract the hash directories. */ |
52 | $hash_dirs = computeHashDirs($username); |
53 | |
54 | /* First, get and make sure the full hash directory exists. */ |
55 | $real_hash_dir = getHashedDir($username, $dir, $hash_dirs); |
56 | |
57 | /* Set the value of our real data file. */ |
4fc152a6 |
58 | $result = "$real_hash_dir/$datafile"; |
3392dc86 |
59 | |
60 | /* Check for this file in the real hash directory. */ |
3a94810f |
61 | if ($hash_search && !@file_exists($result)) { |
3392dc86 |
62 | /* First check the base directory, the most common location. */ |
3a94810f |
63 | if (@file_exists("$dir/$datafile")) { |
3392dc86 |
64 | rename("$dir/$datafile", $result); |
65 | |
66 | /* Then check the full range of possible hash directories. */ |
67 | } else { |
68 | $check_hash_dir = $dir; |
69 | for ($h = 0; $h < 4; ++$h) { |
70 | $check_hash_dir .= '/' . $hash_dirs[$h]; |
311339a0 |
71 | if (@is_readable("$check_hash_dir/$datafile")) { |
3392dc86 |
72 | rename("$check_hash_dir/$datafile", $result); |
73 | break; |
74 | } |
75 | } |
76 | } |
77 | } |
78 | |
79 | /* Return the full hashed datafile path. */ |
80 | return ($result); |
81 | } |
82 | |
83 | function getHashedDir($username, $dir, $hash_dirs = '') { |
84 | global $dir_hash_level; |
85 | |
3a94810f |
86 | /* Remove trailing slash from $dir if found */ |
87 | if (substr($dir, -1) == '/') { |
88 | $dir = substr($dir, 0, strlen($dir) - 1); |
89 | } |
90 | |
3392dc86 |
91 | /* If necessary, populate the hash dir variable. */ |
92 | if ($hash_dirs == '') { |
93 | $hash_dirs = computeHashDirs($username); |
94 | } |
95 | |
96 | /* Make sure the full hash directory exists. */ |
97 | $real_hash_dir = $dir; |
98 | for ($h = 0; $h < $dir_hash_level; ++$h) { |
99 | $real_hash_dir .= '/' . $hash_dirs[$h]; |
3a94810f |
100 | if (!@is_dir($real_hash_dir)) { |
101 | if (!@mkdir($real_hash_dir, 0770)) { |
cab99c3a |
102 | echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' . |
103 | _("Could not create hashed directory structure!") . "<br>\n" . |
104 | _("Please contact your system administrator and report this error.") . "<br>\n"; |
3a94810f |
105 | exit; |
106 | } |
3392dc86 |
107 | } |
108 | } |
109 | |
110 | /* And return that directory. */ |
111 | return ($real_hash_dir); |
112 | } |
113 | |
114 | function computeHashDirs($username) { |
115 | /* Compute the hash for this user and extract the hash directories. */ |
116 | $hash = base_convert(crc32($username), 10, 16); |
117 | $hash_dirs = array(); |
118 | for ($h = 0; $h < 4; ++ $h) { |
119 | $hash_dirs[] = substr($hash, $h, 1); |
120 | } |
121 | |
122 | /* Return our array of hash directories. */ |
123 | return ($hash_dirs); |
124 | } |
125 | |
35586184 |
126 | ?> |