Happy New Year
[squirrelmail.git] / functions / identity.php
CommitLineData
b1f45342 1<?php
2
3/**
4 * identity.php
5 *
b1f45342 6 * This contains utility functions for dealing with multiple identities
7 *
c4faef33 8 * @copyright 1999-2020 The SquirrelMail Project Team
4b4abf93 9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
31841a9e 10 * @version $Id$
d6c32258 11 * @package squirrelmail
0392886d 12 * @since 1.4.2
b1f45342 13 */
14
b1f45342 15
16/**
0392886d 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 */
b1f45342 22function get_identities() {
23
b6e70801 24 global $username, $data_dir, $domain;
25
26 $em = getPref($data_dir,$username,'email_address');
f16477bc 27 if ( ! $em ) {
28 if (strpos($username , '@') == false) {
29 $em = $username.'@'.$domain;
30 } else {
31 $em = $username;
32 }
33 }
b1f45342 34 $identities = array();
35 /* We always have this one, even if the user doesn't use multiple identities */
0f4f003e 36 $identities[] = array('full_name' => getPref($data_dir,$username,'full_name'),
b6e70801 37 'email_address' => $em,
b1f45342 38 'reply_to' => getPref($data_dir,$username,'reply_to'),
39 'signature' => getSig($data_dir,$username,'g'),
40 'index' => 0 );
41
b6e70801 42 $num_ids = getPref($data_dir,$username,'identities');
b1f45342 43 /* If there are any others, add them to the array */
de981f8c 44 if (!empty($num_ids) && $num_ids > 1) {
45 for ($i=1;$i<$num_ids;$i++) {
40e07136 46 $thisem = getPref($data_dir,$username,'email_address' . $i);
0f4f003e 47 $identities[] = array('full_name' => getPref($data_dir,$username,'full_name' . $i),
40e07136 48 'email_address' => empty($thisem)?$em:$thisem,
b1f45342 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
e7f9c987 58/**
59 * Function to save the identities array
60 *
61 * @param array $identities Array of identities
0392886d 62 * @since 1.5.1 and 1.4.5
e7f9c987 63 */
64function 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');
202bcbcc 74
e7f9c987 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
0392886d 112 * @since 1.5.1 and 1.4.5
e7f9c987 113 */
114function 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;
3df61ef3 136
137 // inform plugins about renumbering of ids
859e79b4 138 $temp = array(&$id, 'default');
139 do_hook('options_identities_renumber', $temp);
3df61ef3 140
e7f9c987 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;
3df61ef3 151
152 // inform plugins about renumbering of ids
859e79b4 153 $temp = array(&$id , $id - 1);
154 do_hook('options_identities_renumber', $temp);
3df61ef3 155
e7f9c987 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) {
1d6248ce 170 // inform plugins about deleted id
859e79b4 171 $temp = array(&$action, &$id);
172 do_hook('options_identities_process', $temp);
1d6248ce 173
e7f9c987 174 continue 2;
175 } else {
176 $fixed[$i] = $ident;
177 }
178 break;
179
3df61ef3 180 // Process actions from plugins and save/update action //
e7f9c987 181 default:
3df61ef3 182 /**
202bcbcc 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
3df61ef3 186 * provide information that can be useful for plugins.
187 */
859e79b4 188 $temp = array(&$action, &$id);
189 do_hook('options_identities_process', $temp);
3df61ef3 190
e7f9c987 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
0392886d 209 * @since 1.5.1 and 1.4.5
e7f9c987 210 */
211function 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}
40e07136 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 */
227function build_from_header($identity = 0) {
0b1dddba 228
229 global $domain;
230
40e07136 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'];
0b1dddba 240 if (strpos($from_mail, '@') === FALSE)
241 $from_mail .= '@' . $domain;
40e07136 242
243 if ( isset($from_name) ) {
6b76cffa 244 $from_name_encoded = encodeHeader('"' . $from_name . '"');
40e07136 245 if ($from_name_encoded != $from_name) {
6b76cffa 246 return $from_name_encoded . ' <' . $from_mail . '>';
40e07136 247 }
1f98d935 248 return '"' . $from_name . '" <' . $from_mail . '>';
40e07136 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 */
262function 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 ) {
c4f59e6c 269 if ( strcasecmp($needle, $ident['email_address']) == 0 ) {
40e07136 270 return $nr;
271 }
272 }
273 }
274 }
275 return 0;
276}