file_prefs.php 10 KB

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