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$ |
d6c32258 |
12 | * @package squirrelmail |
35586184 |
13 | */ |
14 | |
d6c32258 |
15 | /** Include global.php */ |
0b97a708 |
16 | require_once(SM_PATH . 'functions/global.php'); |
17 | |
0365891c |
18 | sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION ); |
19 | sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION ); |
0b97a708 |
20 | |
21 | $rg = ini_get('register_globals'); |
93b2364f |
22 | |
cccfa9c2 |
23 | /* if php version >= 4.1 OR (4.0 AND $rg = off) */ |
d7c82551 |
24 | if ( !sqsession_is_registered('prefs_are_cached') || |
93b2364f |
25 | !isset( $prefs_cache) || |
3499f99f |
26 | !is_array( $prefs_cache) || |
cccfa9c2 |
27 | check_php_version(4,1) || |
28 | empty($rg) |
29 | ) { |
35586184 |
30 | $prefs_are_cached = false; |
31 | $prefs_cache = array(); |
32 | } |
31ac7db8 |
33 | |
3499f99f |
34 | if (isset($prefs_dsn) && !empty($prefs_dsn)) { |
b68edc75 |
35 | require_once(SM_PATH . 'functions/db_prefs.php'); |
3499f99f |
36 | } else { |
b68edc75 |
37 | require_once(SM_PATH . 'functions/file_prefs.php'); |
7b294953 |
38 | } |
39 | |
3499f99f |
40 | /* Hashing functions */ |
fb120846 |
41 | |
8b096f0a |
42 | /** |
43 | * Given a username and datafilename, this will return the path to the |
44 | * hashed location of that datafile. |
45 | * |
46 | * @param string username the username of the current user |
47 | * @param string dir the squirrelmail datadir |
48 | * @param string datafile the name of the file to open |
49 | * @param bool hash_seach default true |
50 | * @return string the hashed location of datafile |
51 | */ |
3392dc86 |
52 | function getHashedFile($username, $dir, $datafile, $hash_search = true) { |
53 | global $dir_hash_level; |
54 | |
311339a0 |
55 | /* Remove trailing slash from $dir if found */ |
56 | if (substr($dir, -1) == '/') { |
57 | $dir = substr($dir, 0, strlen($dir) - 1); |
58 | } |
59 | |
3392dc86 |
60 | /* Compute the hash for this user and extract the hash directories. */ |
61 | $hash_dirs = computeHashDirs($username); |
62 | |
63 | /* First, get and make sure the full hash directory exists. */ |
64 | $real_hash_dir = getHashedDir($username, $dir, $hash_dirs); |
65 | |
12719a10 |
66 | /* Set the value of our real data file, after we've removed unwanted characters. */ |
67 | $datafile = str_replace('/', '_', $datafile); |
4fc152a6 |
68 | $result = "$real_hash_dir/$datafile"; |
3392dc86 |
69 | |
70 | /* Check for this file in the real hash directory. */ |
3a94810f |
71 | if ($hash_search && !@file_exists($result)) { |
3392dc86 |
72 | /* First check the base directory, the most common location. */ |
3a94810f |
73 | if (@file_exists("$dir/$datafile")) { |
3392dc86 |
74 | rename("$dir/$datafile", $result); |
75 | |
76 | /* Then check the full range of possible hash directories. */ |
77 | } else { |
78 | $check_hash_dir = $dir; |
79 | for ($h = 0; $h < 4; ++$h) { |
80 | $check_hash_dir .= '/' . $hash_dirs[$h]; |
311339a0 |
81 | if (@is_readable("$check_hash_dir/$datafile")) { |
3392dc86 |
82 | rename("$check_hash_dir/$datafile", $result); |
83 | break; |
84 | } |
85 | } |
86 | } |
87 | } |
88 | |
89 | /* Return the full hashed datafile path. */ |
90 | return ($result); |
91 | } |
92 | |
8b096f0a |
93 | /** |
94 | * Helper function for getHashedFile, given a username returns the hashed |
95 | * dir for that username. |
96 | * |
97 | * @param string username the username of the current user |
98 | * @param string dir the squirrelmail datadir |
99 | * @param string hash_dirs default '' |
100 | * @return the path to the hash dir for username |
101 | */ |
3392dc86 |
102 | function getHashedDir($username, $dir, $hash_dirs = '') { |
103 | global $dir_hash_level; |
104 | |
3a94810f |
105 | /* Remove trailing slash from $dir if found */ |
106 | if (substr($dir, -1) == '/') { |
107 | $dir = substr($dir, 0, strlen($dir) - 1); |
108 | } |
109 | |
3392dc86 |
110 | /* If necessary, populate the hash dir variable. */ |
111 | if ($hash_dirs == '') { |
112 | $hash_dirs = computeHashDirs($username); |
113 | } |
114 | |
115 | /* Make sure the full hash directory exists. */ |
116 | $real_hash_dir = $dir; |
117 | for ($h = 0; $h < $dir_hash_level; ++$h) { |
118 | $real_hash_dir .= '/' . $hash_dirs[$h]; |
3a94810f |
119 | if (!@is_dir($real_hash_dir)) { |
120 | if (!@mkdir($real_hash_dir, 0770)) { |
cab99c3a |
121 | echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' . |
122 | _("Could not create hashed directory structure!") . "<br>\n" . |
123 | _("Please contact your system administrator and report this error.") . "<br>\n"; |
3a94810f |
124 | exit; |
125 | } |
3392dc86 |
126 | } |
127 | } |
128 | |
129 | /* And return that directory. */ |
130 | return ($real_hash_dir); |
131 | } |
132 | |
8b096f0a |
133 | /** |
134 | * Helper function for getHashDir which does the actual hash calculation. |
135 | * |
136 | * @param string username the username to calculate the hash dir for |
137 | * @return array a list of hash dirs for this username |
138 | */ |
3392dc86 |
139 | function computeHashDirs($username) { |
140 | /* Compute the hash for this user and extract the hash directories. */ |
141 | $hash = base_convert(crc32($username), 10, 16); |
142 | $hash_dirs = array(); |
143 | for ($h = 0; $h < 4; ++ $h) { |
144 | $hash_dirs[] = substr($hash, $h, 1); |
145 | } |
146 | |
147 | /* Return our array of hash directories. */ |
148 | return ($hash_dirs); |
149 | } |
150 | |
35586184 |
151 | ?> |