Source for file file_prefs.php

Documentation is available at file_prefs.php

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

Documentation generated on Sat, 07 Oct 2006 16:31:10 +0300 by phpDocumentor 1.3.0RC6