Added Flag/Unflag buttons. I have been up for 2 days straight. Please check for...
[squirrelmail.git] / functions / prefs.php
1 <?php
2
3 /**
4 * prefs.php
5 *
6 * Copyright (c) 1999-2004 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_backend) && file_exists(SM_PATH . $prefs_backend)) {
35 require_once(SM_PATH . $prefs_backend);
36 } elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
37 require_once(SM_PATH . 'functions/db_prefs.php');
38 } else {
39 require_once(SM_PATH . 'functions/file_prefs.php');
40 }
41
42 /* Hashing functions */
43
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 */
54 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
55 global $dir_hash_level;
56
57 /* Remove trailing slash from $dir if found */
58 if (substr($dir, -1) == '/') {
59 $dir = substr($dir, 0, strlen($dir) - 1);
60 }
61
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
68 /* Set the value of our real data file, after we've removed unwanted characters. */
69 $datafile = str_replace('/', '_', $datafile);
70 $result = "$real_hash_dir/$datafile";
71
72 /* Check for this file in the real hash directory. */
73 if ($hash_search && !@file_exists($result)) {
74 /* First check the base directory, the most common location. */
75 if (@file_exists("$dir/$datafile")) {
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];
83 if (@is_readable("$check_hash_dir/$datafile")) {
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
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 */
104 function getHashedDir($username, $dir, $hash_dirs = '') {
105 global $dir_hash_level;
106
107 /* Remove trailing slash from $dir if found */
108 if (substr($dir, -1) == '/') {
109 $dir = substr($dir, 0, strlen($dir) - 1);
110 }
111
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];
121 if (!@is_dir($real_hash_dir)) {
122 if (!@mkdir($real_hash_dir, 0770)) {
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";
126 exit;
127 }
128 }
129 }
130
131 /* And return that directory. */
132 return ($real_hash_dir);
133 }
134
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 */
141 function 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
153 ?>