identities_table and identities_buttons hooks were broken
[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
12719a10 54 /* Set the value of our real data file, after we've removed unwanted characters. */
55 $datafile = str_replace('/', '_', $datafile);
4fc152a6 56 $result = "$real_hash_dir/$datafile";
3392dc86 57
58 /* Check for this file in the real hash directory. */
3a94810f 59 if ($hash_search && !@file_exists($result)) {
3392dc86 60 /* First check the base directory, the most common location. */
3a94810f 61 if (@file_exists("$dir/$datafile")) {
3392dc86 62 rename("$dir/$datafile", $result);
63
64 /* Then check the full range of possible hash directories. */
65 } else {
66 $check_hash_dir = $dir;
67 for ($h = 0; $h < 4; ++$h) {
68 $check_hash_dir .= '/' . $hash_dirs[$h];
311339a0 69 if (@is_readable("$check_hash_dir/$datafile")) {
3392dc86 70 rename("$check_hash_dir/$datafile", $result);
71 break;
72 }
73 }
74 }
75 }
76
77 /* Return the full hashed datafile path. */
78 return ($result);
79}
80
81function getHashedDir($username, $dir, $hash_dirs = '') {
82 global $dir_hash_level;
83
3a94810f 84 /* Remove trailing slash from $dir if found */
85 if (substr($dir, -1) == '/') {
86 $dir = substr($dir, 0, strlen($dir) - 1);
87 }
88
3392dc86 89 /* If necessary, populate the hash dir variable. */
90 if ($hash_dirs == '') {
91 $hash_dirs = computeHashDirs($username);
92 }
93
94 /* Make sure the full hash directory exists. */
95 $real_hash_dir = $dir;
96 for ($h = 0; $h < $dir_hash_level; ++$h) {
97 $real_hash_dir .= '/' . $hash_dirs[$h];
3a94810f 98 if (!@is_dir($real_hash_dir)) {
99 if (!@mkdir($real_hash_dir, 0770)) {
cab99c3a 100 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
101 _("Could not create hashed directory structure!") . "<br>\n" .
102 _("Please contact your system administrator and report this error.") . "<br>\n";
3a94810f 103 exit;
104 }
3392dc86 105 }
106 }
107
108 /* And return that directory. */
109 return ($real_hash_dir);
110}
111
112function computeHashDirs($username) {
113 /* Compute the hash for this user and extract the hash directories. */
114 $hash = base_convert(crc32($username), 10, 16);
115 $hash_dirs = array();
116 for ($h = 0; $h < 4; ++ $h) {
117 $hash_dirs[] = substr($hash, $h, 1);
118 }
119
120 /* Return our array of hash directories. */
121 return ($hash_dirs);
122}
123
35586184 124?>