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