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 $temp = array(&$id, 'default');
139 do_hook('options_identities_renumber', $temp);
143 $fixed[$i+
1] = $ident;
149 if ($key == ($id - 1)) {
152 // inform plugins about renumbering of ids
153 $temp = array(&$id , $id - 1);
154 do_hook('options_identities_renumber', $temp);
162 $fixed[$i] = $tmp_hold;
170 // inform plugins about deleted id
171 $temp = array(&$action, &$id);
172 do_hook('options_identities_process', $temp);
180 // Process actions from plugins and save/update action //
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.
188 $temp = array(&$action, &$id);
189 do_hook('options_identities_process', $temp);
195 // Inc array index //
205 * Function to test if identity is empty
207 * @param array $identity Identitiy Array
209 * @since 1.5.1 and 1.4.5
211 function empty_identity($ident) {
212 if (empty($ident['full_name']) && empty($ident['email_address']) && empty($ident['signature']) && empty($ident['reply_to'])) {
220 * Construct our "From:" header based on
221 * a supplied identity number.
222 * Will fall back when no sensible email address has been defined.
224 * @param int $identity identity# to use
227 function build_from_header($identity = 0) {
231 $idents = get_identities();
233 if (! isset($idents[$identity]) ) $identity = 0;
235 if ( !empty($idents[$identity]['full_name']) ) {
236 $from_name = $idents[$identity]['full_name'];
239 $from_mail = $idents[$identity]['email_address'];
240 if (strpos($from_mail, '@') === FALSE)
241 $from_mail .= '@' . $domain;
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.'>';
248 return '"'.$from_name .'" <'.$from_mail.'>';
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.
258 * @param needles array list of mailadresses
259 * @returns int identity
262 function find_identity($needles) {
263 $idents = get_identities();
264 if ( count($idents) == 1 ||
empty($needles) ) return 0;
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 ) {