8521f39b01e043b3017cfd3836723f62d32db4f5
[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 * @package squirrelmail
13 */
14
15 /** Include global.php */
16 require_once(SM_PATH . 'functions/global.php');
17
18 sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
19 sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
20
21 $rg = ini_get('register_globals');
22
23 /* if php version >= 4.1 OR (4.0 AND $rg = off) */
24 if ( !sqsession_is_registered('prefs_are_cached') ||
25 !isset( $prefs_cache) ||
26 !is_array( $prefs_cache) ||
27 check_php_version(4,1) ||
28 empty($rg)
29 ) {
30 $prefs_are_cached = false;
31 $prefs_cache = array();
32 }
33
34 if (isset($prefs_dsn) && !empty($prefs_dsn)) {
35 require_once(SM_PATH . 'functions/db_prefs.php');
36 } else {
37 require_once(SM_PATH . 'functions/file_prefs.php');
38 }
39
40 /* Hashing functions */
41
42 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
43 global $dir_hash_level;
44
45 /* Remove trailing slash from $dir if found */
46 if (substr($dir, -1) == '/') {
47 $dir = substr($dir, 0, strlen($dir) - 1);
48 }
49
50 /* Compute the hash for this user and extract the hash directories. */
51 $hash_dirs = computeHashDirs($username);
52
53 /* First, get and make sure the full hash directory exists. */
54 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
55
56 /* Set the value of our real data file, after we've removed unwanted characters. */
57 $datafile = str_replace('/', '_', $datafile);
58 $result = "$real_hash_dir/$datafile";
59
60 /* Check for this file in the real hash directory. */
61 if ($hash_search && !@file_exists($result)) {
62 /* First check the base directory, the most common location. */
63 if (@file_exists("$dir/$datafile")) {
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];
71 if (@is_readable("$check_hash_dir/$datafile")) {
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
86 /* Remove trailing slash from $dir if found */
87 if (substr($dir, -1) == '/') {
88 $dir = substr($dir, 0, strlen($dir) - 1);
89 }
90
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];
100 if (!@is_dir($real_hash_dir)) {
101 if (!@mkdir($real_hash_dir, 0770)) {
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";
105 exit;
106 }
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
126 ?>