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