6 * This contains utility functions for dealing with multiple identities
8 * @copyright © 1999-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
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
22 function get_identities() {
24 global $username, $data_dir, $domain;
26 $em = getPref($data_dir,$username,'email_address');
28 if (strpos($username , '@') == false) {
29 $em = $username.'@'.$domain;
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'),
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),
59 * Function to save the identities array
61 * @param array $identities Array of identities
62 * @since 1.5.1 and 1.4.5
64 function save_identities($identities) {
66 global $username, $data_dir, $domain;
68 if (empty($identities) ||
!is_array($identities)) {
73 $num_cur = getPref($data_dir, $username, 'identities');
75 $cnt = count($identities);
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, '');
85 foreach($identities as $id=>$ident) {
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']);
94 setSig($data_dir, $username, 'g', $ident['signature']);
96 setSig($data_dir, $username, $key, $ident['signature']);
101 setPref($data_dir, $username, 'identities', $cnt);
106 * Returns an array with a fixed set of identities
108 * @param array $identities Array of identities
109 * @param int $id Identity to modify
110 * @param string $action Action to perform
112 * @since 1.5.1 and 1.4.5
114 function sqfixidentities( $identities, $id, $action ) {
120 if (empty($identities) ||
!is_array($identities)) {
124 foreach( $identities as $key=>$ident ) {
126 if (empty_identity($ident)) {
137 // inform plugins about renumbering of ids
138 do_hook('options_identities_renumber', $temp=array(&$id, 'default'));
142 $fixed[$i+
1] = $ident;
148 if ($key == ($id - 1)) {
151 // inform plugins about renumbering of ids
152 do_hook('options_identities_renumber', $temp=array(&$id , $id - 1));
160 $fixed[$i] = $tmp_hold;
168 // inform plugins about deleted id
169 do_hook('options_identities_process', $temp=array(&$action, &$id));
177 // Process actions from plugins and save/update action //
180 * send action and id information. number of hook arguments
181 * differs from 1.4.4 or older and 1.5.0. count($args) can
182 * be used to detect modified hook. Older hook does not
183 * provide information that can be useful for plugins.
185 do_hook('options_identities_process', $temp=array(&$action, &$id));
191 // Inc array index //
201 * Function to test if identity is empty
203 * @param array $identity Identitiy Array
205 * @since 1.5.1 and 1.4.5
207 function empty_identity($ident) {
208 if (empty($ident['full_name']) && empty($ident['email_address']) && empty($ident['signature']) && empty($ident['reply_to'])) {
216 * Construct our "From:" header based on
217 * a supplied identity number.
218 * Will fall back when no sensible email address has been defined.
220 * @param int $identity identity# to use
223 function build_from_header($identity = 0) {
224 $idents = get_identities();
226 if (! isset($idents[$identity]) ) $identity = 0;
228 if ( !empty($idents[$identity]['full_name']) ) {
229 $from_name = $idents[$identity]['full_name'];
232 $from_mail = $idents[$identity]['email_address'];
234 if ( isset($from_name) ) {
235 $from_name_encoded = encodeHeader($from_name);
236 if ($from_name_encoded != $from_name) {
237 return $from_name_encoded .' <'.$from_mail.'>';
239 return '"'.$from_name .'" <'.$from_mail.'>';
245 * Find a matching identity based on a set of emailaddresses.
246 * Will return the first identity to have a matching address.
247 * When nothing found, returns the default identity.
249 * @param needles array list of mailadresses
250 * @returns int identity
253 function find_identity($needles) {
254 $idents = get_identities();
255 if ( count($idents) == 1 ||
empty($needles) ) return 0;
257 foreach ( $idents as $nr => $ident ) {
258 if ( isset($ident['email_address']) ) {
259 foreach ( $needles as $needle ) {
260 if ( strcasecmp($needle, $ident['email_address']) == 0 ) {