file_prefs.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /**
  3. * file_prefs.php
  4. *
  5. * This contains functions for manipulating user preferences in files
  6. *
  7. * @copyright &copy; 1999-2007 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 (!$result) {
  99. cachePrefValues($data_dir, $username);
  100. if (isset($prefs_cache[$pref_name])) {
  101. $result = $prefs_cache[$pref_name];
  102. } else {
  103. $temp = array(&$username, &$pref_name);
  104. $result = do_hook('get_pref', $temp);
  105. if (!$result) {
  106. $result = $default;
  107. }
  108. }
  109. }
  110. return ($result);
  111. }
  112. /**
  113. * Save the preferences for this user.
  114. *
  115. * @param string $data_dir data directory
  116. * @param string $username user name
  117. *
  118. * @since 1.1.3
  119. *
  120. */
  121. function savePrefValues($data_dir, $username) {
  122. global $prefs_cache;
  123. $filename = getHashedFile($username, $data_dir, "$username.pref");
  124. /* Open the file for writing, or else display an error to the user. */
  125. if(!$file = @fopen($filename.'.tmp', 'w'))
  126. {
  127. logout_error( sprintf( _("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename.'.tmp') );
  128. exit;
  129. }
  130. foreach ($prefs_cache as $Key => $Value) {
  131. if (isset($Value)) {
  132. if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) {
  133. logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') );
  134. exit;
  135. }
  136. }
  137. }
  138. fclose($file);
  139. if (! @copy($filename . '.tmp',$filename) ) {
  140. 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') );
  141. exit;
  142. }
  143. @unlink($filename . '.tmp');
  144. @chmod($filename, 0600);
  145. sqsession_register($prefs_cache , 'prefs_cache');
  146. }
  147. /**
  148. * Remove a preference for the current user.
  149. *
  150. * @param string $data_dir data directory
  151. * @param string $username user name
  152. * @param string $pref_name preference name
  153. *
  154. */
  155. function removePref($data_dir, $username, $pref_name) {
  156. global $prefs_cache;
  157. cachePrefValues($data_dir, $username);
  158. if (isset($prefs_cache[$pref_name])) {
  159. unset($prefs_cache[$pref_name]);
  160. }
  161. savePrefValues($data_dir, $username);
  162. }
  163. /**
  164. * Set the desired preference setting ($pref_name)
  165. * to whatever is in $value.
  166. *
  167. * @param string $data_dir data directory
  168. * @param string $username user name
  169. * @param string $pref_name preference name
  170. * @param mixed $value preference value
  171. *
  172. */
  173. function setPref($data_dir, $username, $pref_name, $value) {
  174. global $prefs_cache;
  175. cachePrefValues($data_dir, $username);
  176. if (isset($prefs_cache[$pref_name]) && ($prefs_cache[$pref_name] == $value)) {
  177. return;
  178. }
  179. if ($value === '') {
  180. removePref($data_dir, $username, $pref_name);
  181. return;
  182. }
  183. $prefs_cache[$pref_name] = $value;
  184. savePrefValues($data_dir, $username);
  185. }
  186. /**
  187. * Check for a preferences file. If one can not be found, create it.
  188. *
  189. * @param string $data_dir data directory
  190. * @param string $username user name
  191. * @param string $filename (since 1.2.0) preference file name.
  192. * (OPTIONAL; default is an empty string,
  193. * in which case the file name is
  194. * automatically detected)
  195. *
  196. */
  197. function checkForPrefs($data_dir, $username, $filename = '') {
  198. /* First, make sure we have the filename. */
  199. if ($filename == '') {
  200. $filename = getHashedFile($username, $data_dir, "$username.pref");
  201. }
  202. /* Then, check if the file exists. */
  203. if (!@file_exists($filename) ) {
  204. /* If it does not exist, check for default_prefs */
  205. /* First, check legacy locations: data dir */
  206. if(substr($data_dir,-1) != '/') {
  207. $data_dir .= '/';
  208. }
  209. $default_pref = $data_dir . 'default_pref';
  210. /* or legacy location: internal data dir */
  211. if (!@file_exists($default_pref)) {
  212. $default_pref = SM_PATH . 'data/default_pref';
  213. }
  214. /* If no legacies, check where we'd expect it to be located:
  215. * under config/ */
  216. if (!@file_exists($default_pref)) {
  217. $default_pref = SM_PATH . 'config/default_pref';
  218. }
  219. /* If a default_pref file found, try to copy it, if none found,
  220. * try to create an empty one. If that fails, report an error.
  221. */
  222. if (
  223. ( is_readable($default_pref) && !@copy($default_pref, $filename) ) ||
  224. !@touch($filename)
  225. ) {
  226. $uid = 'httpd';
  227. if (function_exists('posix_getuid')){
  228. $user_data = posix_getpwuid(posix_getuid());
  229. $uid = $user_data['name'];
  230. }
  231. $errTitle = _("Could not create initial preference file!");
  232. $errString = $errTitle . "\n" .
  233. sprintf( _("%s should be writable by user %s."), $data_dir, $uid ) . "\n" .
  234. _("Please contact your system administrator and report this error.") ;
  235. logout_error( $errString, $errTitle );
  236. exit;
  237. }
  238. }
  239. }
  240. /**
  241. * Write the User Signature.
  242. *
  243. * @param string $data_dir data directory
  244. * @param string $username user name
  245. * @param integer $number (since 1.2.5) identity number.
  246. * (before 1.2.5., this parameter
  247. * was used for the signature value)
  248. * @param string $value (since 1.2.5) signature value
  249. *
  250. */
  251. function setSig($data_dir, $username, $number, $value) {
  252. // Limit signature size to 64KB (database BLOB limit)
  253. if (strlen($value)>65536) {
  254. error_option_save(_("Signature is too big."));
  255. return;
  256. }
  257. $filename = getHashedFile($username, $data_dir, "$username.si$number");
  258. /* Open the file for writing, or else display an error to the user. */
  259. if(!$file = @fopen("$filename.tmp", 'w')) {
  260. logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') );
  261. exit;
  262. }
  263. if ( sq_fwrite($file, $value) === FALSE ) {
  264. logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp'));
  265. exit;
  266. }
  267. fclose($file);
  268. if (! @copy($filename . '.tmp',$filename) ) {
  269. 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') );
  270. exit;
  271. }
  272. @unlink($filename . '.tmp');
  273. @chmod($filename, 0600);
  274. }
  275. /**
  276. * Get the signature.
  277. *
  278. * @param string $data_dir data directory
  279. * @param string $username user name
  280. * @param integer $number (since 1.2.5) identity number
  281. *
  282. * @return string signature
  283. *
  284. */
  285. function getSig($data_dir, $username, $number) {
  286. $filename = getHashedFile($username, $data_dir, "$username.si$number");
  287. $sig = '';
  288. if (file_exists($filename)) {
  289. /* Open the file, or else display an error to the user. */
  290. if(!$file = @fopen($filename, 'r'))
  291. {
  292. logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename) );
  293. exit;
  294. }
  295. while (!feof($file)) {
  296. $sig .= fgets($file, 1024);
  297. }
  298. fclose($file);
  299. }
  300. return $sig;
  301. }
  302. // vim: et ts=4