Fix & in URLs to & for correctness of HTML, also 100% to "100%" in
[squirrelmail.git] / functions / prefs.php
1 <?php
2
3 /**
4 * prefs.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
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
14 global $prefs_are_cached, $prefs_cache;
15
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();
22 }
23
24 if (isset($prefs_dsn) && !empty($prefs_dsn)) {
25 require_once('../functions/db_prefs.php');
26 } else {
27 require_once('../functions/file_prefs.php');
28 }
29
30 /* Hashing functions */
31
32 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
33 global $dir_hash_level;
34
35 /* Remove trailing slash from $dir if found */
36 if (substr($dir, -1) == '/') {
37 $dir = substr($dir, 0, strlen($dir) - 1);
38 }
39
40 /* Compute the hash for this user and extract the hash directories. */
41 $hash_dirs = computeHashDirs($username);
42
43 /* First, get and make sure the full hash directory exists. */
44 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
45
46 /* Set the value of our real data file. */
47 $result = "$real_hash_dir/$datafile";
48
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);
54
55 /* Then check the full range of possible hash directories. */
56 } else {
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);
62 break;
63 }
64 }
65 }
66 }
67
68 /* Return the full hashed datafile path. */
69 return ($result);
70 }
71
72 function getHashedDir($username, $dir, $hash_dirs = '') {
73 global $dir_hash_level;
74
75 /* Remove trailing slash from $dir if found */
76 if (substr($dir, -1) == '/') {
77 $dir = substr($dir, 0, strlen($dir) - 1);
78 }
79
80 /* If necessary, populate the hash dir variable. */
81 if ($hash_dirs == '') {
82 $hash_dirs = computeHashDirs($username);
83 }
84
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";
94 exit;
95 }
96 }
97 }
98
99 /* And return that directory. */
100 return ($real_hash_dir);
101 }
102
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);
109 }
110
111 /* Return our array of hash directories. */
112 return ($hash_dirs);
113 }
114
115 ?>