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 | |
0392886d |
16 | /** @ignore |
17 | * Used to simplify includes */ |
b1f45342 |
18 | if (!defined('SM_PATH')) { |
19 | define('SM_PATH','../'); |
20 | } |
21 | |
0392886d |
22 | /** preference and signature functions */ |
b1f45342 |
23 | include_once(SM_PATH . 'include/load_prefs.php'); |
24 | |
25 | /** |
0392886d |
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 | */ |
b1f45342 |
31 | function get_identities() { |
32 | |
b6e70801 |
33 | global $username, $data_dir, $domain; |
34 | |
35 | $em = getPref($data_dir,$username,'email_address'); |
f16477bc |
36 | if ( ! $em ) { |
37 | if (strpos($username , '@') == false) { |
38 | $em = $username.'@'.$domain; |
39 | } else { |
40 | $em = $username; |
41 | } |
42 | } |
b1f45342 |
43 | $identities = array(); |
44 | /* We always have this one, even if the user doesn't use multiple identities */ |
0f4f003e |
45 | $identities[] = array('full_name' => getPref($data_dir,$username,'full_name'), |
b6e70801 |
46 | 'email_address' => $em, |
b1f45342 |
47 | 'reply_to' => getPref($data_dir,$username,'reply_to'), |
48 | 'signature' => getSig($data_dir,$username,'g'), |
49 | 'index' => 0 ); |
50 | |
b6e70801 |
51 | $num_ids = getPref($data_dir,$username,'identities'); |
b1f45342 |
52 | /* If there are any others, add them to the array */ |
de981f8c |
53 | if (!empty($num_ids) && $num_ids > 1) { |
54 | for ($i=1;$i<$num_ids;$i++) { |
0f4f003e |
55 | $identities[] = array('full_name' => getPref($data_dir,$username,'full_name' . $i), |
b1f45342 |
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 | |
e7f9c987 |
66 | /** |
67 | * Function to save the identities array |
68 | * |
69 | * @param array $identities Array of identities |
0392886d |
70 | * @since 1.5.1 and 1.4.5 |
e7f9c987 |
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 |
0392886d |
120 | * @since 1.5.1 and 1.4.5 |
e7f9c987 |
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 | continue 2; |
145 | } else { |
146 | $fixed[$i+1] = $ident; |
147 | } |
148 | break; |
149 | |
150 | case 'move': |
151 | |
152 | if ($key == ($id - 1)) { |
153 | $tmp_hold = $ident; |
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 | continue 2; |
169 | } else { |
170 | $fixed[$i] = $ident; |
171 | } |
172 | break; |
173 | |
174 | // we should never hit this but just in case // |
175 | default: |
176 | $fixed[$i] = $ident; |
177 | |
178 | } |
179 | |
180 | // Inc array index // |
181 | $i++; |
182 | } |
183 | |
184 | ksort($fixed); |
185 | return $fixed; |
186 | |
187 | } |
188 | |
189 | /** |
190 | * Function to test if identity is empty |
191 | * |
192 | * @param array $identity Identitiy Array |
193 | * @return boolean |
0392886d |
194 | * @since 1.5.1 and 1.4.5 |
e7f9c987 |
195 | */ |
196 | function empty_identity($ident) { |
197 | if (empty($ident['full_name']) && empty($ident['email_address']) && empty($ident['signature']) && empty($ident['reply_to'])) { |
198 | return true; |
199 | } else { |
200 | return false; |
201 | } |
202 | } |
203 | |
0392886d |
204 | ?> |