file_prefs.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <?php
  2. /**
  3. * file_prefs.php
  4. *
  5. * This contains functions for manipulating user preferences in files
  6. *
  7. * @copyright 1999-2021 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id$
  10. * @package squirrelmail
  11. * @subpackage prefs
  12. * @since 1.2.5
  13. */
  14. /**
  15. * Check the preferences into the session cache.
  16. *
  17. * @param string $data_dir
  18. * @param string $username
  19. *
  20. * @since 1.1.3
  21. *
  22. */
  23. function cachePrefValues($data_dir, $username) {
  24. global $prefs_are_cached, $prefs_cache;
  25. sqgetGlobalVar('prefs_are_cached', $prefs_are_cached, SQ_SESSION );
  26. if ( isset($prefs_are_cached) && $prefs_are_cached) {
  27. sqgetGlobalVar('prefs_cache', $prefs_cache, SQ_SESSION );
  28. // sm_print_r($prefs_cache);
  29. // exit;
  30. return;
  31. }
  32. sqsession_unregister('prefs_cache');
  33. sqsession_unregister('prefs_are_cached');
  34. /* Calculate the filename for the user's preference file */
  35. $filename = getHashedFile($username, $data_dir, "$username.pref");
  36. /* A call to checkForPrefs here should take eliminate the need for */
  37. /* this to be called throughout the rest of the SquirrelMail code. */
  38. checkForPrefs($data_dir, $username, $filename);
  39. /* Make sure that the preference file now DOES exist. */
  40. if (!file_exists($filename)) {
  41. logout_error( sprintf( _("Preference file, %s, does not exist. Log out, and log back in to create a default preference file."), $filename) );
  42. exit;
  43. }
  44. /* Open the file, or else display an error to the user. */
  45. if(!$file = @fopen($filename, 'r'))
  46. {
  47. logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
  48. exit;
  49. }
  50. /* Read in the preferences. */
  51. $highlight_num = 0;
  52. while (! feof($file)) {
  53. $pref = '';
  54. /* keep reading a pref until we reach an eol (\n (or \r for macs)) */
  55. while($read = fgets($file, 1024))
  56. {
  57. $pref .= $read;
  58. if(strpos($read,"\n") || strpos($read,"\r"))
  59. break;
  60. }
  61. $pref = trim($pref);
  62. $equalsAt = strpos($pref, '=');
  63. if ($equalsAt > 0) {
  64. $key = substr($pref, 0, $equalsAt);
  65. $value = substr($pref, $equalsAt + 1);
  66. //FIXME: this code is not in db_prefs.php that I can see
  67. /* this is to 'rescue' old-style highlighting rules. */
  68. if (substr($key, 0, 9) == 'highlight') {
  69. $key = 'highlight' . $highlight_num;
  70. $highlight_num ++;
  71. }
  72. //FIXME: this code is not in db_prefs.php that I can see
  73. if ($value != '') {
  74. $prefs_cache[$key] = $value;
  75. }
  76. }
  77. }
  78. fclose($file);
  79. $prefs_are_cached = TRUE;
  80. sqsession_register($prefs_cache, 'prefs_cache');
  81. sqsession_register($prefs_are_cached, 'prefs_are_cached');
  82. }
  83. /**
  84. * Return the value for the desired preference.
  85. *
  86. * @param string $data_dir data directory
  87. * @param string $username user name
  88. * @param string $pref_name preference name
  89. * @param string $default (since 1.2.0) default preference value
  90. *
  91. * @return mixed
  92. *
  93. */
  94. function getPref($data_dir, $username, $pref_name, $default = '') {
  95. global $prefs_cache;
  96. $temp = array(&$username, &$pref_name);
  97. $result = do_hook('get_pref_override', $temp);
  98. if (is_null($result)) {
  99. cachePrefValues($data_dir, $username);
  100. if (isset($prefs_cache[$pref_name])) {
  101. $result = $prefs_cache[$pref_name];
  102. } else {
  103. //FIXME: is there a justification for having two prefs hooks so close? who uses them?
  104. $temp = array(&$username, &$pref_name);
  105. $result = do_hook('get_pref', $temp);
  106. if (is_null($result)) {
  107. $result = $default;
  108. }
  109. }
  110. }
  111. return ($result);
  112. }
  113. /**
  114. * Save the preferences for this user.
  115. *
  116. * @param string $data_dir data directory
  117. * @param string $username user name
  118. *
  119. * @since 1.1.3
  120. *
  121. */
  122. function savePrefValues($data_dir, $username) {
  123. global $prefs_cache;
  124. $filename = getHashedFile($username, $data_dir, "$username.pref");
  125. /* Open the file for writing, or else display an error to the user. */
  126. if(!$file = @fopen($filename.'.tmp', 'w'))
  127. {
  128. logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
  129. exit;
  130. }
  131. foreach ($prefs_cache as $Key => $Value) {
  132. if (isset($Value)) {
  133. if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) {
  134. logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
  135. exit;
  136. }
  137. }
  138. }
  139. fclose($file);
  140. if (! @copy($filename . '.tmp',$filename) ) {
  141. logout_error( sprintf( _("Preference file, %s, could not be copied from temporary file, %s. Contact your system administrator to resolve this issue."), $filename, $filename . '.tmp') );
  142. exit;
  143. }
  144. @unlink($filename . '.tmp');
  145. @chmod($filename, 0600);
  146. sqsession_register($prefs_cache , 'prefs_cache');
  147. }
  148. /**
  149. * Remove a preference for the current user.
  150. *
  151. * @param string $data_dir data directory
  152. * @param string $username user name
  153. * @param string $pref_name preference name
  154. *
  155. */
  156. function removePref($data_dir, $username, $pref_name) {
  157. global $prefs_cache;
  158. cachePrefValues($data_dir, $username);
  159. if (isset($prefs_cache[$pref_name])) {
  160. unset($prefs_cache[$pref_name]);
  161. }
  162. savePrefValues($data_dir, $username);
  163. }
  164. /**
  165. * Set the desired preference setting ($pref_name)
  166. * to whatever is in $value.
  167. *
  168. * @param string $data_dir data directory
  169. * @param string $username user name
  170. * @param string $pref_name preference name
  171. * @param mixed $value preference value
  172. *
  173. */
  174. function setPref($data_dir, $username, $pref_name, $value) {
  175. global $prefs_cache;
  176. cachePrefValues($data_dir, $username);
  177. if (isset($prefs_cache[$pref_name]) && ($prefs_cache[$pref_name] == $value)) {
  178. return;
  179. }
  180. if ($value === '') {
  181. removePref($data_dir, $username, $pref_name);
  182. return;
  183. }
  184. $prefs_cache[$pref_name] = $value;
  185. savePrefValues($data_dir, $username);
  186. }
  187. /**
  188. * Check for a preferences file. If one can not be found, create it.
  189. *
  190. * @param string $data_dir data directory
  191. * @param string $username user name
  192. * @param string $filename (since 1.2.0) preference file name.
  193. * (OPTIONAL; default is an empty string,
  194. * in which case the file name is
  195. * automatically detected)
  196. *
  197. */
  198. function checkForPrefs($data_dir, $username, $filename = '') {
  199. /* First, make sure we have the filename. */
  200. if ($filename == '') {
  201. $filename = getHashedFile($username, $data_dir, "$username.pref");
  202. }
  203. /* Then, check if the file exists. */
  204. if (!@file_exists($filename) ) {
  205. /* If it does not exist, check for default_prefs */
  206. /* First, check legacy locations: data dir */
  207. if(substr($data_dir,-1) != '/') {
  208. $data_dir .= '/';
  209. }
  210. $default_pref = $data_dir . 'default_pref';
  211. /* or legacy location: internal data dir */
  212. if (!@file_exists($default_pref)) {
  213. $default_pref = SM_PATH . 'data/default_pref';
  214. }
  215. /* If no legacies, check where we'd expect it to be located:
  216. * under config/ */
  217. if (!@file_exists($default_pref)) {
  218. $default_pref = SM_PATH . 'config/default_pref';
  219. }
  220. /* If a default_pref file found, try to copy it, if none found,
  221. * try to create an empty one. If that fails, report an error.
  222. */
  223. if (
  224. ( is_readable($default_pref) && !@copy($default_pref, $filename) ) ||
  225. !@touch($filename)
  226. ) {
  227. $uid = 'that the web server is running as';
  228. if (function_exists('posix_getuid')){
  229. $user_data = posix_getpwuid(posix_getuid());
  230. $uid = $user_data['name'];
  231. }
  232. $errTitle = _("Could not create initial preference file!");
  233. $errString = $errTitle . "\n" .
  234. sprintf( _("%s should be writable by the user %s."), $data_dir, $uid ) . "\n" .
  235. _("Please contact your system administrator and report this error.") ;
  236. logout_error( $errString, $errTitle );
  237. exit;
  238. }
  239. }
  240. }
  241. /**
  242. * Write the User Signature.
  243. *
  244. * @param string $data_dir data directory
  245. * @param string $username user name
  246. * @param integer $number (since 1.2.5) identity number.
  247. * (before 1.2.5., this parameter
  248. * was used for the signature value)
  249. * @param string $value (since 1.2.5) signature value
  250. *
  251. */
  252. function setSig($data_dir, $username, $number, $value) {
  253. // Limit signature size to 64KB (database BLOB limit)
  254. if (strlen($value)>65536) {
  255. error_option_save(_("Signature is too big."));
  256. return;
  257. }
  258. $filename = getHashedFile($username, $data_dir, "$username.si$number");
  259. /* Open the file for writing, or else display an error to the user. */
  260. if(!$file = @fopen("$filename.tmp", 'w')) {
  261. logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
  262. exit;
  263. }
  264. if ( sq_fwrite($file, $value) === FALSE ) {
  265. logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
  266. exit;
  267. }
  268. fclose($file);
  269. if (! @copy($filename . '.tmp',$filename) ) {
  270. logout_error( sprintf( _("Signature file, %s, could not be copied from temporary file, %s. Contact your system administrator to resolve this issue."), $filename, $filename . '.tmp') );
  271. exit;
  272. }
  273. @unlink($filename . '.tmp');
  274. @chmod($filename, 0600);
  275. }
  276. /**
  277. * Get the signature.
  278. *
  279. * @param string $data_dir data directory
  280. * @param string $username user name
  281. * @param integer $number (since 1.2.5) identity number
  282. *
  283. * @return string signature
  284. *
  285. */
  286. function getSig($data_dir, $username, $number) {
  287. $filename = getHashedFile($username, $data_dir, "$username.si$number");
  288. $sig = '';
  289. if (file_exists($filename)) {
  290. /* Open the file, or else display an error to the user. */
  291. if(!$file = @fopen($filename, 'r'))
  292. {
  293. logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
  294. exit;
  295. }
  296. while (!feof($file)) {
  297. $sig .= fgets($file, 1024);
  298. }
  299. fclose($file);
  300. }
  301. return $sig;
  302. }
  303. // vim: et ts=4