b2b82177321ae72d1ceccb3d2e6de3dbf3a0b459
[squirrelmail.git] / functions / identity.php
1 <?php
2
3 /**
4 * identity.php
5 *
6 * This contains utility functions for dealing with multiple identities
7 *
8 * @copyright &copy; 1999-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
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 do_hook('options_identities_renumber', $temp=array(&$id, 'default'));
139
140 continue 2;
141 } else {
142 $fixed[$i+1] = $ident;
143 }
144 break;
145
146 case 'move':
147
148 if ($key == ($id - 1)) {
149 $tmp_hold = $ident;
150
151 // inform plugins about renumbering of ids
152 do_hook('options_identities_renumber', $temp=array(&$id , $id - 1));
153
154 continue 2;
155 } else {
156 $fixed[$i] = $ident;
157
158 if ($key == $id) {
159 $i++;
160 $fixed[$i] = $tmp_hold;
161 }
162 }
163 break;
164
165 case 'delete':
166
167 if ($key == $id) {
168 // inform plugins about deleted id
169 do_hook('options_identities_process', $temp=array(&$action, &$id));
170
171 continue 2;
172 } else {
173 $fixed[$i] = $ident;
174 }
175 break;
176
177 // Process actions from plugins and save/update action //
178 default:
179 /**
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.
184 */
185 do_hook('options_identities_process', $temp=array(&$action, &$id));
186
187 $fixed[$i] = $ident;
188
189 }
190
191 // Inc array index //
192 $i++;
193 }
194
195 ksort($fixed);
196 return $fixed;
197
198 }
199
200 /**
201 * Function to test if identity is empty
202 *
203 * @param array $identity Identitiy Array
204 * @return boolean
205 * @since 1.5.1 and 1.4.5
206 */
207 function empty_identity($ident) {
208 if (empty($ident['full_name']) && empty($ident['email_address']) && empty($ident['signature']) && empty($ident['reply_to'])) {
209 return true;
210 } else {
211 return false;
212 }
213 }
214
215 /**
216 * Construct our "From:" header based on
217 * a supplied identity number.
218 * Will fall back when no sensible email address has been defined.
219 *
220 * @param int $identity identity# to use
221 * @since 1.5.2
222 */
223 function build_from_header($identity = 0) {
224 $idents = get_identities();
225
226 if (! isset($idents[$identity]) ) $identity = 0;
227
228 if ( !empty($idents[$identity]['full_name']) ) {
229 $from_name = $idents[$identity]['full_name'];
230 }
231
232 $from_mail = $idents[$identity]['email_address'];
233
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.'>';
238 }
239 return '"'.$from_name .'" <'.$from_mail.'>';
240 }
241 return $from_mail;
242 }
243
244 /**
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.
248 *
249 * @param needles array list of mailadresses
250 * @returns int identity
251 * @since 1.5.2
252 */
253 function find_identity($needles) {
254 $idents = get_identities();
255 if ( count($idents) == 1 || empty($needles) ) return 0;
256
257 foreach ( $idents as $nr => $ident ) {
258 if ( isset($ident['email_address']) ) {
259 foreach ( $needles as $needle ) {
260 if ( $needle == $ident['email_address'] ) {
261 return $nr;
262 }
263 }
264 }
265 }
266 return 0;
267 }