Source for file identity.php

Documentation is available at identity.php

  1. <?php
  2.  
  3. /**
  4.  * identity.php
  5.  *
  6.  * This contains utility functions for dealing with multiple identities
  7.  *
  8.  * @copyright 1999-2020 The SquirrelMail Project Team
  9.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.  * @version $Id: identity.php 14845 2020-01-07 08:09:34Z pdontthink $
  11.  * @package squirrelmail
  12.  * @since 1.4.2
  13.  */
  14.  
  15.  
  16. /**
  17.  * Returns an array of all the identities.
  18.  * Array is keyed: full_name, reply_to, email_address, index, signature
  19.  * @return array full_name,reply_to,email_address,index,signature
  20.  * @since 1.4.2
  21.  */
  22. function get_identities() {
  23.  
  24.     global $username, $data_dir, $domain;
  25.  
  26.     $em = getPref($data_dir,$username,'email_address');
  27.     if ( ! $em ) {
  28.         if (strpos($username , '@') == false) {
  29.             $em = $username.'@'.$domain;
  30.         } else {
  31.             $em = $username;
  32.         }
  33.     }
  34.     $identities = array();
  35.     /* We always have this one, even if the user doesn't use multiple identities */
  36.     $identities[] = array('full_name' => getPref($data_dir,$username,'full_name'),
  37.         'email_address' => $em,
  38.         'reply_to' => getPref($data_dir,$username,'reply_to'),
  39.         'signature' => getSig($data_dir,$username,'g'),
  40.         'index' => 0 );
  41.  
  42.     $num_ids = getPref($data_dir,$username,'identities');
  43.     /* If there are any others, add them to the array */
  44.     if (!empty($num_ids) && $num_ids > 1) {
  45.         for ($i=1;$i<$num_ids;$i++) {
  46.             $thisem = getPref($data_dir,$username,'email_address' . $i);
  47.             $identities[] = array('full_name' => getPref($data_dir,$username,'full_name' . $i),
  48.             'email_address' => empty($thisem)?$em:$thisem,
  49.             'reply_to' => getPref($data_dir,$username,'reply_to' . $i),
  50.             'signature' => getSig($data_dir,$username,$i),
  51.             'index' => $i );
  52.         }
  53.     }
  54.  
  55.     return $identities;
  56. }
  57.  
  58. /**
  59.  * Function to save the identities array
  60.  *
  61.  * @param  array     $identities     Array of identities
  62.  * @since 1.5.1 and 1.4.5
  63.  */
  64. function save_identities($identities) {
  65.  
  66.     global $username, $data_dir, $domain;
  67.  
  68.     if (empty($identities) || !is_array($identities)) {
  69.         return;
  70.     }
  71.  
  72.  
  73.     $num_cur = getPref($data_dir, $username, 'identities');
  74.  
  75.     $cnt = count($identities);
  76.  
  77.     // Remove any additional identities in prefs //
  78.     for($i=$cnt; $i <= $num_cur; $i++) {
  79.         removePref($data_dir, $username, 'full_name' . $i);
  80.         removePref($data_dir, $username, 'email_address' . $i);
  81.         removePref($data_dir, $username, 'reply_to' . $i);
  82.         setSig($data_dir, $username, $i, '');
  83.     }
  84.  
  85.     foreach($identities as $id=>$ident) {
  86.  
  87.         $key = ($id?$id:'');
  88.  
  89.         setPref($data_dir, $username, 'full_name' . $key, $ident['full_name']);
  90.         setPref($data_dir, $username, 'email_address' . $key, $ident['email_address']);
  91.         setPref($data_dir, $username, 'reply_to' . $key, $ident['reply_to']);
  92.  
  93.         if ($id === 0) {
  94.             setSig($data_dir, $username, 'g', $ident['signature']);
  95.         } else {
  96.             setSig($data_dir, $username, $key, $ident['signature']);
  97.         }
  98.  
  99.     }
  100.  
  101.     setPref($data_dir, $username, 'identities', $cnt);
  102.  
  103. }
  104.  
  105. /**
  106.  * Returns an array with a fixed set of identities
  107.  *
  108.  * @param   array       $identities      Array of identities
  109.  * @param   int         $id             Identity to modify
  110.  * @param   string      $action         Action to perform
  111.  * @return  array 
  112.  * @since 1.5.1 and 1.4.5
  113.  */
  114. function sqfixidentities( $identities, $id, $action ) {
  115.  
  116.     $fixed = array();
  117.     $tmp_hold = array();
  118.     $i = 0;
  119.  
  120.     if (empty($identities) || !is_array($identities)) {
  121.         return $fixed;
  122.     }
  123.  
  124.     foreach( $identities as $key=>$ident ) {
  125.  
  126.         if (empty_identity($ident)) {
  127.             continue;
  128.         }
  129.  
  130.         switch($action) {
  131.  
  132.             case 'makedefault':
  133.  
  134.                 if ($key == $id) {
  135.                     $fixed[0] = $ident;
  136.  
  137.                     // inform plugins about renumbering of ids
  138.                     $temp = array(&$id, 'default');
  139.                     do_hook('options_identities_renumber', $temp);
  140.  
  141.                     continue 2;
  142.                 } else {
  143.                     $fixed[$i+1] = $ident;
  144.                 }
  145.                 break;
  146.  
  147.             case 'move':
  148.  
  149.                 if ($key == ($id - 1)) {
  150.                     $tmp_hold = $ident;
  151.  
  152.                     // inform plugins about renumbering of ids
  153.                     $temp = array(&$id , $id - 1);
  154.                     do_hook('options_identities_renumber', $temp);
  155.  
  156.                     continue 2;
  157.                 } else {
  158.                     $fixed[$i] = $ident;
  159.  
  160.                     if ($key == $id) {
  161.                         $i++;
  162.                         $fixed[$i] = $tmp_hold;
  163.                     }
  164.                 }
  165.                 break;
  166.  
  167.             case 'delete':
  168.  
  169.                 if ($key == $id) {
  170.                     // inform plugins about deleted id
  171.                     $temp = array(&$action, &$id);
  172.                     do_hook('options_identities_process', $temp);
  173.  
  174.                     continue 2;
  175.                 } else {
  176.                     $fixed[$i] = $ident;
  177.                 }
  178.                 break;
  179.  
  180.             // Process actions from plugins and save/update action //
  181.             default:
  182.                 /**
  183.                  * send action and id information. number of hook arguments
  184.                  * differs from 1.4.4 or older and 1.5.0. count($args) can
  185.                  * be used to detect modified hook. Older hook does not
  186.                  * provide information that can be useful for plugins.
  187.                  */
  188.                 $temp = array(&$action, &$id);
  189.                 do_hook('options_identities_process', $temp);
  190.  
  191.                 $fixed[$i] = $ident;
  192.  
  193.         }
  194.  
  195.         // Inc array index //
  196.         $i++;
  197.     }
  198.  
  199.     ksort($fixed);
  200.     return $fixed;
  201.  
  202. }
  203.  
  204. /**
  205.  * Function to test if identity is empty
  206.  *
  207.  * @param   array   $identity   Identitiy Array
  208.  * @return  boolean 
  209.  * @since 1.5.1 and 1.4.5
  210.  */
  211. function empty_identity($ident) {
  212.     if (empty($ident['full_name']) && empty($ident['email_address']) && empty($ident['signature']) && empty($ident['reply_to'])) {
  213.         return true;
  214.     } else {
  215.         return false;
  216.     }
  217. }
  218.  
  219. /**
  220.  * Construct our "From:" header based on
  221.  * a supplied identity number.
  222.  * Will fall back when no sensible email address has been defined.
  223.  *
  224.  * @param   int $identity   identity# to use
  225.  * @since 1.5.2
  226.  */
  227. function build_from_header($identity = 0) {
  228.  
  229.     global $domain;
  230.  
  231.     $idents = get_identities();
  232.  
  233.     if (! isset($idents[$identity]) ) $identity = 0;
  234.  
  235.     if ( !empty($idents[$identity]['full_name']) ) {
  236.         $from_name = $idents[$identity]['full_name'];
  237.     }
  238.  
  239.     $from_mail = $idents[$identity]['email_address'];
  240.     if (strpos($from_mail, '@') === FALSE)
  241.         $from_mail .= '@' . $domain;
  242.     
  243.     if ( isset($from_name) ) {
  244.         $from_name_encoded = encodeHeader('"' . $from_name . '"');
  245.         if ($from_name_encoded != $from_name) {
  246.             return $from_name_encoded . ' <' . $from_mail . '>';
  247.         }
  248.         return '"' . $from_name . '" <' . $from_mail . '>';
  249.     }
  250.     return $from_mail;
  251. }
  252.  
  253. /**
  254.  * Find a matching identity based on a set of emailaddresses.
  255.  * Will return the first identity to have a matching address.
  256.  * When nothing found, returns the default identity.
  257.  *
  258.  * @param needles   array   list of mailadresses
  259.  * @returns int identity
  260.  * @since 1.5.2
  261.  */
  262. function find_identity($needles) {
  263.     $idents = get_identities();
  264.     if ( count($idents) == 1 || empty($needles) ) return 0;
  265.  
  266.     foreach ( $idents as $nr => $ident ) {
  267.         if ( isset($ident['email_address']) ) {
  268.             foreach ( $needles as $needle ) {
  269.                 if ( strcasecmp($needle, $ident['email_address']) == 0 ) {
  270.                     return $nr;
  271.                 }
  272.             }
  273.         }
  274.     }
  275.     return 0;
  276. }

Documentation generated on Mon, 13 Jan 2020 04:22:42 +0100 by phpDocumentor 1.4.3