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