file_prefs.php 10 KB

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