one more identity action to process
[squirrelmail.git] / functions / identity.php
CommitLineData
b1f45342 1<?php
2
3/**
4 * identity.php
5 *
6c84ba1e 6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
b1f45342 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This contains utility functions for dealing with multiple identities
10 *
31841a9e 11 * @version $Id$
d6c32258 12 * @package squirrelmail
0392886d 13 * @since 1.4.2
b1f45342 14 */
15
cf6f9b31 16/** Used to simplify includes
17 * @ignore
18 */
b1f45342 19if (!defined('SM_PATH')) {
20 define('SM_PATH','../');
21}
22
0392886d 23/** preference and signature functions */
b1f45342 24include_once(SM_PATH . 'include/load_prefs.php');
25
26/**
0392886d 27 * Returns an array of all the identities.
28 * Array is keyed: full_name, reply_to, email_address, index, signature
29 * @return array full_name,reply_to,email_address,index,signature
30 * @since 1.4.2
31 */
b1f45342 32function get_identities() {
33
b6e70801 34 global $username, $data_dir, $domain;
35
36 $em = getPref($data_dir,$username,'email_address');
f16477bc 37 if ( ! $em ) {
38 if (strpos($username , '@') == false) {
39 $em = $username.'@'.$domain;
40 } else {
41 $em = $username;
42 }
43 }
b1f45342 44 $identities = array();
45 /* We always have this one, even if the user doesn't use multiple identities */
0f4f003e 46 $identities[] = array('full_name' => getPref($data_dir,$username,'full_name'),
b6e70801 47 'email_address' => $em,
b1f45342 48 'reply_to' => getPref($data_dir,$username,'reply_to'),
49 'signature' => getSig($data_dir,$username,'g'),
50 'index' => 0 );
51
b6e70801 52 $num_ids = getPref($data_dir,$username,'identities');
b1f45342 53 /* If there are any others, add them to the array */
de981f8c 54 if (!empty($num_ids) && $num_ids > 1) {
55 for ($i=1;$i<$num_ids;$i++) {
0f4f003e 56 $identities[] = array('full_name' => getPref($data_dir,$username,'full_name' . $i),
b1f45342 57 'email_address' => getPref($data_dir,$username,'email_address' . $i),
58 'reply_to' => getPref($data_dir,$username,'reply_to' . $i),
59 'signature' => getSig($data_dir,$username,$i),
60 'index' => $i );
61 }
62 }
63
64 return $identities;
65}
66
e7f9c987 67/**
68 * Function to save the identities array
69 *
70 * @param array $identities Array of identities
0392886d 71 * @since 1.5.1 and 1.4.5
e7f9c987 72 */
73function save_identities($identities) {
74
75 global $username, $data_dir, $domain;
76
77 if (empty($identities) || !is_array($identities)) {
78 return;
79 }
80
81
82 $num_cur = getPref($data_dir, $username, 'identities');
83
84 $cnt = count($identities);
85
86 // Remove any additional identities in prefs //
87 for($i=$cnt; $i <= $num_cur; $i++) {
88 removePref($data_dir, $username, 'full_name' . $i);
89 removePref($data_dir, $username, 'email_address' . $i);
90 removePref($data_dir, $username, 'reply_to' . $i);
91 setSig($data_dir, $username, $i, '');
92 }
93
94 foreach($identities as $id=>$ident) {
95
96 $key = ($id?$id:'');
97
98 setPref($data_dir, $username, 'full_name' . $key, $ident['full_name']);
99 setPref($data_dir, $username, 'email_address' . $key, $ident['email_address']);
100 setPref($data_dir, $username, 'reply_to' . $key, $ident['reply_to']);
101
102 if ($id === 0) {
103 setSig($data_dir, $username, 'g', $ident['signature']);
104 } else {
105 setSig($data_dir, $username, $key, $ident['signature']);
106 }
107
108 }
109
110 setPref($data_dir, $username, 'identities', $cnt);
111
112}
113
114/**
115 * Returns an array with a fixed set of identities
116 *
117 * @param array $identities Array of identities
118 * @param int $id Identity to modify
119 * @param string $action Action to perform
120 * @return array
0392886d 121 * @since 1.5.1 and 1.4.5
e7f9c987 122 */
123function sqfixidentities( $identities, $id, $action ) {
124
125 $fixed = array();
126 $tmp_hold = array();
127 $i = 0;
128
129 if (empty($identities) || !is_array($identities)) {
130 return $fixed;
131 }
132
133 foreach( $identities as $key=>$ident ) {
134
135 if (empty_identity($ident)) {
136 continue;
137 }
138
139 switch($action) {
140
141 case 'makedefault':
142
143 if ($key == $id) {
144 $fixed[0] = $ident;
3df61ef3 145
146 // inform plugins about renumbering of ids
147 do_hook('options_identities_renumber', $id, 'default');
148
e7f9c987 149 continue 2;
150 } else {
151 $fixed[$i+1] = $ident;
152 }
153 break;
154
155 case 'move':
156
157 if ($key == ($id - 1)) {
158 $tmp_hold = $ident;
3df61ef3 159
160 // inform plugins about renumbering of ids
161 do_hook('options_identities_renumber', $id , $id - 1);
162
e7f9c987 163 continue 2;
164 } else {
165 $fixed[$i] = $ident;
166
167 if ($key == $id) {
168 $i++;
169 $fixed[$i] = $tmp_hold;
170 }
171 }
172 break;
173
174 case 'delete':
175
176 if ($key == $id) {
1d6248ce 177 // inform plugins about deleted id
178 do_hook('options_identities_process', $action, $id);
179
e7f9c987 180 continue 2;
181 } else {
182 $fixed[$i] = $ident;
183 }
184 break;
185
3df61ef3 186 // Process actions from plugins and save/update action //
e7f9c987 187 default:
3df61ef3 188 /**
189 * send action and id information. number of hook arguments
190 * differs from 1.4.4 or older and 1.5.0. count($args) can
191 * be used to detect modified hook. Older hook does not
192 * provide information that can be useful for plugins.
193 */
194 do_hook('options_identities_process', $action, $id);
195
e7f9c987 196 $fixed[$i] = $ident;
197
198 }
199
200 // Inc array index //
201 $i++;
202 }
203
204 ksort($fixed);
205 return $fixed;
206
207}
208
209/**
210 * Function to test if identity is empty
211 *
212 * @param array $identity Identitiy Array
213 * @return boolean
0392886d 214 * @since 1.5.1 and 1.4.5
e7f9c987 215 */
216function empty_identity($ident) {
217 if (empty($ident['full_name']) && empty($ident['email_address']) && empty($ident['signature']) && empty($ident['reply_to'])) {
218 return true;
219 } else {
220 return false;
221 }
222}
223
0392886d 224?>