oops, this error was already encoded, so back my r1.366 out, and part of
[squirrelmail.git] / functions / prefs.php
CommitLineData
59177427 1<?php
2ba13803 2
35586184 3/**
4 * prefs.php
5 *
35586184 6 * This contains functions for manipulating user preferences
7 *
4b4abf93 8 * @copyright &copy; 1999-2005 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
31841a9e 10 * @version $Id$
d6c32258 11 * @package squirrelmail
ace4c62c 12 * @subpackage prefs
35586184 13 */
14
ace4c62c 15/** @ignore */
16if (!defined('SM_PATH')) define('SM_PATH','../');
17
d6c32258 18/** Include global.php */
0b97a708 19require_once(SM_PATH . 'functions/global.php');
9d0239af 20require_once(SM_PATH . 'functions/plugin.php');
0b97a708 21
0365891c 22sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
23sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
0b97a708 24
d7c82551 25if ( !sqsession_is_registered('prefs_are_cached') ||
93b2364f 26 !isset( $prefs_cache) ||
62f7daa5 27 !is_array( $prefs_cache)
cccfa9c2 28 ) {
35586184 29 $prefs_are_cached = false;
30 $prefs_cache = array();
31}
31ac7db8 32
9d0239af 33$prefs_backend = do_hook_function('prefs_backend');
34if (isset($prefs_backend) && !empty($prefs_backend) && file_exists(SM_PATH . $prefs_backend)) {
55994cd6 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
598294a7 49 * @param string dir the SquirrelMail datadir
8b096f0a 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
ace4c62c 53 * @since 1.2.0
8b096f0a 54 */
3392dc86 55function getHashedFile($username, $dir, $datafile, $hash_search = true) {
3392dc86 56
311339a0 57 /* Remove trailing slash from $dir if found */
58 if (substr($dir, -1) == '/') {
59 $dir = substr($dir, 0, strlen($dir) - 1);
60 }
62f7daa5 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 }
62f7daa5 90
3392dc86 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
598294a7 100 * @param string dir the SquirrelMail datadir
8b096f0a 101 * @param string hash_dirs default ''
102 * @return the path to the hash dir for username
ace4c62c 103 * @since 1.2.0
8b096f0a 104 */
3392dc86 105function getHashedDir($username, $dir, $hash_dirs = '') {
106 global $dir_hash_level;
107
3a94810f 108 /* Remove trailing slash from $dir if found */
109 if (substr($dir, -1) == '/') {
110 $dir = substr($dir, 0, strlen($dir) - 1);
111 }
62f7daa5 112
3392dc86 113 /* If necessary, populate the hash dir variable. */
114 if ($hash_dirs == '') {
115 $hash_dirs = computeHashDirs($username);
116 }
117
118 /* Make sure the full hash directory exists. */
119 $real_hash_dir = $dir;
120 for ($h = 0; $h < $dir_hash_level; ++$h) {
121 $real_hash_dir .= '/' . $hash_dirs[$h];
3a94810f 122 if (!@is_dir($real_hash_dir)) {
123 if (!@mkdir($real_hash_dir, 0770)) {
6fd95361 124 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br />' .
125 _("Could not create hashed directory structure!") . "<br />\n" .
126 _("Please contact your system administrator and report this error.") . "<br />\n";
3a94810f 127 exit;
128 }
3392dc86 129 }
130 }
131
132 /* And return that directory. */
133 return ($real_hash_dir);
134}
135
8b096f0a 136/**
137 * Helper function for getHashDir which does the actual hash calculation.
138 *
139 * @param string username the username to calculate the hash dir for
140 * @return array a list of hash dirs for this username
ace4c62c 141 * @since 1.2.0
8b096f0a 142 */
3392dc86 143function computeHashDirs($username) {
144 /* Compute the hash for this user and extract the hash directories. */
145 $hash = base_convert(crc32($username), 10, 16);
146 $hash_dirs = array();
147 for ($h = 0; $h < 4; ++ $h) {
148 $hash_dirs[] = substr($hash, $h, 1);
149 }
150
151 /* Return our array of hash directories. */
152 return ($hash_dirs);
153}
154
e9219f94 155/**
ace4c62c 156 * Javascript support detection function
157 * @param boolean $reset recheck javascript support if set to true.
c014db31 158 * @return integer SMPREF_JS_ON or SMPREF_JS_OFF ({@see functions/constants.php})
ace4c62c 159 * @since 1.5.1
e9219f94 160 */
ace4c62c 161function checkForJavascript($reset = FALSE) {
ae958cd3 162 global $data_dir, $username, $javascript_on, $javascript_setting;
1a531551 163
ae958cd3 164 if ( !$reset && sqGetGlobalVar('javascript_on', $javascript_on, SQ_SESSION) )
165 return $javascript_on;
166
167 if ( $reset || !isset($javascript_setting) )
168 $javascript_setting = getPref($data_dir, $username, 'javascript_setting', SMPREF_JS_AUTODETECT);
1a531551 169
ae958cd3 170 if ( !sqGetGlobalVar('new_js_autodetect_results', $js_autodetect_results) &&
171 !sqGetGlobalVar('js_autodetect_results', $js_autodetect_results) )
172 $js_autodetect_results = SMPREF_JS_OFF;
1a531551 173
174 if ( $javascript_setting == SMPREF_JS_AUTODETECT )
ae958cd3 175 $javascript_on = $js_autodetect_results;
1a531551 176 else
ae958cd3 177 $javascript_on = $javascript_setting;
1a531551 178
ae958cd3 179 sqsession_register($javascript_on, 'javascript_on');
180 return $javascript_on;
1a531551 181}
182
4b4abf93 183?>