two more subpackage blocks
[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 if ( !sqsession_is_registered('prefs_are_cached') ||
22 !isset( $prefs_cache) ||
23 !is_array( $prefs_cache)
24 ) {
25 $prefs_are_cached = false;
26 $prefs_cache = array();
27 }
28
29 if (isset($prefs_backend) && file_exists(SM_PATH . $prefs_backend)) {
30 require_once(SM_PATH . $prefs_backend);
31 } elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
32 require_once(SM_PATH . 'functions/db_prefs.php');
33 } else {
34 require_once(SM_PATH . 'functions/file_prefs.php');
35 }
36
37 /* Hashing functions */
38
39 /**
40 * Given a username and datafilename, this will return the path to the
41 * hashed location of that datafile.
42 *
43 * @param string username the username of the current user
44 * @param string dir the squirrelmail datadir
45 * @param string datafile the name of the file to open
46 * @param bool hash_seach default true
47 * @return string the hashed location of datafile
48 */
49 function getHashedFile($username, $dir, $datafile, $hash_search = true) {
50 global $dir_hash_level;
51
52 /* Remove trailing slash from $dir if found */
53 if (substr($dir, -1) == '/') {
54 $dir = substr($dir, 0, strlen($dir) - 1);
55 }
56
57 /* Compute the hash for this user and extract the hash directories. */
58 $hash_dirs = computeHashDirs($username);
59
60 /* First, get and make sure the full hash directory exists. */
61 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
62
63 /* Set the value of our real data file, after we've removed unwanted characters. */
64 $datafile = str_replace('/', '_', $datafile);
65 $result = "$real_hash_dir/$datafile";
66
67 /* Check for this file in the real hash directory. */
68 if ($hash_search && !@file_exists($result)) {
69 /* First check the base directory, the most common location. */
70 if (@file_exists("$dir/$datafile")) {
71 rename("$dir/$datafile", $result);
72
73 /* Then check the full range of possible hash directories. */
74 } else {
75 $check_hash_dir = $dir;
76 for ($h = 0; $h < 4; ++$h) {
77 $check_hash_dir .= '/' . $hash_dirs[$h];
78 if (@is_readable("$check_hash_dir/$datafile")) {
79 rename("$check_hash_dir/$datafile", $result);
80 break;
81 }
82 }
83 }
84 }
85
86 /* Return the full hashed datafile path. */
87 return ($result);
88 }
89
90 /**
91 * Helper function for getHashedFile, given a username returns the hashed
92 * dir for that username.
93 *
94 * @param string username the username of the current user
95 * @param string dir the squirrelmail datadir
96 * @param string hash_dirs default ''
97 * @return the path to the hash dir for username
98 */
99 function getHashedDir($username, $dir, $hash_dirs = '') {
100 global $dir_hash_level;
101
102 /* Remove trailing slash from $dir if found */
103 if (substr($dir, -1) == '/') {
104 $dir = substr($dir, 0, strlen($dir) - 1);
105 }
106
107 /* If necessary, populate the hash dir variable. */
108 if ($hash_dirs == '') {
109 $hash_dirs = computeHashDirs($username);
110 }
111
112 /* Make sure the full hash directory exists. */
113 $real_hash_dir = $dir;
114 for ($h = 0; $h < $dir_hash_level; ++$h) {
115 $real_hash_dir .= '/' . $hash_dirs[$h];
116 if (!@is_dir($real_hash_dir)) {
117 if (!@mkdir($real_hash_dir, 0770)) {
118 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br>' .
119 _("Could not create hashed directory structure!") . "<br>\n" .
120 _("Please contact your system administrator and report this error.") . "<br>\n";
121 exit;
122 }
123 }
124 }
125
126 /* And return that directory. */
127 return ($real_hash_dir);
128 }
129
130 /**
131 * Helper function for getHashDir which does the actual hash calculation.
132 *
133 * @param string username the username to calculate the hash dir for
134 * @return array a list of hash dirs for this username
135 */
136 function computeHashDirs($username) {
137 /* Compute the hash for this user and extract the hash directories. */
138 $hash = base_convert(crc32($username), 10, 16);
139 $hash_dirs = array();
140 for ($h = 0; $h < 4; ++ $h) {
141 $hash_dirs[] = substr($hash, $h, 1);
142 }
143
144 /* Return our array of hash directories. */
145 return ($hash_dirs);
146 }
147
148 function checkForJavascript($reset = FALSE)
149 {
150 global $data_dir, $username, $javascript_on, $javascript_setting;
151
152 if ( !$reset && sqGetGlobalVar('javascript_on', $javascript_on, SQ_SESSION) )
153 return $javascript_on;
154
155 if ( $reset || !isset($javascript_setting) )
156 $javascript_setting = getPref($data_dir, $username, 'javascript_setting', SMPREF_JS_AUTODETECT);
157
158 if ( !sqGetGlobalVar('new_js_autodetect_results', $js_autodetect_results) &&
159 !sqGetGlobalVar('js_autodetect_results', $js_autodetect_results) )
160 $js_autodetect_results = SMPREF_JS_OFF;
161
162 if ( $javascript_setting == SMPREF_JS_AUTODETECT )
163 $javascript_on = $js_autodetect_results;
164 else
165 $javascript_on = $javascript_setting;
166
167 sqsession_register($javascript_on, 'javascript_on');
168 return $javascript_on;
169 }
170
171 ?>