hide other special folders
[squirrelmail.git] / functions / prefs.php
CommitLineData
59177427 1<?php
2ba13803 2
35586184 3/**
4 * prefs.php
5 *
6c84ba1e 6 * Copyright (c) 1999-2005 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 *
31841a9e 11 * @version $Id$
d6c32258 12 * @package squirrelmail
ace4c62c 13 * @subpackage prefs
35586184 14 */
15
ace4c62c 16/** @ignore */
17if (!defined('SM_PATH')) define('SM_PATH','../');
18
d6c32258 19/** Include global.php */
0b97a708 20require_once(SM_PATH . 'functions/global.php');
9d0239af 21require_once(SM_PATH . 'functions/plugin.php');
0b97a708 22
0365891c 23sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
24sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
0b97a708 25
d7c82551 26if ( !sqsession_is_registered('prefs_are_cached') ||
93b2364f 27 !isset( $prefs_cache) ||
62f7daa5 28 !is_array( $prefs_cache)
cccfa9c2 29 ) {
35586184 30 $prefs_are_cached = false;
31 $prefs_cache = array();
32}
31ac7db8 33
9d0239af 34$prefs_backend = do_hook_function('prefs_backend');
35if (isset($prefs_backend) && !empty($prefs_backend) && file_exists(SM_PATH . $prefs_backend)) {
55994cd6 36 require_once(SM_PATH . $prefs_backend);
37} elseif (isset($prefs_dsn) && !empty($prefs_dsn)) {
b68edc75 38 require_once(SM_PATH . 'functions/db_prefs.php');
3499f99f 39} else {
b68edc75 40 require_once(SM_PATH . 'functions/file_prefs.php');
7b294953 41}
42
3499f99f 43/* Hashing functions */
fb120846 44
8b096f0a 45/**
46 * Given a username and datafilename, this will return the path to the
47 * hashed location of that datafile.
48 *
49 * @param string username the username of the current user
598294a7 50 * @param string dir the SquirrelMail datadir
8b096f0a 51 * @param string datafile the name of the file to open
52 * @param bool hash_seach default true
53 * @return string the hashed location of datafile
ace4c62c 54 * @since 1.2.0
8b096f0a 55 */
3392dc86 56function getHashedFile($username, $dir, $datafile, $hash_search = true) {
3392dc86 57
311339a0 58 /* Remove trailing slash from $dir if found */
59 if (substr($dir, -1) == '/') {
60 $dir = substr($dir, 0, strlen($dir) - 1);
61 }
62f7daa5 62
3392dc86 63 /* Compute the hash for this user and extract the hash directories. */
64 $hash_dirs = computeHashDirs($username);
65
66 /* First, get and make sure the full hash directory exists. */
67 $real_hash_dir = getHashedDir($username, $dir, $hash_dirs);
68
12719a10 69 /* Set the value of our real data file, after we've removed unwanted characters. */
70 $datafile = str_replace('/', '_', $datafile);
4fc152a6 71 $result = "$real_hash_dir/$datafile";
3392dc86 72
73 /* Check for this file in the real hash directory. */
3a94810f 74 if ($hash_search && !@file_exists($result)) {
3392dc86 75 /* First check the base directory, the most common location. */
3a94810f 76 if (@file_exists("$dir/$datafile")) {
3392dc86 77 rename("$dir/$datafile", $result);
78
79 /* Then check the full range of possible hash directories. */
80 } else {
81 $check_hash_dir = $dir;
82 for ($h = 0; $h < 4; ++$h) {
83 $check_hash_dir .= '/' . $hash_dirs[$h];
311339a0 84 if (@is_readable("$check_hash_dir/$datafile")) {
3392dc86 85 rename("$check_hash_dir/$datafile", $result);
86 break;
87 }
88 }
89 }
90 }
62f7daa5 91
3392dc86 92 /* Return the full hashed datafile path. */
93 return ($result);
94}
95
8b096f0a 96/**
97 * Helper function for getHashedFile, given a username returns the hashed
98 * dir for that username.
99 *
100 * @param string username the username of the current user
598294a7 101 * @param string dir the SquirrelMail datadir
8b096f0a 102 * @param string hash_dirs default ''
103 * @return the path to the hash dir for username
ace4c62c 104 * @since 1.2.0
8b096f0a 105 */
3392dc86 106function getHashedDir($username, $dir, $hash_dirs = '') {
107 global $dir_hash_level;
108
3a94810f 109 /* Remove trailing slash from $dir if found */
110 if (substr($dir, -1) == '/') {
111 $dir = substr($dir, 0, strlen($dir) - 1);
112 }
62f7daa5 113
3392dc86 114 /* If necessary, populate the hash dir variable. */
115 if ($hash_dirs == '') {
116 $hash_dirs = computeHashDirs($username);
117 }
118
119 /* Make sure the full hash directory exists. */
120 $real_hash_dir = $dir;
121 for ($h = 0; $h < $dir_hash_level; ++$h) {
122 $real_hash_dir .= '/' . $hash_dirs[$h];
3a94810f 123 if (!@is_dir($real_hash_dir)) {
124 if (!@mkdir($real_hash_dir, 0770)) {
6fd95361 125 echo sprintf(_("Error creating directory %s."), $real_hash_dir) . '<br />' .
126 _("Could not create hashed directory structure!") . "<br />\n" .
127 _("Please contact your system administrator and report this error.") . "<br />\n";
3a94810f 128 exit;
129 }
3392dc86 130 }
131 }
132
133 /* And return that directory. */
134 return ($real_hash_dir);
135}
136
8b096f0a 137/**
138 * Helper function for getHashDir which does the actual hash calculation.
139 *
140 * @param string username the username to calculate the hash dir for
141 * @return array a list of hash dirs for this username
ace4c62c 142 * @since 1.2.0
8b096f0a 143 */
3392dc86 144function computeHashDirs($username) {
145 /* Compute the hash for this user and extract the hash directories. */
146 $hash = base_convert(crc32($username), 10, 16);
147 $hash_dirs = array();
148 for ($h = 0; $h < 4; ++ $h) {
149 $hash_dirs[] = substr($hash, $h, 1);
150 }
151
152 /* Return our array of hash directories. */
153 return ($hash_dirs);
154}
155
e9219f94 156/**
ace4c62c 157 * Javascript support detection function
158 * @param boolean $reset recheck javascript support if set to true.
159 * @return integer SMPREF_JS_* constants
160 * @since 1.5.1
e9219f94 161 */
ace4c62c 162function checkForJavascript($reset = FALSE) {
ae958cd3 163 global $data_dir, $username, $javascript_on, $javascript_setting;
1a531551 164
ae958cd3 165 if ( !$reset && sqGetGlobalVar('javascript_on', $javascript_on, SQ_SESSION) )
166 return $javascript_on;
167
168 if ( $reset || !isset($javascript_setting) )
169 $javascript_setting = getPref($data_dir, $username, 'javascript_setting', SMPREF_JS_AUTODETECT);
1a531551 170
ae958cd3 171 if ( !sqGetGlobalVar('new_js_autodetect_results', $js_autodetect_results) &&
172 !sqGetGlobalVar('js_autodetect_results', $js_autodetect_results) )
173 $js_autodetect_results = SMPREF_JS_OFF;
1a531551 174
175 if ( $javascript_setting == SMPREF_JS_AUTODETECT )
ae958cd3 176 $javascript_on = $js_autodetect_results;
1a531551 177 else
ae958cd3 178 $javascript_on = $javascript_setting;
1a531551 179
ae958cd3 180 sqsession_register($javascript_on, 'javascript_on');
181 return $javascript_on;
1a531551 182}
183
e9219f94 184?>