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