When $edit_identity is false and $edit_name is true, the if evaluates as
[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$
12 */
13
0b97a708 14require_once(SM_PATH . 'functions/global.php');
15
0365891c 16sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
17sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
0b97a708 18
19$rg = ini_get('register_globals');
93b2364f 20
cccfa9c2 21/* if php version >= 4.1 OR (4.0 AND $rg = off) */
d7c82551 22if ( !sqsession_is_registered('prefs_are_cached') ||
93b2364f 23 !isset( $prefs_cache) ||
3499f99f 24 !is_array( $prefs_cache) ||
cccfa9c2 25 check_php_version(4,1) ||
26 empty($rg)
27 ) {
35586184 28 $prefs_are_cached = false;
29 $prefs_cache = array();
30}
31ac7db8 31
3499f99f 32if (isset($prefs_dsn) && !empty($prefs_dsn)) {
b68edc75 33 require_once(SM_PATH . 'functions/db_prefs.php');
3499f99f 34} else {
b68edc75 35 require_once(SM_PATH . 'functions/file_prefs.php');
7b294953 36}
37
3499f99f 38/* Hashing functions */
fb120846 39
3392dc86 40function getHashedFile($username, $dir, $datafile, $hash_search = true) {
41 global $dir_hash_level;
42
311339a0 43 /* Remove trailing slash from $dir if found */
44 if (substr($dir, -1) == '/') {
45 $dir = substr($dir, 0, strlen($dir) - 1);
46 }
47
3392dc86 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. */
4fc152a6 55 $result = "$real_hash_dir/$datafile";
3392dc86 56
57 /* Check for this file in the real hash directory. */
3a94810f 58 if ($hash_search && !@file_exists($result)) {
3392dc86 59 /* First check the base directory, the most common location. */
3a94810f 60 if (@file_exists("$dir/$datafile")) {
3392dc86 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];
311339a0 68 if (@is_readable("$check_hash_dir/$datafile")) {
3392dc86 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
80function getHashedDir($username, $dir, $hash_dirs = '') {
81 global $dir_hash_level;
82
3a94810f 83 /* Remove trailing slash from $dir if found */
84 if (substr($dir, -1) == '/') {
85 $dir = substr($dir, 0, strlen($dir) - 1);
86 }
87
3392dc86 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];
3a94810f 97 if (!@is_dir($real_hash_dir)) {
98 if (!@mkdir($real_hash_dir, 0770)) {
cab99c3a 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";
3a94810f 102 exit;
103 }
3392dc86 104 }
105 }
106
107 /* And return that directory. */
108 return ($real_hash_dir);
109}
110
111function 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
35586184 123?>