Update
[squirrelmail.git] / functions / prefs.php
CommitLineData
59177427 1<?php
2ba13803 2
35586184 3/**
4 * prefs.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 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$
12 */
13
35586184 14global $prefs_are_cached, $prefs_cache;
93b2364f 15
16if ( !session_is_registered('prefs_are_cached') ||
17 !isset( $prefs_cache) ||
3499f99f 18 !is_array( $prefs_cache) ||
305f13c2 19 substr( phpversion(), 0, 3 ) == '4.1' ) {
35586184 20 $prefs_are_cached = false;
21 $prefs_cache = array();
22}
31ac7db8 23
3499f99f 24if (isset($prefs_dsn) && !empty($prefs_dsn)) {
25 require_once('../functions/db_prefs.php');
26} else {
27 require_once('../functions/file_prefs.php');
7b294953 28}
29
3499f99f 30/* Hashing functions */
fb120846 31
3392dc86 32function getHashedFile($username, $dir, $datafile, $hash_search = true) {
33 global $dir_hash_level;
34
311339a0 35 /* Remove trailing slash from $dir if found */
36 if (substr($dir, -1) == '/') {
37 $dir = substr($dir, 0, strlen($dir) - 1);
38 }
39
3392dc86 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. */
3a94810f 50 if ($hash_search && !@file_exists($result)) {
3392dc86 51 /* First check the base directory, the most common location. */
3a94810f 52 if (@file_exists("$dir/$datafile")) {
3392dc86 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];
311339a0 60 if (@is_readable("$check_hash_dir/$datafile")) {
3392dc86 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
72function getHashedDir($username, $dir, $hash_dirs = '') {
73 global $dir_hash_level;
74
3a94810f 75 /* Remove trailing slash from $dir if found */
76 if (substr($dir, -1) == '/') {
77 $dir = substr($dir, 0, strlen($dir) - 1);
78 }
79
3392dc86 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];
3a94810f 89 if (!@is_dir($real_hash_dir)) {
90 if (!@mkdir($real_hash_dir, 0770)) {
cab99c3a 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";
3a94810f 94 exit;
95 }
3392dc86 96 }
97 }
98
99 /* And return that directory. */
100 return ($real_hash_dir);
101}
102
103function 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
35586184 115?>