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 1999-2012 The SquirrelMail Project Team
  9.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.  * @version $Id: file_prefs.php 14248 2012-01-02 00:18:17Z pdontthink $
  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. //FIXME: testing below for !$result means that a plugin cannot fetch its own pref value of 0, '0', '', FALSE, or anything else that evaluates to boolean FALSE.
  94.     if (!$result{
  95.         cachePrefValues($data_dir$username);
  96.         if (isset($prefs_cache[$string])) {
  97.             $result $prefs_cache[$string];
  98.         else {
  99. //FIXME: is there justification for having these TWO hooks so close together?  who uses these?
  100.             $result do_hook_function('get_pref'array($username$string));
  101. //FIXME: testing below for !$result means that a plugin cannot fetch its own pref value of 0, '0', '', FALSE, or anything else that evaluates to boolean FALSE.
  102.             if (!$result{
  103.                 $result $default;
  104.             }
  105.         }
  106.     }
  107.     return ($result);
  108. }
  109.  
  110. /**
  111.  * Save the preferences for this user.
  112.  */
  113. function savePrefValues($data_dir$username{
  114.     global $prefs_cache;
  115.  
  116.     $filename getHashedFile($username$data_dir"$username.pref");
  117.  
  118.     /* Open the file for writing, or else display an error to the user. */
  119.     if(!$file @fopen($filename.'.tmp''w'))
  120.     {
  121.         logout_errorsprintf_("Preference file, %s, could not be opened. Contact your system administrator to resolve this issue.")$filename.'.tmp') );
  122.         exit;
  123.     }
  124.     foreach ($prefs_cache as $Key => $Value{
  125.         if (isset($Value)) {
  126.             if sq_fwrite($file$Key '=' $Value "\n"=== FALSE {
  127.                logout_errorsprintf_("Preference file, %s, could not be written. Contact your system administrator to resolve this issue."$filename '.tmp') );
  128.                exit;
  129.             }
  130.         }
  131.     }
  132.     fclose($file);
  133.     if (@copy($filename '.tmp',$filename) ) {
  134.         logout_errorsprintf_("Preference file, %s, could not be copied from temporary file, %s. Contact your system administrator to resolve this issue.")$filename$filename '.tmp') );
  135.         exit;
  136.     }
  137.     @unlink($filename '.tmp');
  138.     @chmod($filename0600);
  139.     sqsession_register($prefs_cache 'prefs_cache');
  140. }
  141.  
  142. /**
  143.  * Remove a preference for the current user.
  144.  */
  145. function removePref($data_dir$username$string{
  146.     global $prefs_cache;
  147.  
  148.     cachePrefValues($data_dir$username);
  149.  
  150.     if (isset($prefs_cache[$string])) {
  151.         unset($prefs_cache[$string]);
  152.     }
  153.  
  154.     savePrefValues($data_dir$username);
  155. }
  156.  
  157. /**
  158.  * Set a there preference $string to $value.
  159.  */
  160. function setPref($data_dir$username$string$value{
  161.     global $prefs_cache;
  162.  
  163.     cachePrefValues($data_dir$username);
  164.     if (isset($prefs_cache[$string]&& ($prefs_cache[$string== $value)) {
  165.         return;
  166.     }
  167.  
  168.     if ($value === ''{
  169.         removePref($data_dir$username$string);
  170.         return;
  171.     }
  172.  
  173.     $prefs_cache[$string$value;
  174.     savePrefValues($data_dir$username);
  175. }
  176.  
  177. /**
  178.  * Check for a preferences file. If one can not be found, create it.
  179.  */
  180. function checkForPrefs($data_dir$username$filename ''{
  181.     /* First, make sure we have the filename. */
  182.     if ($filename == ''{
  183.         $filename getHashedFile($username$data_dir"$username.pref");
  184.     }
  185.  
  186.     /* Then, check if the file exists. */
  187.     if (!@file_exists($filename) ) {
  188.         /* First, check the $data_dir for the default preference file. */
  189.         if(substr($data_dir,-1!= '/'{
  190.             $data_dir .= '/';
  191.         }
  192.         $default_pref $data_dir 'default_pref';
  193.  
  194.         /* If it is not there, check the internal data directory. */
  195.         if (!@file_exists($default_pref)) {
  196.             $default_pref SM_PATH 'data/default_pref';
  197.         }
  198.  
  199.         /* Otherwise, report an error. */
  200.         $errTitle sprintf_("Error opening %s")$default_pref );
  201.         if (!is_readable($default_pref)) {
  202.             $errString $errTitle "<br />\n" .
  203.                          _("Default preference file not found or not readable!""<br />\n" .
  204.                          _("Please contact your system administrator and report this error.""<br />\n";
  205.             logout_error$errString$errTitle );
  206.             exit;
  207.         else if (!@copy($default_pref$filename)) {
  208.             $uid 'httpd';
  209.             if (function_exists('posix_getuid')){
  210.                 $user_data posix_getpwuid(posix_getuid());
  211.                 $uid $user_data['name'];
  212.             }
  213.             $errString $errTitle '<br />' .
  214.                        _("Could not create initial preference file!""<br />\n" .
  215.                        sprintf_("%s should be writable by user %s")$data_dir$uid .
  216.                        "<br />\n" _("Please contact your system administrator and report this error.""<br />\n";
  217.             logout_error$errString$errTitle );
  218.             exit;
  219.         }
  220.     }
  221. }
  222.  
  223. /**
  224.  * Write the User Signature.
  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_errorsprintf_("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_errorsprintf_("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_errorsprintf_("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($filename0600);
  249.  
  250. }
  251.  
  252. /**
  253.  * Get the signature.
  254.  */
  255. function getSig($data_dir$username$number{
  256.     $filename getHashedFile($username$data_dir"$username.si$number");
  257.     $sig '';
  258.     if (file_exists($filename)) {
  259.         /* Open the file, or else display an error to the user. */
  260.         if(!$file @fopen($filename'r'))
  261.         {
  262.             logout_errorsprintf_("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue.")$filename) );
  263.             exit;
  264.         }
  265.         while (!feof($file)) {
  266.             $sig .= fgets($file1024);
  267.         }
  268.         fclose($file);
  269.     }
  270.     return $sig;
  271. }

Documentation generated on Fri, 24 May 2013 04:22:07 +0200 by phpDocumentor 1.4.3