| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * prefs.php |
| 5 | * |
| 6 | * This contains functions for manipulating user preferences |
| 7 | * |
| 8 | * @copyright © 1999-2006 The SquirrelMail Project Team |
| 9 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 10 | * @version $Id$ |
| 11 | * @package squirrelmail |
| 12 | * @subpackage prefs |
| 13 | */ |
| 14 | |
| 15 | /** @ignore */ |
| 16 | if (!defined('SM_PATH')) define('SM_PATH','../'); |
| 17 | |
| 18 | /** Include global.php */ |
| 19 | require_once(SM_PATH . 'functions/global.php'); |
| 20 | require_once(SM_PATH . 'functions/plugin.php'); |
| 21 | |
| 22 | sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION ); |
| 23 | sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION ); |
| 24 | |
| 25 | if ( !sqsession_is_registered('prefs_are_cached') || |
| 26 | !isset( $prefs_cache) || |
| 27 | !is_array( $prefs_cache) |
| 28 | ) { |
| 29 | $prefs_are_cached = false; |
| 30 | $prefs_cache = array(); |
| 31 | } |
| 32 | |
| 33 | $prefs_backend = do_hook_function('prefs_backend'); |
| 34 | if (isset($prefs_backend) && !empty($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 | * @since 1.2.0 |
| 54 | */ |
| 55 | function getHashedFile($username, $dir, $datafile, $hash_search = true) { |
| 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 | * @since 1.2.0 |
| 104 | */ |
| 105 | function getHashedDir($username, $dir, $hash_dirs = '') { |
| 106 | global $dir_hash_level; |
| 107 | |
| 108 | /* Remove trailing slash from $dir if found */ |
| 109 | if (substr($dir, -1) == '/') { |
| 110 | $dir = substr($dir, 0, strlen($dir) - 1); |
| 111 | } |
| 112 | |
| 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]; |
| 122 | if (!@is_dir($real_hash_dir)) { |
| 123 | if (!@mkdir($real_hash_dir, 0770)) { |
| 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"; |
| 127 | exit; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* And return that directory. */ |
| 133 | return ($real_hash_dir); |
| 134 | } |
| 135 | |
| 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 |
| 141 | * @since 1.2.0 |
| 142 | */ |
| 143 | function 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 | |
| 155 | /** |
| 156 | * Javascript support detection function |
| 157 | * @param boolean $reset recheck javascript support if set to true. |
| 158 | * @return integer SMPREF_JS_ON or SMPREF_JS_OFF ({@see functions/constants.php}) |
| 159 | * @since 1.5.1 |
| 160 | */ |
| 161 | function checkForJavascript($reset = FALSE) { |
| 162 | global $data_dir, $username, $javascript_on, $javascript_setting; |
| 163 | |
| 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); |
| 169 | |
| 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; |
| 173 | |
| 174 | if ( $javascript_setting == SMPREF_JS_AUTODETECT ) |
| 175 | $javascript_on = $js_autodetect_results; |
| 176 | else |
| 177 | $javascript_on = $javascript_setting; |
| 178 | |
| 179 | sqsession_register($javascript_on, 'javascript_on'); |
| 180 | return $javascript_on; |
| 181 | } |
| 182 | |
| 183 | ?> |