swap in the sqgetGlobalVar function
[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 ( !sqsession_is_registered('prefs_are_cached') ||
22 !isset( $prefs_cache) ||
23 !is_array( $prefs_cache) ||
24 substr( phpversion(), 0, 3 ) == '4.1' ||
25 substr( phpversion(), 0, 3 ) == '4.2' ||
26 (substr( phpversion(), 0, 3 ) == '4.0' && empty($rg))) {
27 $prefs_are_cached = false;
28 $prefs_cache = array();
29 }
30
31 if (isset($prefs_dsn) && !empty($prefs_dsn)) {
32 require_once(SM_PATH . 'functions/db_prefs.php');
33 } else {
34 require_once(SM_PATH . 'functions/file_prefs.php');
35 }
36
37 /* Hashing functions */
38
39 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
40 global $dir_hash_level;
41
42 /* Remove trailing slash from $dir if found */
43 if (substr($dir, -1) == '/') {
44 $dir = substr($dir, 0, strlen($dir) - 1);
45 }
46
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. */
54 $result = "$real_hash_dir/$datafile";
55
56 /* Check for this file in the real hash directory. */
57 if ($hash_search && !@file_exists($result)) {
58 /* First check the base directory, the most common location. */
59 if (@file_exists("$dir/$datafile")) {
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];
67 if (@is_readable("$check_hash_dir/$datafile")) {
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
82 /* Remove trailing slash from $dir if found */
83 if (substr($dir, -1) == '/') {
84 $dir = substr($dir, 0, strlen($dir) - 1);
85 }
86
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];
96 if (!@is_dir($real_hash_dir)) {
97 if (!@mkdir($real_hash_dir, 0770)) {
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";
101 exit;
102 }
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
122 ?>