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 |
19 | if (!defined('SM_PATH')) { |
20 | define('SM_PATH','../'); |
21 | } |
22 | |
0392886d |
23 | /** preference and signature functions */ |
b1f45342 |
24 | include_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 |
32 | function 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 | */ |
73 | function 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 | */ |
123 | function 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; |
145 | continue 2; |
146 | } else { |
147 | $fixed[$i+1] = $ident; |
148 | } |
149 | break; |
150 | |
151 | case 'move': |
152 | |
153 | if ($key == ($id - 1)) { |
154 | $tmp_hold = $ident; |
155 | continue 2; |
156 | } else { |
157 | $fixed[$i] = $ident; |
158 | |
159 | if ($key == $id) { |
160 | $i++; |
161 | $fixed[$i] = $tmp_hold; |
162 | } |
163 | } |
164 | break; |
165 | |
166 | case 'delete': |
167 | |
168 | if ($key == $id) { |
169 | continue 2; |
170 | } else { |
171 | $fixed[$i] = $ident; |
172 | } |
173 | break; |
174 | |
175 | // we should never hit this but just in case // |
176 | default: |
177 | $fixed[$i] = $ident; |
178 | |
179 | } |
180 | |
181 | // Inc array index // |
182 | $i++; |
183 | } |
184 | |
185 | ksort($fixed); |
186 | return $fixed; |
187 | |
188 | } |
189 | |
190 | /** |
191 | * Function to test if identity is empty |
192 | * |
193 | * @param array $identity Identitiy Array |
194 | * @return boolean |
0392886d |
195 | * @since 1.5.1 and 1.4.5 |
e7f9c987 |
196 | */ |
197 | function empty_identity($ident) { |
198 | if (empty($ident['full_name']) && empty($ident['email_address']) && empty($ident['signature']) && empty($ident['reply_to'])) { |
199 | return true; |
200 | } else { |
201 | return false; |
202 | } |
203 | } |
204 | |
0392886d |
205 | ?> |