Whoops, forgot debug code. Thaks Jonathan.
[squirrelmail.git] / functions / prefs.php
CommitLineData
59177427 1<?php
2ba13803 2
35586184 3/**
4 * prefs.php
5 *
76911253 6 * Copyright (c) 1999-2003 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$
d6c32258 12 * @package squirrelmail
35586184 13 */
14
d6c32258 15/** Include global.php */
0b97a708 16require_once(SM_PATH . 'functions/global.php');
17
0365891c 18sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
19sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
0b97a708 20
21$rg = ini_get('register_globals');
93b2364f 22
cccfa9c2 23/* if php version >= 4.1 OR (4.0 AND $rg = off) */
d7c82551 24if ( !sqsession_is_registered('prefs_are_cached') ||
93b2364f 25 !isset( $prefs_cache) ||
3499f99f 26 !is_array( $prefs_cache) ||
cccfa9c2 27 check_php_version(4,1) ||
28 empty($rg)
29 ) {
35586184 30 $prefs_are_cached = false;
31 $prefs_cache = array();
32}
31ac7db8 33
55994cd6 34if (isset($prefs_backend) && file_exists(SM_PATH . $prefs_backend)) {
35 require_once(SM_PATH . $prefs_backend);
36} elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
b68edc75 37 require_once(SM_PATH . 'functions/db_prefs.php');
3499f99f 38} else {
b68edc75 39 require_once(SM_PATH . 'functions/file_prefs.php');
7b294953 40}
41
3499f99f 42/* Hashing functions */
fb120846 43
8b096f0a 44/**
45 * Given a username and datafilename, this will return the path to the
46 * hashed location of that datafile.
47 *
48 * @param string username the username of the current user
49 * @param string dir the squirrelmail datadir
50 * @param string datafile the name of the file to open
51 * @param bool hash_seach default true
52 * @return string the hashed location of datafile
53 */
3392dc86 54function getHashedFile($username, $dir, $datafile, $hash_search = true) {
55 global $dir_hash_level;
56
311339a0 57 /* Remove trailing slash from $dir if found */
58 if (substr($dir, -1) == '/') {
59 $dir = substr($dir, 0, strlen($dir) - 1);
60 }
61
3392dc86 62 /* Compute the hash for this user and extract the hash directories. */
63 $hash_dirs = computeHashDirs($username);
64
65 /* First, get and make sure the full hash directory exists. */
66 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
67
12719a10 68 /* Set the value of our real data file, after we've removed unwanted characters. */
69 $datafile = str_replace('/', '_', $datafile);
4fc152a6 70 $result = "$real_hash_dir/$datafile";
3392dc86 71
72 /* Check for this file in the real hash directory. */
3a94810f 73 if ($hash_search && !@file_exists($result)) {
3392dc86 74 /* First check the base directory, the most common location. */
3a94810f 75 if (@file_exists("$dir/$datafile")) {
3392dc86 76 rename("$dir/$datafile", $result);
77
78 /* Then check the full range of possible hash directories. */
79 } else {
80 $check_hash_dir = $dir;
81 for ($h = 0; $h < 4; ++$h) {
82 $check_hash_dir .= '/' . $hash_dirs[$h];
311339a0 83 if (@is_readable("$check_hash_dir/$datafile")) {
3392dc86 84 rename("$check_hash_dir/$datafile", $result);
85 break;
86 }
87 }
88 }
89 }
90
91 /* Return the full hashed datafile path. */
92 return ($result);
93}
94
8b096f0a 95/**
96 * Helper function for getHashedFile, given a username returns the hashed
97 * dir for that username.
98 *
99 * @param string username the username of the current user
100 * @param string dir the squirrelmail datadir
101 * @param string hash_dirs default ''
102 * @return the path to the hash dir for username
103 */
3392dc86 104function getHashedDir($username, $dir, $hash_dirs = '') {
105 global $dir_hash_level;
106
3a94810f 107 /* Remove trailing slash from $dir if found */
108 if (substr($dir, -1) == '/') {
109 $dir = substr($dir, 0, strlen($dir) - 1);
110 }
111
3392dc86 112 /* If necessary, populate the hash dir variable. */
113 if ($hash_dirs == '') {
114 $hash_dirs = computeHashDirs($username);
115 }
116
117 /* Make sure the full hash directory exists. */
118 $real_hash_dir = $dir;
119 for ($h = 0; $h < $dir_hash_level; ++$h) {
120 $real_hash_dir .= '/' . $hash_dirs[$h];
3a94810f 121 if (!@is_dir($real_hash_dir)) {
122 if (!@mkdir($real_hash_dir, 0770)) {
cab99c3a 123 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
124 _("Could not create hashed directory structure!") . "<br>\n" .
125 _("Please contact your system administrator and report this error.") . "<br>\n";
3a94810f 126 exit;
127 }
3392dc86 128 }
129 }
130
131 /* And return that directory. */
132 return ($real_hash_dir);
133}
134
8b096f0a 135/**
136 * Helper function for getHashDir which does the actual hash calculation.
137 *
138 * @param string username the username to calculate the hash dir for
139 * @return array a list of hash dirs for this username
140 */
3392dc86 141function computeHashDirs($username) {
142 /* Compute the hash for this user and extract the hash directories. */
143 $hash = base_convert(crc32($username), 10, 16);
144 $hash_dirs = array();
145 for ($h = 0; $h < 4; ++ $h) {
146 $hash_dirs[] = substr($hash, $h, 1);
147 }
148
149 /* Return our array of hash directories. */
150 return ($hash_dirs);
151}
152
35586184 153?>